|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Shetabit\Payment\Drivers\Paystar; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use Shetabit\Payment\Abstracts\Driver; |
|
7
|
|
|
use Shetabit\Payment\Exceptions\InvalidPaymentException; |
|
8
|
|
|
use Shetabit\Payment\Exceptions\PurchaseFailedException; |
|
9
|
|
|
use Shetabit\Payment\Contracts\ReceiptInterface; |
|
10
|
|
|
use Shetabit\Payment\Invoice; |
|
11
|
|
|
use Shetabit\Payment\Receipt; |
|
12
|
|
|
|
|
13
|
|
|
class Paystar extends Driver |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Paystar Client. |
|
17
|
|
|
* |
|
18
|
|
|
* @var object |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $client; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Invoice |
|
24
|
|
|
* |
|
25
|
|
|
* @var Invoice |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $invoice; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Driver settings |
|
31
|
|
|
* |
|
32
|
|
|
* @var object |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $settings; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Paystar constructor. |
|
38
|
|
|
* Construct the class with the relevant settings. |
|
39
|
|
|
* |
|
40
|
|
|
* @param Invoice $invoice |
|
41
|
|
|
* @param $settings |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(Invoice $invoice, $settings) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->invoice($invoice); |
|
46
|
|
|
$this->settings = (object) $settings; |
|
47
|
|
|
$this->client = new Client(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Purchase Invoice. |
|
52
|
|
|
* |
|
53
|
|
|
* @return string |
|
54
|
|
|
* |
|
55
|
|
|
* @throws PurchaseFailedException |
|
56
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
57
|
|
|
*/ |
|
58
|
|
|
public function purchase() |
|
59
|
|
|
{ |
|
60
|
|
|
$details = $this->invoice->getDetails(); |
|
61
|
|
|
|
|
62
|
|
|
$data = array( |
|
63
|
|
|
'amount' => $this->invoice->getAmount(), |
|
64
|
|
|
'email' => $details['email'] ?? null, |
|
65
|
|
|
'phone' => $details['mobile'] ?? $details['phone'] ?? null, |
|
66
|
|
|
'pin' => $this->settings->merchantId, |
|
67
|
|
|
'desc' => $details['description'] ?? $this->settings->description, |
|
68
|
|
|
'callback' => $this->settings->callbackUrl, |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
$response = $this |
|
72
|
|
|
->client |
|
73
|
|
|
->request( |
|
74
|
|
|
'POST', |
|
75
|
|
|
$this->settings->apiPurchaseUrl, |
|
76
|
|
|
[ |
|
77
|
|
|
"form_params" => $data, |
|
78
|
|
|
"http_errors" => false, |
|
79
|
|
|
] |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
|
|
$body = $response->getBody()->getContents(); |
|
83
|
|
|
|
|
84
|
|
|
if (is_numeric($body)) { |
|
85
|
|
|
// some error has happened |
|
86
|
|
|
throw new PurchaseFailedException($this->translateStatus($body)); |
|
87
|
|
|
} else { |
|
88
|
|
|
$this->invoice->transactionId($body); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// return the transaction's id |
|
92
|
|
|
return $this->invoice->getTransactionId(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Pay the Invoice |
|
97
|
|
|
* |
|
98
|
|
|
* @return \Illuminate\Http\RedirectResponse|mixed |
|
99
|
|
|
*/ |
|
100
|
|
|
public function pay() |
|
101
|
|
|
{ |
|
102
|
|
|
$apiUrl = $this->settings->apiPaymentUrl; |
|
103
|
|
|
$payUrl = $apiUrl.$this->invoice->getTransactionId(); |
|
104
|
|
|
|
|
105
|
|
|
// redirect using laravel logic |
|
106
|
|
|
return redirect()->to($payUrl); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Verify payment |
|
111
|
|
|
* |
|
112
|
|
|
* @return mixed|void |
|
113
|
|
|
* |
|
114
|
|
|
* @throws InvalidPaymentException |
|
115
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
116
|
|
|
*/ |
|
117
|
|
|
public function verify() : ReceiptInterface |
|
118
|
|
|
{ |
|
119
|
|
|
$transId = $this->invoice->getTransactionId() ?? request()->input('transid'); |
|
120
|
|
|
|
|
121
|
|
|
$data = [ |
|
122
|
|
|
'amount' => $this->invoice->getAmount(), |
|
123
|
|
|
'pin' => $this->settings->merchantId, |
|
124
|
|
|
'transid' => $transId, |
|
125
|
|
|
]; |
|
126
|
|
|
|
|
127
|
|
|
$response = $this->client->request( |
|
128
|
|
|
'POST', |
|
129
|
|
|
$this->settings->apiVerificationUrl, |
|
130
|
|
|
[ |
|
131
|
|
|
'form_params' => $data, |
|
132
|
|
|
"http_errors" => false, |
|
133
|
|
|
] |
|
134
|
|
|
); |
|
135
|
|
|
$body = $response->getBody()->getContents(); |
|
136
|
|
|
|
|
137
|
|
|
if ($body != 1) { |
|
138
|
|
|
throw new InvalidPaymentException($this->translateStatus($body)); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $this->createReceipt($transId); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Generate the payment's receipt |
|
146
|
|
|
* |
|
147
|
|
|
* @param $referenceId |
|
148
|
|
|
* |
|
149
|
|
|
* @return Receipt |
|
150
|
|
|
*/ |
|
151
|
|
|
protected function createReceipt($referenceId) |
|
152
|
|
|
{ |
|
153
|
|
|
$receipt = new Receipt('paystar', $referenceId); |
|
154
|
|
|
|
|
155
|
|
|
return $receipt; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Trigger an exception |
|
160
|
|
|
* |
|
161
|
|
|
* @param $status |
|
162
|
|
|
* |
|
163
|
|
|
* @return mixed|string |
|
164
|
|
|
*/ |
|
165
|
|
|
private function translateStatus($status) |
|
166
|
|
|
{ |
|
167
|
|
|
$status = (string) $status; |
|
168
|
|
|
|
|
169
|
|
|
$translations = array( |
|
170
|
|
|
"−1" => "مبلغ پرداخت نمیتواند خالی باشد.", |
|
171
|
|
|
"−2" => "کد پین درگاه(کد مرچند) نمیتواند خالی باشد.", |
|
172
|
|
|
"−3" => "لینک برگشتی (callback) نمیتواند خالی باشد.", |
|
173
|
|
|
"−4" => "مبلغ پرداخت باید عددی باشد.", |
|
174
|
|
|
"−5" => "مبلغ پرداخت باید بزرگتر از ۱۰۰ باشد.", |
|
175
|
|
|
"−6" => "کد پین درگاه (مرچند) اشتباه است.", |
|
176
|
|
|
"−7" => "آیپی سرور با آیپی درگاه مطابقت ندارد", |
|
177
|
|
|
"−8" => "کد تراکنش (transid) نمیتواند خالی باشد.", |
|
178
|
|
|
"−9" => "تراکنش مورد نظر وجود ندارد.", |
|
179
|
|
|
"−10" => "کدپین درگاه با درگاه تراکنش مطابقت ندارد.", |
|
180
|
|
|
"−11" => "مبلغ با مبلغ تراکنش مطابقت ندارد.", |
|
181
|
|
|
"-12" => "بانک انتخابی اشتباه است.", |
|
182
|
|
|
"-13" => "درگاه غیرفعال است.", |
|
183
|
|
|
"-14" => "آیپی مشتری ارسال نشده است.", |
|
184
|
|
|
); |
|
185
|
|
|
|
|
186
|
|
|
$unknownError = 'خطای ناشناخته رخ داده است.'; |
|
187
|
|
|
|
|
188
|
|
|
return array_key_exists($status, $translations) ? $translations[$status] : $unknownError; |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|