|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PayPlug\SyliusPayPlugPlugin\Command\Handler; |
| 6 | + |
| 7 | +use PayPlug\SyliusPayPlugPlugin\Command\CaptureAliasPaymentRequest; |
| 8 | +use PayPlug\SyliusPayPlugPlugin\Command\PaymentCaptureFlow; |
| 9 | +use PayPlug\SyliusPayPlugPlugin\Entity\Card; |
| 10 | +use PayPlug\SyliusPayPlugPlugin\Resolver\SelectedCardResolver; |
| 11 | +use PayPlug\SyliusPayPlugPlugin\Upc\PaymentCaptureContextBuilder; |
| 12 | +use PayPlug\SyliusPayPlugPlugin\Upc\PaymentCaptureOutcomeApplier; |
| 13 | +use PayPlug\SyliusPayPlugPlugin\Upc\UnifiedApiPaymentCreatorInterface; |
| 14 | +use PayplugUnifiedCore\Dto\CommonFieldsDto; |
| 15 | +use PayplugUnifiedCore\Dto\PaymentDto; |
| 16 | +use PayplugUnifiedCore\Exceptions\ApiException; |
| 17 | +use PayplugUnifiedCore\Exceptions\InvalidPaymentException; |
| 18 | +use Sylius\Abstraction\StateMachine\StateMachineInterface; |
| 19 | +use Sylius\Bundle\PaymentBundle\Provider\PaymentRequestProviderInterface; |
| 20 | +use Sylius\Component\Core\Model\OrderInterface; |
| 21 | +use Sylius\Component\Core\Model\PaymentInterface; |
| 22 | +use Sylius\Component\Payment\Model\PaymentMethodInterface; |
| 23 | +use Sylius\Component\Payment\PaymentRequestTransitions; |
| 24 | +use Symfony\Component\Messenger\Attribute\AsMessageHandler; |
| 25 | + |
| 26 | +/** |
| 27 | + * Pays with an already-created alias (a saved Card selected at checkout) instead of a |
| 28 | + * hosted-fields token — the sibling capture path to CaptureHostedPaymentRequestHandler, dispatched |
| 29 | + * by CaptureHostedPaymentRequestCommandProvider when the customer picked a saved card. |
| 30 | + */ |
| 31 | +#[AsMessageHandler] |
| 32 | +final class CaptureAliasPaymentRequestHandler |
| 33 | +{ |
| 34 | + public function __construct( |
| 35 | + private PaymentRequestProviderInterface $paymentRequestProvider, |
| 36 | + private StateMachineInterface $stateMachine, |
| 37 | + private UnifiedApiPaymentCreatorInterface $unifiedApiPaymentCreator, |
| 38 | + private SelectedCardResolver $selectedCardResolver, |
| 39 | + private PaymentCaptureContextBuilder $contextBuilder, |
| 40 | + private PaymentCaptureOutcomeApplier $outcomeApplier, |
| 41 | + ) { |
| 42 | + } |
| 43 | + |
| 44 | + public function __invoke(CaptureAliasPaymentRequest $captureAliasPaymentRequest): void |
| 45 | + { |
| 46 | + $paymentRequest = $this->paymentRequestProvider->provide($captureAliasPaymentRequest); |
| 47 | + /** @var PaymentInterface $payment */ |
| 48 | + $payment = $paymentRequest->getPayment(); |
| 49 | + |
| 50 | + try { |
| 51 | + $method = $this->contextBuilder->resolvePaymentMethod($payment); |
| 52 | + |
| 53 | + $card = $this->selectedCardResolver->resolve(); |
| 54 | + if (null === $card) { |
| 55 | + throw new \LogicException('No saved card alias selected for the payment.'); |
| 56 | + } |
| 57 | + [$amount, $currencyCode] = $this->contextBuilder->resolveAmountAndCurrency($payment); |
| 58 | + [$accountId, $submerchantExternalId] = $this->contextBuilder->resolveGatewayCredentials($method); |
| 59 | + |
| 60 | + $order = $this->assertCardBelongsToOrder($card, $payment->getOrder(), $method); |
| 61 | + $common = $this->contextBuilder->buildCommonFields($accountId, $amount, $currencyCode, $submerchantExternalId, $paymentRequest, $order); |
| 62 | + $dto = $this->buildPaymentDto($common, $card, $order); |
| 63 | + |
| 64 | + $output = $this->unifiedApiPaymentCreator->createPayment($dto); |
| 65 | + } catch (ApiException | InvalidPaymentException | \LogicException $e) { |
| 66 | + $this->outcomeApplier->failPaymentRequest($paymentRequest, $payment, $e, PaymentCaptureFlow::Alias); |
| 67 | + |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + $payment->setDetails([ |
| 72 | + ...$payment->getDetails(), |
| 73 | + 'alias_id' => $card->getExternalId(), |
| 74 | + 'alias_payment_created_at' => (new \DateTimeImmutable())->format(\DateTimeInterface::ATOM), |
| 75 | + ...$this->contextBuilder->resolveHostedFieldsIds($output->body), |
| 76 | + ]); |
| 77 | + |
| 78 | + $this->outcomeApplier->applyOutcome($paymentRequest, $payment, $output); |
| 79 | + |
| 80 | + $this->stateMachine->apply($paymentRequest, PaymentRequestTransitions::GRAPH, PaymentRequestTransitions::TRANSITION_COMPLETE); |
| 81 | + } |
| 82 | + |
| 83 | + private function assertCardBelongsToOrder( |
| 84 | + Card $card, |
| 85 | + ?OrderInterface $order, |
| 86 | + PaymentMethodInterface $method, |
| 87 | + ): OrderInterface { |
| 88 | + if (null === $order || $card->getCustomer() !== $order->getCustomer() || $card->getPaymentMethod() !== $method) { |
| 89 | + throw new \LogicException('Selected card does not belong to the paying customer or payment method.'); |
| 90 | + } |
| 91 | + |
| 92 | + return $order; |
| 93 | + } |
| 94 | + |
| 95 | + private function buildPaymentDto(CommonFieldsDto $common, Card $card, OrderInterface $order): PaymentDto |
| 96 | + { |
| 97 | + $customerDto = $this->contextBuilder->buildCustomerDto($order); |
| 98 | + $browserDto = $this->contextBuilder->buildBrowserDto(); |
| 99 | + |
| 100 | + $fullName = $this->contextBuilder->resolveFullNameForCardDetails($order); |
| 101 | + $paymentMethod = null !== $fullName |
| 102 | + ? ['details' => ['fullName' => $fullName]] |
| 103 | + : null; |
| 104 | + |
| 105 | + return new PaymentDto($common, $card->getExternalId(), 'ONE_CLICK', $browserDto, $customerDto, $paymentMethod); |
| 106 | + } |
| 107 | +} |
0 commit comments