Skip to content

Commit

Permalink
Allow sending discounts
Browse files Browse the repository at this point in the history
  • Loading branch information
marianogoldman committed Nov 28, 2024
1 parent 31d2dc0 commit 424b418
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 9 deletions.
25 changes: 23 additions & 2 deletions src/OrderBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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' => [
Expand All @@ -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;
}
}
59 changes: 59 additions & 0 deletions tests/OrderBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
14 changes: 7 additions & 7 deletions tests/PayPalApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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')
Expand All @@ -69,7 +70,6 @@ public function create_order()
$this->assertStringStartsWith('https://www.sandbox.paypal.com/checkoutnow', $link['href']);
}


/**
* @return void
* @throws Exception
Expand Down

0 comments on commit 424b418

Please sign in to comment.