1
|
|
|
<?php |
2
|
|
|
namespace CultureKings\Afterpay\Service\Merchant; |
3
|
|
|
|
4
|
|
|
use CultureKings\Afterpay\Exception\ApiException; |
5
|
|
|
use CultureKings\Afterpay\Model\ErrorResponse; |
6
|
|
|
use CultureKings\Afterpay\Model\Merchant\Authorization; |
7
|
|
|
use CultureKings\Afterpay\Model\Merchant\OrderDetails; |
8
|
|
|
use CultureKings\Afterpay\Model\Merchant\OrderToken; |
9
|
|
|
use CultureKings\Afterpay\Traits; |
10
|
|
|
use GuzzleHttp\Client; |
11
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
12
|
|
|
use JMS\Serializer\SerializerInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Orders |
16
|
|
|
* |
17
|
|
|
* @package CultureKings\Afterpay\Service\Merchant |
18
|
|
|
*/ |
19
|
|
|
class Orders |
20
|
|
|
{ |
21
|
|
|
use Traits\ClientTrait; |
22
|
|
|
use Traits\AuthorizationTrait; |
23
|
|
|
use Traits\SerializerTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Orders constructor. |
27
|
|
|
* |
28
|
|
|
* @param Client $client |
29
|
|
|
* @param Authorization $authorization |
30
|
|
|
* @param SerializerInterface $serializer |
31
|
|
|
*/ |
32
|
|
|
public function __construct( |
33
|
|
|
Client $client, |
34
|
|
|
Authorization $authorization, |
35
|
|
|
SerializerInterface $serializer |
36
|
|
|
) { |
37
|
|
|
$this->setClient($client); |
38
|
|
|
$this->setAuthorization($authorization); |
39
|
|
|
$this->setSerializer($serializer); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param OrderDetails $order |
44
|
|
|
* @return OrderToken|object |
45
|
|
|
*/ |
46
|
|
|
public function create(OrderDetails $order) |
47
|
|
|
{ |
48
|
|
|
try { |
49
|
|
|
$result = $this->getClient()->post( |
50
|
|
|
'orders', |
51
|
|
|
[ |
52
|
|
|
'auth' => [ |
53
|
|
|
$this->getAuthorization()->getMerchantId(), |
|
|
|
|
54
|
|
|
$this->getAuthorization()->getSecret(), |
|
|
|
|
55
|
|
|
], |
56
|
|
|
'headers' => [ |
57
|
|
|
'Accept' => 'application/json', |
58
|
|
|
'Content-Type' => 'application/json', |
59
|
|
|
], |
60
|
|
|
'body' => $this->getSerializer()->serialize($order, 'json'), |
61
|
|
|
] |
62
|
|
|
); |
63
|
|
|
} catch (BadResponseException $e) { |
64
|
|
|
throw new ApiException( |
65
|
|
|
$this->getSerializer()->deserialize( |
66
|
|
|
(string) $e->getResponse()->getBody(), |
67
|
|
|
ErrorResponse::class, |
68
|
|
|
'json' |
69
|
|
|
) |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->getSerializer()->deserialize( |
74
|
|
|
(string) $result->getBody(), |
75
|
|
|
OrderToken::class, |
76
|
|
|
'json' |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $token |
82
|
|
|
* @return OrderDetails|object |
83
|
|
|
*/ |
84
|
|
|
public function get($token) |
85
|
|
|
{ |
86
|
|
|
try { |
87
|
|
|
$result = $this->getClient()->get( |
88
|
|
|
sprintf('orders/%s', $token), |
89
|
|
|
[ |
90
|
|
|
'auth' => [ |
91
|
|
|
$this->getAuthorization()->getMerchantId(), |
|
|
|
|
92
|
|
|
$this->getAuthorization()->getSecret(), |
|
|
|
|
93
|
|
|
], |
94
|
|
|
] |
95
|
|
|
); |
96
|
|
|
} catch (BadResponseException $e) { |
97
|
|
|
throw new ApiException( |
98
|
|
|
$this->getSerializer()->deserialize( |
99
|
|
|
(string) $e->getResponse()->getBody(), |
100
|
|
|
ErrorResponse::class, |
101
|
|
|
'json' |
102
|
|
|
) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this->getSerializer()->deserialize( |
107
|
|
|
(string) $result->getBody(), |
108
|
|
|
OrderDetails::class, |
109
|
|
|
'json' |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: