From d31e18d5296ed9bdafae1b6281d16135a00b1a4d Mon Sep 17 00:00:00 2001 From: arti0090 Date: Thu, 5 Sep 2024 11:06:23 +0200 Subject: [PATCH] Add blink input field and capture its data inside of action --- config/services/form.php | 9 +++ config/services/payum/action.php | 5 ++ .../PaymentDetailsTransformer.php | 23 +++++++ src/Form/Type/CompleteTypeExtension.php | 34 ++++++++++ src/Form/Type/PaymentDetailsType.php | 23 +++++++ .../Api/CreateBlik0TransactionAction.php | 63 +++++++++++++++++++ src/Payum/Action/CaptureAction.php | 14 +++++ .../Request/Api/CreateBlik0Transaction.php | 22 +++++++ templates/blik.html.twig | 1 + 9 files changed, 194 insertions(+) create mode 100644 src/Form/DataTransformer/PaymentDetailsTransformer.php create mode 100644 src/Form/Type/CompleteTypeExtension.php create mode 100644 src/Form/Type/PaymentDetailsType.php create mode 100644 src/Payum/Action/Api/CreateBlik0TransactionAction.php create mode 100644 src/Payum/Request/Api/CreateBlik0Transaction.php diff --git a/config/services/form.php b/config/services/form.php index 1a462ff9..08614474 100644 --- a/config/services/form.php +++ b/config/services/form.php @@ -5,8 +5,10 @@ namespace Symfony\Component\DependencyInjection\Loader\Configurator; use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\PreventSavingEmptyClientSecretListener; +use CommerceWeavers\SyliusTpayPlugin\Form\Type\CompleteTypeExtension; use CommerceWeavers\SyliusTpayPlugin\Form\Type\TpayGatewayConfigurationType; use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\TpayGatewayFactory; +use Sylius\Bundle\CoreBundle\Form\Type\Checkout\CompleteType; return function(ContainerConfigurator $container): void { $services = $container->services(); @@ -23,4 +25,11 @@ ; $services->set(PreventSavingEmptyClientSecretListener::class); + + $services->set(CompleteTypeExtension::class) + ->tag( + 'form.type_extension', + ['extended_type' => CompleteType::class] + ) + ; }; diff --git a/config/services/payum/action.php b/config/services/payum/action.php index dce57b5e..55808072 100644 --- a/config/services/payum/action.php +++ b/config/services/payum/action.php @@ -4,6 +4,7 @@ namespace Symfony\Component\DependencyInjection\Loader\Configurator; +use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateBlik0TransactionAction; use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateTransactionAction; use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\FetchPaymentDetailsAction; use CommerceWeavers\SyliusTpayPlugin\Payum\Action\CaptureAction; @@ -24,6 +25,10 @@ ->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.create_transaction']) ; + $services->set(CreateBlik0TransactionAction::class) + ->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.create_blik0_transaction']) + ; + $services->set(FetchPaymentDetailsAction::class) ->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.fetch_payment_details']) ; diff --git a/src/Form/DataTransformer/PaymentDetailsTransformer.php b/src/Form/DataTransformer/PaymentDetailsTransformer.php new file mode 100644 index 00000000..eb9a1443 --- /dev/null +++ b/src/Form/DataTransformer/PaymentDetailsTransformer.php @@ -0,0 +1,23 @@ + $value]; + } +} diff --git a/src/Form/Type/CompleteTypeExtension.php b/src/Form/Type/CompleteTypeExtension.php new file mode 100644 index 00000000..6658cf03 --- /dev/null +++ b/src/Form/Type/CompleteTypeExtension.php @@ -0,0 +1,34 @@ +add('notes', TextareaType::class, [ + 'label' => 'sylius.form.notes', + 'required' => false, + ]); + + $builder->add('others', PaymentDetailsType::class, [ + 'label' => 'commerce_weavers_sylius_tpay.payment.blik.token', + // TODO missing validation + 'property_path' => 'payments[0].details', // TODO looks awfull and what about other payments? +// 'mapped' => false, + 'required' => false, + ]); + } + + public static function getExtendedTypes(): iterable + { + return [CompleteType::class]; + } +} diff --git a/src/Form/Type/PaymentDetailsType.php b/src/Form/Type/PaymentDetailsType.php new file mode 100644 index 00000000..36385bcb --- /dev/null +++ b/src/Form/Type/PaymentDetailsType.php @@ -0,0 +1,23 @@ +addModelTransformer(new PaymentDetailsTransformer()); + } + + public function getParent(): string + { + return TextType::class; + } +} diff --git a/src/Payum/Action/Api/CreateBlik0TransactionAction.php b/src/Payum/Action/Api/CreateBlik0TransactionAction.php new file mode 100644 index 00000000..816cf633 --- /dev/null +++ b/src/Payum/Action/Api/CreateBlik0TransactionAction.php @@ -0,0 +1,63 @@ +getModel(); + $details = $model->getDetails(); + + $order = $model->getOrder(); + $customer = $order->getCustomer(); + $billingAddress = $order->getBillingAddress(); + + $blikToken = $model->getDetails()['blik']; + + $response = $this->api->transactions()->createTransaction([ + 'amount' => number_format($model->getAmount() / 100, 2, thousands_separator: ''), + 'description' => sprintf('zamówienie #%s', $order->getNumber()), // TODO: Introduce translations + 'payer' => [ + 'email' => $customer->getEmail(), + 'name' => $billingAddress->getFullName(), + ], + 'pay' => [ + 'groupId' => 150, + 'blikPaymentData' => [ + 'blikToken' => $blikToken, + ], + ], + 'callbacks' => [ + 'payerUrls' => [ + 'success' => $request->getAfterUrl(), + 'error' => $request->getAfterUrl(), + ], + ], + ]); + + // blik token could be removed here from $details + $details['tpay']['transaction_id'] = $response['transactionId']; + + $model->setDetails($details); + } + + public function supports($request): bool + { + return $request instanceof CreateBlik0Transaction && $request->getModel() instanceof PaymentInterface; + } +} diff --git a/src/Payum/Action/CaptureAction.php b/src/Payum/Action/CaptureAction.php index 2260a3d7..99d19c82 100644 --- a/src/Payum/Action/CaptureAction.php +++ b/src/Payum/Action/CaptureAction.php @@ -4,6 +4,7 @@ namespace CommerceWeavers\SyliusTpayPlugin\Payum\Action; +use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\CreateBlik0Transaction; use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\CreateTransaction; use Payum\Core\Action\ActionInterface; use Payum\Core\GatewayAwareInterface; @@ -24,6 +25,14 @@ public function execute($request): void /** @var PaymentInterface $model */ $model = $request->getModel(); + if ($this->transactionIsBlik($model)) { + $this->gateway->execute( + new CreateBlik0Transaction($request->getToken()->getAfterUrl(), $model), + ); + + return; + } + $this->gateway->execute( new CreateTransaction($request->getToken()->getAfterUrl(), $model), ); @@ -37,4 +46,9 @@ public function supports($request): bool { return $request instanceof Capture && $request->getModel() instanceof PaymentInterface; } + + private function transactionIsBlik($model): bool + { + return array_key_exists('blik', $model->getDetails()); + } } diff --git a/src/Payum/Request/Api/CreateBlik0Transaction.php b/src/Payum/Request/Api/CreateBlik0Transaction.php new file mode 100644 index 00000000..0dfcbcb5 --- /dev/null +++ b/src/Payum/Request/Api/CreateBlik0Transaction.php @@ -0,0 +1,22 @@ +afterUrl; + } +} diff --git a/templates/blik.html.twig b/templates/blik.html.twig index 53ab59b4..255a9a22 100644 --- a/templates/blik.html.twig +++ b/templates/blik.html.twig @@ -19,6 +19,7 @@

{{ money.format(payment.amount, payment.currencyCode) }}

+ {{ form_row(form.others) }}