From f11eee76438dd020dabc0331f4faa0613d71a6a9 Mon Sep 17 00:00:00 2001 From: Justus Laske Date: Mon, 14 Dec 2020 16:52:09 +0100 Subject: [PATCH 1/5] fix: added discount to PayPal data to prevent PayPal throw an error while using an discount in order --- src/Api/CreateOrderApi.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Api/CreateOrderApi.php b/src/Api/CreateOrderApi.php index a2e18ef5..4849a872 100644 --- a/src/Api/CreateOrderApi.php +++ b/src/Api/CreateOrderApi.php @@ -83,6 +83,10 @@ public function create(string $token, PaymentInterface $payment, string $referen 'currency_code' => $order->getCurrencyCode(), 'value' => $payPalItemData['total_tax'], ], + 'discount' => [ + 'currency_code' => $order->getCurrencyCode(), + 'value' => abs($order->getOrderPromotionTotal()) / 100, + ] ], ], 'payee' => [ From b1db3655b7711ccfca3301a3a1e7280a65b5d6c7 Mon Sep 17 00:00:00 2001 From: Justus Laske Date: Mon, 14 Dec 2020 17:52:08 +0100 Subject: [PATCH 2/5] CreateOrderApi coding standard fix --- src/Api/CreateOrderApi.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Api/CreateOrderApi.php b/src/Api/CreateOrderApi.php index 4849a872..3f0c8717 100644 --- a/src/Api/CreateOrderApi.php +++ b/src/Api/CreateOrderApi.php @@ -86,7 +86,7 @@ public function create(string $token, PaymentInterface $payment, string $referen 'discount' => [ 'currency_code' => $order->getCurrencyCode(), 'value' => abs($order->getOrderPromotionTotal()) / 100, - ] + ], ], ], 'payee' => [ From 23cd28ecc9da351216a71eadd272f6d12064a8e8 Mon Sep 17 00:00:00 2001 From: Justus Laske Date: Mon, 14 Dec 2020 19:57:50 +0100 Subject: [PATCH 3/5] CreateOrderApiSpec getOrderPromotionTotal added --- spec/Api/CreateOrderApiSpec.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/Api/CreateOrderApiSpec.php b/spec/Api/CreateOrderApiSpec.php index dc460e3b..50ab5c89 100644 --- a/spec/Api/CreateOrderApiSpec.php +++ b/spec/Api/CreateOrderApiSpec.php @@ -56,6 +56,7 @@ function it_creates_pay_pal_order_based_on_given_payment( $order->getItemsTotal()->willReturn(9000); $order->getShippingTotal()->willReturn(1000); $order->isShippingRequired()->willReturn(true); + $order->getOrderPromotionTotal()->willReturn(0); $payPalItemDataProvider->provide($order)->willReturn([ 'items' => [ @@ -124,6 +125,7 @@ function it_creates_pay_pal_order_with_shipping_address_based_on_given_payment( $order->getItemsTotal()->willReturn(9000); $order->getShippingTotal()->willReturn(1000); $order->isShippingRequired()->willReturn(true); + $order->getOrderPromotionTotal()->willReturn(0); $shippingAddress->getFullName()->willReturn('Gandalf The Grey'); $shippingAddress->getStreet()->willReturn('Hobbit St. 123'); @@ -201,6 +203,7 @@ function it_creates_pay_pal_order_with_more_than_one_product( $order->getItemsTotal()->willReturn(17000); $order->getShippingTotal()->willReturn(3000); $order->isShippingRequired()->willReturn(true); + $order->getOrderPromotionTotal()->willReturn(0); $shippingAddress->getFullName()->willReturn('Gandalf The Grey'); $shippingAddress->getStreet()->willReturn('Hobbit St. 123'); @@ -293,6 +296,7 @@ function it_creates_pay_pal_order_with_non_neutral_tax_and_changed_quantity( $order->getItemsTotal()->willReturn(12000); $order->getShippingTotal()->willReturn(1000); $order->isShippingRequired()->willReturn(true); + $order->getOrderPromotionTotal()->willReturn(0); $shippingAddress->getFullName()->willReturn('Gandalf The Grey'); $shippingAddress->getStreet()->willReturn('Hobbit St. 123'); @@ -389,6 +393,7 @@ function it_creates_pay_pal_order_with_more_than_one_product_with_different_tax_ $order->getItemsTotal()->willReturn(17400); $order->getShippingTotal()->willReturn(3000); $order->isShippingRequired()->willReturn(true); + $order->getOrderPromotionTotal()->willReturn(0); $shippingAddress->getFullName()->willReturn('Gandalf The Grey'); $shippingAddress->getStreet()->willReturn('Hobbit St. 123'); @@ -503,6 +508,7 @@ function it_allows_to_create_digital_order( $order->getItemsTotal()->willReturn(20000); $order->getShippingTotal()->willReturn(0); $order->isShippingRequired()->willReturn(false); + $order->getOrderPromotionTotal()->willReturn(0); $payPalItemDataProvider->provide($order)->willReturn([ 'items' => [ From f94082da6b1cbb05daf7947904b0d1ee835003a3 Mon Sep 17 00:00:00 2001 From: Justus Laske Date: Thu, 17 Dec 2020 19:17:54 +0100 Subject: [PATCH 4/5] it_creates_pay_pal_order_with_promotion added to CreateOrderApiSpec --- spec/Api/CreateOrderApiSpec.php | 72 +++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/spec/Api/CreateOrderApiSpec.php b/spec/Api/CreateOrderApiSpec.php index 50ab5c89..0e09e6b4 100644 --- a/spec/Api/CreateOrderApiSpec.php +++ b/spec/Api/CreateOrderApiSpec.php @@ -555,4 +555,76 @@ function it_allows_to_create_digital_order( $this->create('TOKEN', $payment, 'REFERENCE_ID')->shouldReturn(['status' => 'CREATED', 'id' => 123]); } + + function it_creates_pay_pal_order_with_promotion( + PayPalClientInterface $client, + PaymentReferenceNumberProviderInterface $paymentReferenceNumberProvider, + PaymentInterface $payment, + OrderInterface $order, + PaymentMethodInterface $paymentMethod, + GatewayConfigInterface $gatewayConfig, + PayPalItemDataProviderInterface $payPalItemDataProvider + ): void { + $payment->getOrder()->willReturn($order); + $payment->getAmount()->willReturn(2999); + $order->getCurrencyCode()->willReturn('PLN'); + $order->getShippingAddress()->willReturn(null); + $order->getItemsTotal()->willReturn(2250); + $order->getShippingTotal()->willReturn(749); + $order->isShippingRequired()->willReturn(true); + $order->getOrderPromotionTotal()->willReturn(-250); + + $payPalItemDataProvider->provide($order)->willReturn([ + 'items' => [ + [ + 'name' => 'PRODUCT_ONE', + 'unit_amount' => [ + 'value' => 25, + 'currency_code' => 'PLN', + ], + 'quantity' => 1, + 'tax' => [ + 'value' => 0, + 'currency_code' => 'PLN', + ], + ], + ], + 'total_item_value' => 25, + 'total_tax' => 0, + ]); + + $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_number'] === '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' && + $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 && + $data['purchase_units'][0]['amount']['breakdown']['discount']['currency_code'] === 'PLN' && + $data['purchase_units'][0]['amount']['breakdown']['discount']['value'] === 2.5 && + $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 && + $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]); + } } From 04717282811c87cdb793ec00ad0a02f218247bc1 Mon Sep 17 00:00:00 2001 From: Justus Laske Date: Thu, 7 Jan 2021 21:09:00 +0100 Subject: [PATCH 5/5] discount added to UpdateOrderApi and Spec --- spec/Api/UpdateOrderApiSpec.php | 2 ++ src/Api/UpdateOrderApi.php | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/spec/Api/UpdateOrderApiSpec.php b/spec/Api/UpdateOrderApiSpec.php index ed040e17..e6874323 100644 --- a/spec/Api/UpdateOrderApiSpec.php +++ b/spec/Api/UpdateOrderApiSpec.php @@ -58,6 +58,7 @@ function it_updates_pay_pal_order_with_given_new_total( $order->getTotal()->willReturn(1122); $order->getCurrencyCode()->willReturn('USD'); $order->getShippingTotal()->willReturn(22); + $order->getOrderPromotionTotal()->willReturn(0); $shippingAddress->getFullName()->willReturn('John Doe'); $shippingAddress->getStreet()->willReturn('Main St. 123'); @@ -115,6 +116,7 @@ function it_updates_digital_order( $order->getTotal()->willReturn(1122); $order->getCurrencyCode()->willReturn('USD'); $order->getShippingTotal()->willReturn(0); + $order->getOrderPromotionTotal()->willReturn(0); $order->isShippingRequired()->willReturn(false); diff --git a/src/Api/UpdateOrderApi.php b/src/Api/UpdateOrderApi.php index ae20975f..85002527 100644 --- a/src/Api/UpdateOrderApi.php +++ b/src/Api/UpdateOrderApi.php @@ -74,6 +74,10 @@ public function update( 'currency_code' => $order->getCurrencyCode(), 'value' => $payPalItemData['total_tax'], ], + 'discount' => [ + 'currency_code' => $order->getCurrencyCode(), + 'value' => abs($order->getOrderPromotionTotal()) / 100, + ], ], ], 'payee' => [