|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Shetabit\Payment\Drivers\Behpardakht; |
|
4
|
|
|
|
|
5
|
|
|
use Shetabit\Payment\Abstracts\Driver; |
|
6
|
|
|
use Shetabit\Payment\Exceptions\{InvalidPaymentException, PurchaseFailedException}; |
|
7
|
|
|
use Shetabit\Payment\{Contracts\ReceiptInterface, Invoice, Receipt}; |
|
8
|
|
|
|
|
9
|
|
|
class Behpardakht extends Driver |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Invoice |
|
13
|
|
|
* |
|
14
|
|
|
* @var Invoice |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $invoice; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Driver settings |
|
20
|
|
|
* |
|
21
|
|
|
* @var object |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $settings; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Behpardakht constructor. |
|
27
|
|
|
* Construct the class with the relevant settings. |
|
28
|
|
|
* |
|
29
|
|
|
* @param Invoice $invoice |
|
30
|
|
|
* @param $settings |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(Invoice $invoice, $settings) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->invoice($invoice); |
|
35
|
|
|
$this->settings = (object)$settings; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Purchase Invoice. |
|
40
|
|
|
* |
|
41
|
|
|
* @return string |
|
42
|
|
|
* |
|
43
|
|
|
* @throws PurchaseFailedException |
|
44
|
|
|
* @throws \SoapFault |
|
45
|
|
|
*/ |
|
46
|
|
|
public function purchase() |
|
47
|
|
|
{ |
|
48
|
|
|
$soap = new \SoapClient($this->settings->apiPurchaseUrl); |
|
49
|
|
|
$response = $soap->bpPayRequest( |
|
50
|
|
|
$this->preparePurchaseData(), |
|
51
|
|
|
$this->settings->apiNamespaceUrl |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
// fault has happened in bank gateway |
|
55
|
|
|
if ($soap->fault) { |
|
|
|
|
|
|
56
|
|
|
throw new PurchaseFailedException('an error has happened'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// an error has happened |
|
60
|
|
|
if ($error = $soap->getError()) { |
|
61
|
|
|
throw new PurchaseFailedException($error); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$data = explode (',', $response); |
|
65
|
|
|
|
|
66
|
|
|
// purchase was not successful |
|
67
|
|
|
if ($data[0] != "0") { |
|
68
|
|
|
throw new PurchaseFailedException($response); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$this->invoice->transactionId($data[1]); |
|
72
|
|
|
|
|
73
|
|
|
// return the transaction's id |
|
74
|
|
|
return $this->invoice->getTransactionId(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Pay the Invoice |
|
79
|
|
|
* |
|
80
|
|
|
* @return \Illuminate\Http\RedirectResponse|mixed |
|
81
|
|
|
*/ |
|
82
|
|
|
public function pay() |
|
83
|
|
|
{ |
|
84
|
|
|
$payUrl = $this->settings->apiPaymentUrl; |
|
85
|
|
|
|
|
86
|
|
|
return $this->redirectWithForm( |
|
87
|
|
|
$payUrl, |
|
88
|
|
|
[ |
|
89
|
|
|
'RefId' => $this->invoice->getTransactionId(), |
|
90
|
|
|
], |
|
91
|
|
|
'POST' |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Verify payment |
|
97
|
|
|
* |
|
98
|
|
|
* @return mixed|Receipt |
|
99
|
|
|
* |
|
100
|
|
|
* @throws InvalidPaymentException |
|
101
|
|
|
* @throws \SoapFault |
|
102
|
|
|
*/ |
|
103
|
|
|
public function verify() : ReceiptInterface |
|
104
|
|
|
{ |
|
105
|
|
|
$resCode = request()->get('ResCode'); |
|
106
|
|
|
if ($resCode != '0') { |
|
107
|
|
|
$message = $resCode ?? 'تراکنش نا موفق بوده است.'; |
|
108
|
|
|
throw new InvalidPaymentException($message); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$data = $this->prepareVerificationData(); |
|
112
|
|
|
$soap = new \SoapClient($this->settings->apiVerificationUrl); |
|
113
|
|
|
|
|
114
|
|
|
// step1: verify request |
|
115
|
|
|
$verifyResponse = $soap->bpVerifyRequest($data, $this->settings->apiNamespaceUrl); |
|
116
|
|
|
if ($verifyResponse != 0) { |
|
117
|
|
|
// rollback money and throw exception |
|
118
|
|
|
$soap->bpReversalRequest($data, $this->settings->apiNamespaceUrl); |
|
119
|
|
|
throw new InvalidPaymentException($verifyResponse ?? "خطا در عملیات وریفای تراکنش"); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
// step2: settle request |
|
123
|
|
|
$settleResponse = $soap->bpSettleRequest($data, $this->settings->apiNamespaceUrl); |
|
124
|
|
|
if ($settleResponse != 0) { |
|
125
|
|
|
// rollback money and throw exception |
|
126
|
|
|
$soap->bpReversalRequest($data, $this->settings->apiNamespaceUrl); |
|
127
|
|
|
throw new InvalidPaymentException($settleResponse ?? "خطا در ثبت درخواست واریز وجه"); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $this->createReceipt($data['saleReferenceId']); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Generate the payment's receipt |
|
135
|
|
|
* |
|
136
|
|
|
* @param $referenceId |
|
137
|
|
|
* |
|
138
|
|
|
* @return Receipt |
|
139
|
|
|
*/ |
|
140
|
|
|
public function createReceipt($referenceId) |
|
141
|
|
|
{ |
|
142
|
|
|
$receipt = new Receipt('behpardakht', $referenceId); |
|
143
|
|
|
|
|
144
|
|
|
return $receipt; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Prepare data for payment verification |
|
149
|
|
|
* |
|
150
|
|
|
* @return array |
|
151
|
|
|
*/ |
|
152
|
|
|
public function prepareVerificationData() |
|
153
|
|
|
{ |
|
154
|
|
|
$orderId = request()->get('SaleOrderId'); |
|
155
|
|
|
$verifySaleOrderId = request()->get('SaleOrderId'); |
|
156
|
|
|
$verifySaleReferenceId = request()->get('SaleReferenceId'); |
|
157
|
|
|
|
|
158
|
|
|
return array( |
|
159
|
|
|
'terminalId' => $this->settings->terminalId, |
|
160
|
|
|
'userName' => $this->settings->username, |
|
161
|
|
|
'userPassword' => $this->settings->password, |
|
162
|
|
|
'orderId' => $orderId, |
|
163
|
|
|
'saleOrderId' => $verifySaleOrderId, |
|
164
|
|
|
'saleReferenceId' => $verifySaleReferenceId |
|
165
|
|
|
); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Prepare data for purchasing invoice |
|
170
|
|
|
* |
|
171
|
|
|
* @return array |
|
172
|
|
|
*/ |
|
173
|
|
|
protected function preparePurchaseData() |
|
174
|
|
|
{ |
|
175
|
|
|
if (!empty($this->invoice->getDetails()['description'])) { |
|
176
|
|
|
$description = $this->invoice->getDetails()['description']; |
|
177
|
|
|
} else { |
|
178
|
|
|
$description = $this->settings->description; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
$payerId = $details['payerId'] ?? 0; |
|
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
return array( |
|
184
|
|
|
'terminalId' => $this->settings->terminalId, |
|
185
|
|
|
'userName' => $this->settings->username, |
|
186
|
|
|
'userPassword' => $this->settings->password, |
|
187
|
|
|
'callBackUrl' => $this->settings->callbackUrl, |
|
188
|
|
|
'amount' => $this->invoice->getAmount() * 10, // convert to rial |
|
189
|
|
|
'localDate' => now()->format('Ymd'), |
|
190
|
|
|
'localTime' => now()->format('Gis'), |
|
191
|
|
|
'orderId' => crc32($this->invoice->getUuid()), |
|
192
|
|
|
'additionalData' => $description, |
|
193
|
|
|
'payerId' => $payerId |
|
194
|
|
|
); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|