1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BPCI\SumUp\Checkout; |
4
|
|
|
|
5
|
|
|
use BPCI\SumUp\ContextInterface; |
6
|
|
|
use BPCI\SumUp\Exception\InvalidCheckoutException; |
7
|
|
|
use BPCI\SumUp\OAuth\AccessToken; |
8
|
|
|
use BPCI\SumUp\SumUp; |
9
|
|
|
use BPCI\SumUp\SumUpClientInterface; |
10
|
|
|
use BPCI\SumUp\Traits\Client; |
11
|
|
|
use BPCI\SumUp\Traits\ClientInterface; |
12
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
13
|
|
|
use GuzzleHttp\Exception\ConnectException; |
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class CheckoutClient |
18
|
|
|
* @package BPCI\SumUp\Checkout |
19
|
|
|
*/ |
20
|
|
|
class CheckoutClient implements CheckoutClientInterface, ClientInterface |
21
|
|
|
{ |
22
|
|
|
use Client { |
23
|
|
|
request as protected traitRequest; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
protected $context; |
27
|
|
|
protected $lastResponse; |
28
|
|
|
protected $options = []; |
29
|
|
|
protected $currentToken; |
30
|
|
|
|
31
|
2 |
|
public function __construct(ContextInterface $context, ?array $options = []) |
32
|
|
|
{ |
33
|
2 |
|
$this->context = $context; |
34
|
2 |
|
$this->options = $options; |
35
|
2 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param CheckoutInterface $checkout |
39
|
|
|
* @param string|null $type |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
2 |
|
static function getBody($checkout, string $type = null): array |
|
|
|
|
43
|
|
|
{ |
44
|
2 |
|
$defaultBody = self::getCheckoutBody($checkout); |
45
|
2 |
|
switch ($checkout->getType()) { |
46
|
2 |
|
case 'card': |
47
|
|
|
$completeBody = self::getCardBody($checkout); |
48
|
|
|
break; |
49
|
|
|
|
50
|
2 |
|
case 'boleto': |
51
|
|
|
$completeBody = self::getBoletoBody($checkout); |
52
|
|
|
break; |
53
|
|
|
|
54
|
|
|
default: |
55
|
2 |
|
$completeBody = []; |
56
|
|
|
} |
57
|
|
|
|
58
|
2 |
|
return array_merge($defaultBody, $completeBody); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Generate a body to create a new checkout |
63
|
|
|
* |
64
|
|
|
* @param CheckoutInterface $checkout |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
2 |
|
protected static function getCheckoutBody(CheckoutInterface $checkout): array |
68
|
|
|
{ |
69
|
|
|
return [ |
70
|
2 |
|
"checkout_reference" => $checkout->getReference(), |
71
|
2 |
|
"amount" => $checkout->getAmount(), |
72
|
2 |
|
"currency" => $checkout->getCurrency(), |
73
|
2 |
|
"fee_amount" => $checkout->getFeeAmount(), |
74
|
2 |
|
"pay_to_email" => $checkout->getPayToEmail(), |
75
|
2 |
|
"pay_from_email" => $checkout->getPayFromEmail(), |
76
|
2 |
|
"description" => $checkout->getDescription(), |
77
|
2 |
|
"return_url" => $checkout->getRedirectUrl(), |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private static function getCardBody(CheckoutInterface $checkout): array |
82
|
|
|
{ |
83
|
|
|
return [ |
84
|
|
|
'payment_type' => $checkout->getType(), |
85
|
|
|
'token' => $checkout->getCard()->getToken(), |
86
|
|
|
'customer_id' => $checkout->getCustomer()->getCustomerId(), |
87
|
|
|
]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private static function getBoletoBody(CheckoutInterface $checkout): array |
91
|
|
|
{ |
92
|
|
|
$customer = $checkout->getCustomer(); |
93
|
|
|
|
94
|
|
|
return [ |
95
|
|
|
'payment_type' => $checkout->getType(), |
96
|
|
|
'description' => $checkout->getDescription(), |
97
|
|
|
'boleto_details' => [ |
98
|
|
|
// 'cpf_cnpj' => $customer->getCpfCnpj(), |
99
|
|
|
'payer_name' => $customer->getName(), |
100
|
|
|
'payer_address' => $customer->getAddress(), |
101
|
|
|
'payer_city' => $customer->getAddress()->getCity(), |
102
|
|
|
'payer_state_code' => $customer->getAddress()->getState(), |
103
|
|
|
'payer_post_code' => $customer->getAddress()->getPostalCode(), |
104
|
|
|
], |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @inheritDoc |
110
|
|
|
*/ |
111
|
2 |
|
static function getScopes(): array |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
return [ |
114
|
2 |
|
'payments', |
115
|
|
|
'boleto', |
116
|
|
|
]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param CheckoutInterface $checkout |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
1 |
|
static function getCompleteUrl(CheckoutInterface $checkout): string |
|
|
|
|
124
|
|
|
{ |
125
|
1 |
|
if ($checkout->getId()) { |
126
|
1 |
|
return SumUp::getEntrypoint().$checkout->getId(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return ''; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @inheritDoc |
134
|
|
|
* @throws BadResponseException |
135
|
|
|
* @throws ConnectException |
136
|
|
|
*/ |
137
|
1 |
|
public function create(CheckoutInterface $checkout, AccessToken $accessToken = null):? CheckoutInterface |
138
|
|
|
{ |
139
|
1 |
|
return $this->request('post', $checkout) ? $checkout : null; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $action |
144
|
|
|
* @param CheckoutInterface $checkout |
145
|
|
|
* @param string|null $endpoint |
146
|
|
|
* @return bool|null |
147
|
|
|
*/ |
148
|
2 |
|
public function request(string $action, $checkout, string $endpoint = null):? bool |
149
|
|
|
{ |
150
|
2 |
|
if (!$checkout->isValid()) { |
151
|
|
|
throw new InvalidCheckoutException($checkout); |
152
|
|
|
} |
153
|
|
|
|
154
|
2 |
|
return $this->traitRequest($action, $checkout); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @inheritDoc |
159
|
|
|
* @throws BadResponseException |
160
|
|
|
* @throws ConnectException |
161
|
|
|
*/ |
162
|
1 |
|
public function complete(CheckoutInterface $checkout, AccessToken $accessToken = null):? CheckoutInterface |
163
|
|
|
{ |
164
|
1 |
|
return $this->request('put', $checkout) ? $checkout : null; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return ResponseInterface |
169
|
|
|
*/ |
170
|
1 |
|
public function getLastResponse(): ResponseInterface |
171
|
|
|
{ |
172
|
1 |
|
return $this->lastResponse; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param ResponseInterface $response |
177
|
|
|
* @return SumUpClientInterface |
178
|
|
|
*/ |
179
|
2 |
|
public function setLastResponse(ResponseInterface $response): SumUpClientInterface |
180
|
|
|
{ |
181
|
2 |
|
$this->lastResponse = $response; |
182
|
|
|
|
183
|
2 |
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* return the context used to created the client. |
188
|
|
|
* @return ContextInterface |
189
|
|
|
*/ |
190
|
2 |
|
public function getContext(): ContextInterface |
191
|
|
|
{ |
192
|
2 |
|
return $this->context; |
193
|
|
|
} |
194
|
|
|
|
195
|
2 |
|
public function getOptions(): array |
196
|
|
|
{ |
197
|
2 |
|
return $this->options??[]; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
2 |
|
public function getEndPoint(): string |
204
|
|
|
{ |
205
|
2 |
|
return 'checkouts'; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param AccessToken $token |
210
|
|
|
* @return SumUpClientInterface |
211
|
|
|
*/ |
212
|
2 |
|
public function setToken(AccessToken $token): SumUpClientInterface |
213
|
|
|
{ |
214
|
2 |
|
$this->currentToken = $token; |
215
|
|
|
|
216
|
2 |
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return AccessToken |
221
|
|
|
*/ |
222
|
2 |
|
public function getToken():? AccessToken |
223
|
|
|
{ |
224
|
2 |
|
return $this->currentToken; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
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.