1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Shetabit\Payment\Drivers\Zibal; |
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 Zibal extends Driver |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Zibal 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
|
|
|
* Zibal 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 \GuzzleHttp\Exception\GuzzleException |
56
|
|
|
*/ |
57
|
|
|
public function purchase() |
58
|
|
|
{ |
59
|
|
|
$details = $this->invoice->getDetails(); |
60
|
|
|
|
61
|
|
|
// convert to toman |
62
|
|
|
$toman = $this->invoice->getAmount() * 10; |
63
|
|
|
|
64
|
|
|
if (!empty($details['orderId'])) { |
65
|
|
|
$orderId = $details['orderId']; |
66
|
|
|
} else if(!empty($details['order_id'])) { |
67
|
|
|
$orderId = $details['order_id']; |
68
|
|
|
} else { |
69
|
|
|
$orderId = crc32($this->invoice->getUuid()).time(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$mobile = null; |
73
|
|
|
if(!empty($details['mobile'])) { |
74
|
|
|
$mobile = $details['mobile']; |
75
|
|
|
} else if(!empty($details['phone'])) { |
76
|
|
|
$mobile = $details['phone']; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$data = array( |
80
|
|
|
"merchant"=> $this->settings->merchantId, //required |
81
|
|
|
"callbackUrl"=> $this->settings->callbackUrl, //required |
82
|
|
|
"amount"=> $toman, //required |
83
|
|
|
"orderId"=> $orderId, //optional |
84
|
|
|
'mobile' => $mobile, //optional for mpg |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
$json = json_encode($data, JSON_UNESCAPED_UNICODE); |
88
|
|
|
|
89
|
|
|
$response = $this->client->request( |
90
|
|
|
'POST', |
91
|
|
|
$this->settings->apiPurchaseUrl, |
92
|
|
|
[ |
93
|
|
|
"form_params" => $json, |
94
|
|
|
"http_errors" => false, |
95
|
|
|
] |
96
|
|
|
); |
97
|
|
|
$body = json_decode($response->getBody()->getContents(), true); |
|
|
|
|
98
|
|
|
|
99
|
|
|
if ($response->result != 100) { |
100
|
|
|
// some error has happened |
101
|
|
|
throw new PurchaseFailedException($response->message); |
102
|
|
|
} else { |
103
|
|
|
$this->invoice->transactionId($response->trackId); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// return the transaction's id |
107
|
|
|
return $this->invoice->getTransactionId(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Pay the Invoice |
112
|
|
|
* |
113
|
|
|
* @return \Illuminate\Http\RedirectResponse|mixed |
114
|
|
|
*/ |
115
|
|
|
public function pay() |
116
|
|
|
{ |
117
|
|
|
$payUrl = $this->settings->apiPaymentUrl.$this->invoice->getTransactionId(); |
118
|
|
|
|
119
|
|
|
// redirect using laravel logic |
120
|
|
|
return redirect()->to($payUrl); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Verify payment |
125
|
|
|
* |
126
|
|
|
* @return mixed|void |
127
|
|
|
* |
128
|
|
|
* @throws InvalidPaymentException |
129
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
130
|
|
|
*/ |
131
|
|
|
public function verify() : ReceiptInterface |
132
|
|
|
{ |
133
|
|
|
$successFlag = request()->input('success'); |
134
|
|
|
$orderId = request()->input('orderId'); |
135
|
|
|
$transactionId = $this->invoice->getTransactionId() ?? request()->input('trackId'); |
136
|
|
|
|
137
|
|
|
if (!$successFlag != 1) { |
138
|
|
|
$this->notVerified('پرداخت با شکست مواجه شد'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
//start verfication |
142
|
|
|
$data = array( |
143
|
|
|
"merchant" => $this->settings->merchantId, //required |
144
|
|
|
"trackId" => $transactionId, //required |
145
|
|
|
); |
146
|
|
|
|
147
|
|
|
$json = json_encode($data, JSON_UNESCAPED_LINE_TERMINATORS); |
148
|
|
|
|
149
|
|
|
$response = $this->client->request( |
150
|
|
|
'POST', |
151
|
|
|
$this->settings->apiVerificationUrl, |
152
|
|
|
["form_params" => $json, "http_errors" => false] |
153
|
|
|
); |
154
|
|
|
$body = json_decode($response->getBody()->getContents(), true); |
155
|
|
|
|
156
|
|
|
if ($body->result != 100) { |
157
|
|
|
$this->notVerified($body->message); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/* |
161
|
|
|
for more info: |
162
|
|
|
var_dump($body); |
163
|
|
|
*/ |
164
|
|
|
|
165
|
|
|
return $this->createReceipt($orderId); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Generate the payment's receipt |
170
|
|
|
* |
171
|
|
|
* @param $referenceId |
172
|
|
|
* |
173
|
|
|
* @return Receipt |
174
|
|
|
*/ |
175
|
|
|
protected function createReceipt($referenceId) |
176
|
|
|
{ |
177
|
|
|
$receipt = new Receipt('Zibal', $referenceId); |
178
|
|
|
|
179
|
|
|
return $receipt; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Trigger an exception |
184
|
|
|
* |
185
|
|
|
* @param $message |
186
|
|
|
* @throws InvalidPaymentException |
187
|
|
|
*/ |
188
|
|
|
private function notVerified($message) |
189
|
|
|
{ |
190
|
|
|
if (empty($message)) { |
191
|
|
|
throw new InvalidPaymentException('خطای ناشناخته ای رخ داده است.'); |
192
|
|
|
} else { |
193
|
|
|
throw new InvalidPaymentException($message); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|