1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Shetabit\Multipay\Drivers\Local; |
4
|
|
|
|
5
|
|
|
use Shetabit\Multipay\Abstracts\Driver; |
6
|
|
|
use Shetabit\Multipay\Exceptions\InvalidPaymentException; |
7
|
|
|
use Shetabit\Multipay\Exceptions\PurchaseFailedException; |
8
|
|
|
use Shetabit\Multipay\Contracts\ReceiptInterface; |
9
|
|
|
use Shetabit\Multipay\Invoice; |
10
|
|
|
use Shetabit\Multipay\Receipt; |
11
|
|
|
use Shetabit\Multipay\RedirectionForm; |
12
|
|
|
use Shetabit\Multipay\Request; |
13
|
|
|
|
14
|
|
|
class Local extends Driver |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Invoice |
18
|
|
|
* |
19
|
|
|
* @var Invoice |
20
|
|
|
*/ |
21
|
|
|
protected $invoice; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Driver settings |
25
|
|
|
* |
26
|
|
|
* @var object |
27
|
|
|
*/ |
28
|
|
|
protected $settings; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Local constructor. |
32
|
|
|
* Construct the class with the relevant settings. |
33
|
|
|
* |
34
|
|
|
* @param Invoice $invoice |
35
|
|
|
* @param $settings |
36
|
|
|
*/ |
37
|
|
|
public function __construct(Invoice $invoice, $settings) |
38
|
|
|
{ |
39
|
|
|
$this->invoice($invoice); |
40
|
|
|
$this->settings = (object) $settings; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Purchase Invoice. |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
* |
48
|
|
|
* @throws PurchaseFailedException |
49
|
|
|
*/ |
50
|
|
|
public function purchase(): string |
51
|
|
|
{ |
52
|
|
|
// throw PurchaseFailedException if set in invoice message |
53
|
|
|
if ($message = $this->invoice->getDetail('failedPurchase')) { |
54
|
|
|
throw new PurchaseFailedException($message); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// set a randomly grenerated transactionId |
58
|
|
|
$transactionId = mt_rand(1000000, 9999999); |
59
|
|
|
$this->invoice->transactionId($transactionId); |
60
|
|
|
|
61
|
|
|
return $this->invoice->getTransactionId(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Pay the Invoice |
66
|
|
|
* |
67
|
|
|
* @return RedirectionForm |
68
|
|
|
*/ |
69
|
|
|
public function pay(): RedirectionForm |
70
|
|
|
{ |
71
|
|
|
RedirectionForm::setViewPath(dirname(__DIR__).'/../../resources/views/local-form.php'); |
72
|
|
|
|
73
|
|
|
return new RedirectionForm('', $this->getFormData(), 'POST'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Verify payment |
78
|
|
|
* |
79
|
|
|
* @return ReceiptInterface |
80
|
|
|
* |
81
|
|
|
* @throws InvalidPaymentException |
82
|
|
|
*/ |
83
|
|
|
public function verify(): ReceiptInterface |
84
|
|
|
{ |
85
|
|
|
$data = array( |
86
|
|
|
'transactionId' => Request::input('transactionId'), |
87
|
|
|
'cancel' => Request::input('cancel') |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
$success = $data['transactionId'] && !$data['cancel']; |
91
|
|
|
|
92
|
|
|
if (!$success) { |
93
|
|
|
$this->notVerified(0); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$receipt = $this->createReceipt($data['transactionId']); |
97
|
|
|
$receipt->detail([ |
98
|
|
|
'orderId' => $this->invoice->getDetail('orderId') ?? mt_rand(1111, 9999), |
99
|
|
|
'traceNo' => $this->invoice->getDetail('traceNo') ?? mt_rand(11111, 99999), |
100
|
|
|
'referenceNo' => $data['transactionId'], |
101
|
|
|
'cardNo' => $this->invoice->getDetail('cartNo') ?? mt_rand(1111, 9999), |
102
|
|
|
]); |
103
|
|
|
|
104
|
|
|
return $receipt; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Generate the payment's receipt |
109
|
|
|
* |
110
|
|
|
* @param $referenceId |
111
|
|
|
* |
112
|
|
|
* @return Receipt |
113
|
|
|
*/ |
114
|
|
|
protected function createReceipt($referenceId): Receipt |
115
|
|
|
{ |
116
|
|
|
return new Receipt('local', $referenceId); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Populate payment form data |
121
|
|
|
* |
122
|
|
|
* |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
protected function getFormData(): array |
126
|
|
|
{ |
127
|
|
|
return [ |
128
|
|
|
'orderId' => $this->invoice->getDetail('orderId'), |
129
|
|
|
'price' => number_format($this->invoice->getAmount(), 0, ','), |
130
|
|
|
'successUrl' => $this->addUrlQuery($this->settings->callbackUrl, [ |
131
|
|
|
'transactionId' => $this->invoice->getTransactionId(), |
132
|
|
|
]), |
133
|
|
|
'cancelUrl' => $this->addUrlQuery($this->settings->callbackUrl, [ |
134
|
|
|
'transactionId' => $this->invoice->getTransactionId(), |
135
|
|
|
'cancel' => 'true', |
136
|
|
|
]), |
137
|
|
|
'title' => $this->settings->title, |
138
|
|
|
'description' => $this->settings->description, |
139
|
|
|
'orderLabel' => $this->settings->orderLabel, |
140
|
|
|
'amountLabel' => $this->settings->amountLabel, |
141
|
|
|
'payButton' => $this->settings->payButton, |
142
|
|
|
'cancelButton' => $this->settings->cancelButton, |
143
|
|
|
]; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Add array parameters as url query |
149
|
|
|
* |
150
|
|
|
* @param $url |
151
|
|
|
* @param $params |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
protected function addUrlQuery($url, $params): string |
156
|
|
|
{ |
157
|
|
|
$urlWithQuery = $url; |
158
|
|
|
foreach ($params as $key => $value) { |
159
|
|
|
$urlWithQuery .= (parse_url($urlWithQuery, PHP_URL_QUERY) ? '&' : '?') . "{$key}={$value}"; |
160
|
|
|
} |
161
|
|
|
return $urlWithQuery; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Trigger an exception |
167
|
|
|
* |
168
|
|
|
* @param $status |
169
|
|
|
* |
170
|
|
|
* @throws InvalidPaymentException |
171
|
|
|
*/ |
172
|
|
|
private function notVerified($status) |
173
|
|
|
{ |
174
|
|
|
$translations = array( |
175
|
|
|
0 => 'تراکنش توسط خریدار لغو شده است.', |
176
|
|
|
); |
177
|
|
|
|
178
|
|
|
if (array_key_exists($status, $translations)) { |
179
|
|
|
throw new InvalidPaymentException($translations[$status]); |
180
|
|
|
} else { |
181
|
|
|
throw new InvalidPaymentException('تراکنش با خطا مواجه شد.'); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|