-
Notifications
You must be signed in to change notification settings - Fork 19
PRE-3585: Set alias with Unified hosted field #315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hdelaforce-payplug
merged 1 commit into
develop
from
feature/PRE-3585_create_uhf_aliasing
Sep 2, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace PayPlug\SyliusPayPlugPlugin\Migrations; | ||
|
|
||
| use Doctrine\DBAL\Schema\Schema; | ||
| use Doctrine\Migrations\AbstractMigration; | ||
|
|
||
| /** | ||
| * Closes a TOCTOU race in PayplugCardPersister::persist(): its findOneBy-then-add dedup guard is | ||
| * reachable from two independent paths for the same alias (the synchronous frictionless capture | ||
| * and the async webhook), so without a DB-level constraint a race between them could create two | ||
| * Card rows for one alias/liveness pair. | ||
| */ | ||
| final class Version20260901120000 extends AbstractMigration | ||
| { | ||
| public function getDescription(): string | ||
| { | ||
| return 'Added a unique constraint on payplug_cards (external_id, is_live) to prevent duplicate card aliases under a race.'; | ||
| } | ||
|
|
||
| public function up(Schema $schema): void | ||
| { | ||
| // The race this constraint closes has been reachable since the payplug_cards table's | ||
| // introduction, so an existing merchant database may already carry duplicate | ||
| // (external_id, is_live) rows β remove all but the lowest-id row per pair first, or the | ||
| // CREATE UNIQUE INDEX below fails outright on any DB where that already happened. | ||
| $this->addSql('DELETE t1 FROM payplug_cards t1 INNER JOIN payplug_cards t2 ON t1.external_id = t2.external_id AND t1.is_live = t2.is_live AND t1.id > t2.id'); | ||
| $this->addSql('CREATE UNIQUE INDEX UNIQ_payplug_cards_external_id_is_live ON payplug_cards (external_id, is_live)'); | ||
| } | ||
|
|
||
| public function down(Schema $schema): void | ||
| { | ||
| $this->addSql('DROP INDEX UNIQ_payplug_cards_external_id_is_live ON payplug_cards'); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace PayPlug\SyliusPayPlugPlugin\Command; | ||
|
|
||
| class CaptureAliasPaymentRequest extends AbstractPayplugPaymentRequest | ||
| { | ||
| } |
107 changes: 107 additions & 0 deletions
107
src/Command/Handler/CaptureAliasPaymentRequestHandler.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace PayPlug\SyliusPayPlugPlugin\Command\Handler; | ||
|
|
||
| use PayPlug\SyliusPayPlugPlugin\Command\CaptureAliasPaymentRequest; | ||
| use PayPlug\SyliusPayPlugPlugin\Command\PaymentCaptureFlow; | ||
| use PayPlug\SyliusPayPlugPlugin\Entity\Card; | ||
| use PayPlug\SyliusPayPlugPlugin\Resolver\SelectedCardResolver; | ||
| use PayPlug\SyliusPayPlugPlugin\Upc\PaymentCaptureContextBuilder; | ||
| use PayPlug\SyliusPayPlugPlugin\Upc\PaymentCaptureOutcomeApplier; | ||
| use PayPlug\SyliusPayPlugPlugin\Upc\UnifiedApiPaymentCreatorInterface; | ||
| use PayplugUnifiedCore\Dto\CommonFieldsDto; | ||
| use PayplugUnifiedCore\Dto\PaymentDto; | ||
| use PayplugUnifiedCore\Exceptions\ApiException; | ||
| use PayplugUnifiedCore\Exceptions\InvalidPaymentException; | ||
| use Sylius\Abstraction\StateMachine\StateMachineInterface; | ||
| use Sylius\Bundle\PaymentBundle\Provider\PaymentRequestProviderInterface; | ||
| use Sylius\Component\Core\Model\OrderInterface; | ||
| use Sylius\Component\Core\Model\PaymentInterface; | ||
| use Sylius\Component\Payment\Model\PaymentMethodInterface; | ||
| use Sylius\Component\Payment\PaymentRequestTransitions; | ||
| use Symfony\Component\Messenger\Attribute\AsMessageHandler; | ||
|
|
||
| /** | ||
| * Pays with an already-created alias (a saved Card selected at checkout) instead of a | ||
| * hosted-fields token β the sibling capture path to CaptureHostedPaymentRequestHandler, dispatched | ||
| * by CaptureHostedPaymentRequestCommandProvider when the customer picked a saved card. | ||
| */ | ||
| #[AsMessageHandler] | ||
| final class CaptureAliasPaymentRequestHandler | ||
| { | ||
| public function __construct( | ||
| private PaymentRequestProviderInterface $paymentRequestProvider, | ||
| private StateMachineInterface $stateMachine, | ||
| private UnifiedApiPaymentCreatorInterface $unifiedApiPaymentCreator, | ||
| private SelectedCardResolver $selectedCardResolver, | ||
| private PaymentCaptureContextBuilder $contextBuilder, | ||
| private PaymentCaptureOutcomeApplier $outcomeApplier, | ||
| ) { | ||
| } | ||
|
|
||
| public function __invoke(CaptureAliasPaymentRequest $captureAliasPaymentRequest): void | ||
| { | ||
| $paymentRequest = $this->paymentRequestProvider->provide($captureAliasPaymentRequest); | ||
| /** @var PaymentInterface $payment */ | ||
| $payment = $paymentRequest->getPayment(); | ||
|
|
||
| try { | ||
| $method = $this->contextBuilder->resolvePaymentMethod($payment); | ||
|
|
||
| $card = $this->selectedCardResolver->resolve(); | ||
| if (null === $card) { | ||
| throw new \LogicException('No saved card alias selected for the payment.'); | ||
| } | ||
|
adumont-payplug marked this conversation as resolved.
|
||
| [$amount, $currencyCode] = $this->contextBuilder->resolveAmountAndCurrency($payment); | ||
| [$accountId, $submerchantExternalId] = $this->contextBuilder->resolveGatewayCredentials($method); | ||
|
|
||
| $order = $this->assertCardBelongsToOrder($card, $payment->getOrder(), $method); | ||
| $common = $this->contextBuilder->buildCommonFields($accountId, $amount, $currencyCode, $submerchantExternalId, $paymentRequest, $order); | ||
| $dto = $this->buildPaymentDto($common, $card, $order); | ||
|
|
||
| $output = $this->unifiedApiPaymentCreator->createPayment($dto); | ||
| } catch (ApiException | InvalidPaymentException | \LogicException $e) { | ||
| $this->outcomeApplier->failPaymentRequest($paymentRequest, $payment, $e, PaymentCaptureFlow::Alias); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $payment->setDetails([ | ||
| ...$payment->getDetails(), | ||
| 'alias_id' => $card->getExternalId(), | ||
| 'alias_payment_created_at' => (new \DateTimeImmutable())->format(\DateTimeInterface::ATOM), | ||
| ...$this->contextBuilder->resolveHostedFieldsIds($output->body), | ||
| ]); | ||
|
|
||
| $this->outcomeApplier->applyOutcome($paymentRequest, $payment, $output); | ||
|
|
||
| $this->stateMachine->apply($paymentRequest, PaymentRequestTransitions::GRAPH, PaymentRequestTransitions::TRANSITION_COMPLETE); | ||
| } | ||
|
|
||
| private function assertCardBelongsToOrder( | ||
| Card $card, | ||
| ?OrderInterface $order, | ||
| PaymentMethodInterface $method, | ||
| ): OrderInterface { | ||
| if (null === $order || $card->getCustomer() !== $order->getCustomer() || $card->getPaymentMethod() !== $method) { | ||
| throw new \LogicException('Selected card does not belong to the paying customer or payment method.'); | ||
| } | ||
|
|
||
| return $order; | ||
| } | ||
|
|
||
| private function buildPaymentDto(CommonFieldsDto $common, Card $card, OrderInterface $order): PaymentDto | ||
| { | ||
| $customerDto = $this->contextBuilder->buildCustomerDto($order); | ||
| $browserDto = $this->contextBuilder->buildBrowserDto(); | ||
|
|
||
| $fullName = $this->contextBuilder->resolveFullNameForCardDetails($order); | ||
| $paymentMethod = null !== $fullName | ||
| ? ['details' => ['fullName' => $fullName]] | ||
| : null; | ||
|
|
||
| return new PaymentDto($common, $card->getExternalId(), 'ONE_CLICK', $browserDto, $customerDto, $paymentMethod); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.