|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Apoca\Sibs\Brands; |
|
4
|
|
|
|
|
5
|
|
|
use Apoca\Sibs\Contracts\PaymentInterface; |
|
6
|
|
|
use GuzzleHttp\Client; |
|
7
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Checkout |
|
11
|
|
|
* |
|
12
|
|
|
* @package Apoca\Sibs\Brands |
|
13
|
|
|
*/ |
|
14
|
|
|
class Checkout implements PaymentInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var float |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $amount; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $currency; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $paymentType = 'DB'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $endpoint; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $clientConfig = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $optionalParameters = []; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Checkout constructor. |
|
48
|
|
|
* |
|
49
|
|
|
* @param float $amount |
|
50
|
|
|
* @param string $currency |
|
51
|
|
|
* @param string $paymentType |
|
52
|
|
|
* @param array $optionalParameters |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct(float $amount, string $currency, string $paymentType, array $optionalParameters) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->setAmount($amount); |
|
57
|
|
|
$this->setCurrency($currency); |
|
58
|
|
|
$this->setPaymentType($paymentType); |
|
59
|
|
|
$this->setOptionalParameters($optionalParameters); |
|
60
|
|
|
|
|
61
|
|
|
if (config('sibs.mode') === 'test') { |
|
62
|
|
|
$this->clientConfig = [ |
|
63
|
|
|
'verify' => false, |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
$this->endpoint = config('sibs.host') . config('sibs.version') . '/'; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getOptionalParameters(): array |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->optionalParameters; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param array $optionalParameters |
|
79
|
|
|
*/ |
|
80
|
|
|
public function setOptionalParameters(array $optionalParameters): void |
|
81
|
|
|
{ |
|
82
|
|
|
$this->optionalParameters = $optionalParameters; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return float |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getAmount(): float |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->amount; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param float $amount |
|
95
|
|
|
*/ |
|
96
|
|
|
public function setAmount(float $amount): void |
|
97
|
|
|
{ |
|
98
|
|
|
$this->amount = $amount; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getCurrency(): string |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->currency; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param string $currency |
|
111
|
|
|
*/ |
|
112
|
|
|
public function setCurrency(string $currency): void |
|
113
|
|
|
{ |
|
114
|
|
|
$this->currency = $currency; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getPaymentType(): string |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->paymentType; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param string $paymentType |
|
127
|
|
|
*/ |
|
128
|
|
|
public function setPaymentType(string $paymentType): void |
|
129
|
|
|
{ |
|
130
|
|
|
$this->paymentType = $paymentType; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @return object |
|
135
|
|
|
*/ |
|
136
|
|
|
public function pay(): object |
|
137
|
|
|
{ |
|
138
|
|
|
$data = (object)null; |
|
139
|
|
|
|
|
140
|
|
|
try { |
|
141
|
|
|
$client = new Client($this->clientConfig); |
|
142
|
|
|
|
|
143
|
|
|
$payload = [ |
|
144
|
|
|
'entityId' => config('sibs.authentication.entityId'), |
|
145
|
|
|
'amount' => number_format($this->amount, 2), |
|
146
|
|
|
'currency' => $this->currency, |
|
147
|
|
|
'paymentType' => $this->paymentType, |
|
148
|
|
|
]; |
|
149
|
|
|
|
|
150
|
|
|
$response = $client->post($this->endpoint . 'checkouts', [ |
|
151
|
|
|
'headers' => [ |
|
152
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded', |
|
153
|
|
|
'Content-Length' => ob_get_length(), |
|
154
|
|
|
'Authorization' => config('sibs.authentication.token'), |
|
155
|
|
|
], |
|
156
|
|
|
'form_params' => array_merge($payload, $this->getOptionalParameters()), |
|
157
|
|
|
]); |
|
158
|
|
|
|
|
159
|
|
|
$data->status = $response->getStatusCode(); |
|
160
|
|
|
$data->response = json_decode($response->getBody()->getContents(), false); |
|
161
|
|
|
|
|
162
|
|
|
return $data; |
|
163
|
|
|
} catch (ClientException $e) { |
|
164
|
|
|
$response = $e->getResponse(); |
|
165
|
|
|
|
|
166
|
|
|
$data->status = $response->getStatusCode(); |
|
167
|
|
|
$data->response = json_decode($response->getBody()->getContents(), false); |
|
168
|
|
|
|
|
169
|
|
|
return $data; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|