-
Notifications
You must be signed in to change notification settings - Fork 0
PPSYL-138 - Use Payment request flow for payplug #169
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
Changes from all commits
53ccf94
d362e25
9f2272a
3c3fa5b
ec468e4
4a9eea9
f1a0ac2
2df316b
759bd9e
df00d8d
f7bba93
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace PayPlug\SyliusPayPlugPlugin\Command; | ||
|
|
||
| use Sylius\Bundle\PaymentBundle\Command\PaymentRequestHashAwareInterface; | ||
| use Sylius\Bundle\PaymentBundle\Command\PaymentRequestHashAwareTrait; | ||
|
|
||
| abstract class AbstractPayplugPaymentRequest implements PaymentRequestHashAwareInterface | ||
| { | ||
| use PaymentRequestHashAwareTrait; | ||
|
|
||
| public function __construct(protected ?string $hash) {} | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace PayPlug\SyliusPayPlugPlugin\Command; | ||
|
|
||
| class CapturePaymentRequest extends AbstractPayplugPaymentRequest | ||
| { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace PayPlug\SyliusPayPlugPlugin\Command\Handler; | ||
|
|
||
| use PayPlug\SyliusPayPlugPlugin\ApiClient\PayPlugApiClientFactoryInterface; | ||
| use PayPlug\SyliusPayPlugPlugin\ApiClient\PayPlugApiClientInterface; | ||
| use PayPlug\SyliusPayPlugPlugin\Command\CapturePaymentRequest; | ||
| use PayPlug\SyliusPayPlugPlugin\Creator\PayPlugPaymentDataCreator; | ||
| use Sylius\Abstraction\StateMachine\StateMachineInterface; | ||
| use Sylius\Bundle\CoreBundle\OrderPay\Provider\UrlProviderInterface; | ||
| use Sylius\Bundle\PaymentBundle\Provider\PaymentRequestProviderInterface; | ||
| use Sylius\Component\Payment\PaymentRequestTransitions; | ||
| use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
| use Symfony\Component\Messenger\Attribute\AsMessageHandler; | ||
| use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
|
|
||
| #[AsMessageHandler] | ||
| final class CapturePaymentRequestHandler | ||
| { | ||
| public function __construct( | ||
| private PaymentRequestProviderInterface $paymentRequestProvider, | ||
| private StateMachineInterface $stateMachine, | ||
| private PayPlugApiClientFactoryInterface $apiClientFactory, | ||
| private PayPlugPaymentDataCreator $paymentDataCreator, | ||
| #[Autowire(service: 'sylius_shop.provider.order_pay.after_pay_url')] | ||
| private UrlProviderInterface $afterPayUrlProvider, | ||
| private UrlGeneratorInterface $urlGenerator, | ||
| ) {} | ||
|
|
||
| public function __invoke(CapturePaymentRequest $capturePaymentRequest): void | ||
| { | ||
| // Retrieve the current PaymentRequest based on the hash provided in the CapturePaymentRequest command | ||
| $paymentRequest = $this->paymentRequestProvider->provide($capturePaymentRequest); | ||
| $payment = $paymentRequest->getPayment(); | ||
|
|
||
| $client = $this->apiClientFactory->createForPaymentMethod($paymentRequest->getPayment()->getMethod()); | ||
| $data = $this->paymentDataCreator->create($payment)->getArrayCopy(); | ||
|
|
||
| $returnUrl = $this->afterPayUrlProvider->getUrl($paymentRequest, UrlGeneratorInterface::ABSOLUTE_URL); | ||
| $data['hosted_payment'] = [ | ||
| 'return_url' => $returnUrl, | ||
| 'cancel_url' => $returnUrl . '?&' . http_build_query(['status' => PayPlugApiClientInterface::STATUS_CANCELED]), | ||
| ]; | ||
|
|
||
| $notificationUrl = $this->urlGenerator->generate('sylius_payment_method_notify', ['code' => $payment->getMethod()?->getCode()], UrlGeneratorInterface::ABSOLUTE_URL); | ||
| $details['notification_url'] = $notificationUrl; | ||
|
|
||
| $paymentRequest->setPayload($data); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand why you set this here, but the purpose of the |
||
| $payplugPayment = $client->createPayment($data); | ||
| $arrayPayplugPayment = (array) $payplugPayment; | ||
| $payment->setDetails([ | ||
| ...$payment->getDetails(), | ||
| 'status' => PayPlugApiClientInterface::STATUS_CREATED, | ||
| 'payment_id' => $payplugPayment->__get('id'), | ||
| 'payplug_response' => $arrayPayplugPayment, | ||
| ]); | ||
|
|
||
| $paymentRequest->setResponseData(array_merge($arrayPayplugPayment, [ | ||
| 'payment_id' => $payplugPayment->__get('id'), | ||
| 'redirect_url' => $payplugPayment->hosted_payment->payment_url | ||
| ])); | ||
|
|
||
| $this->stateMachine->apply( | ||
| $paymentRequest, | ||
| PaymentRequestTransitions::GRAPH, | ||
| PaymentRequestTransitions::TRANSITION_COMPLETE | ||
| ); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see why a specific service to create this url can be required...