1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Orkhanahmadov\Goldenpay; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Orkhanahmadov\Goldenpay\Exceptions\GoldenpayPaymentKeyException; |
7
|
|
|
|
8
|
|
|
class Goldenpay |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
private $authKey; |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $merchantName; |
18
|
|
|
/** |
19
|
|
|
* @var Client |
20
|
|
|
*/ |
21
|
|
|
private $client; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Goldenpay constructor. |
25
|
|
|
* |
26
|
|
|
* @param string $authKey |
27
|
|
|
* @param string $merchantName |
28
|
|
|
* @param Client|null $client |
29
|
|
|
*/ |
30
|
|
|
public function __construct(string $authKey, string $merchantName, Client $client = null) |
31
|
|
|
{ |
32
|
|
|
$this->authKey = $authKey; |
33
|
|
|
$this->merchantName = $merchantName; |
34
|
|
|
$this->client = $client ?: new Client(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Gets new payment key. |
39
|
|
|
* |
40
|
|
|
* @param int $amount |
41
|
|
|
* @param string $cardType |
42
|
|
|
* @param string $description |
43
|
|
|
* @param string $lang |
44
|
|
|
* |
45
|
|
|
* @return PaymentKey |
46
|
|
|
*/ |
47
|
|
|
public function newPaymentKey(int $amount, string $cardType, string $description, string $lang = 'lv') |
48
|
|
|
{ |
49
|
|
|
$result = $this->sendRequest('https://rest.goldenpay.az/web/service/merchant/getPaymentKey', [ |
50
|
|
|
'merchantName' => $this->merchantName, |
51
|
|
|
'amount' => $amount, |
52
|
|
|
'cardType' => $cardType, |
53
|
|
|
'description' => $description, |
54
|
|
|
'lang' => $lang, |
55
|
|
|
'hashCode' => md5($this->authKey.$this->merchantName.$cardType.$amount.$description), |
56
|
|
|
]); |
57
|
|
|
|
58
|
|
|
if ($result['status']['code'] !== 1) { |
59
|
|
|
throw new GoldenpayPaymentKeyException($result['status']['message']); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return new PaymentKey($result['status']['code'], $result['status']['message'], $result['paymentKey']); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Checks payment result. |
67
|
|
|
* |
68
|
|
|
* @param string $authKey |
69
|
|
|
* @param string $paymentKey |
70
|
|
|
* |
71
|
|
|
* @return PaymentResult |
72
|
|
|
*/ |
73
|
|
|
public function checkPaymentResult(string $authKey, string $paymentKey) |
74
|
|
|
{ |
75
|
|
|
$result = $this->sendRequest('https://rest.goldenpay.az/web/service/merchant/getPaymentResult', [ |
76
|
|
|
'payment_key' => $paymentKey, |
77
|
|
|
'Hash_code' => md5($authKey.$paymentKey), |
78
|
|
|
]); |
79
|
|
|
|
80
|
|
|
return new PaymentResult($result); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Sends requests to GoldenPay endpoint. |
85
|
|
|
* |
86
|
|
|
* @param string $url |
87
|
|
|
* @param array $json |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
private function sendRequest(string $url, array $json) |
91
|
|
|
{ |
92
|
|
|
$response = $this->client->post($url, [ |
93
|
|
|
'headers' => ['Accept' => 'application/json'], |
94
|
|
|
'json' => $json, |
95
|
|
|
]); |
96
|
|
|
|
97
|
|
|
return json_decode($response->getBody()->getContents(), true); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|