From b1d6dd58d22ecad42fcb43a2eaed5fa84c9d8f70 Mon Sep 17 00:00:00 2001 From: arti0090 Date: Mon, 9 Sep 2024 12:33:55 +0200 Subject: [PATCH] refactor type extension --- config/services/form.php | 3 +- .../OrderLastNewPaymentAwareInterface.php | 12 ++ src/Entity/OrderLastNewPaymentAwareTrait.php | 15 ++ .../CompleteTypeExtension.php | 5 +- .../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(+), 7 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 6ec6984a..76147686 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 e230711e..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,7 +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? + '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 523c70cb..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 ], ]); - unset($details['blik']); $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 6a4c8965..1fb9ca3b 100644 --- a/src/Payum/Action/CaptureAction.php +++ b/src/Payum/Action/CaptureAction.php @@ -54,6 +54,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 18acdd44..74bd8d27 100644 --- a/tests/Application/config/packages/_sylius.yaml +++ b/tests/Application/config/packages/_sylius.yaml @@ -16,3 +16,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; + } +} +