Skip to content

Commit

Permalink
Update: add both sofinco & sofinco cacf payment gateways
Browse files Browse the repository at this point in the history
  • Loading branch information
BeBlood committed Jun 23, 2021
1 parent dfd5d06 commit 926416d
Show file tree
Hide file tree
Showing 7 changed files with 311 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Symfony\Component\OptionsResolver\OptionsResolver;
use Twig\Environment;

class SofincoPaymentGatewayClient
class SofincoCACFPaymentGatewayClient
{
const BUSINESS_TOKEN_DURATION = 30;
const BUSINESS_TOKEN_FORMAT_OPAQUE = 'OPAQUE';
Expand Down Expand Up @@ -145,7 +145,7 @@ public function getAccessTokenUrl(): string
public function getAccessTokenResponse(): ?Response
{
if (null === $this->clientId || null === $this->secretId) {
throw new \LogicException('You must define "idci_payment.sofinco.client_id" and "idci_payment.sofinco.secret_id" parameters to use SofincoPaymentGatewayClient');
throw new \LogicException('You must define "idci_payment.sofinco.client_id" and "idci_payment.sofinco.secret_id" parameters to use SofincoCACFPaymentGatewayClient');
}

try {
Expand Down
188 changes: 188 additions & 0 deletions Gateway/SofincoCACFPaymentGateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<?php

namespace IDCI\Bundle\PaymentBundle\Gateway;

use IDCI\Bundle\PaymentBundle\Gateway\Client\SofincoCACFPaymentGatewayClient;
use IDCI\Bundle\PaymentBundle\Model\GatewayResponse;
use IDCI\Bundle\PaymentBundle\Model\PaymentGatewayConfigurationInterface;
use IDCI\Bundle\PaymentBundle\Model\Transaction;
use IDCI\Bundle\PaymentBundle\Payment\PaymentStatus;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Twig\Environment;

class SofincoCACFPaymentGateway extends AbstractPaymentGateway
{
const OFFER_MODULE = 'WSACCROCHE';

const PRODUCT_MODULE = 'PRODUCT';

const CART_MODULE = 'PANIER';

/**
* @var SofincoCACFPaymentGatewayClient
*/
private $client;

public function __construct(
Environment $templating,
EventDispatcherInterface $dispatcher,
SofincoCACFPaymentGatewayClient $client
) {
parent::__construct($templating, $dispatcher);

$this->client = $client;
}

/**
* Build gateway form options.
*
* @method buildOptions
*/
private function buildOptions(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction
): array {
return [
'businessContext' => [
'providerContext' => [
'businessProviderId' => $paymentGatewayConfiguration->get('business_provider_id'),
'returnUrl' => $paymentGatewayConfiguration->get('return_url'),
],
'customerContext' => [
'externalCustomerId' => $transaction->getCustomerId(),
'civilityCode' => $transaction->getMetadata('customerContext.civilityCode'),
'firstName' => $transaction->getMetadata('customerContext.firstName'),
'lastName' => $transaction->getMetadata('customerContext.lastName'),
'birthName' => $transaction->getMetadata('customerContext.birthName'),
'birthDate' => $transaction->getMetadata('customerContext.birthDate'),
'citizenshipCode' => $transaction->getMetadata('customerContext.citizenshipCode'),
'birthCountryCode' => $transaction->getMetadata('customerContext.birthCountryCode'),
'additionalStreet' => $transaction->getMetadata('customerContext.additionalStreet'),
'street' => $transaction->getMetadata('customerContext.street'),
'city' => $transaction->getMetadata('customerContext.city'),
'zipCode' => $transaction->getMetadata('customerContext.zipCode'),
'distributerOffice' => $transaction->getMetadata('customerContext.distributerOffice'),
'countryCode' => $transaction->getMetadata('customerContext.countryCode'),
'phoneNumber' => $transaction->getMetadata('customerContext.phoneNumber'),
'mobileNumber' => $transaction->getMetadata('customerContext.mobileNumber'),
'emailAddress' => $transaction->getCustomerEmail(),
'loyaltyCardId' => $transaction->getMetadata('customerContext.loyaltyCardId'),
],
'coBorrowerContext' => [
'externalCustomerId' => $transaction->getMetadata('coBorrowerContext.externalCustomerId'),
'civilityCode' => $transaction->getMetadata('coBorrowerContext.civilityCode'),
'firstName' => $transaction->getMetadata('coBorrowerContext.firstName'),
'lastName' => $transaction->getMetadata('coBorrowerContext.lastName'),
'birthName' => $transaction->getMetadata('coBorrowerContext.birthName'),
'birthDate' => $transaction->getMetadata('coBorrowerContext.birthDate'),
'citizenshipCode' => $transaction->getMetadata('coBorrowerContext.citizenshipCode'),
'birthCountryCode' => $transaction->getMetadata('coBorrowerContext.birthCountryCode'),
'additionalStreet' => $transaction->getMetadata('coBorrowerContext.additionalStreet'),
'street' => $transaction->getMetadata('coBorrowerContext.street'),
'city' => $transaction->getMetadata('coBorrowerContext.city'),
'zipCode' => $transaction->getMetadata('coBorrowerContext.zipCode'),
'distributerOffice' => $transaction->getMetadata('coBorrowerContext.distributerOffice'),
'countryCode' => $transaction->getMetadata('coBorrowerContext.countryCode'),
'phoneNumber' => $transaction->getMetadata('coBorrowerContext.phoneNumber'),
'mobileNumber' => $transaction->getMetadata('coBorrowerContext.mobileNumber'),
'emailAddress' => $transaction->getMetadata('coBorrowerContext.emailAddress'),
'loyaltyCardId' => $transaction->getMetadata('coBorrowerContext.loyaltyCardId'),
],
'offerContext' => [
'orderId' => $transaction->getId(),
'scaleId' => $transaction->getMetadata('offerContext.scaleId'),
'equipmentCode' => $paymentGatewayConfiguration->get('equipment_code'),
'amount' => $transaction->getAmount(),
'orderAmount' => $transaction->getMetadata('offerContext.orderAmount'),
'personalContributionAmount' => $transaction->getMetadata('offerContext.personalContributionAmount'),
'duration' => $transaction->getMetadata('offerContext.duration'),
'preScoringCode' => $transaction->getMetadata('offerContext.preScoringCode'),
],
],
];
}

/**
* {@inheritdoc}
*/
public function initialize(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction
): array {
$options = $this->buildOptions($paymentGatewayConfiguration, $transaction);

return [
'url' => $this->client->getCreditUrl($options),
'options' => $options,
];
}

/**
* {@inheritdoc}
*/
public function buildHTMLView(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction
): string {
$initializationData = $this->initialize($paymentGatewayConfiguration, $transaction);

return $this->templating->render('@IDCIPayment/Gateway/sofinco.html.twig', [
'initializationData' => $initializationData,
]);
}

/**
* {@inheritdoc}
*
* @throws \UnexpectedValueException If the request method is not GET
*/
public function getResponse(
Request $request,
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration
): GatewayResponse {
if (!$request->isMethod(Request::METHOD_POST)) {
throw new \UnexpectedValueException('Sofinco : Payment Gateway error (Request method should be POST)');
}

$gatewayResponse = (new GatewayResponse())
->setTransactionUuid($request->request->get('ORDER_ID'))
->setAmount($request->request->get('AMOUNT'))
->setDate(new \DateTime())
->setStatus(PaymentStatus::STATUS_FAILED)
->setRaw($request->request->all())
;

if (in_array(
$request->request->get('CONTRACT_STATUS'),
[
SofincoCACFPaymentGatewayClient::DOCUMENT_STATUS_REFUSED,
SofincoCACFPaymentGatewayClient::DOCUMENT_STATUS_CANCELED,
SofincoCACFPaymentGatewayClient::DOCUMENT_STATUS_NOT_FOUND,
SofincoCACFPaymentGatewayClient::DOCUMENT_STATUS_ERROR,
]
)) {
return $gatewayResponse->setMessage('Transaction unauthorized');
}

if (SofincoCACFPaymentGatewayClient::DOCUMENT_STATUS_FUNDED === $request->request->get('CONTRACT_STATUS')) {
return $gatewayResponse->setStatus(PaymentStatus::STATUS_APPROVED);
}

return $gatewayResponse->setStatus(PaymentStatus::STATUS_UNVERIFIED);
}

/**
* {@inheritdoc}
*/
public static function getParameterNames(): ?array
{
return array_merge(
parent::getParameterNames(),
[
'business_provider_id',
'equipment_code',
]
);
}
}
Loading

0 comments on commit 926416d

Please sign in to comment.