1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AcquiroPay\Services; |
6
|
|
|
|
7
|
|
|
use stdClass; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use AcquiroPay\Models\Pay\P2PResponse; |
11
|
|
|
use AcquiroPay\Models\Pay\PayResponse; |
12
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
13
|
|
|
use AcquiroPay\Models\Pay\CardResponse; |
14
|
|
|
use AcquiroPay\Models\Pay\InitResponse; |
15
|
|
|
use AcquiroPay\Models\Pay\CancelResponse; |
16
|
|
|
use AcquiroPay\Models\Pay\ChargeResponse; |
17
|
|
|
use AcquiroPay\Models\Pay\StatusResponse; |
18
|
|
|
use AcquiroPay\Models\Pay\CardInitResponse; |
19
|
|
|
use AcquiroPay\Contracts\Pay as PayContract; |
20
|
|
|
use AcquiroPay\Models\Pay\RecurrentResponse; |
21
|
|
|
use function GuzzleHttp\json_decode as decode; |
22
|
|
|
use AcquiroPay\Models\Pay\CompleteAuthResponse; |
23
|
|
|
use AcquiroPay\Contracts\Mapper as MapperContract; |
24
|
|
|
|
25
|
|
|
class Pay implements PayContract |
26
|
|
|
{ |
27
|
|
|
protected $client; |
28
|
|
|
protected $mapper; |
29
|
|
|
|
30
|
|
|
public function __construct(GuzzleClient $client, MapperContract $mapper) |
31
|
|
|
{ |
32
|
|
|
$this->client = $client; |
33
|
|
|
$this->mapper = $mapper; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function init(int $merchantId, string $secret, array $parameters): InitResponse |
37
|
|
|
{ |
38
|
|
|
$parameters['token'] = static::createToken($secret, $parameters); |
39
|
|
|
|
40
|
|
|
return $this->map($this->makeRequest('init', $parameters), new InitResponse()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
View Code Duplication |
public function card(array $params, int $merchantId, string $secretWord): CardResponse |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$params['token'] = md5( |
46
|
|
|
$merchantId |
47
|
|
|
.($params['product_id'] ?? '') |
48
|
|
|
.($params['amount'] ?? '') |
49
|
|
|
.($params['cf'] ?? '') |
50
|
|
|
.($params['cf2'] ?? '') |
51
|
|
|
.($params['cf3'] ?? '') |
52
|
|
|
.($secretWord ?? '') |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
return $this->map($this->makeRequest('card', $params), new CardResponse()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
View Code Duplication |
public function cancel(array $params, int $merchantId, string $secretWord): CancelResponse |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$params['token'] = md5( |
61
|
|
|
$merchantId |
62
|
|
|
.($params['payment_id'] ?? '') |
63
|
|
|
.($params['amount'] ?? '') |
64
|
|
|
.$secretWord |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
return $this->map($this->makeRequest('cancel', $params), new CancelResponse()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
View Code Duplication |
public function charge(array $params, int $merchantId, string $secretWord): ChargeResponse |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$params['token'] = md5( |
73
|
|
|
$merchantId |
74
|
|
|
.($params['payment_id'] ?? '') |
75
|
|
|
.$secretWord |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
return $this->map($this->makeRequest('charge', $params), new ChargeResponse()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
View Code Duplication |
public function completeAuth(array $params, int $merchantId, string $secretWord): CompleteAuthResponse |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$params['token'] = md5( |
84
|
|
|
$merchantId |
85
|
|
|
.($params['payment_id'] ?? '') |
86
|
|
|
.$secretWord |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
return $this->map($this->makeRequest('complete-auth', $params), new CompleteAuthResponse()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
View Code Duplication |
public function cardInit(array $params, int $merchantId, string $secretWord): CardInitResponse |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
$params['token'] = md5( |
95
|
|
|
$merchantId |
96
|
|
|
.($params['product_id'] ?? '') |
97
|
|
|
.($params['amount'] ?? '') |
98
|
|
|
.($params['cf'] ?? '') |
99
|
|
|
.($params['cf2'] ?? '') |
100
|
|
|
.($params['cf3'] ?? '') |
101
|
|
|
.$secretWord |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
return $this->map($this->makeRequest('card-init', $params), new CardInitResponse()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
View Code Duplication |
public function p2p(array $params, int $merchantId, string $secretWord): P2PResponse |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
$params['token'] = md5( |
110
|
|
|
$merchantId |
111
|
|
|
.($params['product_id'] ?? '') |
112
|
|
|
.($params['amount'] ?? '') |
113
|
|
|
.($params['cf'] ?? '') |
114
|
|
|
.($params['cf2'] ?? '') |
115
|
|
|
.($params['cf3'] ?? '') |
116
|
|
|
.$secretWord |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
return $this->map($this->makeRequest('p2p', $params), new P2PResponse()); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
View Code Duplication |
public function recurrent(array $params, int $merchantId, string $secretWord): RecurrentResponse |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
$params['token'] = md5( |
125
|
|
|
$merchantId |
126
|
|
|
.($params['payment_id'] ?? '') |
127
|
|
|
.($params['amount'] ?? '') |
128
|
|
|
.$secretWord |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
return $this->map($this->makeRequest('recurrent', $params), new RecurrentResponse()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
View Code Duplication |
public function status(array $params, int $merchantId, string $secretWord): StatusResponse |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
$params['token'] = md5( |
137
|
|
|
$merchantId |
138
|
|
|
.($params['payment_id'] ?? '') |
139
|
|
|
.$secretWord |
140
|
|
|
); |
141
|
|
|
|
142
|
|
|
return $this->map($this->makeRequest('status', $params), new StatusResponse()); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Generate token for request. |
147
|
|
|
* |
148
|
|
|
* @param string $secret |
149
|
|
|
* @param array $parameters |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
public static function createToken(string $secret, array $parameters): string |
154
|
|
|
{ |
155
|
|
|
ksort($parameters); |
156
|
|
|
|
157
|
|
|
$data = []; |
158
|
|
|
|
159
|
|
|
foreach ($parameters as $key => $value) { |
160
|
|
|
$data[] = $key.'='.$value; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$data[] = $secret; |
164
|
|
|
|
165
|
|
|
return hash('sha256', implode('&', $data)); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param string $url |
170
|
|
|
* @param array $options |
171
|
|
|
* |
172
|
|
|
* @return stdClass |
173
|
|
|
* @throws RuntimeException |
174
|
|
|
* @throws InvalidArgumentException |
175
|
|
|
*/ |
176
|
|
|
protected function makeRequest(string $url, array $options): stdClass |
177
|
|
|
{ |
178
|
|
|
$resp = $this->client->post($url, ['form_params' => $options]); |
179
|
|
|
$resp = (string) $resp->getBody()->getContents(); |
180
|
|
|
$resp = decode($resp); |
181
|
|
|
|
182
|
|
|
return $resp ?? new stdClass(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
protected function map($resp, PayResponse $payResponse): PayResponse |
186
|
|
|
{ |
187
|
|
|
if (isset($resp->result)) { |
188
|
|
|
$this->mapper->map($resp->result, $payResponse); |
189
|
|
|
} elseif (isset($resp->error)) { |
190
|
|
|
$payResponse->setErrorMessage($resp->error->message); |
191
|
|
|
$payResponse->setErrorCode((string) $resp->error->code); |
192
|
|
|
if (!empty($resp->error->data)) { |
193
|
|
|
$payResponse->setErrors((array) $resp->error->data); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $payResponse; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.