Check if the retrieved payment request is not already finalized

This commit is contained in:
Francis Hilaire 2024-10-31 17:29:26 +01:00
parent 6b1e89f845
commit e5840012a6
No known key found for this signature in database
GPG key ID: 3392F830BF33D421
2 changed files with 25 additions and 10 deletions

View file

@ -14,11 +14,14 @@ declare(strict_types=1);
namespace Sylius\Bundle\CoreBundle\OrderPay\Provider;
use Sylius\Bundle\CoreBundle\OrderPay\Resolver\PaymentToPayResolverInterface;
use Sylius\Bundle\PaymentBundle\Checker\FinalizedPaymentRequestCheckerInterface;
use Sylius\Bundle\PaymentBundle\Provider\DefaultActionProviderInterface;
use Sylius\Bundle\PaymentBundle\Provider\DefaultPayloadProviderInterface;
use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Payment\Factory\PaymentRequestFactoryInterface;
use Sylius\Component\Payment\Model\PaymentInterface;
use Sylius\Component\Payment\Model\PaymentMethodInterface;
use Sylius\Component\Payment\Model\PaymentRequestInterface;
use Sylius\Component\Payment\Repository\PaymentRequestRepositoryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
@ -37,6 +40,7 @@ final class PaymentRequestPayResponseProvider implements PayResponseProviderInte
private PaymentRequestRepositoryInterface $paymentRequestRepository,
private DefaultActionProviderInterface $defaultActionProvider,
private DefaultPayloadProviderInterface $defaultPayloadProvider,
private FinalizedPaymentRequestCheckerInterface $finalizedPaymentRequestChecker,
private PaymentToPayResolverInterface $paymentToPayResolver,
private UrlProviderInterface $paymentRequestPayUrlProvider,
) {
@ -50,6 +54,23 @@ final class PaymentRequestPayResponseProvider implements PayResponseProviderInte
$paymentMethod = $payment->getMethod();
Assert::notNull($paymentMethod, sprintf('Payment (id %s) must have payment method.', $payment->getId()));
$paymentRequest = $this->getPaymentRequest($payment, $paymentMethod);
if (null === $paymentRequest->getHash()) {
$this->paymentRequestRepository->add($paymentRequest);
}
return new RedirectResponse($this->paymentRequestPayUrlProvider->getUrl($paymentRequest));
}
public function supports(RequestConfiguration $requestConfiguration, OrderInterface $order): bool
{
return true;
}
private function getPaymentRequest(
PaymentInterface $payment,
PaymentMethodInterface $paymentMethod
): PaymentRequestInterface {
$paymentRequest = $this->paymentRequestFactory->create($payment, $paymentMethod);
$action = $this->defaultActionProvider->getAction($paymentRequest);
$paymentRequest->setAction($action);
@ -60,18 +81,11 @@ final class PaymentRequestPayResponseProvider implements PayResponseProviderInte
$paymentMethod
);
if (null === $existingPaymentRequest) {
if (null === $existingPaymentRequest || $this->finalizedPaymentRequestChecker->isFinal($existingPaymentRequest)) {
$paymentRequest->setPayload($this->defaultPayloadProvider->getPayload($paymentRequest));
$this->paymentRequestRepository->add($paymentRequest);
} else {
$paymentRequest = $existingPaymentRequest;
return $paymentRequest;
}
return new RedirectResponse($this->paymentRequestPayUrlProvider->getUrl($paymentRequest));
}
public function supports(RequestConfiguration $requestConfiguration, OrderInterface $order): bool
{
return true;
return $existingPaymentRequest;
}
}

View file

@ -36,6 +36,7 @@
<argument type="service" id="sylius.repository.payment_request" />
<argument type="service" id="sylius.provider.default_action" />
<argument type="service" id="sylius.provider.default_payload" />
<argument type="service" id="sylius.checker.finalized_payment_request" />
</service>
<service abstract="true" id="sylius.provider.order_pay.pay_response.no_payment" class="Sylius\Bundle\CoreBundle\OrderPay\Provider\NoPaymentPayResponseProvider" />