|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AcquiroPay; |
|
6
|
|
|
|
|
7
|
|
|
use DOMElement; |
|
8
|
|
|
use DOMDocument; |
|
9
|
|
|
use GuzzleHttp\Client as Http; |
|
10
|
|
|
use Illuminate\Support\Collection; |
|
11
|
|
|
use Symfony\Component\Serializer\Serializer; |
|
12
|
|
|
use AcquiroPay\Gateway\Responses\InitResponse; |
|
13
|
|
|
use Symfony\Component\Serializer\Encoder\XmlEncoder; |
|
14
|
|
|
use AcquiroPay\Gateway\Responses\PaymentStatusByCfResponse; |
|
15
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
|
16
|
|
|
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; |
|
17
|
|
|
|
|
18
|
|
|
class Gateway |
|
19
|
|
|
{ |
|
20
|
|
|
private const LIVE_URL = 'https://gateway.acquiropay.com'; |
|
21
|
|
|
private const TEST_URL = 'https://gateway.acqp.co'; |
|
22
|
|
|
|
|
23
|
|
|
protected $http; |
|
24
|
|
|
protected $url; |
|
25
|
|
|
|
|
26
|
|
|
protected $merchantId; |
|
27
|
|
|
protected $secretWord; |
|
28
|
|
|
|
|
29
|
|
|
protected $request; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct( |
|
32
|
|
|
Http $http, |
|
33
|
|
|
int $merchantId = null, |
|
|
|
|
|
|
34
|
|
|
string $secretWord = null, |
|
|
|
|
|
|
35
|
|
|
bool $live = false |
|
|
|
|
|
|
36
|
|
|
) { |
|
37
|
|
|
$this->http = $http; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function setUrl(string $url): self |
|
41
|
|
|
{ |
|
42
|
|
|
$this->url = $url; |
|
43
|
|
|
|
|
44
|
|
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function setMerchantId(int $merchantId): self |
|
48
|
|
|
{ |
|
49
|
|
|
$this->merchantId = $merchantId; |
|
50
|
|
|
|
|
51
|
|
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function setSecretWord(string $secretWord): self |
|
55
|
|
|
{ |
|
56
|
|
|
$this->secretWord = $secretWord; |
|
57
|
|
|
|
|
58
|
|
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function setLive(bool $live): self |
|
62
|
|
|
{ |
|
63
|
|
|
$this->url = $live ? self::LIVE_URL : self::TEST_URL; |
|
64
|
|
|
|
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Init payment. |
|
70
|
|
|
* |
|
71
|
|
|
* @param int $productId |
|
72
|
|
|
* @param string $pan |
|
73
|
|
|
* @param string $cardHolder |
|
74
|
|
|
* @param int $expiryMonth |
|
75
|
|
|
* @param int $expiryYear |
|
76
|
|
|
* @param string $cvv |
|
77
|
|
|
* @param string $cf |
|
78
|
|
|
* @param float|string $amount |
|
79
|
|
|
* @param array $parameters |
|
80
|
|
|
* |
|
81
|
|
|
* @return InitResponse |
|
82
|
|
|
*/ |
|
83
|
|
|
public function init( |
|
84
|
|
|
int $productId, |
|
85
|
|
|
string $pan, |
|
86
|
|
|
string $cardHolder, |
|
87
|
|
|
int $expiryMonth, |
|
88
|
|
|
int $expiryYear, |
|
89
|
|
|
string $cvv, |
|
90
|
|
|
string $cf, |
|
91
|
|
|
float $amount, |
|
92
|
|
|
array $parameters = [] |
|
93
|
|
|
): InitResponse { |
|
94
|
|
|
$amount = $this->formatAmount($amount); |
|
95
|
|
|
|
|
96
|
|
|
$parameters = array_merge($parameters, [ |
|
97
|
|
|
'opcode' => 0, |
|
98
|
|
|
'product_id' => $productId, |
|
99
|
|
|
'amount' => $amount, |
|
100
|
|
|
'cf' => $cf, |
|
101
|
|
|
'pan' => $pan, |
|
102
|
|
|
'cvv' => $cvv, |
|
103
|
|
|
'exp_month' => $expiryMonth, |
|
104
|
|
|
'exp_year' => $expiryYear, |
|
105
|
|
|
'cardholder' => $cardHolder, |
|
106
|
|
|
'token' => md5( |
|
107
|
|
|
$this->merchantId. |
|
108
|
|
|
$productId. |
|
109
|
|
|
$amount. |
|
110
|
|
|
$cf. |
|
111
|
|
|
array_get($parameters, 'cf2'). |
|
112
|
|
|
array_get($parameters, 'cf3'). |
|
113
|
|
|
$this->secretWord |
|
114
|
|
|
), |
|
115
|
|
|
]); |
|
116
|
|
|
|
|
117
|
|
|
$response = $this->send($parameters); |
|
118
|
|
|
|
|
119
|
|
|
return $this->deserialize($response, InitResponse::class); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get payment status using cf. |
|
124
|
|
|
* |
|
125
|
|
|
* @param int $productId |
|
126
|
|
|
* @param string $cf |
|
127
|
|
|
* |
|
128
|
|
|
* @return Collection|PaymentStatusByCfResponse[] |
|
129
|
|
|
*/ |
|
130
|
|
|
public function paymentStatusByCf(int $productId, string $cf) |
|
131
|
|
|
{ |
|
132
|
|
|
$parameters = [ |
|
133
|
|
|
'opcode' => 2, |
|
134
|
|
|
'product_id' => $productId, |
|
135
|
|
|
'cf' => $cf, |
|
136
|
|
|
'token' => md5($this->merchantId.$productId.$cf.$this->secretWord), |
|
137
|
|
|
]; |
|
138
|
|
|
|
|
139
|
|
|
$response = $this->send($parameters); |
|
140
|
|
|
|
|
141
|
|
|
$xml = new DOMDocument; |
|
142
|
|
|
$xml->formatOutput = true; |
|
143
|
|
|
$xml->loadXML($response); |
|
144
|
|
|
|
|
145
|
|
|
// один платеж |
|
146
|
|
|
if ($xml->getElementsByTagName('payments')->length === 0) { |
|
147
|
|
|
return collect([$this->deserialize($response, PaymentStatusByCfResponse::class)]); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
$payments = collect(); |
|
151
|
|
|
$elements = $xml->getElementsByTagName('payments')->item(0)->getElementsByTagName('payment'); |
|
152
|
|
|
|
|
153
|
|
|
/** @var DOMElement $element */ |
|
154
|
|
|
foreach ($elements as $element) { |
|
155
|
|
|
$payments->push($this->deserialize($xml->saveXML($element), PaymentStatusByCfResponse::class)); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return $payments; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function getRequest(): ?array |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->request; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
private function send(array $parameters): string |
|
167
|
|
|
{ |
|
168
|
|
|
$this->request = $parameters; |
|
169
|
|
|
|
|
170
|
|
|
$response = $this->http->post($this->url, ['form_params' => $parameters, 'verify' => false]); |
|
171
|
|
|
|
|
172
|
|
|
return (string) $response->getBody(); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
private function deserialize(string $data, string $class) |
|
176
|
|
|
{ |
|
177
|
|
|
$normalizers = [new ObjectNormalizer(null, new CamelCaseToSnakeCaseNameConverter)]; |
|
178
|
|
|
$encoders = [new XmlEncoder]; |
|
179
|
|
|
|
|
180
|
|
|
return (new Serializer($normalizers, $encoders))->deserialize($data, $class, 'xml'); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
private function formatAmount(float $amount): string |
|
184
|
|
|
{ |
|
185
|
|
|
return number_format($amount, 2, '.', ''); |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.