1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Orkhanahmadov\Goldenpay; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Orkhanahmadov\Goldenpay\Enums\CardType; |
7
|
|
|
use Orkhanahmadov\Goldenpay\Enums\Language; |
8
|
|
|
use Orkhanahmadov\Goldenpay\Exceptions\GoldenpayPaymentKeyException; |
9
|
|
|
use Orkhanahmadov\Goldenpay\Response\PaymentKey; |
10
|
|
|
use Orkhanahmadov\Goldenpay\Response\PaymentResult; |
11
|
|
|
use function GuzzleHttp\json_decode; |
12
|
|
|
|
13
|
|
|
class Goldenpay implements PaymentInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string|null |
17
|
|
|
*/ |
18
|
|
|
private $authKey = null; |
19
|
|
|
/** |
20
|
|
|
* @var string|null |
21
|
|
|
*/ |
22
|
|
|
private $merchantName = null; |
23
|
|
|
/** |
24
|
|
|
* @var Client |
25
|
|
|
*/ |
26
|
|
|
private $client; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Goldenpay constructor. |
30
|
|
|
*/ |
31
|
|
|
public function __construct() |
32
|
|
|
{ |
33
|
|
|
$this->client = new Client(['base_uri' => 'https://rest.goldenpay.az/web/service/merchant/']); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Sets Goldenpay authentication credentials. |
38
|
|
|
* |
39
|
|
|
* @param string $authKey |
40
|
|
|
* @param string $merchantName |
41
|
|
|
* |
42
|
|
|
* @return self |
43
|
|
|
*/ |
44
|
|
|
public function auth(string $authKey, string $merchantName): PaymentInterface |
45
|
|
|
{ |
46
|
|
|
$this->authKey = $authKey; |
47
|
|
|
$this->merchantName = $merchantName; |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Gets new payment key from Goldenpay. |
54
|
|
|
* |
55
|
|
|
* @param int $amount |
56
|
|
|
* @param CardType $cardType |
57
|
|
|
* @param string $description |
58
|
|
|
* @param Language|null $lang |
59
|
|
|
* |
60
|
|
|
* @throws GoldenpayPaymentKeyException |
61
|
|
|
* |
62
|
|
|
* @return PaymentKey |
63
|
|
|
*/ |
64
|
|
|
public function payment(int $amount, CardType $cardType, string $description, ?Language $lang = null): PaymentKey |
65
|
|
|
{ |
66
|
|
|
$result = $this->request('getPaymentKey', [ |
67
|
|
|
'merchantName' => $this->merchantName, |
68
|
|
|
'amount' => $amount, |
69
|
|
|
'cardType' => $cardType->getValue(), |
70
|
|
|
'description' => $description, |
71
|
|
|
'lang' => $lang ? $lang->getValue() : 'lv', |
72
|
|
|
'hashCode' => md5($this->authKey.$this->merchantName.$cardType->getValue().$amount.$description), |
73
|
|
|
]); |
74
|
|
|
|
75
|
|
|
if ($result['status']['code'] !== 1) { |
76
|
|
|
throw new GoldenpayPaymentKeyException($result['status']['message'].'. Code: '.$result['status']['code']); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return new PaymentKey( |
80
|
|
|
$result['status']['code'], |
81
|
|
|
$result['status']['message'], |
82
|
|
|
$result['paymentKey'] |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Checks result of payment using existing payment key. |
88
|
|
|
* |
89
|
|
|
* @param PaymentKey|string $paymentKey |
90
|
|
|
* |
91
|
|
|
* @return PaymentResult |
92
|
|
|
*/ |
93
|
|
|
public function result($paymentKey): PaymentResult |
94
|
|
|
{ |
95
|
|
|
$paymentKey = $paymentKey instanceof PaymentKey ? $paymentKey->getPaymentKey() : $paymentKey; |
96
|
|
|
|
97
|
|
|
$result = $this->request('getPaymentResult', [ |
98
|
|
|
'payment_key' => $paymentKey, |
99
|
|
|
'hash_code' => md5($this->authKey.$paymentKey), |
100
|
|
|
]); |
101
|
|
|
|
102
|
|
|
return new PaymentResult( |
103
|
|
|
$result['status']['code'], |
104
|
|
|
$result['status']['message'], |
105
|
|
|
$result |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Sends requests to GoldenPay endpoint. |
111
|
|
|
* |
112
|
|
|
* @param string $endpoint |
113
|
|
|
* @param array $data |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
private function request(string $endpoint, array $data) |
118
|
|
|
{ |
119
|
|
|
if ($endpoint === 'getPaymentResult') { |
120
|
|
|
$response = $this->client->get($endpoint, [ |
121
|
|
|
'headers' => ['Accept' => 'application/json'], |
122
|
|
|
'query' => $data, |
123
|
|
|
]); |
124
|
|
|
} else { |
125
|
|
|
$response = $this->client->post($endpoint, [ |
126
|
|
|
'headers' => ['Accept' => 'application/json', 'Content-Type' => 'application/json'], |
127
|
|
|
'json' => $data, |
128
|
|
|
]); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return json_decode($response->getBody()->getContents(), true); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return string|null |
136
|
|
|
*/ |
137
|
|
|
public function getAuthKey(): ?string |
138
|
|
|
{ |
139
|
|
|
return $this->authKey; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return string|null |
144
|
|
|
*/ |
145
|
|
|
public function getMerchantName(): ?string |
146
|
|
|
{ |
147
|
|
|
return $this->merchantName; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|