From f4d63b2e05b33cb5af11562cacf97b21efd7e931 Mon Sep 17 00:00:00 2001 From: Maik Rosenthal Date: Tue, 11 Jun 2024 10:08:37 +0200 Subject: [PATCH 1/4] Use constructor property promotion --- src/Model/PayPalPurchaseUnit.php | 78 ++++++-------------------------- 1 file changed, 13 insertions(+), 65 deletions(-) diff --git a/src/Model/PayPalPurchaseUnit.php b/src/Model/PayPalPurchaseUnit.php index e28f76f8..1952b869 100644 --- a/src/Model/PayPalPurchaseUnit.php +++ b/src/Model/PayPalPurchaseUnit.php @@ -18,73 +18,21 @@ class PayPalPurchaseUnit { - /** @var string */ - private $referenceId; - - /** @var string */ - private $invoiceNumber; - - /** @var string */ - private $currencyCode; - - /** @var int */ - private $totalAmount; - - /** @var int */ - private $shippingValue; - - /** @var float */ - private $itemTotalValue; - - /** @var float */ - private $taxTotalValue; - - /** @var int */ - private $discountValue; - - /** @var string */ - private $softDescriptor; - - /** @var string */ - private $merchantId; - - /** @var ?AddressInterface */ - private $shippingAddress; - - /** @var bool */ - private $shippingRequired; - - /** @var array */ - private $items; - public function __construct( - string $referenceId, - string $invoiceNumber, - string $currencyCode, - int $totalAmount, - int $shippingValue, - float $itemTotalValue, - float $taxTotalValue, - int $discountValue, - string $merchantId, - array $items, - bool $shippingRequired, - ?AddressInterface $shippingAddress = null, - string $softDescriptor = 'Sylius PayPal Payment', + private readonly string $referenceId, + private readonly string $invoiceNumber, + private readonly string $currencyCode, + private readonly int $totalAmount, + private readonly int $shippingValue, + private readonly float $itemTotalValue, + private readonly float $taxTotalValue, + private readonly int $discountValue, + private readonly string $merchantId, + private readonly array $items, + private readonly bool $shippingRequired, + private readonly ?AddressInterface $shippingAddress = null, + private readonly string $softDescriptor = 'Sylius PayPal Payment', ) { - $this->referenceId = $referenceId; - $this->invoiceNumber = $invoiceNumber; - $this->currencyCode = $currencyCode; - $this->totalAmount = $totalAmount; - $this->shippingValue = $shippingValue; - $this->itemTotalValue = $itemTotalValue; - $this->taxTotalValue = $taxTotalValue; - $this->discountValue = $discountValue; - $this->merchantId = $merchantId; - $this->items = $items; - $this->shippingRequired = $shippingRequired; - $this->shippingAddress = $shippingAddress; - $this->softDescriptor = $softDescriptor; } public function toArray(): array From a97fada90dfda0b2a9ed411424e4407cca62939b Mon Sep 17 00:00:00 2001 From: Maik Rosenthal Date: Tue, 11 Jun 2024 10:52:38 +0200 Subject: [PATCH 2/4] Fixed wrong attribute invoice_number --- spec/Api/CreateOrderApiSpec.php | 6 +++--- spec/Api/RefundPaymentApiSpec.php | 2 +- spec/Api/UpdateOrderApiSpec.php | 8 ++++---- spec/Model/PayPalOrderSpec.php | 12 ++++++------ spec/Model/PayPalPurchaseUnitSpec.php | 12 ++++++------ src/Api/RefundPaymentApi.php | 4 ++-- src/Api/RefundPaymentApiInterface.php | 2 +- src/Model/PayPalPurchaseUnit.php | 4 ++-- tests/Service/DummyRefundPaymentApi.php | 2 +- 9 files changed, 26 insertions(+), 26 deletions(-) diff --git a/spec/Api/CreateOrderApiSpec.php b/spec/Api/CreateOrderApiSpec.php index d78e47a3..bca9c4a2 100644 --- a/spec/Api/CreateOrderApiSpec.php +++ b/spec/Api/CreateOrderApiSpec.php @@ -92,7 +92,7 @@ function it_creates_pay_pal_order_based_on_given_payment( Argument::that(function (array $data): bool { return $data['intent'] === 'CAPTURE' && - $data['purchase_units'][0]['invoice_number'] === 'REFERENCE-NUMBER' && + $data['purchase_units'][0]['invoice_id'] === 'REFERENCE-NUMBER' && $data['purchase_units'][0]['amount']['value'] === '100.00' && $data['purchase_units'][0]['amount']['currency_code'] === 'PLN' && $data['purchase_units'][0]['amount']['breakdown']['shipping']['currency_code'] === 'PLN' && @@ -167,7 +167,7 @@ function it_creates_pay_pal_order_with_shipping_address_based_on_given_payment( Argument::that(function (array $data): bool { return $data['intent'] === 'CAPTURE' && - $data['purchase_units'][0]['invoice_number'] === 'REFERENCE-NUMBER' && + $data['purchase_units'][0]['invoice_id'] === 'REFERENCE-NUMBER' && $data['purchase_units'][0]['amount']['value'] === '100.00' && $data['purchase_units'][0]['amount']['currency_code'] === 'PLN' && $data['purchase_units'][0]['shipping']['name']['full_name'] === 'Gandalf The Grey' && @@ -608,7 +608,7 @@ function it_creates_pay_pal_order_with_promotion( Argument::that(function (array $data): bool { return $data['intent'] === 'CAPTURE' && - $data['purchase_units'][0]['invoice_number'] === 'REFERENCE-NUMBER' && + $data['purchase_units'][0]['invoice_id'] === 'REFERENCE-NUMBER' && $data['purchase_units'][0]['amount']['value'] === '29.99' && $data['purchase_units'][0]['amount']['currency_code'] === 'PLN' && $data['purchase_units'][0]['amount']['breakdown']['shipping']['currency_code'] === 'PLN' && diff --git a/spec/Api/RefundPaymentApiSpec.php b/spec/Api/RefundPaymentApiSpec.php index e7383965..5bd13146 100644 --- a/spec/Api/RefundPaymentApiSpec.php +++ b/spec/Api/RefundPaymentApiSpec.php @@ -35,7 +35,7 @@ function it_refunds_pay_pal_payment_with_given_id(PayPalClientInterface $client) ->post( 'v2/payments/captures/123123/refund', 'TOKEN', - ['amount' => ['value' => '10.99', 'currency_code' => 'USD'], 'invoice_number' => '123-11-11-2010'], + ['amount' => ['value' => '10.99', 'currency_code' => 'USD'], 'invoice_id' => '123-11-11-2010'], ['PayPal-Auth-Assertion' => 'PAY-PAL-AUTH-ASSERTION'], ) ->willReturn(['status' => 'COMPLETED', 'id' => '123123']) diff --git a/spec/Api/UpdateOrderApiSpec.php b/spec/Api/UpdateOrderApiSpec.php index 0bbf97db..42df7f51 100644 --- a/spec/Api/UpdateOrderApiSpec.php +++ b/spec/Api/UpdateOrderApiSpec.php @@ -55,7 +55,7 @@ function it_updates_pay_pal_order_with_given_new_total( ->willReturn(['items' => ['data'], 'total_item_value' => '10.00', 'total_tax' => '1.00']) ; - $paymentReferenceNumberProvider->provide($payment)->willReturn('INVOICE_NUMBER'); + $paymentReferenceNumberProvider->provide($payment)->willReturn('INVOICE_ID'); $order->getTotal()->willReturn(1122); $order->getCurrencyCode()->willReturn('USD'); @@ -78,7 +78,7 @@ function it_updates_pay_pal_order_with_given_new_total( $data[0]['op'] === 'replace' && $data[0]['path'] === '/purchase_units/@reference_id==\'REFERENCE-ID\'' && $data[0]['value']['reference_id'] === 'REFERENCE-ID' && - $data[0]['value']['invoice_number'] === 'INVOICE_NUMBER' && + $data[0]['value']['invoice_id'] === 'INVOICE_ID' && $data[0]['value']['amount']['value'] === '11.22' && $data[0]['value']['amount']['currency_code'] === 'USD' && $data[0]['value']['amount']['breakdown']['shipping']['value'] === '0.22' && @@ -115,7 +115,7 @@ function it_updates_digital_order( ->willReturn(['items' => ['data'], 'total_item_value' => '10.00', 'total_tax' => '1.22']) ; - $paymentReferenceNumberProvider->provide($payment)->willReturn('INVOICE_NUMBER'); + $paymentReferenceNumberProvider->provide($payment)->willReturn('INVOICE_ID'); $order->getTotal()->willReturn(1122); $order->getCurrencyCode()->willReturn('USD'); @@ -132,7 +132,7 @@ function it_updates_digital_order( $data[0]['op'] === 'replace' && $data[0]['path'] === '/purchase_units/@reference_id==\'REFERENCE-ID\'' && $data[0]['value']['reference_id'] === 'REFERENCE-ID' && - $data[0]['value']['invoice_number'] === 'INVOICE_NUMBER' && + $data[0]['value']['invoice_id'] === 'INVOICE_ID' && $data[0]['value']['amount']['value'] === '11.22' && $data[0]['value']['amount']['currency_code'] === 'USD' && $data[0]['value']['amount']['breakdown']['shipping']['value'] === '0.00' && diff --git a/spec/Model/PayPalOrderSpec.php b/spec/Model/PayPalOrderSpec.php index d08b1b9c..5698054e 100644 --- a/spec/Model/PayPalOrderSpec.php +++ b/spec/Model/PayPalOrderSpec.php @@ -36,7 +36,7 @@ public function it_returns_full_paypal_order_data( $payPalPurchaseUnit->toArray()->willReturn( [ 'reference_id' => 'REFERENCE_ID', - 'invoice_number' => 'INVOICE_NUMBER', + 'invoice_id' => 'INVOICE_ID', 'amount' => [ 'currency_code' => 'CURRENCY_CODE', 'value' => 100, @@ -86,7 +86,7 @@ public function it_returns_full_paypal_order_data( 'purchase_units' => [ [ 'reference_id' => 'REFERENCE_ID', - 'invoice_number' => 'INVOICE_NUMBER', + 'invoice_id' => 'INVOICE_ID', 'amount' => [ 'currency_code' => 'CURRENCY_CODE', 'value' => 100, @@ -146,7 +146,7 @@ public function it_returns_paypal_order_data_without_shipping_address( $payPalPurchaseUnit->toArray()->willReturn( [ 'reference_id' => 'REFERENCE_ID', - 'invoice_number' => 'INVOICE_NUMBER', + 'invoice_id' => 'INVOICE_ID', 'amount' => [ 'currency_code' => 'CURRENCY_CODE', 'value' => 100, @@ -185,7 +185,7 @@ public function it_returns_paypal_order_data_without_shipping_address( 'purchase_units' => [ [ 'reference_id' => 'REFERENCE_ID', - 'invoice_number' => 'INVOICE_NUMBER', + 'invoice_id' => 'INVOICE_ID', 'amount' => [ 'currency_code' => 'CURRENCY_CODE', 'value' => 100, @@ -234,7 +234,7 @@ public function it_returns_paypal_order_data_if_shipping_is_not_required( $payPalPurchaseUnit->toArray()->willReturn( [ 'reference_id' => 'REFERENCE_ID', - 'invoice_number' => 'INVOICE_NUMBER', + 'invoice_id' => 'INVOICE_ID', 'amount' => [ 'currency_code' => 'CURRENCY_CODE', 'value' => 100, @@ -273,7 +273,7 @@ public function it_returns_paypal_order_data_if_shipping_is_not_required( 'purchase_units' => [ [ 'reference_id' => 'REFERENCE_ID', - 'invoice_number' => 'INVOICE_NUMBER', + 'invoice_id' => 'INVOICE_ID', 'amount' => [ 'currency_code' => 'CURRENCY_CODE', 'value' => 100, diff --git a/spec/Model/PayPalPurchaseUnitSpec.php b/spec/Model/PayPalPurchaseUnitSpec.php index 72656a81..5c580c06 100644 --- a/spec/Model/PayPalPurchaseUnitSpec.php +++ b/spec/Model/PayPalPurchaseUnitSpec.php @@ -22,7 +22,7 @@ function let(AddressInterface $shippingAddress): void { $this->beConstructedWith( 'REFERENCE_ID', - 'INVOICE_NUMBER', + 'INVOICE_ID', 'CURRENCY_CODE', 10000, 1000, @@ -48,7 +48,7 @@ function it_returns_proper_paypal_purchase_unit(AddressInterface $shippingAddres $this->toArray()->shouldReturn( [ 'reference_id' => 'REFERENCE_ID', - 'invoice_number' => 'INVOICE_NUMBER', + 'invoice_id' => 'INVOICE_ID', 'amount' => [ 'currency_code' => 'CURRENCY_CODE', 'value' => '100.00', @@ -97,7 +97,7 @@ function it_returns_proper_paypal_purchase_unit_if_shipping_is_not_required(Addr { $this->beConstructedWith( 'REFERENCE_ID', - 'INVOICE_NUMBER', + 'INVOICE_ID', 'CURRENCY_CODE', 10000, 1000, @@ -113,7 +113,7 @@ function it_returns_proper_paypal_purchase_unit_if_shipping_is_not_required(Addr $this->toArray()->shouldReturn( [ 'reference_id' => 'REFERENCE_ID', - 'invoice_number' => 'INVOICE_NUMBER', + 'invoice_id' => 'INVOICE_ID', 'amount' => [ 'currency_code' => 'CURRENCY_CODE', 'value' => '100.00', @@ -151,7 +151,7 @@ function it_returns_proper_paypal_purchase_unit_if_shipping_is_not_set(): void { $this->beConstructedWith( 'REFERENCE_ID', - 'INVOICE_NUMBER', + 'INVOICE_ID', 'CURRENCY_CODE', 10000, 1000, @@ -167,7 +167,7 @@ function it_returns_proper_paypal_purchase_unit_if_shipping_is_not_set(): void $this->toArray()->shouldReturn( [ 'reference_id' => 'REFERENCE_ID', - 'invoice_number' => 'INVOICE_NUMBER', + 'invoice_id' => 'INVOICE_ID', 'amount' => [ 'currency_code' => 'CURRENCY_CODE', 'value' => '100.00', diff --git a/src/Api/RefundPaymentApi.php b/src/Api/RefundPaymentApi.php index cb9e4ce2..c8550a4d 100644 --- a/src/Api/RefundPaymentApi.php +++ b/src/Api/RefundPaymentApi.php @@ -28,14 +28,14 @@ public function refund( string $token, string $paymentId, string $payPalAuthAssertion, - string $invoiceNumber, + string $invoiceId, string $amount, string $currencyCode, ): array { return $this->client->post( sprintf('v2/payments/captures/%s/refund', $paymentId), $token, - ['amount' => ['value' => $amount, 'currency_code' => $currencyCode], 'invoice_number' => $invoiceNumber], + ['amount' => ['value' => $amount, 'currency_code' => $currencyCode], 'invoice_id' => $invoiceId], ['PayPal-Auth-Assertion' => $payPalAuthAssertion], ); } diff --git a/src/Api/RefundPaymentApiInterface.php b/src/Api/RefundPaymentApiInterface.php index d18e7fc8..5b07d241 100644 --- a/src/Api/RefundPaymentApiInterface.php +++ b/src/Api/RefundPaymentApiInterface.php @@ -19,7 +19,7 @@ public function refund( string $token, string $paymentId, string $payPalAuthAssertion, - string $invoiceNumber, + string $invoiceId, string $amount, string $currencyCode, ): array; diff --git a/src/Model/PayPalPurchaseUnit.php b/src/Model/PayPalPurchaseUnit.php index 1952b869..2fa401ad 100644 --- a/src/Model/PayPalPurchaseUnit.php +++ b/src/Model/PayPalPurchaseUnit.php @@ -20,7 +20,7 @@ class PayPalPurchaseUnit { public function __construct( private readonly string $referenceId, - private readonly string $invoiceNumber, + private readonly string $invoiceId, private readonly string $currencyCode, private readonly int $totalAmount, private readonly int $shippingValue, @@ -39,7 +39,7 @@ public function toArray(): array { $paypalPurchaseUnit = [ 'reference_id' => $this->referenceId, - 'invoice_number' => $this->invoiceNumber, + 'invoice_id' => $this->invoiceId, 'amount' => [ 'currency_code' => $this->currencyCode, 'value' => number_format($this->totalAmount / 100, 2, '.', ''), diff --git a/tests/Service/DummyRefundPaymentApi.php b/tests/Service/DummyRefundPaymentApi.php index 76d7c30d..12c97629 100644 --- a/tests/Service/DummyRefundPaymentApi.php +++ b/tests/Service/DummyRefundPaymentApi.php @@ -21,7 +21,7 @@ public function refund( string $token, string $paymentId, string $payPalAuthAssertion, - string $invoiceNumber, + string $invoiceId, string $amount, string $currencyCode, ): array { From b7ab860057ecbef20e87192c55ac2d5eee963649 Mon Sep 17 00:00:00 2001 From: Maik Rosenthal Date: Tue, 11 Jun 2024 13:05:22 +0200 Subject: [PATCH 3/4] Also transmit the shipping discount --- spec/Api/CreateOrderApiSpec.php | 84 +++++++++++++++++++++++++++ spec/Api/UpdateOrderApiSpec.php | 3 + spec/Model/PayPalPurchaseUnitSpec.php | 15 +++++ src/Api/CreateOrderApi.php | 8 ++- src/Api/UpdateOrderApi.php | 8 ++- src/Model/PayPalPurchaseUnit.php | 5 ++ 6 files changed, 121 insertions(+), 2 deletions(-) diff --git a/spec/Api/CreateOrderApiSpec.php b/spec/Api/CreateOrderApiSpec.php index bca9c4a2..042e312f 100644 --- a/spec/Api/CreateOrderApiSpec.php +++ b/spec/Api/CreateOrderApiSpec.php @@ -17,6 +17,7 @@ use PhpSpec\ObjectBehavior; use Prophecy\Argument; use Sylius\Component\Core\Model\AddressInterface; +use Sylius\Component\Core\Model\AdjustmentInterface; use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Core\Model\PaymentInterface; use Sylius\Component\Core\Model\PaymentMethodInterface; @@ -57,6 +58,7 @@ function it_creates_pay_pal_order_based_on_given_payment( $order->getShippingTotal()->willReturn(1000); $order->isShippingRequired()->willReturn(true); $order->getOrderPromotionTotal()->willReturn(0); + $order->getAdjustmentsTotalRecursively(AdjustmentInterface::ORDER_SHIPPING_PROMOTION_ADJUSTMENT)->willReturn(0); $payPalItemDataProvider->provide($order)->willReturn([ 'items' => [ @@ -126,6 +128,7 @@ function it_creates_pay_pal_order_with_shipping_address_based_on_given_payment( $order->getShippingTotal()->willReturn(1000); $order->isShippingRequired()->willReturn(true); $order->getOrderPromotionTotal()->willReturn(0); + $order->getAdjustmentsTotalRecursively(AdjustmentInterface::ORDER_SHIPPING_PROMOTION_ADJUSTMENT)->willReturn(0); $shippingAddress->getFullName()->willReturn('Gandalf The Grey'); $shippingAddress->getStreet()->willReturn('Hobbit St. 123'); @@ -204,6 +207,7 @@ function it_creates_pay_pal_order_with_more_than_one_product( $order->getShippingTotal()->willReturn(3000); $order->isShippingRequired()->willReturn(true); $order->getOrderPromotionTotal()->willReturn(0); + $order->getAdjustmentsTotalRecursively(AdjustmentInterface::ORDER_SHIPPING_PROMOTION_ADJUSTMENT)->willReturn(0); $shippingAddress->getFullName()->willReturn('Gandalf The Grey'); $shippingAddress->getStreet()->willReturn('Hobbit St. 123'); @@ -297,6 +301,7 @@ function it_creates_pay_pal_order_with_non_neutral_tax_and_changed_quantity( $order->getShippingTotal()->willReturn(1000); $order->isShippingRequired()->willReturn(true); $order->getOrderPromotionTotal()->willReturn(0); + $order->getAdjustmentsTotalRecursively(AdjustmentInterface::ORDER_SHIPPING_PROMOTION_ADJUSTMENT)->willReturn(0); $shippingAddress->getFullName()->willReturn('Gandalf The Grey'); $shippingAddress->getStreet()->willReturn('Hobbit St. 123'); @@ -394,6 +399,7 @@ function it_creates_pay_pal_order_with_more_than_one_product_with_different_tax_ $order->getShippingTotal()->willReturn(3000); $order->isShippingRequired()->willReturn(true); $order->getOrderPromotionTotal()->willReturn(0); + $order->getAdjustmentsTotalRecursively(AdjustmentInterface::ORDER_SHIPPING_PROMOTION_ADJUSTMENT)->willReturn(0); $shippingAddress->getFullName()->willReturn('Gandalf The Grey'); $shippingAddress->getStreet()->willReturn('Hobbit St. 123'); @@ -509,6 +515,7 @@ function it_allows_to_create_digital_order( $order->getShippingTotal()->willReturn(0); $order->isShippingRequired()->willReturn(false); $order->getOrderPromotionTotal()->willReturn(0); + $order->getAdjustmentsTotalRecursively(AdjustmentInterface::ORDER_SHIPPING_PROMOTION_ADJUSTMENT)->willReturn(0); $payPalItemDataProvider->provide($order)->willReturn([ 'items' => [ @@ -573,6 +580,7 @@ function it_creates_pay_pal_order_with_promotion( $order->getShippingTotal()->willReturn(749); $order->isShippingRequired()->willReturn(true); $order->getOrderPromotionTotal()->willReturn(-250); + $order->getAdjustmentsTotalRecursively(AdjustmentInterface::ORDER_SHIPPING_PROMOTION_ADJUSTMENT)->willReturn(0); $payPalItemDataProvider->provide($order)->willReturn([ 'items' => [ @@ -627,4 +635,80 @@ function it_creates_pay_pal_order_with_promotion( $this->create('TOKEN', $payment, 'REFERENCE_ID')->shouldReturn(['status' => 'CREATED', 'id' => 123]); } + + function it_creates_pay_pal_order_with_shipping_promotion( + PayPalClientInterface $client, + PaymentReferenceNumberProviderInterface $paymentReferenceNumberProvider, + PaymentInterface $payment, + OrderInterface $order, + PaymentMethodInterface $paymentMethod, + GatewayConfigInterface $gatewayConfig, + PayPalItemDataProviderInterface $payPalItemDataProvider, + ): void { + $payment->getOrder()->willReturn($order); + $payment->getAmount()->willReturn(3000); + $order->getCurrencyCode()->willReturn('PLN'); + $order->getShippingAddress()->willReturn(null); + $order->getItemsTotal()->willReturn(2500); + $order->getShippingTotal()->willReturn(500); + $order->isShippingRequired()->willReturn(true); + $order->getOrderPromotionTotal()->willReturn(0); + $order + ->getAdjustmentsTotalRecursively(AdjustmentInterface::ORDER_SHIPPING_PROMOTION_ADJUSTMENT) + ->willReturn(-249) + ; + + $payPalItemDataProvider->provide($order)->willReturn([ + 'items' => [ + [ + 'name' => 'PRODUCT_ONE', + 'unit_amount' => [ + 'value' => '25.00', + 'currency_code' => 'PLN', + ], + 'quantity' => 1, + 'tax' => [ + 'value' => '0.00', + 'currency_code' => 'PLN', + ], + ], + ], + 'total_item_value' => '25.00', + 'total_tax' => '0.00', + ]); + + $payment->getMethod()->willReturn($paymentMethod); + $paymentMethod->getGatewayConfig()->willReturn($gatewayConfig); + + $paymentReferenceNumberProvider->provide($payment)->willReturn('REFERENCE-NUMBER'); + + $gatewayConfig->getConfig()->willReturn( + ['merchant_id' => 'merchant-id', 'sylius_merchant_id' => 'sylius-merchant-id'], + ); + + $client->post( + 'v2/checkout/orders', + 'TOKEN', + Argument::that(function (array $data): bool { + return + $data['intent'] === 'CAPTURE' && + $data['purchase_units'][0]['invoice_id'] === 'REFERENCE-NUMBER' && + $data['purchase_units'][0]['amount']['value'] === '30.00' && + $data['purchase_units'][0]['amount']['currency_code'] === 'PLN' && + $data['purchase_units'][0]['amount']['breakdown']['shipping']['currency_code'] === 'PLN' && + $data['purchase_units'][0]['amount']['breakdown']['shipping']['value'] === '7.49' && + $data['purchase_units'][0]['amount']['breakdown']['item_total']['currency_code'] === 'PLN' && + $data['purchase_units'][0]['amount']['breakdown']['item_total']['value'] === '25.00' && + $data['purchase_units'][0]['amount']['breakdown']['shipping_discount']['currency_code'] === 'PLN' && + $data['purchase_units'][0]['amount']['breakdown']['shipping_discount']['value'] === '2.49' && + $data['purchase_units'][0]['items'][0]['name'] === 'PRODUCT_ONE' && + $data['purchase_units'][0]['items'][0]['quantity'] === 1 && + $data['purchase_units'][0]['items'][0]['unit_amount']['value'] === '25.00' && + $data['purchase_units'][0]['items'][0]['unit_amount']['currency_code'] === 'PLN' + ; + }), + )->willReturn(['status' => 'CREATED', 'id' => 123]); + + $this->create('TOKEN', $payment, 'REFERENCE_ID')->shouldReturn(['status' => 'CREATED', 'id' => 123]); + } } diff --git a/spec/Api/UpdateOrderApiSpec.php b/spec/Api/UpdateOrderApiSpec.php index 42df7f51..203820d1 100644 --- a/spec/Api/UpdateOrderApiSpec.php +++ b/spec/Api/UpdateOrderApiSpec.php @@ -16,6 +16,7 @@ use PhpSpec\ObjectBehavior; use Prophecy\Argument; use Sylius\Component\Core\Model\AddressInterface; +use Sylius\Component\Core\Model\AdjustmentInterface; use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Core\Model\PaymentInterface; use Sylius\PayPalPlugin\Api\UpdateOrderApiInterface; @@ -61,6 +62,7 @@ function it_updates_pay_pal_order_with_given_new_total( $order->getCurrencyCode()->willReturn('USD'); $order->getShippingTotal()->willReturn(22); $order->getOrderPromotionTotal()->willReturn(0); + $order->getAdjustmentsTotalRecursively(AdjustmentInterface::ORDER_SHIPPING_PROMOTION_ADJUSTMENT)->willReturn(0); $shippingAddress->getFullName()->willReturn('John Doe'); $shippingAddress->getStreet()->willReturn('Main St. 123'); @@ -121,6 +123,7 @@ function it_updates_digital_order( $order->getCurrencyCode()->willReturn('USD'); $order->getShippingTotal()->willReturn(0); $order->getOrderPromotionTotal()->willReturn(0); + $order->getAdjustmentsTotalRecursively(AdjustmentInterface::ORDER_SHIPPING_PROMOTION_ADJUSTMENT)->willReturn(0); $order->isShippingRequired()->willReturn(false); diff --git a/spec/Model/PayPalPurchaseUnitSpec.php b/spec/Model/PayPalPurchaseUnitSpec.php index 5c580c06..0f168baa 100644 --- a/spec/Model/PayPalPurchaseUnitSpec.php +++ b/spec/Model/PayPalPurchaseUnitSpec.php @@ -29,6 +29,7 @@ function let(AddressInterface $shippingAddress): void 80, 10, 0, + 0, 'MERCHANT_ID', [['test_item']], true, @@ -69,6 +70,10 @@ function it_returns_proper_paypal_purchase_unit(AddressInterface $shippingAddres 'currency_code' => 'CURRENCY_CODE', 'value' => '0.00', ], + 'shipping_discount' => [ + 'currency_code' => 'CURRENCY_CODE', + 'value' => '0.00', + ], ], ], 'payee' => [ @@ -104,6 +109,7 @@ function it_returns_proper_paypal_purchase_unit_if_shipping_is_not_required(Addr 80, 10, 0, + 0, 'MERCHANT_ID', [['test_item']], false, @@ -134,6 +140,10 @@ function it_returns_proper_paypal_purchase_unit_if_shipping_is_not_required(Addr 'currency_code' => 'CURRENCY_CODE', 'value' => '0.00', ], + 'shipping_discount' => [ + 'currency_code' => 'CURRENCY_CODE', + 'value' => '0.00', + ], ], ], 'payee' => [ @@ -158,6 +168,7 @@ function it_returns_proper_paypal_purchase_unit_if_shipping_is_not_set(): void 80, 10, 0, + 0, 'MERCHANT_ID', [['test_item']], false, @@ -188,6 +199,10 @@ function it_returns_proper_paypal_purchase_unit_if_shipping_is_not_set(): void 'currency_code' => 'CURRENCY_CODE', 'value' => '0.00', ], + 'shipping_discount' => [ + 'currency_code' => 'CURRENCY_CODE', + 'value' => '0.00', + ], ], ], 'payee' => [ diff --git a/src/Api/CreateOrderApi.php b/src/Api/CreateOrderApi.php index ac197631..23b92a8d 100644 --- a/src/Api/CreateOrderApi.php +++ b/src/Api/CreateOrderApi.php @@ -14,6 +14,7 @@ namespace Sylius\PayPalPlugin\Api; use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface; +use Sylius\Component\Core\Model\AdjustmentInterface; use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Core\Model\PaymentInterface; use Sylius\Component\Core\Model\PaymentMethodInterface; @@ -62,15 +63,20 @@ public function create(string $token, PaymentInterface $payment, string $referen Assert::keyExists($config, 'merchant_id'); Assert::keyExists($config, 'sylius_merchant_id'); + $shippingDiscount = $order->getAdjustmentsTotalRecursively( + AdjustmentInterface::ORDER_SHIPPING_PROMOTION_ADJUSTMENT, + ); + $payPalPurchaseUnit = new PayPalPurchaseUnit( $referenceId, $this->paymentReferenceNumberProvider->provide($payment), (string) $order->getCurrencyCode(), (int) $payment->getAmount(), - $order->getShippingTotal(), + $order->getShippingTotal() - $shippingDiscount, (float) $payPalItemData['total_item_value'], (float) $payPalItemData['total_tax'], $order->getOrderPromotionTotal(), + $shippingDiscount, (string) $config['merchant_id'], (array) $payPalItemData['items'], $order->isShippingRequired(), diff --git a/src/Api/UpdateOrderApi.php b/src/Api/UpdateOrderApi.php index 20b6ec7d..b03ed486 100644 --- a/src/Api/UpdateOrderApi.php +++ b/src/Api/UpdateOrderApi.php @@ -13,6 +13,7 @@ namespace Sylius\PayPalPlugin\Api; +use Sylius\Component\Core\Model\AdjustmentInterface; use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Core\Model\PaymentInterface; use Sylius\PayPalPlugin\Client\PayPalClientInterface; @@ -50,15 +51,20 @@ public function update( $payPalItemData = $this->payPalItemsDataProvider->provide($order); + $shippingDiscount = $order->getAdjustmentsTotalRecursively( + AdjustmentInterface::ORDER_SHIPPING_PROMOTION_ADJUSTMENT, + ); + $data = new PayPalPurchaseUnit( $referenceId, $this->paymentReferenceNumberProvider->provide($payment), (string) $order->getCurrencyCode(), (int) $payment->getAmount(), - $order->getShippingTotal(), + $order->getShippingTotal() - $shippingDiscount, (float) $payPalItemData['total_item_value'], (float) $payPalItemData['total_tax'], $order->getOrderPromotionTotal(), + $shippingDiscount, $merchantId, (array) $payPalItemData['items'], $order->isShippingRequired(), diff --git a/src/Model/PayPalPurchaseUnit.php b/src/Model/PayPalPurchaseUnit.php index 2fa401ad..b758f82a 100644 --- a/src/Model/PayPalPurchaseUnit.php +++ b/src/Model/PayPalPurchaseUnit.php @@ -27,6 +27,7 @@ public function __construct( private readonly float $itemTotalValue, private readonly float $taxTotalValue, private readonly int $discountValue, + private readonly int $shippingDiscountValue, private readonly string $merchantId, private readonly array $items, private readonly bool $shippingRequired, @@ -60,6 +61,10 @@ public function toArray(): array 'currency_code' => $this->currencyCode, 'value' => number_format(abs($this->discountValue) / 100, 2, '.', ''), ], + 'shipping_discount' => [ + 'currency_code' => $this->currencyCode, + 'value' => number_format(abs($this->shippingDiscountValue) / 100, 2, '.', ''), + ], ], ], 'payee' => [ From 11788846467ee22d5152d59770a5575dc5f3fafa Mon Sep 17 00:00:00 2001 From: Maik Rosenthal Date: Wed, 19 Jun 2024 11:46:11 +0200 Subject: [PATCH 4/4] Implemented suggested changes --- UPGRADE.md | 35 ++++++++++++++++++++----- spec/Model/PayPalPurchaseUnitSpec.php | 3 --- src/Api/CreateOrderApi.php | 2 +- src/Api/RefundPaymentApi.php | 4 +-- src/Api/RefundPaymentApiInterface.php | 2 +- src/Api/UpdateOrderApi.php | 2 +- src/Model/PayPalPurchaseUnit.php | 6 ++--- tests/Service/DummyRefundPaymentApi.php | 2 +- 8 files changed, 38 insertions(+), 18 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index 9dfd6fc6..7a4779f3 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -12,9 +12,9 @@ use GuzzleHttp\ClientInterface as GuzzleClientInterface; use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\StreamFactoryInterface; - + public function __construct( - - private readonly GuzzleClientInterface $client, + - private readonly GuzzleClientInterface $client, + private readonly GuzzleClientInterface|ClientInterface $client, private readonly LoggerInterface $logger, private readonly UuidProviderInterface $uuidProvider, @@ -33,7 +33,7 @@ use Psr\Http\Client\ClientInterface; use GuzzleHttp\ClientInterface as GuzzleClientInterface; use Psr\Http\Message\RequestFactoryInterface; - + public function __construct( - private readonly GuzzleClientInterface $client, + private readonly GuzzleClientInterface|ClientInterface $client, @@ -47,7 +47,7 @@ use GuzzleHttp\ClientInterface as GuzzleClientInterface; use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\StreamFactoryInterface; - + public function __construct( - private readonly GuzzleClientInterface $client, + private readonly GuzzleClientInterface|ClientInterface $client, @@ -62,7 +62,7 @@ use Psr\Http\Client\ClientInterface; use GuzzleHttp\ClientInterface as GuzzleClientInterface; use Psr\Http\Message\RequestFactoryInterface; - + public function __construct( - private readonly GuzzleClientInterface $client, + private readonly GuzzleClientInterface|ClientInterface $client, @@ -71,7 +71,30 @@ + private readonly ?RequestFactoryInterface $requestFactory = null, ) ``` - + + `Sylius\PayPalPlugin\Model\PayPalPurchaseUnit`: + ```diff + use Sylius\Component\Core\Model\AddressInterface; + use Webmozart\Assert\Assert; + + public function __construct( + private readonly string $referenceId, + private readonly string $invoiceNumber, + private readonly string $currencyCode, + private readonly int $totalAmount, + private readonly int $shippingValue, + private readonly float $itemTotalValue, + private readonly float $taxTotalValue, + private readonly int $discountValue, + private readonly string $merchantId, + private readonly array $items, + private readonly bool $shippingRequired, + private readonly ?AddressInterface $shippingAddress = null, + private readonly string $softDescriptor = 'Sylius PayPal Payment', + + private readonly int $shippingDiscountValue = 0, + ) + ``` + 1. Added doctrine migration for PostgreSQL. For more information, please refer to the [Sylius 1.13 UPGRADE.md](https://github.com/Sylius/Sylius/blob/1.13/UPGRADE-1.13.md) ### UPGRADE FROM 1.3.0 to 1.3.1 diff --git a/spec/Model/PayPalPurchaseUnitSpec.php b/spec/Model/PayPalPurchaseUnitSpec.php index 0f168baa..d174f26f 100644 --- a/spec/Model/PayPalPurchaseUnitSpec.php +++ b/spec/Model/PayPalPurchaseUnitSpec.php @@ -29,7 +29,6 @@ function let(AddressInterface $shippingAddress): void 80, 10, 0, - 0, 'MERCHANT_ID', [['test_item']], true, @@ -109,7 +108,6 @@ function it_returns_proper_paypal_purchase_unit_if_shipping_is_not_required(Addr 80, 10, 0, - 0, 'MERCHANT_ID', [['test_item']], false, @@ -168,7 +166,6 @@ function it_returns_proper_paypal_purchase_unit_if_shipping_is_not_set(): void 80, 10, 0, - 0, 'MERCHANT_ID', [['test_item']], false, diff --git a/src/Api/CreateOrderApi.php b/src/Api/CreateOrderApi.php index 23b92a8d..b196e9af 100644 --- a/src/Api/CreateOrderApi.php +++ b/src/Api/CreateOrderApi.php @@ -76,11 +76,11 @@ public function create(string $token, PaymentInterface $payment, string $referen (float) $payPalItemData['total_item_value'], (float) $payPalItemData['total_tax'], $order->getOrderPromotionTotal(), - $shippingDiscount, (string) $config['merchant_id'], (array) $payPalItemData['items'], $order->isShippingRequired(), $order->getShippingAddress(), + shippingDiscountValue: $shippingDiscount, ); $payPalOrder = new PayPalOrder($order, $payPalPurchaseUnit, self::PAYPAL_INTENT_CAPTURE); diff --git a/src/Api/RefundPaymentApi.php b/src/Api/RefundPaymentApi.php index c8550a4d..aaa40d9a 100644 --- a/src/Api/RefundPaymentApi.php +++ b/src/Api/RefundPaymentApi.php @@ -28,14 +28,14 @@ public function refund( string $token, string $paymentId, string $payPalAuthAssertion, - string $invoiceId, + string $invoiceNumber, string $amount, string $currencyCode, ): array { return $this->client->post( sprintf('v2/payments/captures/%s/refund', $paymentId), $token, - ['amount' => ['value' => $amount, 'currency_code' => $currencyCode], 'invoice_id' => $invoiceId], + ['amount' => ['value' => $amount, 'currency_code' => $currencyCode], 'invoice_id' => $invoiceNumber], ['PayPal-Auth-Assertion' => $payPalAuthAssertion], ); } diff --git a/src/Api/RefundPaymentApiInterface.php b/src/Api/RefundPaymentApiInterface.php index 5b07d241..d18e7fc8 100644 --- a/src/Api/RefundPaymentApiInterface.php +++ b/src/Api/RefundPaymentApiInterface.php @@ -19,7 +19,7 @@ public function refund( string $token, string $paymentId, string $payPalAuthAssertion, - string $invoiceId, + string $invoiceNumber, string $amount, string $currencyCode, ): array; diff --git a/src/Api/UpdateOrderApi.php b/src/Api/UpdateOrderApi.php index b03ed486..7989b371 100644 --- a/src/Api/UpdateOrderApi.php +++ b/src/Api/UpdateOrderApi.php @@ -64,11 +64,11 @@ public function update( (float) $payPalItemData['total_item_value'], (float) $payPalItemData['total_tax'], $order->getOrderPromotionTotal(), - $shippingDiscount, $merchantId, (array) $payPalItemData['items'], $order->isShippingRequired(), $order->getShippingAddress(), + shippingDiscountValue: $shippingDiscount, ); return $this->client->patch( diff --git a/src/Model/PayPalPurchaseUnit.php b/src/Model/PayPalPurchaseUnit.php index b758f82a..d587fa7e 100644 --- a/src/Model/PayPalPurchaseUnit.php +++ b/src/Model/PayPalPurchaseUnit.php @@ -20,19 +20,19 @@ class PayPalPurchaseUnit { public function __construct( private readonly string $referenceId, - private readonly string $invoiceId, + private readonly string $invoiceNumber, private readonly string $currencyCode, private readonly int $totalAmount, private readonly int $shippingValue, private readonly float $itemTotalValue, private readonly float $taxTotalValue, private readonly int $discountValue, - private readonly int $shippingDiscountValue, private readonly string $merchantId, private readonly array $items, private readonly bool $shippingRequired, private readonly ?AddressInterface $shippingAddress = null, private readonly string $softDescriptor = 'Sylius PayPal Payment', + private readonly int $shippingDiscountValue = 0, ) { } @@ -40,7 +40,7 @@ public function toArray(): array { $paypalPurchaseUnit = [ 'reference_id' => $this->referenceId, - 'invoice_id' => $this->invoiceId, + 'invoice_id' => $this->invoiceNumber, 'amount' => [ 'currency_code' => $this->currencyCode, 'value' => number_format($this->totalAmount / 100, 2, '.', ''), diff --git a/tests/Service/DummyRefundPaymentApi.php b/tests/Service/DummyRefundPaymentApi.php index 12c97629..76d7c30d 100644 --- a/tests/Service/DummyRefundPaymentApi.php +++ b/tests/Service/DummyRefundPaymentApi.php @@ -21,7 +21,7 @@ public function refund( string $token, string $paymentId, string $payPalAuthAssertion, - string $invoiceId, + string $invoiceNumber, string $amount, string $currencyCode, ): array {