From 424b41822a700fcdddf58d4a113b228c7c5608b4 Mon Sep 17 00:00:00 2001 From: Mariano Goldman Date: Wed, 27 Nov 2024 21:09:29 -0300 Subject: [PATCH] Allow sending discounts --- src/OrderBuilder.php | 25 ++++++++++++++-- tests/OrderBuilderTest.php | 59 ++++++++++++++++++++++++++++++++++++++ tests/PayPalApiTest.php | 14 ++++----- 3 files changed, 89 insertions(+), 9 deletions(-) diff --git a/src/OrderBuilder.php b/src/OrderBuilder.php index 119c8e0..9af35e8 100644 --- a/src/OrderBuilder.php +++ b/src/OrderBuilder.php @@ -9,6 +9,7 @@ class OrderBuilder private string $externalId = ''; private string $currency = ''; private float $amount = 0; + private float $discount = 0; private string $description = ''; private string $brandName = ''; private string $locale = 'es-AR'; @@ -48,6 +49,12 @@ public function amount(float $amount): OrderBuilder return $this; } + public function discount(float $discount): OrderBuilder + { + $this->discount = $discount; + return $this; + } + /** * @param string $description * @return OrderBuilder @@ -100,14 +107,20 @@ public function cancelUrl(string $cancelUrl): OrderBuilder public function make(): array { - return [ + $arr = [ 'intent' => 'CAPTURE', 'purchase_units' => [ [ 'custom_id' => $this->externalId, 'amount' => [ 'currency_code' => $this->currency, - 'value' => round($this->amount, 2), + 'value' => round($this->amount - $this->discount, 2), + 'breakdown' => [ + 'item_total' => [ + 'currency_code' => $this->currency, + 'value' => round($this->amount, 2), + ], + ], ], 'description' => $this->description, 'payment_options' => [ @@ -127,5 +140,13 @@ public function make(): array 'cancel_url' => $this->cancelUrl ], ]; + + if ($this->discount > 0) { + $arr['purchase_units'][0]['amount']['breakdown']['discount'] = [ + 'currency_code' => $this->currency, + 'value' => round($this->discount, 2), + ]; + } + return $arr; } } diff --git a/tests/OrderBuilderTest.php b/tests/OrderBuilderTest.php index cea6f00..c08fc23 100644 --- a/tests/OrderBuilderTest.php +++ b/tests/OrderBuilderTest.php @@ -29,6 +29,65 @@ public function create_order_with_int_amount() 'amount' => [ 'currency_code' => 'USD', 'value' => 23.21, + 'breakdown' => [ + 'item_total' => [ + 'currency_code' => 'USD', + 'value' => 23.21, + ], + ] + ], + 'description' => 'My custom product', + 'payment_options' => [ + 'allowed_payment_method' => 'INSTANT_FUNDING_SOURCE' + ], + ] + ], + 'application_context' => [ + 'brand_name' => 'My brand name', + 'locale' => 'es-AR', + 'user_action' => 'PAY_NOW', + 'payment_method' => [ + 'payee_preferred' => 'IMMEDIATE_PAYMENT_REQUIRED', + ], + 'shipping_preference' => 'NO_SHIPPING', + 'return_url' => 'http://localhost:8080/return', + 'cancel_url' => 'http://localhost:8080/cancel' + ], + ], $order); + } + + #[Test] + public function create_order_with_discount() + { + $order = (new OrderBuilder()) + ->externalId('31fe5538-8589-437d-8823-3b0574186a5f') + ->currency('USD') + ->amount(23.99) + ->discount(3.98) + ->description('My custom product') + ->brandName('My brand name') + ->returnUrl('http://localhost:8080/return') + ->cancelUrl('http://localhost:8080/cancel') + ->make(); + + $this->assertEquals([ + 'intent' => 'CAPTURE', + 'purchase_units' => [ + [ + 'custom_id' => '31fe5538-8589-437d-8823-3b0574186a5f', + 'amount' => [ + 'currency_code' => 'USD', + 'value' => 20.01, + 'breakdown' => [ + 'item_total' => [ + 'currency_code' => 'USD', + 'value' => 23.99, + ], + 'discount' => [ + 'currency_code' => 'USD', + 'value' => 3.98, + ], + ] ], 'description' => 'My custom product', 'payment_options' => [ diff --git a/tests/PayPalApiTest.php b/tests/PayPalApiTest.php index f1c2fa7..69fa575 100644 --- a/tests/PayPalApiTest.php +++ b/tests/PayPalApiTest.php @@ -26,12 +26,12 @@ public function setUp(): void ); } - protected function getEnvironmentSetUp($app) - { - $app['config']->set('paypal.client_id', env('PAYPAL_API_CLIENT_ID')); - $app['config']->set('paypal.client_secret', env('PAYPAL_API_CLIENT_SECRET')); - $app['config']->set('paypal.use_sandbox', env('SANDBOX_GATEWAYS')); - } +// protected function getEnvironmentSetUp($app) +// { +// $app['config']->set('paypal.client_id', env('PAYPAL_API_CLIENT_ID')); +// $app['config']->set('paypal.client_secret', env('PAYPAL_API_CLIENT_SECRET')); +// $app['config']->set('paypal.use_sandbox', env('SANDBOX_GATEWAYS')); +// } #[Test] public function verify_ipn() @@ -50,6 +50,7 @@ public function create_order() ->externalId($this->faker->uuid) ->currency('USD') ->amount(23.20) + ->discount(2.19999) ->description('My custom product') ->brandName('My brand name') ->returnUrl('http://localhost:8080/return') @@ -69,7 +70,6 @@ public function create_order() $this->assertStringStartsWith('https://www.sandbox.paypal.com/checkoutnow', $link['href']); } - /** * @return void * @throws Exception