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