Skip to content

Commit

Permalink
bug #175 CreateOrderApi fix for orders with promotion (LaskeJu)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.0-dev branch.

Discussion
----------

Order payments fails, when using promotion codes.

Commits
-------

f11eee7 fix: added discount to PayPal data to prevent PayPal throw an error while using an discount in order
b1db365 CreateOrderApi coding standard fix
23cd28e CreateOrderApiSpec getOrderPromotionTotal added
f94082d it_creates_pay_pal_order_with_promotion added to CreateOrderApiSpec
0471728 discount added to UpdateOrderApi and Spec
  • Loading branch information
Zales0123 authored Jan 15, 2021
2 parents be62241 + 0471728 commit 41b7ef7
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
78 changes: 78 additions & 0 deletions spec/Api/CreateOrderApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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' => [
Expand Down Expand Up @@ -549,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]);
}
}
2 changes: 2 additions & 0 deletions spec/Api/UpdateOrderApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);

Expand Down
4 changes: 4 additions & 0 deletions src/Api/CreateOrderApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
4 changes: 4 additions & 0 deletions src/Api/UpdateOrderApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down

0 comments on commit 41b7ef7

Please sign in to comment.