1
|
|
|
<?php |
2
|
|
|
namespace BPCI\SumUp\Customer; |
3
|
|
|
|
4
|
|
|
use BPCI\SumUp\ContextInterface; |
5
|
|
|
use BPCI\SumUp\Customer\PaymentInstrument\PaymentInstrumentClient; |
6
|
|
|
use BPCI\SumUp\Customer\PaymentInstrument\PaymentInstrumentInterface; |
7
|
|
|
use BPCI\SumUp\Exception\BadRequestException; |
8
|
|
|
use BPCI\SumUp\Exception\InvalidCustomerException; |
9
|
|
|
use BPCI\SumUp\OAuth\AccessToken; |
10
|
|
|
use BPCI\SumUp\SumUpClientInterface; |
11
|
|
|
use BPCI\SumUp\Traits\Client; |
12
|
|
|
use BPCI\SumUp\Traits\ClientInterface; |
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
|
15
|
|
|
class CustomerClient implements CustomerClientInterface, ClientInterface |
16
|
|
|
{ |
17
|
|
|
use Client; |
18
|
|
|
|
19
|
|
|
const ENDPOINT = 'customers'; |
20
|
|
|
protected $context; |
21
|
|
|
protected $token; |
22
|
|
|
protected $lastResponse; |
23
|
|
|
protected $options = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* CheckoutClientInterface constructor. |
27
|
|
|
* @param ContextInterface $context |
28
|
|
|
* @param array $options |
29
|
|
|
*/ |
30
|
3 |
|
public function __construct(ContextInterface $context, ?array $options = []) |
31
|
|
|
{ |
32
|
3 |
|
$this->context = $context; |
33
|
3 |
|
$this->options = $options; |
34
|
3 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param $customer |
38
|
|
|
* @param string|null $type |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
1 |
|
static function getBody($customer, string $type = null) |
|
|
|
|
42
|
|
|
{ |
43
|
1 |
|
if (!$customer instanceof CustomerInterface) { |
44
|
|
|
throw new InvalidCustomerException('Invalid customer or $customer does not implement CustomerInterface!'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$body = [ |
48
|
1 |
|
'customer_id' => $customer->getCustomerId(), |
49
|
|
|
'personal_details' => [ |
50
|
1 |
|
'name' => $customer->getName(), |
51
|
1 |
|
'phone' => $customer->getPhone(), |
52
|
|
|
'address' => [ |
53
|
1 |
|
'line1' => $customer->getAddress()->getLine1(), |
54
|
1 |
|
'line2' => $customer->getAddress()->getLine2(), |
55
|
1 |
|
'country' => $customer->getAddress()->getCountry(), |
56
|
1 |
|
'postal_code' => $customer->getAddress()->getPostalCode(), |
57
|
1 |
|
'city' => $customer->getAddress()->getCity(), |
58
|
1 |
|
'state' => $customer->getAddress()->getState(), |
59
|
|
|
], |
60
|
|
|
], |
61
|
|
|
]; |
62
|
|
|
|
63
|
1 |
|
return $body; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritDoc |
68
|
|
|
*/ |
69
|
1 |
|
public static function getScopes(): array |
70
|
|
|
{ |
71
|
|
|
return [ |
72
|
1 |
|
'payment_instruments', |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritDoc |
78
|
|
|
* @throws BadRequestException |
79
|
|
|
*/ |
80
|
1 |
|
public function create(CustomerInterface $customer): ?CustomerInterface |
81
|
|
|
{ |
82
|
1 |
|
return $this->request('post', $customer) ? $customer : null; |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
public function getLastResponse(): ResponseInterface |
86
|
|
|
{ |
87
|
1 |
|
return $this->lastResponse; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param ResponseInterface $response |
92
|
|
|
* @return SumUpClientInterface |
93
|
|
|
*/ |
94
|
3 |
|
public function setLastResponse(ResponseInterface $response): SumUpClientInterface |
95
|
|
|
{ |
96
|
3 |
|
$this->lastResponse = $response; |
97
|
|
|
|
98
|
3 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Delete a customer card. |
103
|
|
|
* |
104
|
|
|
* @param CustomerInterface $customer |
105
|
|
|
* @param PaymentInstrumentInterface $paymentInstrument |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
1 |
|
public function disablePaymentInstrument( |
109
|
|
|
CustomerInterface $customer, |
110
|
|
|
PaymentInstrumentInterface $paymentInstrument |
111
|
|
|
): bool |
112
|
|
|
{ |
113
|
1 |
|
$instrumentClient = new PaymentInstrumentClient($this->getContext(), $this->getOptions()); |
114
|
1 |
|
$instrumentClient->setCustomer($customer); |
115
|
1 |
|
$instrumentClient->setToken($this->getToken()); |
116
|
1 |
|
$response = $instrumentClient->disable($paymentInstrument); |
117
|
1 |
|
$this->setLastResponse($instrumentClient->getLastResponse()); |
118
|
|
|
|
119
|
1 |
|
return $response??false; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* return the context used to created the client. |
124
|
|
|
* @return ContextInterface |
125
|
|
|
*/ |
126
|
3 |
|
public function getContext(): ContextInterface |
127
|
|
|
{ |
128
|
3 |
|
return $this->context; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function setContext(ContextInterface $context): CustomerClientInterface |
132
|
|
|
{ |
133
|
|
|
$this->context = $context; |
134
|
|
|
|
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
3 |
|
public function getOptions(): array |
142
|
|
|
{ |
143
|
3 |
|
return $this->options??[]; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return AccessToken |
148
|
|
|
*/ |
149
|
3 |
|
public function getToken():? AccessToken |
150
|
|
|
{ |
151
|
3 |
|
return $this->token; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param AccessToken $token |
156
|
|
|
* @return SumUpClientInterface |
157
|
|
|
*/ |
158
|
3 |
|
public function setToken(AccessToken $token): SumUpClientInterface |
159
|
|
|
{ |
160
|
3 |
|
$this->token = $token; |
161
|
|
|
|
162
|
3 |
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* This must return an Array of PaymentInstrumentInterface |
167
|
|
|
* |
168
|
|
|
* @param CustomerInterface $customer |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
1 |
|
public function getPaymentInstruments(CustomerInterface $customer): array |
172
|
|
|
{ |
173
|
1 |
|
$instrumentClient = new PaymentInstrumentClient($this->getContext(), $this->getOptions()); |
174
|
1 |
|
$instrumentClient->setCustomer($customer); |
175
|
1 |
|
$instrumentClient->setToken($this->getToken()); |
176
|
1 |
|
$response = $instrumentClient->get(); |
177
|
1 |
|
$this->setLastResponse($instrumentClient->getLastResponse()); |
178
|
|
|
|
179
|
1 |
|
return $response??[]; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
1 |
|
public function getEndPoint(): string |
186
|
|
|
{ |
187
|
1 |
|
return 'customers'; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.