|
1
|
|
|
<?php |
|
2
|
|
|
namespace CultureKings\Afterpay\Service\InStore; |
|
3
|
|
|
|
|
4
|
|
|
use CultureKings\Afterpay\Exception\ApiException; |
|
5
|
|
|
use CultureKings\Afterpay\Model; |
|
6
|
|
|
use CultureKings\Afterpay\Traits; |
|
7
|
|
|
use GuzzleHttp\Client; |
|
8
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
|
9
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
10
|
|
|
use GuzzleHttp\HandlerStack; |
|
11
|
|
|
use JMS\Serializer\SerializerInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Refund |
|
15
|
|
|
* @package CultureKings\Afterpay\Service\InStore |
|
16
|
|
|
*/ |
|
17
|
|
|
class Refund |
|
18
|
|
|
{ |
|
19
|
|
|
use Traits\ClientTrait; |
|
20
|
|
|
use Traits\AuthorizationTrait; |
|
21
|
|
|
use Traits\SerializerTrait; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Device constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* @param Model\InStore\Authorization $auth |
|
27
|
|
|
* @param Client $client |
|
28
|
|
|
* @param SerializerInterface $serializer |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(Model\InStore\Authorization $auth, Client $client, SerializerInterface $serializer) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->setAuthorization($auth); |
|
33
|
|
|
$this->setClient($client); |
|
34
|
|
|
$this->setSerializer($serializer); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param Model\InStore\Refund $refund |
|
39
|
|
|
* @param HandlerStack|null $stack |
|
40
|
|
|
* |
|
41
|
|
|
* @return array|\JMS\Serializer\scalar|object |
|
42
|
|
|
*/ |
|
43
|
|
|
public function create(Model\InStore\Refund $refund, HandlerStack $stack = null) |
|
44
|
|
|
{ |
|
45
|
|
|
try { |
|
46
|
|
|
$params = [ |
|
47
|
|
|
'headers' => [ |
|
48
|
|
|
'Accept' => 'application/json', |
|
49
|
|
|
'Content-Type' => 'application/json', |
|
50
|
|
|
'Authorization' => sprintf('Bearer %s', $this->getAuthorization()->getDeviceToken()), |
|
|
|
|
|
|
51
|
|
|
'Operator' => $this->getAuthorization()->getOperator(), |
|
|
|
|
|
|
52
|
|
|
'User-Agent' => $this->getAuthorization()->getUserAgent() |
|
|
|
|
|
|
53
|
|
|
], |
|
54
|
|
|
'body' => $this->getSerializer()->serialize( |
|
55
|
|
|
$refund, |
|
56
|
|
|
'json' |
|
57
|
|
|
), |
|
58
|
|
|
]; |
|
59
|
|
|
if ($stack !== null) { |
|
60
|
|
|
$params['handler'] = $stack; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$result = $this->getClient()->post('refunds', $params); |
|
64
|
|
|
|
|
65
|
|
|
return $this->getSerializer()->deserialize( |
|
66
|
|
|
(string) $result->getBody(), |
|
67
|
|
|
Model\InStore\Refund::class, |
|
68
|
|
|
'json' |
|
69
|
|
|
); |
|
70
|
|
|
} catch (BadResponseException $e) { |
|
71
|
|
|
throw new ApiException( |
|
72
|
|
|
$this->getSerializer()->deserialize( |
|
73
|
|
|
(string) $e->getResponse()->getBody(), |
|
74
|
|
|
Model\ErrorResponse::class, |
|
75
|
|
|
'json' |
|
76
|
|
|
) |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param Model\InStore\Reversal $reversal |
|
83
|
|
|
* @param HandlerStack|null $stack |
|
84
|
|
|
* |
|
85
|
|
|
* @return array|\JMS\Serializer\scalar|Model\InStore\Reversal |
|
86
|
|
|
*/ |
|
87
|
|
|
public function reverse(Model\InStore\Reversal $reversal, HandlerStack $stack = null) |
|
88
|
|
|
{ |
|
89
|
|
|
try { |
|
90
|
|
|
$params = [ |
|
91
|
|
|
'headers' => [ |
|
92
|
|
|
'Accept' => 'application/json', |
|
93
|
|
|
'Content-Type' => 'application/json', |
|
94
|
|
|
'Authorization' => sprintf('Bearer %s', $this->getAuthorization()->getDeviceToken()), |
|
|
|
|
|
|
95
|
|
|
'Operator' => $this->getAuthorization()->getOperator(), |
|
|
|
|
|
|
96
|
|
|
'User-Agent' => $this->getAuthorization()->getUserAgent() |
|
|
|
|
|
|
97
|
|
|
], |
|
98
|
|
|
'body' => $this->getSerializer()->serialize( |
|
99
|
|
|
$reversal, |
|
100
|
|
|
'json' |
|
101
|
|
|
), |
|
102
|
|
|
]; |
|
103
|
|
|
if ($stack !== null) { |
|
104
|
|
|
$params['handler'] = $stack; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$result = $this->getClient()->post('refunds/reverse', $params); |
|
108
|
|
|
|
|
109
|
|
|
return $this->getSerializer()->deserialize( |
|
110
|
|
|
(string) $result->getBody(), |
|
111
|
|
|
Model\InStore\Reversal::class, |
|
112
|
|
|
'json' |
|
113
|
|
|
); |
|
114
|
|
|
} catch (BadResponseException $e) { |
|
115
|
|
|
throw new ApiException( |
|
116
|
|
|
$this->getSerializer()->deserialize( |
|
117
|
|
|
(string) $e->getResponse()->getBody(), |
|
118
|
|
|
Model\ErrorResponse::class, |
|
119
|
|
|
'json' |
|
120
|
|
|
) |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
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: