From f51cd8f123a9a17e44f4a74a13e81657dc757612 Mon Sep 17 00:00:00 2001 From: arti0090 Date: Wed, 4 Sep 2024 08:58:31 +0200 Subject: [PATCH 1/2] Blik input --- config/packages.php | 9 +++++ config/packages/events.php | 23 +++++++++++ templates/blik.html.twig | 38 +++++++++++++++++++ .../packages/commerce_weavers_tpay.yaml | 2 + translations/messages.en.yaml | 5 +++ translations/messages.pl.yaml | 4 ++ 6 files changed, 81 insertions(+) create mode 100644 config/packages.php create mode 100644 config/packages/events.php create mode 100644 templates/blik.html.twig create mode 100644 tests/Application/config/packages/commerce_weavers_tpay.yaml diff --git a/config/packages.php b/config/packages.php new file mode 100644 index 00000000..daf459e9 --- /dev/null +++ b/config/packages.php @@ -0,0 +1,9 @@ +import('packages/**/*.php'); +}; diff --git a/config/packages/events.php b/config/packages/events.php new file mode 100644 index 00000000..6fc9cb45 --- /dev/null +++ b/config/packages/events.php @@ -0,0 +1,23 @@ +extension('sylius_ui', [ + 'events' => [ + 'sylius.shop.checkout.complete.summary' => [ + 'blocks' => [ + 'my_block_name' => [ + 'template' => '@CommerceWeaversSyliusTpayPlugin/blik.html.twig', + 'priority' => 5, + 'context' => [ + 'message' => 'Hello!', + ], + ], + ], + ], + ], + ]); +}; diff --git a/templates/blik.html.twig b/templates/blik.html.twig new file mode 100644 index 00000000..53ab59b4 --- /dev/null +++ b/templates/blik.html.twig @@ -0,0 +1,38 @@ +
+
+ {% import "@SyliusShop/Common/Macro/money.html.twig" as money %} + + {% set state = order.paymentState %} + + {% if state != 'cart' %} + {% include "@SyliusShop/Common/Order/Label/PaymentState/orderPaymentState.html.twig" %} + {% endif %} + + {% for payment in order.payments %} + {% set state = payment.state %} + +
+ +
+
+ {{ payment.method }} +
+

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

+ + + + + {% if state != 'cart' %} +

+ {% include "@SyliusShop/Common/Order/Label/PaymentState/singlePaymentState.html.twig" with { 'state': state } %} +

+ {% endif %} +
+
+ {% endfor %} +
+
+
+
diff --git a/tests/Application/config/packages/commerce_weavers_tpay.yaml b/tests/Application/config/packages/commerce_weavers_tpay.yaml new file mode 100644 index 00000000..f758fcec --- /dev/null +++ b/tests/Application/config/packages/commerce_weavers_tpay.yaml @@ -0,0 +1,2 @@ +imports: + - { resource: "@CommerceWeaversSyliusTpayPlugin/config/packages.php" } diff --git a/translations/messages.en.yaml b/translations/messages.en.yaml index 76cc3b46..b92b90ea 100644 --- a/translations/messages.en.yaml +++ b/translations/messages.en.yaml @@ -5,3 +5,8 @@ commerce_weavers_sylius_tpay: client_id: 'Client ID' client_secret: 'Client secret' production_mode: 'Production mode' + + payment: + blik: + token: 'BLIK token' + diff --git a/translations/messages.pl.yaml b/translations/messages.pl.yaml index 95275946..2cda7c78 100644 --- a/translations/messages.pl.yaml +++ b/translations/messages.pl.yaml @@ -5,3 +5,7 @@ commerce_weavers_sylius_tpay: client_id: 'Identyfikator klienta' client_secret: 'Hasło klienta' production_mode: 'Tryb produkcyjny' + + payment: + blik: + token: 'Token BLIK' From d31e18d5296ed9bdafae1b6281d16135a00b1a4d Mon Sep 17 00:00:00 2001 From: arti0090 Date: Thu, 5 Sep 2024 11:06:23 +0200 Subject: [PATCH 2/2] 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) }}