From d21eb2c17bb676e93faa7fdac3b1fae18c9f372c Mon Sep 17 00:00:00 2001 From: arti0090 Date: Wed, 30 Oct 2024 17:22:05 +0100 Subject: [PATCH] Create Payer Payload only from required and existing data --- ...eateRedirectBasedPaymentPayloadFactory.php | 42 ++++-- ...RedirectBasedPaymentPayloadFactoryTest.php | 141 +++++++++++++++++- 2 files changed, 166 insertions(+), 17 deletions(-) diff --git a/src/Tpay/Factory/CreateRedirectBasedPaymentPayloadFactory.php b/src/Tpay/Factory/CreateRedirectBasedPaymentPayloadFactory.php index e07941f8..af866ec2 100644 --- a/src/Tpay/Factory/CreateRedirectBasedPaymentPayloadFactory.php +++ b/src/Tpay/Factory/CreateRedirectBasedPaymentPayloadFactory.php @@ -5,6 +5,7 @@ namespace CommerceWeavers\SyliusTpayPlugin\Tpay\Factory; use CommerceWeavers\SyliusTpayPlugin\Tpay\Routing\Generator\CallbackUrlGeneratorInterface; +use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Core\Model\PaymentInterface; use Symfony\Contracts\Translation\TranslatorInterface; use Webmozart\Assert\Assert; @@ -24,10 +25,6 @@ public function createFrom(PaymentInterface $payment, string $notifyUrl, string { $order = $payment->getOrder(); Assert::notNull($order); - $customer = $order->getCustomer(); - Assert::notNull($customer); - $billingAddress = $order->getBillingAddress(); - Assert::notNull($billingAddress); $amount = $payment->getAmount(); Assert::notNull($amount); @@ -38,15 +35,7 @@ public function createFrom(PaymentInterface $payment, string $notifyUrl, string ['%orderNumber%' => $order->getNumber()], ), 'lang' => substr($localeCode, 0, 2), - 'payer' => [ - 'email' => $customer->getEmail(), - 'name' => $billingAddress->getFullName(), - 'phone' => $billingAddress->getPhoneNumber() ?? $customer->getPhoneNumber() ?? '', - 'address' => $billingAddress->getStreet() ?? '', - 'city' => $billingAddress->getCity() ?? '', - 'code' => $billingAddress->getPostcode() ?? '', - 'country' => $billingAddress->getCountryCode() ?? '', - ], + 'payer' => $this->createPayerPayload($order), 'callbacks' => [ 'payerUrls' => [ 'success' => $this->callbackUrlGenerator->generateSuccessUrl($payment, $localeCode), @@ -58,4 +47,31 @@ public function createFrom(PaymentInterface $payment, string $notifyUrl, string ], ]; } + + private function createPayerPayload(OrderInterface $order): array + { + $customer = $order->getCustomer(); + Assert::notNull($customer); + $billingAddress = $order->getBillingAddress(); + Assert::notNull($billingAddress); + + $requiredResult = [ + 'email' => $customer->getEmail(), + 'name' => $billingAddress->getFullName(), + ]; + + $result = [ + 'phone' => $billingAddress->getPhoneNumber() ?? $customer->getPhoneNumber() ?? '', + 'address' => $billingAddress->getStreet() ?? '', + 'city' => $billingAddress->getCity() ?? '', + 'code' => $billingAddress->getPostcode() ?? '', + 'country' => $billingAddress->getCountryCode() ?? '', + ]; + + $result = array_filter($result, static function (string $value) { + return $value !== ''; + }); + + return array_merge($requiredResult, $result); + } } diff --git a/tests/Unit/Tpay/Factory/CreateRedirectBasedPaymentPayloadFactoryTest.php b/tests/Unit/Tpay/Factory/CreateRedirectBasedPaymentPayloadFactoryTest.php index 760fbfc5..2e98264d 100644 --- a/tests/Unit/Tpay/Factory/CreateRedirectBasedPaymentPayloadFactoryTest.php +++ b/tests/Unit/Tpay/Factory/CreateRedirectBasedPaymentPayloadFactoryTest.php @@ -31,14 +31,74 @@ protected function setUp(): void { $this->callbackUrlGenerator = $this->prophesize(CallbackUrlGeneratorInterface::class); $this->translator = $this->prophesize(TranslatorInterface::class); + } + + public function test_it_returns_a_payload_for_a_redirect_based_payment(): void + { + $billingAddress = $this->prophesize(AddressInterface::class); + $billingAddress->getFullName()->willReturn('Don Matteo'); + + $customer = $this->prophesize(CustomerInterface::class); + $customer->getEmail()->willReturn('don.matteo@sandomierz.org'); + + $order = $this->prophesize(OrderInterface::class); + $order->getCustomer()->willReturn($customer); + $order->getBillingAddress()->willReturn($billingAddress); + $order->getNumber()->willReturn('000000001'); + + $payment = $this->prophesize(PaymentInterface::class); + $payment->getDetails()->willReturn([]); + $payment->getOrder()->willReturn($order); + $payment->getAmount()->willReturn(1050); + + $billingAddress->getPhoneNumber()->willReturn('123123123'); + $billingAddress->getStreet()->willReturn('Sesame Street'); + $billingAddress->getCity()->willReturn('Sesame City'); + $billingAddress->getPostcode()->willReturn('90 210'); + $billingAddress->getCountryCode()->willReturn('PL'); $this->translator->trans( 'commerce_weavers_sylius_tpay.tpay.transaction_description', ['%orderNumber%' => '000000001'] )->willReturn(self::TRANSLATED_DESCRIPTION); + + $this->callbackUrlGenerator + ->generateSuccessUrl($payment, 'pl_PL') + ->willReturn('https://cw.org/success') + ; + $this->callbackUrlGenerator + ->generateFailureUrl($payment, 'pl_PL') + ->willReturn('https://cw.org/error') + ; + + $payload = $this->createTestSubject()->createFrom($payment->reveal(), 'https://cw.org/notify', 'pl_PL'); + + $this->assertSame([ + 'amount' => '10.50', + 'description' => self::TRANSLATED_DESCRIPTION, + 'lang' => 'pl', + 'payer' => [ + 'email' => 'don.matteo@sandomierz.org', + 'name' => 'Don Matteo', + 'phone' => '123123123', + 'address' => 'Sesame Street', + 'city' => 'Sesame City', + 'code' => '90 210', + 'country' => 'PL', + ], + 'callbacks' => [ + 'payerUrls' => [ + 'success' => 'https://cw.org/success', + 'error' => 'https://cw.org/error', + ], + 'notification' => [ + 'url' => 'https://cw.org/notify', + ], + ], + ], $payload); } - public function test_it_returns_a_payload_for_a_redirect_based_payment(): void + public function test_it_returns_a_payload_with_fields_that_have_value_only(): void { $billingAddress = $this->prophesize(AddressInterface::class); $billingAddress->getFullName()->willReturn('Don Matteo'); @@ -57,11 +117,16 @@ public function test_it_returns_a_payload_for_a_redirect_based_payment(): void $payment->getAmount()->willReturn(1050); $billingAddress->getPhoneNumber()->willReturn('123123123'); - $billingAddress->getStreet()->willReturn('Sesame Street'); - $billingAddress->getCity()->willReturn('Sesame City'); + $billingAddress->getStreet()->willReturn(''); + $billingAddress->getCity()->willReturn(''); $billingAddress->getPostcode()->willReturn('90 210'); $billingAddress->getCountryCode()->willReturn('PL'); + $this->translator->trans( + 'commerce_weavers_sylius_tpay.tpay.transaction_description', + ['%orderNumber%' => '000000001'] + )->willReturn(self::TRANSLATED_DESCRIPTION); + $this->callbackUrlGenerator ->generateSuccessUrl($payment, 'pl_PL') ->willReturn('https://cw.org/success') @@ -81,6 +146,69 @@ public function test_it_returns_a_payload_for_a_redirect_based_payment(): void 'email' => 'don.matteo@sandomierz.org', 'name' => 'Don Matteo', 'phone' => '123123123', + 'code' => '90 210', + 'country' => 'PL', + ], + 'callbacks' => [ + 'payerUrls' => [ + 'success' => 'https://cw.org/success', + 'error' => 'https://cw.org/error', + ], + 'notification' => [ + 'url' => 'https://cw.org/notify', + ], + ], + ], $payload); + } + + public function test_it_always_returns_a_payload_with_required_fields_even_if_their_value_is_empty(): void + { + $billingAddress = $this->prophesize(AddressInterface::class); + $billingAddress->getFullName()->willReturn(''); + + $customer = $this->prophesize(CustomerInterface::class); + $customer->getEmail()->willReturn(''); + + $order = $this->prophesize(OrderInterface::class); + $order->getCustomer()->willReturn($customer); + $order->getBillingAddress()->willReturn($billingAddress); + $order->getNumber()->willReturn('000000001'); + + $payment = $this->prophesize(PaymentInterface::class); + $payment->getDetails()->willReturn([]); + $payment->getOrder()->willReturn($order); + $payment->getAmount()->willReturn(1050); + + $billingAddress->getPhoneNumber()->willReturn('123123123'); + $billingAddress->getStreet()->willReturn('Sesame Street'); + $billingAddress->getCity()->willReturn('Sesame City'); + $billingAddress->getPostcode()->willReturn('90 210'); + $billingAddress->getCountryCode()->willReturn('PL'); + + $this->translator->trans( + 'commerce_weavers_sylius_tpay.tpay.transaction_description', + ['%orderNumber%' => '000000001'] + )->willReturn(self::TRANSLATED_DESCRIPTION); + + $this->callbackUrlGenerator + ->generateSuccessUrl($payment, 'pl_PL') + ->willReturn('https://cw.org/success') + ; + $this->callbackUrlGenerator + ->generateFailureUrl($payment, 'pl_PL') + ->willReturn('https://cw.org/error') + ; + + $payload = $this->createTestSubject()->createFrom($payment->reveal(), 'https://cw.org/notify', 'pl_PL'); + + $this->assertSame([ + 'amount' => '10.50', + 'description' => self::TRANSLATED_DESCRIPTION, + 'lang' => 'pl', + 'payer' => [ + 'email' => '', + 'name' => '', + 'phone' => '123123123', 'address' => 'Sesame Street', 'city' => 'Sesame City', 'code' => '90 210', @@ -103,6 +231,8 @@ public function test_it_throws_an_exception_if_the_order_is_null(): void $this->expectException(InvalidArgumentException::class); $payment = $this->prophesize(PaymentInterface::class); + $payment->getAmount()->willReturn(1050); + $payment->getOrder()->willReturn(null); $this->createTestSubject()->createFrom($payment->reveal(), 'https://cw.org/notify', 'pl_PL'); } @@ -116,7 +246,8 @@ public function test_it_throws_an_exception_if_the_customer_is_null(): void $order->getCustomer()->willReturn(null); $payment = $this->prophesize(PaymentInterface::class); - $payment->getOrder()->willReturn($order); + $payment->getAmount()->willReturn(1050); + $payment->getOrder()->willReturn(null); $this->createTestSubject()->createFrom($payment->reveal(), 'https://cw.org/notify', 'pl_PL'); } @@ -129,9 +260,11 @@ public function test_it_throws_an_exception_if_the_billing_address_is_null(): vo $order = $this->prophesize(OrderInterface::class); $order->getCustomer()->willReturn($customer); + $order->getNumber()->willReturn('000000001'); $order->getBillingAddress()->willReturn(null); $payment = $this->prophesize(PaymentInterface::class); + $payment->getAmount()->willReturn(1050); $payment->getOrder()->willReturn($order); $this->createTestSubject()->createFrom($payment->reveal(), 'https://cw.org/notify', 'pl_PL');