1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Shetabit\Multipay\Drivers\Vandar; |
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 Vandar extends Driver |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Vandar 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 = 'FAILED'; |
39
|
|
|
const PAYMENT_STATUS_OK = 'OK'; |
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
|
|
|
'api_key' => $this->settings->merchantId, |
57
|
|
|
'amount' => $this->invoice->getAmount(), |
58
|
|
|
'callback_url' => $this->settings->callbackUrl |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
$response = $this->client |
62
|
|
|
->request( |
63
|
|
|
'POST', |
64
|
|
|
$this->settings->apiPurchaseUrl, |
65
|
|
|
[ |
66
|
|
|
'json' => $data, |
67
|
|
|
'headers' => [ |
68
|
|
|
"Accept" => "application/json", |
69
|
|
|
], |
70
|
|
|
'http_errors' => false, |
71
|
|
|
] |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$responseBody = json_decode($response->getBody()->getContents(), true); |
75
|
|
|
$statusCode = (int) $responseBody['status']; |
76
|
|
|
|
77
|
|
|
if ($statusCode !== 1) { |
78
|
|
|
$errors = array_pop($responseBody['errors']); |
79
|
|
|
|
80
|
|
|
throw new PurchaseFailedException($errors); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->invoice->transactionId($responseBody['token']); |
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->apiPaymentUrl . $this->invoice->getTransactionId(); |
94
|
|
|
|
95
|
|
|
return $this->redirectWithForm($url, [], 'GET'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return ReceiptInterface |
100
|
|
|
* |
101
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
102
|
|
|
* @throws \Shetabit\Multipay\Exceptions\InvalidPaymentException |
103
|
|
|
*/ |
104
|
|
|
public function verify(): ReceiptInterface |
105
|
|
|
{ |
106
|
|
|
$token = Request::get('token'); |
|
|
|
|
107
|
|
|
$paymentStatus = Request::get('payment_status'); |
|
|
|
|
108
|
|
|
$data = [ |
109
|
|
|
'api_key' => $this->settings->merchantId, |
110
|
|
|
'token' => $token |
111
|
|
|
]; |
112
|
|
|
|
113
|
|
|
if ($paymentStatus == self::PAYMENT_STATUS_FAILED ) { |
|
|
|
|
114
|
|
|
$this->notVerified('پرداخت با شکست مواجه شد.'); |
115
|
|
|
} |
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
|
|
|
$statusCode = (int) $responseBody['status']; |
132
|
|
|
|
133
|
|
|
if ($statusCode !== 1) { |
134
|
|
|
if (isset($responseBody['error'])) { |
135
|
|
|
$message = is_array($responseBody['error']) ? array_pop($responseBody['error']) : $responseBody['error']; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (isset($responseBody['errors']) and is_array($responseBody['errors'])) { |
139
|
|
|
$message = array_pop($responseBody['errors']); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$this->notVerified($message ?? ''); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $this->createReceipt($token); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Generate the payment's receipt |
150
|
|
|
* |
151
|
|
|
* @param $referenceId |
152
|
|
|
* |
153
|
|
|
* @return Receipt |
154
|
|
|
*/ |
155
|
|
|
protected function createReceipt($referenceId) |
156
|
|
|
{ |
157
|
|
|
$receipt = new Receipt('vandar', $referenceId); |
158
|
|
|
|
159
|
|
|
return $receipt; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param $message |
164
|
|
|
* @throws \Shetabit\Multipay\Exceptions\InvalidPaymentException |
165
|
|
|
*/ |
166
|
|
|
protected function notVerified($message) |
167
|
|
|
{ |
168
|
|
|
if (empty($message)) { |
169
|
|
|
throw new InvalidPaymentException('خطای ناشناخته ای رخ داده است.'); |
170
|
|
|
} else { |
171
|
|
|
throw new InvalidPaymentException($message); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.