Skip to content

Commit 440d918

Browse files
Order amount should support floating numbers and format them with 2 decimals (#8)
1 parent a174bcc commit 440d918

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

src/OrderBuilder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class OrderBuilder
88
{
99
private string $externalId = '';
1010
private string $currency = '';
11-
private int $amount = 0;
11+
private float $amount = 0;
1212
private string $description = '';
1313
private string $brandName = '';
1414
private string $locale = 'es-AR';
@@ -39,10 +39,10 @@ public function currency(string $currency): OrderBuilder
3939
}
4040

4141
/**
42-
* @param int $amount
42+
* @param float $amount
4343
* @return OrderBuilder
4444
*/
45-
public function amount(int $amount): OrderBuilder
45+
public function amount(float $amount): OrderBuilder
4646
{
4747
$this->amount = $amount;
4848
return $this;
@@ -107,7 +107,7 @@ public function make(): array
107107
'custom_id' => $this->externalId,
108108
'amount' => [
109109
'currency_code' => $this->currency,
110-
'value' => $this->amount,
110+
'value' => round($this->amount, 2),
111111
],
112112
'description' => $this->description,
113113
'payment_options' => [

tests/OrderBuilderTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Puntodev\Payments\OrderBuilder;
7+
8+
class OrderBuilderTest extends TestCase
9+
{
10+
/** @test */
11+
public function create_order_with_int_amount()
12+
{
13+
$order = (new OrderBuilder())
14+
->externalId('31fe5538-8589-437d-8823-3b0574186a5f')
15+
->currency('USD')
16+
->amount(23.206)
17+
->description('My custom product')
18+
->brandName('My brand name')
19+
->returnUrl('http://localhost:8080/return')
20+
->cancelUrl('http://localhost:8080/cancel')
21+
->make();
22+
23+
$this->assertEquals([
24+
'intent' => 'CAPTURE',
25+
'purchase_units' => [
26+
[
27+
'custom_id' => '31fe5538-8589-437d-8823-3b0574186a5f',
28+
'amount' => [
29+
'currency_code' => 'USD',
30+
'value' => 23.21,
31+
],
32+
'description' => 'My custom product',
33+
'payment_options' => [
34+
'allowed_payment_method' => 'INSTANT_FUNDING_SOURCE'
35+
],
36+
]
37+
],
38+
'application_context' => [
39+
'brand_name' => 'My brand name',
40+
'locale' => 'es-AR',
41+
'user_action' => 'PAY_NOW',
42+
'payment_method' => [
43+
'payee_preferred' => 'IMMEDIATE_PAYMENT_REQUIRED',
44+
],
45+
'shipping_preference' => 'NO_SHIPPING',
46+
'return_url' => 'http://localhost:8080/return',
47+
'cancel_url' => 'http://localhost:8080/cancel'
48+
],
49+
], $order);
50+
}
51+
}

tests/PayPalApiTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function testCreateOrder()
4646
$order = (new OrderBuilder())
4747
->externalId($this->faker->uuid)
4848
->currency('USD')
49-
->amount(23)
49+
->amount(23.20)
5050
->description('My custom product')
5151
->brandName('My brand name')
5252
->returnUrl('http://localhost:8080/return')

0 commit comments

Comments
 (0)