1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Shetabit\Multipay\Drivers\Gooyapay; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Shetabit\Multipay\Abstracts\Driver; |
7
|
|
|
use Shetabit\Multipay\Exceptions\InvalidPaymentException; |
8
|
|
|
use Shetabit\Multipay\Exceptions\PurchaseFailedException; |
9
|
|
|
use Shetabit\Multipay\Contracts\ReceiptInterface; |
10
|
|
|
use Shetabit\Multipay\Invoice; |
11
|
|
|
use Shetabit\Multipay\Receipt; |
12
|
|
|
use Shetabit\Multipay\RedirectionForm; |
13
|
|
|
use Shetabit\Multipay\Request; |
14
|
|
|
|
15
|
|
|
class Gooyapay extends Driver |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Gooyapay 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
|
|
|
/** |
39
|
|
|
* Gooyapay constructor. |
40
|
|
|
* Construct the class with the relevant settings. |
41
|
|
|
* |
42
|
|
|
* @param Invoice $invoice |
43
|
|
|
* @param $settings |
44
|
|
|
*/ |
45
|
|
|
public function __construct(Invoice $invoice, $settings) |
46
|
|
|
{ |
47
|
|
|
$this->invoice($invoice); |
48
|
|
|
$this->settings = (object) $settings; |
49
|
|
|
$this->client = new Client(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Purchase Invoice. |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
* |
57
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
58
|
|
|
*/ |
59
|
|
|
public function purchase() |
60
|
|
|
{ |
61
|
|
|
$details = $this->invoice->getDetails(); |
62
|
|
|
|
63
|
|
|
$name = ''; |
64
|
|
|
if (!empty($details['name'])) { |
65
|
|
|
$name = $details['name']; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$mobile = ''; |
69
|
|
|
if (!empty($details['mobile'])) { |
70
|
|
|
$mobile = $details['mobile']; |
71
|
|
|
} elseif (!empty($details['phone'])) { |
72
|
|
|
$mobile = $details['phone']; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$email = ''; |
76
|
|
|
if (!empty($details['mail'])) { |
77
|
|
|
$email = $details['mail']; |
78
|
|
|
} elseif (!empty($details['email'])) { |
79
|
|
|
$email = $details['email']; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$desc = ''; |
83
|
|
|
if (!empty($details['desc'])) { |
84
|
|
|
$desc = $details['desc']; |
85
|
|
|
} elseif (!empty($details['description'])) { |
86
|
|
|
$desc = $details['description']; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$amount = $this->invoice->getAmount(); |
90
|
|
|
if ($this->settings->currency != 'T') { |
91
|
|
|
$amount /= 10; |
92
|
|
|
} |
93
|
|
|
$amount = intval(ceil($amount)); |
94
|
|
|
|
95
|
|
|
$orderId = crc32($this->invoice->getUuid()); |
96
|
|
|
if (!empty($details['orderId'])) { |
97
|
|
|
$orderId = $details['orderId']; |
98
|
|
|
} elseif (!empty($details['order_id'])) { |
99
|
|
|
$orderId = $details['order_id']; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$data = array( |
103
|
|
|
"MerchantID" => $this->settings->merchantId, |
104
|
|
|
"Amount" => $amount, |
105
|
|
|
"InvoiceID" => $orderId, |
106
|
|
|
"Description" => $desc, |
107
|
|
|
"FullName" => $name, |
108
|
|
|
"Email" => $email, |
109
|
|
|
"Mobile" => $mobile, |
110
|
|
|
"CallbackURL" => $this->settings->callbackUrl, |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
$response = $this->client->request('POST', $this->settings->apiPurchaseUrl, ["json" => $data, "http_errors" => false]); |
114
|
|
|
|
115
|
|
|
$body = json_decode($response->getBody()->getContents(), false); |
116
|
|
|
|
117
|
|
|
if ($body->Status != 100) { |
118
|
|
|
// some error has happened |
119
|
|
|
throw new PurchaseFailedException($body->Message); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$this->invoice->transactionId($body->Authority); |
123
|
|
|
|
124
|
|
|
// return the transaction's id |
125
|
|
|
return $this->invoice->getTransactionId(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Pay the Invoice |
130
|
|
|
* |
131
|
|
|
* @return RedirectionForm |
132
|
|
|
*/ |
133
|
|
|
public function pay() : RedirectionForm |
134
|
|
|
{ |
135
|
|
|
$payUrl = $this->settings->apiPaymentUrl.$this->invoice->getTransactionId(); |
136
|
|
|
|
137
|
|
|
return $this->redirectWithForm($payUrl, [], 'GET'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Verify payment |
142
|
|
|
* |
143
|
|
|
* @return mixed|void |
144
|
|
|
* |
145
|
|
|
* @throws InvalidPaymentException |
146
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
147
|
|
|
*/ |
148
|
|
|
public function verify() : ReceiptInterface |
149
|
|
|
{ |
150
|
|
|
$PaymentStatus = Request::input('PaymentStatus'); |
151
|
|
|
if ($PaymentStatus != 'OK') { |
152
|
|
|
throw new InvalidPaymentException('تراکنش از سوی کاربر لغو شد', $PaymentStatus); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$Authority = Request::input('Authority'); |
156
|
|
|
$InvoiceID = Request::input('InvoiceID'); |
157
|
|
|
|
158
|
|
|
if ($Authority != $this->invoice->getTransactionId()) { |
159
|
|
|
throw new InvalidPaymentException('اطلاعات تراکنش دریافتی با صورتحساب همخوانی ندارد', 'DATAMISMATCH'); |
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$amount = $this->invoice->getAmount(); |
163
|
|
|
if ($this->settings->currency != 'T') { |
164
|
|
|
$amount /= 10; |
165
|
|
|
} |
166
|
|
|
$amount = intval(ceil($amount)); |
167
|
|
|
|
168
|
|
|
//start verfication |
169
|
|
|
$data = array( |
170
|
|
|
"MerchantID" => $this->settings->merchantId, |
171
|
|
|
"Authority" => $Authority, |
172
|
|
|
"Amount" => $amount, |
173
|
|
|
); |
174
|
|
|
|
175
|
|
|
$response = $this->client->request('POST', $this->settings->apiVerificationUrl, ["json" => $data, "http_errors" => false]); |
176
|
|
|
|
177
|
|
|
$body = json_decode($response->getBody()->getContents(), false); |
178
|
|
|
|
179
|
|
|
if ($body->Status != 100) { |
180
|
|
|
throw new InvalidPaymentException($body->Message, $body->Status); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$receipt = new Receipt('gooyapay', $body->RefID); |
184
|
|
|
|
185
|
|
|
$receipt->detail([ |
186
|
|
|
'Authority' => $data['Authority'], |
187
|
|
|
'InvoiceID1' => $InvoiceID, |
188
|
|
|
'InvoiceID2' => $body->InvoiceID, |
189
|
|
|
'Amount1' => $data['Amount'], |
190
|
|
|
'Amount2' => $body->Amount, |
191
|
|
|
'CardNumber' => $body->MaskCardNumber, |
192
|
|
|
'PaymentTime' => $body->PaymentTime, |
193
|
|
|
'PaymenterIP' => $body->BuyerIP |
194
|
|
|
]); |
195
|
|
|
|
196
|
|
|
return $receipt; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|