Skip to content

Commit

Permalink
bug #296 Fix and enhance PayPalPurchaseUnit (maikrosenthal)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.6 branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | 1.6 (bug fixes, improvements)
| Bug fix?        | yes
| New feature?    | no
| Related tickets | fixes #215

This will fix the wrong attribute "invoice_number" and add the "shipping_discount" to PayPalPurchaseUnit

Commits
-------

f4d63b2 Use constructor property promotion
a97fada Fixed wrong attribute invoice_number
b7ab860 Also transmit the shipping discount
1178884 Implemented suggested changes
  • Loading branch information
GSadee committed Jul 2, 2024
2 parents a2a6bbd + 1178884 commit 7c1b1fc
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 95 deletions.
35 changes: 29 additions & 6 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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
Expand Down
90 changes: 87 additions & 3 deletions spec/Api/CreateOrderApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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' => [
Expand Down Expand Up @@ -92,7 +94,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' &&
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -167,7 +170,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' &&
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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' => [
Expand Down Expand Up @@ -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' => [
Expand Down Expand Up @@ -608,7 +616,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' &&
Expand All @@ -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]);
}
}
2 changes: 1 addition & 1 deletion spec/Api/RefundPaymentApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down
11 changes: 7 additions & 4 deletions spec/Api/UpdateOrderApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,12 +56,13 @@ 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');
$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');
Expand All @@ -78,7 +80,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' &&
Expand Down Expand Up @@ -115,12 +117,13 @@ 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');
$order->getShippingTotal()->willReturn(0);
$order->getOrderPromotionTotal()->willReturn(0);
$order->getAdjustmentsTotalRecursively(AdjustmentInterface::ORDER_SHIPPING_PROMOTION_ADJUSTMENT)->willReturn(0);

$order->isShippingRequired()->willReturn(false);

Expand All @@ -132,7 +135,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' &&
Expand Down
12 changes: 6 additions & 6 deletions spec/Model/PayPalOrderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Loading

0 comments on commit 7c1b1fc

Please sign in to comment.