Skip to content

Commit e179480

Browse files
authored
hotifx comparing float prices (#1)
* add type hints to all classes * add calculated total test
1 parent ad95a18 commit e179480

File tree

12 files changed

+173
-207
lines changed

12 files changed

+173
-207
lines changed

readme.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ Get client ID and client secret by going to [https://developer.paypal.com/develo
3434

3535
Inorder to communicate with paypal platform we need to setup a client first :
3636

37-
- Create a client with sandbox environment
37+
- Create a client with sandbox environment :
38+
3839
```php
3940
// import namespace
4041
use PayPal\Checkout\Environment\SandboxEnvironment;
@@ -51,27 +52,26 @@ $environment = new SandboxEnvironment($clientId, $clientSecret);
5152
$client = new PayPalClient($environment);
5253
```
5354

54-
- Create a client with production environment
55+
- Create a client with production environment :
56+
5557
```php
5658
// import namespace
57-
use PayPal\Checkout\Environment\Production;
59+
use PayPal\Checkout\Environment\ProductionEnvironment;
5860
use PayPal\Checkout\Http\PayPalClient;
5961

6062
// client id and client secret retrieved from paypal
6163
$clientId = "<<PAYPAL-CLIENT-ID>>";
6264
$clientSecret = "<<PAYPAL-CLIENT-SECRET>>";
6365

6466
// create a new sandbox environment
65-
$environment = new Production($clientId, $clientSecret);
67+
$environment = new ProductionEnvironment($clientId, $clientSecret);
6668

6769
// create a new client
6870
$client = new PayPalClient($environment);
6971
```
7072

71-
7273
### Create a new Order
7374

74-
7575
```php
7676
// import namespace
7777
use PayPal\Checkout\Http\OrderCreateRequest;
@@ -99,7 +99,6 @@ $result = json_decode((string) $response->getBody());
9999
echo $result->id; // id of the created order
100100
```
101101

102-
103102
## Change log
104103

105104
Please see the [changelog](changelog.md) for more information on what has changed recently.

src/Http/AccessToken.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class AccessToken
3232
*/
3333
protected $created_at;
3434

35-
public function __construct($token, $token_type, $expires_in)
35+
public function __construct(string $token, string $token_type, int $expires_in)
3636
{
3737
$this->token = $token;
3838
$this->token_type = $token_type;
@@ -43,23 +43,23 @@ public function __construct($token, $token_type, $expires_in)
4343
/**
4444
* gets the token.
4545
*/
46-
public function getToken()
46+
public function getToken(): ?string
4747
{
4848
return $this->token;
4949
}
5050

5151
/**
5252
* gets the token.
5353
*/
54-
public function getTokenType(): string
54+
public function getTokenType(): ?string
5555
{
5656
return $this->token_type;
5757
}
5858

5959
/**
6060
* returns authorization string.
6161
*/
62-
public function authorizationString(): string
62+
public function authorizationString(): ?string
6363
{
6464
return $this->token_type.' '.$this->token;
6565
}

src/Orders/Amount.php

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,69 +32,58 @@ class Amount implements Arrayable, Jsonable
3232
protected $value;
3333

3434
/**
35-
* Create a new collection.
36-
*
37-
* @param string $currency_code
38-
* @param float $value
39-
*
40-
* @return void
35+
* create a new amount instance.
4136
*/
42-
public function __construct($currency_code, $value)
37+
public function __construct(string $currency_code, float $value)
4338
{
4439
$this->currency_code = $currency_code;
4540
$this->value = $value;
4641
}
4742

4843
/**
49-
* gets Amount value.
44+
* gets amount value.
5045
*/
51-
public function getValue()
46+
public function getValue(): float
5247
{
53-
return $this->value;
48+
return (float) $this->value;
5449
}
5550

5651
/**
57-
* gets Amount's currency code.
52+
* return amount's currency code.
5853
*/
59-
public function getCurrencyCode()
54+
public function getCurrencyCode(): string
6055
{
6156
return $this->currency_code;
6257
}
6358

6459
/**
65-
* sets Amount value.
66-
*
67-
* @param float $value
60+
* sets amount value.
6861
*/
69-
public function setValue($value)
62+
public function setValue(float $value): self
7063
{
7164
$this->value = $value;
7265

7366
return $this;
7467
}
7568

7669
/**
77-
* sets Amount's currency code.
78-
*
79-
* @param string $currency_code
70+
* sets amount's currency code.
8071
*/
81-
public function setCurrencyCode($currency_code)
72+
public function setCurrencyCode(string $currency_code): self
8273
{
8374
$this->currency_code = $currency_code;
8475

8576
return $this;
8677
}
8778

8879
/**
89-
* Get the instance as an array.
90-
*
91-
* @return array
80+
* convert amount to an array.
9281
*/
93-
public function toArray()
82+
public function toArray(): array
9483
{
9584
return [
96-
'currency_code' => $this->currency_code,
97-
'value' => $this->value,
85+
'currency_code' => $this->getCurrencyCode(),
86+
'value' => $this->getValue(),
9887
];
9988
}
10089
}

src/Orders/AmountBreakdown.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,16 @@ class AmountBreakdown extends Amount
99
{
1010
/**
1111
* Get the instance as an array.
12-
*
13-
* @return array
1412
*/
15-
public function toArray()
13+
public function toArray(): array
1614
{
1715
return [
18-
'currency_code' => $this->currency_code,
19-
'value' => $this->value,
16+
'currency_code' => $this->getCurrencyCode(),
17+
'value' => $this->getValue(),
2018
'breakdown' => [
2119
'item_total' => [
22-
'currency_code' => $this->currency_code,
23-
'value' => $this->value,
20+
'currency_code' => $this->getCurrencyCode(),
21+
'value' => $this->getValue(),
2422
],
2523
],
2624
];

src/Orders/ApplicationContext.php

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,15 @@ public function __construct(
125125
/**
126126
* gets brand_name.
127127
*/
128-
public function getBrandName()
128+
public function getBrandName(): ?string
129129
{
130130
return $this->brand_name;
131131
}
132132

133133
/**
134134
* sets the brand_name.
135135
*/
136-
public function setBrandName($brand_name)
136+
public function setBrandName($brand_name): self
137137
{
138138
$this->brand_name = $brand_name;
139139

@@ -143,15 +143,15 @@ public function setBrandName($brand_name)
143143
/**
144144
* gets brand_name.
145145
*/
146-
public function getLocale()
146+
public function getLocale(): string
147147
{
148148
return $this->locale;
149149
}
150150

151151
/**
152152
* sets the locale.
153153
*/
154-
public function setLocale($locale)
154+
public function setLocale($locale): self
155155
{
156156
$this->locale = $locale;
157157

@@ -161,15 +161,15 @@ public function setLocale($locale)
161161
/**
162162
* gets landing_page.
163163
*/
164-
public function getLandingPage()
164+
public function getLandingPage(): string
165165
{
166166
return $this->landing_page;
167167
}
168168

169169
/**
170170
* sets the landing_page.
171171
*/
172-
public function setLandingPage($landing_page)
172+
public function setLandingPage($landing_page): self
173173
{
174174
$validOptions = [LOGIN, BILLING, NO_PREFERENCE];
175175
if (!in_array($landing_page, $validOptions)) {
@@ -184,15 +184,15 @@ public function setLandingPage($landing_page)
184184
/**
185185
* gets shipping_preference.
186186
*/
187-
public function getShippingPreference()
187+
public function getShippingPreference(): string
188188
{
189189
return $this->shipping_preference;
190190
}
191191

192192
/**
193193
* sets the shipping_preference.
194194
*/
195-
public function setShippingPreference($shipping_preference)
195+
public function setShippingPreference($shipping_preference): self
196196
{
197197
$validOptions = [GET_FROM_FILE, NO_SHIPPING, SET_PROVIDED_ADDRESS];
198198
if (!in_array($shipping_preference, $validOptions)) {
@@ -207,15 +207,15 @@ public function setShippingPreference($shipping_preference)
207207
/**
208208
* gets return_url.
209209
*/
210-
public function getReturnUrl()
210+
public function getReturnUrl(): ?string
211211
{
212212
return $this->return_url;
213213
}
214214

215215
/**
216216
* sets the return_url.
217217
*/
218-
public function setReturnUrl($return_url)
218+
public function setReturnUrl($return_url): self
219219
{
220220
$this->return_url = $return_url;
221221

@@ -225,15 +225,15 @@ public function setReturnUrl($return_url)
225225
/**
226226
* gets cancel_url.
227227
*/
228-
public function getCancelUrl()
228+
public function getCancelUrl(): ?string
229229
{
230230
return $this->cancel_url;
231231
}
232232

233233
/**
234234
* sets the cancel_url.
235235
*/
236-
public function setCancelUrl($cancel_url)
236+
public function setCancelUrl($cancel_url): self
237237
{
238238
$this->cancel_url = $cancel_url;
239239

@@ -243,15 +243,15 @@ public function setCancelUrl($cancel_url)
243243
/**
244244
* gets user_action.
245245
*/
246-
public function getUserAction()
246+
public function getUserAction(): string
247247
{
248248
return $this->user_action;
249249
}
250250

251251
/**
252252
* sets the user_action.
253253
*/
254-
public function setUserAction($user_action)
254+
public function setUserAction($user_action): self
255255
{
256256
$validOptions = [ACTION_CONTINUE, ACTION_PAY_NOW];
257257
if (!in_array($user_action, $validOptions)) {
@@ -264,19 +264,17 @@ public function setUserAction($user_action)
264264

265265
/**
266266
* Get the instance as an array.
267-
*
268-
* @return array
269267
*/
270-
public function toArray()
268+
public function toArray(): array
271269
{
272270
$arrayable = [
273-
'brand_name' => $this->brand_name ?? null,
274-
'locale' => $this->locale ?? null,
275-
'shipping_preference' => $this->shipping_preference ?? null,
276-
'landing_page' => $this->landing_page ?? null,
277-
'user_action' => $this->user_action ?? null,
278-
'return_url' => $this->return_url ?? null,
279-
'cancel_url' => $this->cancel_url ?? null,
271+
'brand_name' => $this->getBrandName() ?? null,
272+
'locale' => $this->getLocale() ?? null,
273+
'shipping_preference' => $this->getShippingPreference() ?? null,
274+
'landing_page' => $this->getLandingPage() ?? null,
275+
'user_action' => $this->getUserAction() ?? null,
276+
'return_url' => $this->getReturnUrl() ?? null,
277+
'cancel_url' => $this->getCancelUrl() ?? null,
280278
];
281279

282280
return array_filter(

0 commit comments

Comments
 (0)