Skip to content

Commit

Permalink
Avoid create duplicated webhook token
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Aug 5, 2024
1 parent 0140fa4 commit baabe02
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions config/services/action.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
service('webgriffe_sylius_pagolight.generator.webhook_token'),
service('webgriffe_sylius_pagolight.logger'),
service('request_stack'),
service('webgriffe_sylius_pagolight.repository.webhook_token'),
])
->tag('payum.action', ['factory' => PagolightApi::PAGOLIGHT_GATEWAY_CODE, 'alias' => 'payum.action.capture'])
->tag('payum.action', ['factory' => PagolightApi::PAGOLIGHT_PRO_GATEWAY_CODE, 'alias' => 'payum.action.capture'])
Expand Down
2 changes: 1 addition & 1 deletion src/PaymentDetailsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static function getContractUuid(array $paymentDetails): string
}

/**
* @param PaymentDetails $paymentDetails
* @param PaymentDetails|array{} $paymentDetails
*/
public static function addPaymentStatus(array $paymentDetails, string $status): array
{
Expand Down
32 changes: 30 additions & 2 deletions src/Payum/Action/CaptureAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\RouterInterface;
use Webgriffe\SyliusPagolightPlugin\Client\Exception\ClientException;
use Webgriffe\SyliusPagolightPlugin\Client\PaymentState;
use Webgriffe\SyliusPagolightPlugin\Client\ValueObject\Contract;
use Webgriffe\SyliusPagolightPlugin\Client\ValueObject\Response\ContractCreateResult;
use Webgriffe\SyliusPagolightPlugin\Controller\PaymentController;
Expand All @@ -32,6 +33,7 @@
use Webgriffe\SyliusPagolightPlugin\Payum\PagolightApi;
use Webgriffe\SyliusPagolightPlugin\Payum\Request\Api\CreateContract;
use Webgriffe\SyliusPagolightPlugin\Payum\Request\ConvertPaymentToContract;
use Webgriffe\SyliusPagolightPlugin\Repository\WebhookTokenRepositoryInterface;
use Webmozart\Assert\Assert;

/**
Expand All @@ -48,6 +50,7 @@ public function __construct(
private readonly WebhookTokenGeneratorInterface $webhookTokenGenerator,
private readonly LoggerInterface $logger,
private readonly RequestStack $requestStack,
private readonly WebhookTokenRepositoryInterface $webhookTokenRepository,
) {
$this->apiClass = PagolightApi::class;
}
Expand Down Expand Up @@ -122,17 +125,32 @@ public function execute($request): void
$pagolightApi = $this->api;
Assert::isInstanceOf($pagolightApi, PagolightApi::class);

$webhookToken = $this->getWebhookToken($payment);
$convertPaymentToContract = new ConvertPaymentToContract(
$payment,
$captureUrl,
$cancelUrl,
$cancelUrl,
$notifyUrl,
$this->webhookTokenGenerator->generateForPayment($payment)->getToken(),
$webhookToken,
$pagolightApi->getAllowedTerms(),
$additionalData,
);
$this->gateway->execute($convertPaymentToContract);

try {
$this->gateway->execute($convertPaymentToContract);
} catch (\Throwable $e) {
$this->logger->error(sprintf(
'An error occurred while converting the payment to a contract: %s',
$e->getMessage(),
), $e->getTrace());
$payment->setDetails(PaymentDetailsHelper::addPaymentStatus(
$paymentDetails,
PaymentState::CANCELLED,
));

return;
}
$contract = $convertPaymentToContract->getContract();
Assert::isInstanceOf($contract, Contract::class);

Expand Down Expand Up @@ -160,4 +178,14 @@ public function supports($request): bool
$request->getModel() instanceof SyliusPaymentInterface
;
}

private function getWebhookToken(SyliusPaymentInterface $payment): string
{
$webhookToken = $this->webhookTokenRepository->findOneByPayment($payment);
if ($webhookToken !== null) {
return $webhookToken->getToken();
}

return $this->webhookTokenGenerator->generateForPayment($payment)->getToken();
}
}

0 comments on commit baabe02

Please sign in to comment.