Skip to content

Commit

Permalink
Create Payer Payload only from required and existing data (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
lchrusciel authored Oct 30, 2024
2 parents 631a8f9 + d21eb2c commit 2f813f0
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 17 deletions.
42 changes: 29 additions & 13 deletions src/Tpay/Factory/CreateRedirectBasedPaymentPayloadFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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),
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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('[email protected]');

$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' => '[email protected]',
'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');
Expand All @@ -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')
Expand All @@ -81,6 +146,69 @@ public function test_it_returns_a_payload_for_a_redirect_based_payment(): void
'email' => '[email protected]',
'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',
Expand All @@ -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');
}
Expand All @@ -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');
}
Expand All @@ -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');
Expand Down

0 comments on commit 2f813f0

Please sign in to comment.