1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Shetabit\Multipay\Drivers\Aqayepardakht; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Shetabit\Multipay\Abstracts\Driver; |
7
|
|
|
use Shetabit\Multipay\Contracts\ReceiptInterface; |
8
|
|
|
use Shetabit\Multipay\Exceptions\InvalidPaymentException; |
9
|
|
|
use Shetabit\Multipay\Exceptions\PurchaseFailedException; |
10
|
|
|
use Shetabit\Multipay\Invoice; |
11
|
|
|
use Shetabit\Multipay\Receipt; |
12
|
|
|
use Shetabit\Multipay\RedirectionForm; |
13
|
|
|
use Shetabit\Multipay\Request; |
14
|
|
|
|
15
|
|
|
class Aqayepardakht extends Driver |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Aqayepardakht Client. |
19
|
|
|
* |
20
|
|
|
* @var object |
21
|
|
|
*/ |
22
|
|
|
protected $client; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Invoice |
26
|
|
|
* |
27
|
|
|
* @var Invoice |
28
|
|
|
*/ |
29
|
|
|
protected $invoice; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Driver settings |
33
|
|
|
* |
34
|
|
|
* @var object |
35
|
|
|
*/ |
36
|
|
|
protected $settings; |
37
|
|
|
|
38
|
|
|
const PAYMENT_STATUS_FAILED = 'error'; |
39
|
|
|
const PAYMENT_STATUS_OK = 'success'; |
40
|
|
|
|
41
|
|
|
public function __construct(Invoice $invoice, $settings) |
42
|
|
|
{ |
43
|
|
|
$this->invoice($invoice); |
44
|
|
|
$this->settings = (object) $settings; |
45
|
|
|
$this->client = new Client(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string |
50
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
51
|
|
|
* @throws \Shetabit\Multipay\Exceptions\PurchaseFailedException |
52
|
|
|
*/ |
53
|
|
|
public function purchase() |
54
|
|
|
{ |
55
|
|
|
$data = [ |
56
|
|
|
'pin' => $this->settings->mode === "normal" ? $this->settings->pin : "sandbox", |
57
|
|
|
'amount' => $this->invoice->getAmount() / ($this->settings->currency == 'T' ? 1 : 10), // convert to toman |
58
|
|
|
'callback' => $this->settings->callbackUrl, |
59
|
|
|
'invoice_id' => $this->settings->invoice_id, |
60
|
|
|
'mobile' => $this->settings->mobile, |
61
|
|
|
'email' => $this->settings->email, |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
$response = $this->client |
65
|
|
|
->request( |
66
|
|
|
'POST', |
67
|
|
|
$this->settings->apiPurchaseUrl, |
68
|
|
|
[ |
69
|
|
|
'json' => $data, |
70
|
|
|
'headers' => [ |
71
|
|
|
"Accept" => "application/json", |
72
|
|
|
], |
73
|
|
|
'http_errors' => false, |
74
|
|
|
] |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$responseBody = json_decode($response->getBody()->getContents(), true); |
78
|
|
|
|
79
|
|
|
if ($responseBody['status'] !== self::PAYMENT_STATUS_OK) { |
80
|
|
|
throw new PurchaseFailedException($this->getErrorMessage($responseBody['code'])); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->invoice->transactionId($responseBody['transid']); |
84
|
|
|
|
85
|
|
|
return $this->invoice->getTransactionId(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return \Shetabit\Multipay\RedirectionForm |
90
|
|
|
*/ |
91
|
|
|
public function pay(): RedirectionForm |
92
|
|
|
{ |
93
|
|
|
$url = $this->settings->mode === "normal" ? $this->settings->apiPaymentUrl : $this->settings->apiPaymentUrlSandbox; |
94
|
|
|
$url = $url . $this->invoice->getTransactionId(); |
95
|
|
|
|
96
|
|
|
return $this->redirectWithForm($url, [], 'GET'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return ReceiptInterface |
101
|
|
|
* |
102
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
103
|
|
|
* @throws \Shetabit\Multipay\Exceptions\InvalidPaymentException |
104
|
|
|
*/ |
105
|
|
|
public function verify(): ReceiptInterface |
106
|
|
|
{ |
107
|
|
|
$tracking_number = Request::post("tracking_number"); |
108
|
|
|
$transid = Request::post("transid"); |
109
|
|
|
if ($tracking_number === null || $tracking_number === ""|| $transid === ""|| $transid === null) { |
110
|
|
|
$this->notVerified('پرداخت ناموفق.'); |
111
|
|
|
} |
112
|
|
|
$data = [ |
113
|
|
|
'pin' => $this->settings->pin, |
114
|
|
|
'amount' => $this->invoice->getAmount() / ($this->settings->currency == 'T' ? 1 : 10), // convert to toman |
115
|
|
|
'transid' => $transid |
116
|
|
|
]; |
117
|
|
|
$response = $this->client |
118
|
|
|
->request( |
119
|
|
|
'POST', |
120
|
|
|
$this->settings->apiVerificationUrl, |
121
|
|
|
[ |
122
|
|
|
'json' => $data, |
123
|
|
|
'headers' => [ |
124
|
|
|
"Accept" => "application/json", |
125
|
|
|
], |
126
|
|
|
'http_errors' => false, |
127
|
|
|
] |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
$responseBody = json_decode($response->getBody()->getContents(), true); |
131
|
|
|
|
132
|
|
|
if ($responseBody['status'] !== self::PAYMENT_STATUS_OK) { |
133
|
|
|
if (isset($responseBody['code'])) { |
134
|
|
|
$message = $this->getErrorMessage($responseBody["code"]); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$this->notVerified($message ?? '', $responseBody["code"]); |
138
|
|
|
} |
139
|
|
|
return $this->createReceipt($tracking_number); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Generate the payment's receipt |
144
|
|
|
* |
145
|
|
|
* @param $referenceId |
146
|
|
|
* |
147
|
|
|
* @return Receipt |
148
|
|
|
*/ |
149
|
|
|
protected function createReceipt($referenceId) |
150
|
|
|
{ |
151
|
|
|
$receipt = new Receipt('aqayepardakht', $referenceId); |
152
|
|
|
|
153
|
|
|
return $receipt; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param $message |
158
|
|
|
* @throws \Shetabit\Multipay\Exceptions\InvalidPaymentException |
159
|
|
|
*/ |
160
|
|
|
protected function notVerified($message, $status = 0) |
161
|
|
|
{ |
162
|
|
|
if (empty($message)) { |
163
|
|
|
throw new InvalidPaymentException('خطای ناشناخته ای رخ داده است.', (int)$status); |
164
|
|
|
} else { |
165
|
|
|
throw new InvalidPaymentException($message, (int)$status); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param $code |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
|
|
protected function getErrorMessage($code) |
174
|
|
|
{ |
175
|
|
|
$code = (int)$code; |
176
|
|
|
switch ($code) { |
177
|
|
|
case -1: return "مبلغ نباید خالی باشد."; |
178
|
|
|
case -2: return "کد پین درگاه نمیتواند خالی باشد."; |
179
|
|
|
case -3: return "آدرس بازگشت نمیتواند خالی باشد."; |
180
|
|
|
case -4: return "مبلغ وارد شده اشتباه است."; |
181
|
|
|
case -5: return "مبلع باید بین 100 تومان تا 50 میلیون تومان باشد."; |
182
|
|
|
case -6: return "کد پین وارد شده اشتباه است."; |
183
|
|
|
case -7: return "کد تراکنش نمیتواند خالی باشد."; |
184
|
|
|
case -8: return "تراکنش مورد نظر وجود ندارد."; |
185
|
|
|
case -9: return "کد پین درگاه با درگاه تراکنش مطابقت ندارد."; |
186
|
|
|
case -10: return "مبلغ با مبلغ تراکنش مطابقت ندارد."; |
187
|
|
|
case -11: return "درگاه در انتظار تایید و یا غیرفعال است."; |
188
|
|
|
case -12: return "امکان ارسال درخواست برای این پذیرنده وجود ندارد."; |
189
|
|
|
case -13: return "شماره کارت باید 16 رقم چسبیده بهم باشد."; |
190
|
|
|
case 0: return "پرداخت انجام نشد."; |
191
|
|
|
case 1: return "پرداخت با موفقیت انجام شد."; |
192
|
|
|
case 2: return "تراکنش قبلا وریفای شده است."; |
193
|
|
|
default: return "خطای نامشخص."; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|