1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apoca\Sibs\Brands; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Exception\ClientException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class PaymentWithCard |
10
|
|
|
* |
11
|
|
|
* @package Apoca\Sibs\Brands |
12
|
|
|
*/ |
13
|
|
|
class PaymentWithCard extends Payment |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Card |
17
|
|
|
*/ |
18
|
|
|
protected $card; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $endpoint; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $clientConfig = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* PaymentWithCard constructor. |
32
|
|
|
* |
33
|
|
|
* @param float $amount |
34
|
|
|
* @param string $currency |
35
|
|
|
* @param string $brand |
36
|
|
|
* @param string $type |
37
|
|
|
* @param array $optionalParameters |
38
|
|
|
* @param Card $card |
39
|
|
|
*/ |
40
|
|
|
public function __construct( |
41
|
|
|
float $amount, |
42
|
|
|
string $currency, |
43
|
|
|
string $brand, |
44
|
|
|
string $type, |
45
|
|
|
array $optionalParameters, |
46
|
|
|
Card $card |
47
|
|
|
) { |
48
|
|
|
parent::__construct($amount, $currency, $brand, $type, $optionalParameters); |
49
|
|
|
$this->card = $card; |
50
|
|
|
|
51
|
|
|
if (config('sibs.mode') === 'test') { |
52
|
|
|
$this->clientConfig = [ |
53
|
|
|
'verify' => false, |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
$this->endpoint = config('sibs.host') . config('sibs.version') . '/'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Execute the payment |
61
|
|
|
* |
62
|
|
|
* @return object |
63
|
|
|
*/ |
64
|
|
|
public function pay(): object |
65
|
|
|
{ |
66
|
|
|
$data = (object)null; |
67
|
|
|
|
68
|
|
|
try { |
69
|
|
|
$client = new Client($this->clientConfig); |
70
|
|
|
|
71
|
|
|
$payload = [ |
72
|
|
|
'authentication.userId' => config('sibs.authentication.userId'), |
73
|
|
|
'authentication.password' => config('sibs.authentication.password'), |
74
|
|
|
'authentication.entityId' => config('sibs.authentication.entityId'), |
75
|
|
|
'amount' => number_format($this->amount, 2, '.', ''), |
76
|
|
|
'currency' => $this->currency, |
77
|
|
|
'paymentBrand' => $this->brand, |
78
|
|
|
'paymentType' => $this->type, |
79
|
|
|
'card.number' => $this->card->getNumber(), |
80
|
|
|
'card.holder' => $this->card->getHolder(), |
81
|
|
|
'card.expiryMonth' => str_pad($this->card->getExpiryMonth(), 2, '0', STR_PAD_LEFT), |
82
|
|
|
'card.expiryYear' => $this->card->getExpiryYear(), |
83
|
|
|
'card.cvv' => $this->card->getCvv(), |
84
|
|
|
]; |
85
|
|
|
|
86
|
|
|
$response = $client->post($this->endpoint . 'payments', [ |
87
|
|
|
'headers' => [ |
88
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded', |
89
|
|
|
], |
90
|
|
|
'form_params' => array_merge($payload, $this->getOptionalParameters()), |
91
|
|
|
]); |
92
|
|
|
|
93
|
|
|
$data->status = $response->getStatusCode(); |
94
|
|
|
$data->response = json_decode($response->getBody()->getContents(), false); |
95
|
|
|
|
96
|
|
|
return $data; |
97
|
|
|
} catch (ClientException $e) { |
98
|
|
|
$response = $e->getResponse(); |
99
|
|
|
|
100
|
|
|
$data->status = $response->getStatusCode(); |
101
|
|
|
$data->response = json_decode($response->getBody()->getContents(), false); |
102
|
|
|
|
103
|
|
|
return $data; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|