From bbc8c240aa5dd54e0443dc4a7544ce5ad09c3fe3 Mon Sep 17 00:00:00 2001 From: arti0090 Date: Wed, 4 Sep 2024 08:58:31 +0200 Subject: [PATCH 01/11] Blik input --- config/packages.php | 9 +++++ config/packages/events.php | 23 +++++++++++ templates/blik.html.twig | 38 +++++++++++++++++++ .../packages/commerce_weavers_tpay.yaml | 1 + translations/messages.en.yaml | 5 +++ translations/messages.pl.yaml | 4 ++ 6 files changed, 80 insertions(+) create mode 100644 config/packages.php create mode 100644 config/packages/events.php create mode 100644 templates/blik.html.twig 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 index 5defcc58..5968a4f4 100644 --- a/tests/Application/config/packages/commerce_weavers_tpay.yaml +++ b/tests/Application/config/packages/commerce_weavers_tpay.yaml @@ -1,2 +1,3 @@ imports: - { resource: "@CommerceWeaversSyliusTpayPlugin/config/config.php" } + - { 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 be73810c27ad5f61a575a58c554f8cc2618561af Mon Sep 17 00:00:00 2001 From: arti0090 Date: Thu, 5 Sep 2024 11:06:23 +0200 Subject: [PATCH 02/11] Add blink input field and capture its data inside of action --- config/services/form.php | 9 +++ config/services/payum/action.php | 4 ++ .../PaymentDetailsTransformer.php | 23 +++++++ src/Form/Type/CompleteTypeExtension.php | 34 ++++++++++ src/Form/Type/PaymentDetailsType.php | 23 +++++++ .../Api/CreateBlik0TransactionAction.php | 63 +++++++++++++++++++ src/Payum/Action/CaptureAction.php | 13 ++++ .../Request/Api/CreateBlik0Transaction.php | 22 +++++++ templates/blik.html.twig | 1 + 9 files changed, 192 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 def41c28..b7420519 100644 --- a/config/services/payum/action.php +++ b/config/services/payum/action.php @@ -38,6 +38,10 @@ ->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.notify']) ; + $services->set(CreateBlik0TransactionAction::class) + ->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.create_blik0_transaction']) + ; + $services->set(GetStatusAction::class) ->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.get_status']) ; 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 7cb1d5bc..726e2c1e 100644 --- a/src/Payum/Action/CaptureAction.php +++ b/src/Payum/Action/CaptureAction.php @@ -29,6 +29,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( $this->createTransactionFactory->createNewWithModel($request->getToken()), ); @@ -42,4 +50,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) }} From c8d3d35c3ce6e5cc0c0d9b1c8378e4da4eae8408 Mon Sep 17 00:00:00 2001 From: arti0090 Date: Thu, 5 Sep 2024 14:40:04 +0200 Subject: [PATCH 03/11] Bunch of PR comment fixes --- config/{packages => config}/events.php | 5 +---- config/packages.php | 9 --------- config/services/form.php | 7 +------ config/services/payum/action.php | 1 + src/Form/DataTransformer/PaymentDetailsTransformer.php | 2 +- src/Form/Type/CompleteTypeExtension.php | 1 - src/Payum/Action/Api/CreateBlik0TransactionAction.php | 2 +- src/Payum/Action/CaptureAction.php | 3 ++- src/Payum/Request/Api/CreateBlik0Transaction.php | 6 ------ templates/blik.html.twig | 4 ---- translations/messages.en.yaml | 2 +- translations/messages.pl.yaml | 2 +- 12 files changed, 9 insertions(+), 35 deletions(-) rename config/{packages => config}/events.php (78%) delete mode 100644 config/packages.php diff --git a/config/packages/events.php b/config/config/events.php similarity index 78% rename from config/packages/events.php rename to config/config/events.php index 6fc9cb45..71d8e1f9 100644 --- a/config/packages/events.php +++ b/config/config/events.php @@ -9,12 +9,9 @@ 'events' => [ 'sylius.shop.checkout.complete.summary' => [ 'blocks' => [ - 'my_block_name' => [ + 'blik' => [ 'template' => '@CommerceWeaversSyliusTpayPlugin/blik.html.twig', 'priority' => 5, - 'context' => [ - 'message' => 'Hello!', - ], ], ], ], diff --git a/config/packages.php b/config/packages.php deleted file mode 100644 index daf459e9..00000000 --- a/config/packages.php +++ /dev/null @@ -1,9 +0,0 @@ -import('packages/**/*.php'); -}; diff --git a/config/services/form.php b/config/services/form.php index 08614474..6ec6984a 100644 --- a/config/services/form.php +++ b/config/services/form.php @@ -26,10 +26,5 @@ $services->set(PreventSavingEmptyClientSecretListener::class); - $services->set(CompleteTypeExtension::class) - ->tag( - 'form.type_extension', - ['extended_type' => CompleteType::class] - ) - ; + $services->set(CompleteTypeExtension::class)->tag('form.type_extension'); }; diff --git a/config/services/payum/action.php b/config/services/payum/action.php index b7420519..779eaf07 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\NotifyAction; use CommerceWeavers\SyliusTpayPlugin\Payum\Action\CaptureAction; diff --git a/src/Form/DataTransformer/PaymentDetailsTransformer.php b/src/Form/DataTransformer/PaymentDetailsTransformer.php index eb9a1443..0283e8f0 100644 --- a/src/Form/DataTransformer/PaymentDetailsTransformer.php +++ b/src/Form/DataTransformer/PaymentDetailsTransformer.php @@ -18,6 +18,6 @@ public function transform($value): string } public function reverseTransform($value): array { - return ['blik' => $value]; + return $value ? ['blik' => $value] : []; } } diff --git a/src/Form/Type/CompleteTypeExtension.php b/src/Form/Type/CompleteTypeExtension.php index 6658cf03..e230711e 100644 --- a/src/Form/Type/CompleteTypeExtension.php +++ b/src/Form/Type/CompleteTypeExtension.php @@ -22,7 +22,6 @@ public function buildForm(FormBuilderInterface $builder, array $options): void '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, ]); } diff --git a/src/Payum/Action/Api/CreateBlik0TransactionAction.php b/src/Payum/Action/Api/CreateBlik0TransactionAction.php index 816cf633..b3740e6c 100644 --- a/src/Payum/Action/Api/CreateBlik0TransactionAction.php +++ b/src/Payum/Action/Api/CreateBlik0TransactionAction.php @@ -50,7 +50,7 @@ public function execute($request): void ], ]); - // blik token could be removed here from $details + unset($details['blik']); $details['tpay']['transaction_id'] = $response['transactionId']; $model->setDetails($details); diff --git a/src/Payum/Action/CaptureAction.php b/src/Payum/Action/CaptureAction.php index 726e2c1e..bdd8290d 100644 --- a/src/Payum/Action/CaptureAction.php +++ b/src/Payum/Action/CaptureAction.php @@ -5,6 +5,7 @@ namespace CommerceWeavers\SyliusTpayPlugin\Payum\Action; use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\CreateTransactionFactoryInterface; +use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\CreateBlik0Transaction; use Payum\Core\Action\ActionInterface; use Payum\Core\GatewayAwareInterface; use Payum\Core\GatewayAwareTrait; @@ -31,7 +32,7 @@ public function execute($request): void if ($this->transactionIsBlik($model)) { $this->gateway->execute( - new CreateBlik0Transaction($request->getToken()->getAfterUrl(), $model), + new CreateBlik0Transaction($request->getToken()), ); return; diff --git a/src/Payum/Request/Api/CreateBlik0Transaction.php b/src/Payum/Request/Api/CreateBlik0Transaction.php index 0dfcbcb5..63445159 100644 --- a/src/Payum/Request/Api/CreateBlik0Transaction.php +++ b/src/Payum/Request/Api/CreateBlik0Transaction.php @@ -9,14 +9,8 @@ class CreateBlik0Transaction extends Generic { public function __construct ( - private string $afterUrl, mixed $model, ) { parent::__construct($model); } - - public function getAfterUrl(): string - { - return $this->afterUrl; - } } diff --git a/templates/blik.html.twig b/templates/blik.html.twig index 255a9a22..e00662cb 100644 --- a/templates/blik.html.twig +++ b/templates/blik.html.twig @@ -20,10 +20,6 @@

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

{{ form_row(form.others) }} - - {% if state != 'cart' %}

diff --git a/translations/messages.en.yaml b/translations/messages.en.yaml index b92b90ea..33822ce0 100644 --- a/translations/messages.en.yaml +++ b/translations/messages.en.yaml @@ -8,5 +8,5 @@ commerce_weavers_sylius_tpay: payment: blik: - token: 'BLIK token' + token: 'BLIK code' diff --git a/translations/messages.pl.yaml b/translations/messages.pl.yaml index 2cda7c78..0848b435 100644 --- a/translations/messages.pl.yaml +++ b/translations/messages.pl.yaml @@ -8,4 +8,4 @@ commerce_weavers_sylius_tpay: payment: blik: - token: 'Token BLIK' + token: 'Kod BLIK' From 58ea867a4f77de53045eb76c5bed5ae8f6dda62d Mon Sep 17 00:00:00 2001 From: arti0090 Date: Wed, 4 Sep 2024 08:58:31 +0200 Subject: [PATCH 04/11] Blik input --- config/packages.php | 9 ++++++++ config/packages/events.php | 23 +++++++++++++++++++ .../packages/commerce_weavers_tpay.yaml | 1 - 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 config/packages.php create mode 100644 config/packages/events.php 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/tests/Application/config/packages/commerce_weavers_tpay.yaml b/tests/Application/config/packages/commerce_weavers_tpay.yaml index 5968a4f4..f758fcec 100644 --- a/tests/Application/config/packages/commerce_weavers_tpay.yaml +++ b/tests/Application/config/packages/commerce_weavers_tpay.yaml @@ -1,3 +1,2 @@ imports: - - { resource: "@CommerceWeaversSyliusTpayPlugin/config/config.php" } - { resource: "@CommerceWeaversSyliusTpayPlugin/config/packages.php" } From 61ef6c678bea41230ef157234e95ccc27a490d2b Mon Sep 17 00:00:00 2001 From: arti0090 Date: Thu, 5 Sep 2024 11:06:23 +0200 Subject: [PATCH 05/11] Add blink input field and capture its data inside of action --- config/services/form.php | 7 +++++- config/services/payum/action.php | 23 ++++--------------- .../PaymentDetailsTransformer.php | 2 +- src/Form/Type/CompleteTypeExtension.php | 1 + .../Api/CreateBlik0TransactionAction.php | 2 +- src/Payum/Action/CaptureAction.php | 11 +++------ .../Request/Api/CreateBlik0Transaction.php | 6 +++++ templates/blik.html.twig | 4 ++++ 8 files changed, 27 insertions(+), 29 deletions(-) diff --git a/config/services/form.php b/config/services/form.php index 6ec6984a..08614474 100644 --- a/config/services/form.php +++ b/config/services/form.php @@ -26,5 +26,10 @@ $services->set(PreventSavingEmptyClientSecretListener::class); - $services->set(CompleteTypeExtension::class)->tag('form.type_extension'); + $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 779eaf07..55808072 100644 --- a/config/services/payum/action.php +++ b/config/services/payum/action.php @@ -6,10 +6,9 @@ use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateBlik0TransactionAction; use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateTransactionAction; -use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\NotifyAction; +use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\FetchPaymentDetailsAction; use CommerceWeavers\SyliusTpayPlugin\Payum\Action\CaptureAction; use CommerceWeavers\SyliusTpayPlugin\Payum\Action\GetStatusAction; -use CommerceWeavers\SyliusTpayPlugin\Payum\Action\RefundAction; use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\TpayGatewayFactory; return function(ContainerConfigurator $container): void { @@ -19,34 +18,22 @@ ; $services->set(CaptureAction::class) - ->args([ - service('commerce_weavers.tpay.payum.factory.create_transaction'), - ]) ->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.capture']) ; $services->set(CreateTransactionAction::class) - ->args([ - service('router'), - param('commerce_weavers_tpay.payum.create_transaction.success_route'), - param('commerce_weavers_tpay.payum.create_transaction.error_route'), - param('commerce_weavers_tpay.payum.create_transaction.notify_route'), - ]) ->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.create_transaction']) ; - $services->set(NotifyAction::class) - ->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.notify']) - ; - $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']) + ; + $services->set(GetStatusAction::class) ->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.get_status']) ; - - $services->set(RefundAction::class) - ->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.refund']); }; diff --git a/src/Form/DataTransformer/PaymentDetailsTransformer.php b/src/Form/DataTransformer/PaymentDetailsTransformer.php index 0283e8f0..eb9a1443 100644 --- a/src/Form/DataTransformer/PaymentDetailsTransformer.php +++ b/src/Form/DataTransformer/PaymentDetailsTransformer.php @@ -18,6 +18,6 @@ public function transform($value): string } public function reverseTransform($value): array { - return $value ? ['blik' => $value] : []; + return ['blik' => $value]; } } diff --git a/src/Form/Type/CompleteTypeExtension.php b/src/Form/Type/CompleteTypeExtension.php index e230711e..6658cf03 100644 --- a/src/Form/Type/CompleteTypeExtension.php +++ b/src/Form/Type/CompleteTypeExtension.php @@ -22,6 +22,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void '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, ]); } diff --git a/src/Payum/Action/Api/CreateBlik0TransactionAction.php b/src/Payum/Action/Api/CreateBlik0TransactionAction.php index b3740e6c..816cf633 100644 --- a/src/Payum/Action/Api/CreateBlik0TransactionAction.php +++ b/src/Payum/Action/Api/CreateBlik0TransactionAction.php @@ -50,7 +50,7 @@ public function execute($request): void ], ]); - unset($details['blik']); + // blik token could be removed here from $details $details['tpay']['transaction_id'] = $response['transactionId']; $model->setDetails($details); diff --git a/src/Payum/Action/CaptureAction.php b/src/Payum/Action/CaptureAction.php index bdd8290d..99d19c82 100644 --- a/src/Payum/Action/CaptureAction.php +++ b/src/Payum/Action/CaptureAction.php @@ -4,8 +4,8 @@ namespace CommerceWeavers\SyliusTpayPlugin\Payum\Action; -use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\CreateTransactionFactoryInterface; use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\CreateBlik0Transaction; +use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\CreateTransaction; use Payum\Core\Action\ActionInterface; use Payum\Core\GatewayAwareInterface; use Payum\Core\GatewayAwareTrait; @@ -17,11 +17,6 @@ final class CaptureAction implements ActionInterface, GatewayAwareInterface { use GatewayAwareTrait; - public function __construct( - private CreateTransactionFactoryInterface $createTransactionFactory, - ) { - } - /** * @param Capture $request */ @@ -32,14 +27,14 @@ public function execute($request): void if ($this->transactionIsBlik($model)) { $this->gateway->execute( - new CreateBlik0Transaction($request->getToken()), + new CreateBlik0Transaction($request->getToken()->getAfterUrl(), $model), ); return; } $this->gateway->execute( - $this->createTransactionFactory->createNewWithModel($request->getToken()), + new CreateTransaction($request->getToken()->getAfterUrl(), $model), ); $paymentDetails = $model->getDetails(); diff --git a/src/Payum/Request/Api/CreateBlik0Transaction.php b/src/Payum/Request/Api/CreateBlik0Transaction.php index 63445159..0dfcbcb5 100644 --- a/src/Payum/Request/Api/CreateBlik0Transaction.php +++ b/src/Payum/Request/Api/CreateBlik0Transaction.php @@ -9,8 +9,14 @@ class CreateBlik0Transaction extends Generic { public function __construct ( + private string $afterUrl, mixed $model, ) { parent::__construct($model); } + + public function getAfterUrl(): string + { + return $this->afterUrl; + } } diff --git a/templates/blik.html.twig b/templates/blik.html.twig index e00662cb..255a9a22 100644 --- a/templates/blik.html.twig +++ b/templates/blik.html.twig @@ -20,6 +20,10 @@

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

{{ form_row(form.others) }} + + {% if state != 'cart' %}

From 984bc9b7d33961a80e873678f5ef0d44486dff67 Mon Sep 17 00:00:00 2001 From: arti0090 Date: Thu, 5 Sep 2024 14:49:35 +0200 Subject: [PATCH 06/11] Fix config files after rebase --- config/packages.php | 9 -------- config/packages/events.php | 23 ------------------- config/services/payum/action.php | 23 +++++++++++++------ .../packages/commerce_weavers_tpay.yaml | 2 +- 4 files changed, 17 insertions(+), 40 deletions(-) delete mode 100644 config/packages.php delete mode 100644 config/packages/events.php diff --git a/config/packages.php b/config/packages.php deleted file mode 100644 index daf459e9..00000000 --- a/config/packages.php +++ /dev/null @@ -1,9 +0,0 @@ -import('packages/**/*.php'); -}; diff --git a/config/packages/events.php b/config/packages/events.php deleted file mode 100644 index 6fc9cb45..00000000 --- a/config/packages/events.php +++ /dev/null @@ -1,23 +0,0 @@ -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/config/services/payum/action.php b/config/services/payum/action.php index 55808072..dcb0b5b7 100644 --- a/config/services/payum/action.php +++ b/config/services/payum/action.php @@ -6,7 +6,7 @@ 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\Api\NotifyAction; use CommerceWeavers\SyliusTpayPlugin\Payum\Action\CaptureAction; use CommerceWeavers\SyliusTpayPlugin\Payum\Action\GetStatusAction; use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\TpayGatewayFactory; @@ -18,22 +18,31 @@ ; $services->set(CaptureAction::class) + ->args([ + service('commerce_weavers.tpay.payum.factory.create_transaction'), + ]) ->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.capture']) ; $services->set(CreateTransactionAction::class) + ->args([ + service('router'), + param('commerce_weavers_tpay.payum.create_transaction.success_route'), + param('commerce_weavers_tpay.payum.create_transaction.error_route'), + param('commerce_weavers_tpay.payum.create_transaction.notify_route'), + ]) ->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']) + $services->set(NotifyAction::class) + ->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.notify']) ; $services->set(GetStatusAction::class) ->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.get_status']) ; + + $services->set(CreateBlik0TransactionAction::class) + ->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.create_blik0_transaction']) + ; }; diff --git a/tests/Application/config/packages/commerce_weavers_tpay.yaml b/tests/Application/config/packages/commerce_weavers_tpay.yaml index f758fcec..5defcc58 100644 --- a/tests/Application/config/packages/commerce_weavers_tpay.yaml +++ b/tests/Application/config/packages/commerce_weavers_tpay.yaml @@ -1,2 +1,2 @@ imports: - - { resource: "@CommerceWeaversSyliusTpayPlugin/config/packages.php" } + - { resource: "@CommerceWeaversSyliusTpayPlugin/config/config.php" } From 24cb7ab1043190ceb18f0b170dd690f3e51e9a91 Mon Sep 17 00:00:00 2001 From: arti0090 Date: Fri, 6 Sep 2024 10:45:58 +0200 Subject: [PATCH 07/11] Add notifications --- config/services/payum/action.php | 6 +++ .../Api/CreateBlik0TransactionAction.php | 37 +++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/config/services/payum/action.php b/config/services/payum/action.php index dcb0b5b7..ccf9497e 100644 --- a/config/services/payum/action.php +++ b/config/services/payum/action.php @@ -43,6 +43,12 @@ ; $services->set(CreateBlik0TransactionAction::class) + ->args([ + service('router'), + param('commerce_weavers_tpay.payum.create_transaction.success_route'), + param('commerce_weavers_tpay.payum.create_transaction.error_route'), + param('commerce_weavers_tpay.payum.create_transaction.notify_route'), + ]) ->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.create_blik0_transaction']) ; }; diff --git a/src/Payum/Action/Api/CreateBlik0TransactionAction.php b/src/Payum/Action/Api/CreateBlik0TransactionAction.php index 816cf633..e4ebe068 100644 --- a/src/Payum/Action/Api/CreateBlik0TransactionAction.php +++ b/src/Payum/Action/Api/CreateBlik0TransactionAction.php @@ -6,14 +6,30 @@ use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\CreateBlik0Transaction; use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\CreateTransaction; +use Payum\Core\Security\GenericTokenFactoryAwareInterface; +use Payum\Core\Security\GenericTokenFactoryAwareTrait; +use Payum\Core\Security\TokenInterface; use Sylius\Component\Core\Model\PaymentInterface; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; +use Symfony\Component\Routing\RouterInterface; use Tpay\OpenApi\Api\TpayApi; /** * @property TpayApi $api */ -final class CreateBlik0TransactionAction extends BaseApiAwareAction +final class CreateBlik0TransactionAction extends BaseApiAwareAction implements GenericTokenFactoryAwareInterface { + use GenericTokenFactoryAwareTrait; + + public function __construct ( + private RouterInterface $router, + private string $successRoute, + private string $errorRoute, + private string $notifyRoute, + ) { + parent::__construct(); + } + /** * @param CreateTransaction $request */ @@ -25,7 +41,9 @@ public function execute($request): void $order = $model->getOrder(); $customer = $order->getCustomer(); + $localeCode = $order->getLocaleCode(); $billingAddress = $order->getBillingAddress(); + $notifyToken = $this->createNotifyToken($model, $request->getToken(), $localeCode); $blikToken = $model->getDetails()['blik']; @@ -44,14 +62,18 @@ public function execute($request): void ], 'callbacks' => [ 'payerUrls' => [ - 'success' => $request->getAfterUrl(), - 'error' => $request->getAfterUrl(), + 'success' => $this->router->generate($this->successRoute, ['_locale' => $localeCode], UrlGeneratorInterface::ABSOLUTE_URL), + 'error' => $this->router->generate($this->errorRoute, ['_locale' => $localeCode], UrlGeneratorInterface::ABSOLUTE_URL), + ], + 'notification' => [ + 'url' => $notifyToken->getTargetUrl(), ], ], ]); // blik token could be removed here from $details $details['tpay']['transaction_id'] = $response['transactionId']; + $details['tpay']['status'] = $response['status']; $model->setDetails($details); } @@ -60,4 +82,13 @@ public function supports($request): bool { return $request instanceof CreateBlik0Transaction && $request->getModel() instanceof PaymentInterface; } + + private function createNotifyToken(PaymentInterface $payment, TokenInterface $token, string $localeCode): TokenInterface + { + return $this->tokenFactory->createToken( + $token->getGatewayName(), + $payment, + $this->router->generate($this->notifyRoute, ['_locale' => $localeCode], UrlGeneratorInterface::ABSOLUTE_URL), + ); + } } From e89fc41057465c37492a8cabce966f090ec5cc25 Mon Sep 17 00:00:00 2001 From: arti0090 Date: Mon, 9 Sep 2024 12:33:55 +0200 Subject: [PATCH 08/11] refactor type extension --- config/services/form.php | 3 +- .../OrderLastNewPaymentAwareInterface.php | 12 ++ src/Entity/OrderLastNewPaymentAwareTrait.php | 15 ++ .../CompleteTypeExtension.php | 6 +- .../Api/CreateBlik0TransactionAction.php | 3 +- src/Payum/Action/CaptureAction.php | 4 +- .../Application/config/packages/_sylius.yaml | 6 + tests/Application/src/Entity/Order.php | 17 ++ .../Api/CreateBlik0TransactionActionTest.php | 161 ++++++++++++++++++ 9 files changed, 219 insertions(+), 8 deletions(-) create mode 100644 src/Entity/OrderLastNewPaymentAwareInterface.php create mode 100644 src/Entity/OrderLastNewPaymentAwareTrait.php rename src/Form/{Type => Extension}/CompleteTypeExtension.php (82%) create mode 100644 tests/Application/src/Entity/Order.php create mode 100644 tests/Unit/Payum/Action/Api/CreateBlik0TransactionActionTest.php diff --git a/config/services/form.php b/config/services/form.php index 08614474..e6c7b2fd 100644 --- a/config/services/form.php +++ b/config/services/form.php @@ -5,10 +5,9 @@ namespace Symfony\Component\DependencyInjection\Loader\Configurator; use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\PreventSavingEmptyClientSecretListener; -use CommerceWeavers\SyliusTpayPlugin\Form\Type\CompleteTypeExtension; +use CommerceWeavers\SyliusTpayPlugin\Form\Extension\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(); diff --git a/src/Entity/OrderLastNewPaymentAwareInterface.php b/src/Entity/OrderLastNewPaymentAwareInterface.php new file mode 100644 index 00000000..3b4ba681 --- /dev/null +++ b/src/Entity/OrderLastNewPaymentAwareInterface.php @@ -0,0 +1,12 @@ +getLastPayment('cart'); + } +} diff --git a/src/Form/Type/CompleteTypeExtension.php b/src/Form/Extension/CompleteTypeExtension.php similarity index 82% rename from src/Form/Type/CompleteTypeExtension.php rename to src/Form/Extension/CompleteTypeExtension.php index 6658cf03..25bcf380 100644 --- a/src/Form/Type/CompleteTypeExtension.php +++ b/src/Form/Extension/CompleteTypeExtension.php @@ -2,8 +2,9 @@ declare(strict_types=1); -namespace CommerceWeavers\SyliusTpayPlugin\Form\Type; +namespace CommerceWeavers\SyliusTpayPlugin\Form\Extension; +use CommerceWeavers\SyliusTpayPlugin\Form\Type\PaymentDetailsType; use Sylius\Bundle\CoreBundle\Form\Type\Checkout\CompleteType; use Symfony\Component\Form\AbstractTypeExtension; use Symfony\Component\Form\Extension\Core\Type\TextareaType; @@ -21,8 +22,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void $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, + 'property_path' => 'last_new_payment.details["tpay"]', 'required' => false, ]); } diff --git a/src/Payum/Action/Api/CreateBlik0TransactionAction.php b/src/Payum/Action/Api/CreateBlik0TransactionAction.php index e4ebe068..bf677cd8 100644 --- a/src/Payum/Action/Api/CreateBlik0TransactionAction.php +++ b/src/Payum/Action/Api/CreateBlik0TransactionAction.php @@ -45,7 +45,7 @@ public function execute($request): void $billingAddress = $order->getBillingAddress(); $notifyToken = $this->createNotifyToken($model, $request->getToken(), $localeCode); - $blikToken = $model->getDetails()['blik']; + $blikToken = $model->getDetails()['tpay']['blik']; $response = $this->api->transactions()->createTransaction([ 'amount' => number_format($model->getAmount() / 100, 2, thousands_separator: ''), @@ -71,7 +71,6 @@ public function execute($request): void ], ]); - // blik token could be removed here from $details $details['tpay']['transaction_id'] = $response['transactionId']; $details['tpay']['status'] = $response['status']; diff --git a/src/Payum/Action/CaptureAction.php b/src/Payum/Action/CaptureAction.php index 99d19c82..25440cfc 100644 --- a/src/Payum/Action/CaptureAction.php +++ b/src/Payum/Action/CaptureAction.php @@ -49,6 +49,8 @@ public function supports($request): bool private function transactionIsBlik($model): bool { - return array_key_exists('blik', $model->getDetails()); + return array_key_exists('tpay', $model->getDetails()) + && array_key_exists('blik', $model->getDetails()['tpay']) + ; } } diff --git a/tests/Application/config/packages/_sylius.yaml b/tests/Application/config/packages/_sylius.yaml index f12c2ad3..99d5458b 100644 --- a/tests/Application/config/packages/_sylius.yaml +++ b/tests/Application/config/packages/_sylius.yaml @@ -17,3 +17,9 @@ sylius_shop: sylius_api: enabled: true + +sylius_order: + resources: + order: + classes: + model: Tests\CommerceWeavers\SyliusTpayPlugin\Application\Entity\Order diff --git a/tests/Application/src/Entity/Order.php b/tests/Application/src/Entity/Order.php new file mode 100644 index 00000000..9ce916e4 --- /dev/null +++ b/tests/Application/src/Entity/Order.php @@ -0,0 +1,17 @@ +request = $this->prophesize(CreateTransaction::class); + $this->model = $this->prophesize(PaymentInterface::class); + $this->api = $this->prophesize(TpayApi::class); + $this->router = $this->prophesize(RouterInterface::class); + $this->tokenFactory = $this->prophesize(GenericTokenFactoryInterface::class); + + $this->request->getModel()->willReturn($this->model->reveal()); + } + + public function test_it_supports_only_create_blik0_transaction_request(): void + { + $action = $this->createTestSubject(); + + $this->assertFalse($action->supports(new Sync($this->model->reveal()))); + $this->assertTrue($action->supports(new CreateBlik0Transaction($this->model->reveal()))); + } + + public function test_it_supports_only_payment_interface_based_models(): void + { + $action = $this->createTestSubject(); + + $this->assertFalse($action->supports(new CreateBlik0Transaction(new \stdClass()))); + $this->assertTrue($action->supports(new CreateBlik0Transaction($this->model->reveal()))); + } + + public function test_it_creates_blik0_transaction(): void + { + $createTransactionToken = $this->prophesize(TokenInterface::class); + $createTransactionToken->getGatewayName()->willReturn('tpay'); + + $this->request->getToken()->willReturn($createTransactionToken); + + $customer = $this->prophesize(CustomerInterface::class); + $customer->getEmail()->willReturn('domino@jahas.com'); + + $billingAddress = $this->prophesize(AddressInterface::class); + $billingAddress->getFullName()->willReturn('Domino Jahas'); + + $order = $this->prophesize(OrderInterface::class); + $order->getLocaleCode()->willReturn('en_US'); + $order->getCustomer()->willReturn($customer); + $order->getBillingAddress()->willReturn($billingAddress); + $order->getNumber()->willReturn('00000001'); + + $this->model->getAmount()->willReturn(123); + $this->model->getOrder()->willReturn($order); + $blikCode = '777456'; + $this->model->getDetails()->willReturn([ + 'blik' => $blikCode, + ]); + $this->model->setDetails([ + 'tpay' => [ + 'transaction_id' => '123awsd', + 'transaction_payment_url' => 'https://tpay.pay', + ], + ])->shouldBeCalled(); + + $NOTIFY_URL = 'https://cw.org/notify'; + $THANK_YOU_URL = 'https://cw.org/thank-you'; + + $this->tokenFactory->createToken( + 'tpay', + $this->model, + $NOTIFY_URL, + )->willReturn($token = $this->prophesize(TokenInterface::class)); + $token->getTargetUrl()->willReturn($NOTIFY_URL); + + $this->router + ->generate('sylius_shop_order_thank_you', ['_locale' => 'en_US', UrlGeneratorInterface::ABSOLUTE_URL]) + ->willReturn($THANK_YOU_URL) + ; + + $this->router + ->generate('commerce_weavers_tpay_payment_notification', ['_locale' => 'en_US'], UrlGeneratorInterface::ABSOLUTE_URL) + ->willReturn($NOTIFY_URL) + ; + + $transactionsApi = $this->prophesize(TransactionsApi::class); + $transactionsApi->createTransaction([ + 'amount' => 12.3, + 'description' => 'zamówienie #00000001', + 'payer' => [ + 'email' => 'domino@jahas.com', + 'name' => 'Domino Jahas', + ], + 'pay' => [ + 'groupId' => 150, + 'blikPaymentData' => [ + 'blikToken' => $blikCode, + ], + ], + 'callbacks' => [ + 'payerUrls' => [ + 'success' => $THANK_YOU_URL, + 'error' => $THANK_YOU_URL, + ], + 'notification' => [ + 'url' => $NOTIFY_URL, + ], + ], + ])->shouldBeCalled()->willReturn([ + 'transactionId' => '123awsd', + 'transactionPaymentUrl' => 'https://tpay.pay', + ]); + + $this->api->transactions()->willReturn($transactionsApi); + $this->createTestSubject()->execute($this->request->reveal()); + } + + private function createTestSubject(): CreateBlik0TransactionAction + { + $action = new CreateBlik0TransactionAction( + $this->router->reveal(), + 'sylius_shop_order_thank_you', + 'sylius_shop_order_thank_you', + 'commerce_weavers_tpay_payment_notification', + ); + + $action->setApi($this->api->reveal()); + $action->setGenericTokenFactory($this->tokenFactory->reveal()); + + return $action; + } +} + From c560aa2eed50a5b8dc51c541e9c4f60bcd5de9af Mon Sep 17 00:00:00 2001 From: Jacob Tobiasz Date: Mon, 9 Sep 2024 13:06:41 +0200 Subject: [PATCH 09/11] Fix the Order test entity configuration --- tests/Application/config/packages/_sylius.yaml | 2 +- tests/Application/config/packages/doctrine.yaml | 9 +++++++++ tests/Application/src/Entity/.gitignore | 0 3 files changed, 10 insertions(+), 1 deletion(-) delete mode 100644 tests/Application/src/Entity/.gitignore diff --git a/tests/Application/config/packages/_sylius.yaml b/tests/Application/config/packages/_sylius.yaml index 99d5458b..ce960b37 100644 --- a/tests/Application/config/packages/_sylius.yaml +++ b/tests/Application/config/packages/_sylius.yaml @@ -22,4 +22,4 @@ sylius_order: resources: order: classes: - model: Tests\CommerceWeavers\SyliusTpayPlugin\Application\Entity\Order + model: App\Entity\Order diff --git a/tests/Application/config/packages/doctrine.yaml b/tests/Application/config/packages/doctrine.yaml index f51ba5a2..7743f35a 100644 --- a/tests/Application/config/packages/doctrine.yaml +++ b/tests/Application/config/packages/doctrine.yaml @@ -12,3 +12,12 @@ doctrine: charset: UTF8 url: '%env(resolve:DATABASE_URL)%' + orm: + entity_managers: + default: + mappings: + App: + is_bundle: false + type: attribute + dir: '%kernel.project_dir%/src/Entity' + prefix: App\Entity diff --git a/tests/Application/src/Entity/.gitignore b/tests/Application/src/Entity/.gitignore deleted file mode 100644 index e69de29b..00000000 From ddf63ec75704e4983193ee3b51a1e647ee2e1259 Mon Sep 17 00:00:00 2001 From: arti0090 Date: Tue, 10 Sep 2024 16:01:48 +0200 Subject: [PATCH 10/11] Add tests --- config/services/payum/action.php | 5 +++ config/services/payum/factory.php | 5 +++ src/Form/Extension/CompleteTypeExtension.php | 13 +++---- src/Payum/Action/CaptureAction.php | 14 +++++-- .../Factory/CreateBlik0TransactionFactory.php | 15 ++++++++ .../CreateTransactionFactoryInterface.php | 4 +- templates/blik.html.twig | 4 -- .../Api/CreateBlik0TransactionActionTest.php | 37 +++++++++---------- .../Api/CreateTransactionActionTest.php | 4 +- tests/Unit/Payum/Action/CaptureActionTest.php | 30 ++++++++++++++- translations/messages.en.yaml | 1 - 11 files changed, 90 insertions(+), 42 deletions(-) create mode 100644 src/Payum/Factory/CreateBlik0TransactionFactory.php diff --git a/config/services/payum/action.php b/config/services/payum/action.php index ccf9497e..83b7c616 100644 --- a/config/services/payum/action.php +++ b/config/services/payum/action.php @@ -9,6 +9,7 @@ use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\NotifyAction; use CommerceWeavers\SyliusTpayPlugin\Payum\Action\CaptureAction; use CommerceWeavers\SyliusTpayPlugin\Payum\Action\GetStatusAction; +use CommerceWeavers\SyliusTpayPlugin\Payum\Action\RefundAction; use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\TpayGatewayFactory; return function(ContainerConfigurator $container): void { @@ -20,6 +21,7 @@ $services->set(CaptureAction::class) ->args([ service('commerce_weavers.tpay.payum.factory.create_transaction'), + service('commerce_weavers.tpay.payum.factory.create_blik0_transaction'), ]) ->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.capture']) ; @@ -42,6 +44,9 @@ ->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.get_status']) ; + $services->set(RefundAction::class) + ->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.refund']); + $services->set(CreateBlik0TransactionAction::class) ->args([ service('router'), diff --git a/config/services/payum/factory.php b/config/services/payum/factory.php index bc2bb30e..980d69a5 100644 --- a/config/services/payum/factory.php +++ b/config/services/payum/factory.php @@ -4,6 +4,7 @@ namespace Symfony\Component\DependencyInjection\Loader\Configurator; +use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\CreateBlik0TransactionFactory; use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\CreateTransactionFactory; use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\CreateTransactionFactoryInterface; use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\NotifyFactory; @@ -28,4 +29,8 @@ $services->set('commerce_weavers.tpay.payum.factory.create_transaction', CreateTransactionFactory::class) ->alias(CreateTransactionFactoryInterface::class, 'commerce_weavers.tpay.payum.factory.create_transaction') ; + + $services->set('commerce_weavers.tpay.payum.factory.create_blik0_transaction', CreateBlik0TransactionFactory::class) + ->alias(CreateTransactionFactoryInterface::class, 'commerce_weavers.tpay.payum.factory.create_blik0_transaction') + ; }; diff --git a/src/Form/Extension/CompleteTypeExtension.php b/src/Form/Extension/CompleteTypeExtension.php index 25bcf380..a5e98055 100644 --- a/src/Form/Extension/CompleteTypeExtension.php +++ b/src/Form/Extension/CompleteTypeExtension.php @@ -7,22 +7,19 @@ use CommerceWeavers\SyliusTpayPlugin\Form\Type\PaymentDetailsType; use Sylius\Bundle\CoreBundle\Form\Type\Checkout\CompleteType; use Symfony\Component\Form\AbstractTypeExtension; -use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\FormBuilderInterface; final class CompleteTypeExtension extends AbstractTypeExtension { public function buildForm(FormBuilderInterface $builder, array $options): void { - $builder->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' => 'last_new_payment.details["tpay"]', +// TODO some validation that works becuase this kind does not +// 'constraints' => [ +// new Length(['value' => 6, 'min' => 6, 'max' => 6, 'groups' => ['sylius']]), +// ], + 'property_path' => 'last_new_payment.details[tpay]', 'required' => false, ]); } diff --git a/src/Payum/Action/CaptureAction.php b/src/Payum/Action/CaptureAction.php index 25440cfc..ffa37764 100644 --- a/src/Payum/Action/CaptureAction.php +++ b/src/Payum/Action/CaptureAction.php @@ -4,8 +4,8 @@ namespace CommerceWeavers\SyliusTpayPlugin\Payum\Action; -use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\CreateBlik0Transaction; -use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\CreateTransaction; +use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\CreateBlik0TransactionFactory; +use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\CreateTransactionFactoryInterface; use Payum\Core\Action\ActionInterface; use Payum\Core\GatewayAwareInterface; use Payum\Core\GatewayAwareTrait; @@ -17,6 +17,12 @@ final class CaptureAction implements ActionInterface, GatewayAwareInterface { use GatewayAwareTrait; + public function __construct( + private CreateTransactionFactoryInterface $createTransactionFactory, + private CreateTransactionFactoryInterface $createBlik0TransactionFactory, + ) { + } + /** * @param Capture $request */ @@ -27,14 +33,14 @@ public function execute($request): void if ($this->transactionIsBlik($model)) { $this->gateway->execute( - new CreateBlik0Transaction($request->getToken()->getAfterUrl(), $model), + $this->createBlik0TransactionFactory->createNewWithModel($request->getToken()) ); return; } $this->gateway->execute( - new CreateTransaction($request->getToken()->getAfterUrl(), $model), + $this->createTransactionFactory->createNewWithModel($request->getToken()), ); $paymentDetails = $model->getDetails(); diff --git a/src/Payum/Factory/CreateBlik0TransactionFactory.php b/src/Payum/Factory/CreateBlik0TransactionFactory.php new file mode 100644 index 00000000..b4da864e --- /dev/null +++ b/src/Payum/Factory/CreateBlik0TransactionFactory.php @@ -0,0 +1,15 @@ +{{ money.format(payment.amount, payment.currencyCode) }}

{{ form_row(form.others) }} - - {% if state != 'cart' %}

diff --git a/tests/Unit/Payum/Action/Api/CreateBlik0TransactionActionTest.php b/tests/Unit/Payum/Action/Api/CreateBlik0TransactionActionTest.php index 8302bd90..a6cf8a74 100644 --- a/tests/Unit/Payum/Action/Api/CreateBlik0TransactionActionTest.php +++ b/tests/Unit/Payum/Action/Api/CreateBlik0TransactionActionTest.php @@ -78,42 +78,42 @@ public function test_it_creates_blik0_transaction(): void $order->getBillingAddress()->willReturn($billingAddress); $order->getNumber()->willReturn('00000001'); - $this->model->getAmount()->willReturn(123); + $this->model->getAmount()->willReturn(1230); $this->model->getOrder()->willReturn($order); $blikCode = '777456'; $this->model->getDetails()->willReturn([ - 'blik' => $blikCode, + 'tpay' => [ + 'blik' => $blikCode, + ], ]); $this->model->setDetails([ 'tpay' => [ - 'transaction_id' => '123awsd', - 'transaction_payment_url' => 'https://tpay.pay', + 'blik' => $blikCode, + 'transaction_id' => '1234awsd', + 'status' => 'success', ], ])->shouldBeCalled(); - $NOTIFY_URL = 'https://cw.org/notify'; - $THANK_YOU_URL = 'https://cw.org/thank-you'; - $this->tokenFactory->createToken( 'tpay', $this->model, - $NOTIFY_URL, + 'https://cw.org/notify', )->willReturn($token = $this->prophesize(TokenInterface::class)); - $token->getTargetUrl()->willReturn($NOTIFY_URL); + $token->getTargetUrl()->willReturn('https://cw.org/notify'); $this->router - ->generate('sylius_shop_order_thank_you', ['_locale' => 'en_US', UrlGeneratorInterface::ABSOLUTE_URL]) - ->willReturn($THANK_YOU_URL) + ->generate('sylius_shop_order_thank_you', ['_locale' => 'en_US'], UrlGeneratorInterface::ABSOLUTE_URL) + ->willReturn('https://cw.org/thank-you') ; $this->router ->generate('commerce_weavers_tpay_payment_notification', ['_locale' => 'en_US'], UrlGeneratorInterface::ABSOLUTE_URL) - ->willReturn($NOTIFY_URL) + ->willReturn('https://cw.org/notify') ; $transactionsApi = $this->prophesize(TransactionsApi::class); $transactionsApi->createTransaction([ - 'amount' => 12.3, + 'amount' => 12.30, 'description' => 'zamówienie #00000001', 'payer' => [ 'email' => 'domino@jahas.com', @@ -127,16 +127,16 @@ public function test_it_creates_blik0_transaction(): void ], 'callbacks' => [ 'payerUrls' => [ - 'success' => $THANK_YOU_URL, - 'error' => $THANK_YOU_URL, + 'success' => 'https://cw.org/thank-you', + 'error' => 'https://cw.org/thank-you', ], 'notification' => [ - 'url' => $NOTIFY_URL, + 'url' => 'https://cw.org/notify', ], ], ])->shouldBeCalled()->willReturn([ - 'transactionId' => '123awsd', - 'transactionPaymentUrl' => 'https://tpay.pay', + 'transactionId' => '1234awsd', + 'status' => 'success', ]); $this->api->transactions()->willReturn($transactionsApi); @@ -158,4 +158,3 @@ private function createTestSubject(): CreateBlik0TransactionAction return $action; } } - diff --git a/tests/Unit/Payum/Action/Api/CreateTransactionActionTest.php b/tests/Unit/Payum/Action/Api/CreateTransactionActionTest.php index 994e5d48..1aa1eb57 100644 --- a/tests/Unit/Payum/Action/Api/CreateTransactionActionTest.php +++ b/tests/Unit/Payum/Action/Api/CreateTransactionActionTest.php @@ -46,12 +46,12 @@ protected function setUp(): void $this->request->getModel()->willReturn($this->model->reveal()); } - public function it_supports_only_create_transaction_requests(): void + public function test_it_supports_only_create_transaction_requests(): void { $action = $this->createTestSubject(); $this->assertFalse($action->supports(new Sync($this->model->reveal()))); - $this->assertTrue($action->supports(new CreateTransaction('https://cw.org', $this->model->reveal()))); + $this->assertTrue($action->supports(new CreateTransaction($this->model->reveal()))); } public function test_it_supports_only_payment_interface_based_models(): void diff --git a/tests/Unit/Payum/Action/CaptureActionTest.php b/tests/Unit/Payum/Action/CaptureActionTest.php index 67171e84..4c68e745 100644 --- a/tests/Unit/Payum/Action/CaptureActionTest.php +++ b/tests/Unit/Payum/Action/CaptureActionTest.php @@ -5,7 +5,9 @@ namespace Tests\CommerceWeavers\SyliusTpayPlugin\Unit\Payum\Action; use CommerceWeavers\SyliusTpayPlugin\Payum\Action\CaptureAction; +use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\CreateBlik0TransactionFactory; use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\CreateTransactionFactoryInterface; +use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\CreateBlik0Transaction; use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\CreateTransaction; use Payum\Core\GatewayInterface; use Payum\Core\Reply\HttpRedirect; @@ -29,17 +31,20 @@ final class CaptureActionTest extends TestCase private CreateTransactionFactoryInterface|ObjectProphecy $createTransactionFactory; + private CreateTransactionFactoryInterface|ObjectProphecy $createBlik0TransactionFactory; + protected function setUp(): void { $this->gateway = $this->prophesize(GatewayInterface::class); $this->request = $this->prophesize(Capture::class); $this->model = $this->prophesize(PaymentInterface::class); $this->createTransactionFactory = $this->prophesize(CreateTransactionFactoryInterface::class); + $this->createBlik0TransactionFactory = $this->prophesize(CreateTransactionFactoryInterface::class); $this->request->getModel()->willReturn($this->model->reveal()); } - public function it_supports_only_capture_requests(): void + public function test_it_supports_only_capture_requests(): void { $action = $this->createTestSubject(); @@ -74,9 +79,30 @@ public function test_it_throws_http_redirect_with_using_transaction_payment_url( $this->createTestSubject()->execute($this->request->reveal()); } + public function test_it_creates_blik0_transaction_if_transaction_is_blik0(): void + { + $token = $this->prophesize(TokenInterface::class); + $token->getAfterUrl()->willReturn('http://foo.bar'); + + $this->model->getDetails()->willReturn([ + 'tpay' => [ + 'blik' => '777123', + ], + ]); + + $this->request->getToken()->willReturn($token); + $this->createBlik0TransactionFactory->createNewWithModel($token)->willReturn($createBlik0Transaction = $this->prophesize(CreateBlik0Transaction::class)); + $this->gateway->execute($createBlik0Transaction)->shouldBeCalled(); + + $this->createTestSubject()->execute($this->request->reveal()); + } + private function createTestSubject(): CaptureAction { - $action = new CaptureAction($this->createTransactionFactory->reveal()); + $action = new CaptureAction( + $this->createTransactionFactory->reveal(), + $this->createBlik0TransactionFactory->reveal(), + ); $action->setGateway($this->gateway->reveal()); diff --git a/translations/messages.en.yaml b/translations/messages.en.yaml index 33822ce0..dd7f81f7 100644 --- a/translations/messages.en.yaml +++ b/translations/messages.en.yaml @@ -9,4 +9,3 @@ commerce_weavers_sylius_tpay: payment: blik: token: 'BLIK code' - From 69bc7fdea292c0b1177ec42f7a181569476b5795 Mon Sep 17 00:00:00 2001 From: arti0090 Date: Tue, 10 Sep 2024 19:04:00 +0200 Subject: [PATCH 11/11] Fix static analytics --- phpstan.neon | 3 ++- .../DataTransformer/PaymentDetailsTransformer.php | 7 +++++-- .../Action/Api/CreateBlik0TransactionAction.php | 15 ++++++++++++--- src/Payum/Action/CaptureAction.php | 9 ++++----- src/Payum/Request/Api/CreateBlik0Transaction.php | 8 +------- 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 99b906fe..917b2aca 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,7 +1,6 @@ parameters: level: max reportUnmatchedIgnoredErrors: false - checkMissingIterableValueType: false paths: - src @@ -11,6 +10,8 @@ parameters: - 'tests/Application/src/**.php' ignoreErrors: + - identifier: missingType.iterableValue + - identifier: missingType.generics - '/Parameter #1 \$configuration of method Symfony\\Component\\DependencyInjection\\Extension\\Extension::processConfiguration\(\) expects Symfony\\Component\\Config\\Definition\\ConfigurationInterface, Symfony\\Component\\Config\\Definition\\ConfigurationInterface\|null given\./' - '/Parameter \#1 \$request \([^)]+\) of method [^:]+::execute\(\) should be contravariant with parameter \$request \(mixed\) of method Payum\\Core\\Action\\ActionInterface::execute\(\)/' - '/Parameter \$event of method CommerceWeavers\\SyliusTpayPlugin\\Refunding\\Workflow\\Listener\\DispatchRefundListener::__invoke\(\) has invalid type Symfony\\Component\\Workflow\\Event\\TransitionEvent\./' diff --git a/src/Form/DataTransformer/PaymentDetailsTransformer.php b/src/Form/DataTransformer/PaymentDetailsTransformer.php index eb9a1443..9220270f 100644 --- a/src/Form/DataTransformer/PaymentDetailsTransformer.php +++ b/src/Form/DataTransformer/PaymentDetailsTransformer.php @@ -5,17 +5,20 @@ namespace CommerceWeavers\SyliusTpayPlugin\Form\DataTransformer; use Symfony\Component\Form\DataTransformerInterface; +use Webmozart\Assert\Assert; class PaymentDetailsTransformer implements DataTransformerInterface { - public function transform($value): string + public function transform(mixed $value): string { - if (!$value || !array_key_exists('blik', $value)) { + Assert::isArray($value); + if ($value === [] || !array_key_exists('blik', $value)) { return ''; } return $value['blik']; } + public function reverseTransform($value): array { return ['blik' => $value]; diff --git a/src/Payum/Action/Api/CreateBlik0TransactionAction.php b/src/Payum/Action/Api/CreateBlik0TransactionAction.php index bf677cd8..33b38bc5 100644 --- a/src/Payum/Action/Api/CreateBlik0TransactionAction.php +++ b/src/Payum/Action/Api/CreateBlik0TransactionAction.php @@ -13,6 +13,7 @@ use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\RouterInterface; use Tpay\OpenApi\Api\TpayApi; +use Webmozart\Assert\Assert; /** * @property TpayApi $api @@ -21,7 +22,7 @@ final class CreateBlik0TransactionAction extends BaseApiAwareAction implements G { use GenericTokenFactoryAwareTrait; - public function __construct ( + public function __construct( private RouterInterface $router, private string $successRoute, private string $errorRoute, @@ -38,17 +39,25 @@ public function execute($request): void /** @var PaymentInterface $model */ $model = $request->getModel(); $details = $model->getDetails(); + $token = $request->getToken(); + Assert::notNull($token); $order = $model->getOrder(); + Assert::notNull($order); $customer = $order->getCustomer(); + Assert::notNull($customer); $localeCode = $order->getLocaleCode(); + Assert::notNull($localeCode); $billingAddress = $order->getBillingAddress(); - $notifyToken = $this->createNotifyToken($model, $request->getToken(), $localeCode); + Assert::notNull($billingAddress); + $notifyToken = $this->createNotifyToken($model, $token, $localeCode); + $amount = $model->getAmount(); + Assert::notNull($amount); $blikToken = $model->getDetails()['tpay']['blik']; $response = $this->api->transactions()->createTransaction([ - 'amount' => number_format($model->getAmount() / 100, 2, thousands_separator: ''), + 'amount' => number_format($amount / 100, 2, thousands_separator: ''), 'description' => sprintf('zamówienie #%s', $order->getNumber()), // TODO: Introduce translations 'payer' => [ 'email' => $customer->getEmail(), diff --git a/src/Payum/Action/CaptureAction.php b/src/Payum/Action/CaptureAction.php index ffa37764..6db4fc16 100644 --- a/src/Payum/Action/CaptureAction.php +++ b/src/Payum/Action/CaptureAction.php @@ -4,7 +4,6 @@ namespace CommerceWeavers\SyliusTpayPlugin\Payum\Action; -use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\CreateBlik0TransactionFactory; use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\CreateTransactionFactoryInterface; use Payum\Core\Action\ActionInterface; use Payum\Core\GatewayAwareInterface; @@ -33,7 +32,7 @@ public function execute($request): void if ($this->transactionIsBlik($model)) { $this->gateway->execute( - $this->createBlik0TransactionFactory->createNewWithModel($request->getToken()) + $this->createBlik0TransactionFactory->createNewWithModel($request->getToken()), ); return; @@ -53,10 +52,10 @@ public function supports($request): bool return $request instanceof Capture && $request->getModel() instanceof PaymentInterface; } - private function transactionIsBlik($model): bool + private function transactionIsBlik(PaymentInterface $model): bool { - return array_key_exists('tpay', $model->getDetails()) - && array_key_exists('blik', $model->getDetails()['tpay']) + return array_key_exists('tpay', $model->getDetails()) && + array_key_exists('blik', $model->getDetails()['tpay']) ; } } diff --git a/src/Payum/Request/Api/CreateBlik0Transaction.php b/src/Payum/Request/Api/CreateBlik0Transaction.php index 0dfcbcb5..49303b4e 100644 --- a/src/Payum/Request/Api/CreateBlik0Transaction.php +++ b/src/Payum/Request/Api/CreateBlik0Transaction.php @@ -8,15 +8,9 @@ class CreateBlik0Transaction extends Generic { - public function __construct ( - private string $afterUrl, + public function __construct( mixed $model, ) { parent::__construct($model); } - - public function getAfterUrl(): string - { - return $this->afterUrl; - } }