|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Shetabit\Multipay\Drivers\Pna; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
|
7
|
|
|
use Shetabit\Multipay\Abstracts\Driver; |
|
8
|
|
|
use Shetabit\Multipay\Contracts\ReceiptInterface; |
|
9
|
|
|
use Shetabit\Multipay\Exceptions\InvalidPaymentException; |
|
10
|
|
|
use Shetabit\Multipay\Exceptions\PurchaseFailedException; |
|
11
|
|
|
use Shetabit\Multipay\Invoice; |
|
12
|
|
|
use Shetabit\Multipay\Receipt; |
|
13
|
|
|
use Shetabit\Multipay\RedirectionForm; |
|
14
|
|
|
use Shetabit\Multipay\Request; |
|
15
|
|
|
|
|
16
|
|
|
class Pna extends Driver |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Pna Client. |
|
20
|
|
|
* |
|
21
|
|
|
* @var object |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $client; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Invoice |
|
27
|
|
|
* |
|
28
|
|
|
* @var Invoice |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $invoice; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Driver settings |
|
34
|
|
|
* |
|
35
|
|
|
* @var object |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $settings; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* payment token |
|
41
|
|
|
* |
|
42
|
|
|
* @var $token |
|
|
|
|
|
|
43
|
|
|
*/ |
|
44
|
|
|
protected $token; |
|
45
|
|
|
|
|
46
|
|
|
public function __construct(Invoice $invoice, $settings) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->invoice($invoice); |
|
49
|
|
|
$this->settings = (object)$settings; |
|
50
|
|
|
$this->client = new Client(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* |
|
55
|
|
|
* @return string |
|
56
|
|
|
* |
|
57
|
|
|
* @throws PurchaseFailedException |
|
58
|
|
|
* @throws GuzzleException |
|
59
|
|
|
*/ |
|
60
|
|
|
public function purchase() |
|
61
|
|
|
{ |
|
62
|
|
|
$amount = $this->invoice->getAmount() * ($this->settings->currency == 'T' ? 10 : 1); // convert to rial |
|
63
|
|
|
if (!empty($this->invoice->getDetails()['description'])) { |
|
64
|
|
|
$description = $this->invoice->getDetails()['description']; |
|
65
|
|
|
} else { |
|
66
|
|
|
$description = $this->settings->description; |
|
67
|
|
|
} |
|
68
|
|
|
$data = [ |
|
69
|
|
|
"CorporationPin" => $this->settings->CorporationPin, |
|
70
|
|
|
"Amount" => $amount, |
|
71
|
|
|
"OrderId" => intval(1, time()).crc32($this->invoice->getUuid()), |
|
72
|
|
|
"CallBackUrl" => $this->settings->callbackUrl, |
|
73
|
|
|
"AdditionalData" => $description, |
|
74
|
|
|
]; |
|
75
|
|
|
if (!empty($this->invoice->getDetails()['mobile'])) { |
|
76
|
|
|
$data['Originator'] = $this->invoice->getDetails()['mobile']; |
|
77
|
|
|
} |
|
78
|
|
|
$response = $this->client->request('POST', |
|
79
|
|
|
$this->settings->apiNormalSale, |
|
80
|
|
|
[ |
|
81
|
|
|
'json' => $data, |
|
82
|
|
|
'headers' => [ |
|
83
|
|
|
'Content-Type' => 'application/json', |
|
84
|
|
|
], |
|
85
|
|
|
'http_errors' => false, |
|
86
|
|
|
] |
|
87
|
|
|
); |
|
88
|
|
|
$result = json_decode($response->getBody()->getContents(), true); |
|
89
|
|
|
if (isset($result['errors'])) { |
|
90
|
|
|
throw new PurchaseFailedException($result['title'] ?? 'اطلاعات وارد شده اشتباه می باشد.', $result['status'] ?? 400); |
|
91
|
|
|
} |
|
92
|
|
|
if (!isset($result['status']) || (string)$result['status'] !== '0') { |
|
93
|
|
|
throw new PurchaseFailedException($result['message'] ?? "خطای ناشناخته رخ داده است. در صورت کسر مبلغ از حساب حداکثر پس از 72 ساعت به حسابتان برمیگردد", $result['status'] ?? 400); |
|
94
|
|
|
} |
|
95
|
|
|
$this->invoice->transactionId($result['token']); |
|
96
|
|
|
|
|
97
|
|
|
return $this->invoice->getTransactionId(); |
|
98
|
|
|
} |
|
99
|
|
|
/** |
|
100
|
|
|
* Pay the Invoice |
|
101
|
|
|
* |
|
102
|
|
|
* @return RedirectionForm |
|
103
|
|
|
*/ |
|
104
|
|
|
public function pay(): RedirectionForm |
|
105
|
|
|
{ |
|
106
|
|
|
$transactionId = $this->invoice->getTransactionId(); |
|
107
|
|
|
$paymentUrl = $this->settings->apiPaymentUrl; |
|
108
|
|
|
|
|
109
|
|
|
$payUrl = $paymentUrl . $transactionId; |
|
110
|
|
|
|
|
111
|
|
|
return $this->redirectWithForm($payUrl, [], 'GET'); |
|
112
|
|
|
} |
|
113
|
|
|
/** |
|
114
|
|
|
* Verify payment |
|
115
|
|
|
* |
|
116
|
|
|
* @throws InvalidPaymentException|GuzzleException |
|
117
|
|
|
*/ |
|
118
|
|
|
public function verify(): ReceiptInterface |
|
119
|
|
|
{ |
|
120
|
|
|
$transactionId = $this->invoice->getTransactionId() ?? Request::input('Token') ?? Request::input('token'); |
|
121
|
|
|
$data = [ |
|
122
|
|
|
"CorporationPin" => $this->settings->CorporationPin, |
|
123
|
|
|
"Token"=>$transactionId |
|
124
|
|
|
]; |
|
125
|
|
|
$response = $this->client->request( |
|
126
|
|
|
'POST', |
|
127
|
|
|
$this->settings->apiConfirmationUrl, |
|
128
|
|
|
[ |
|
129
|
|
|
'json'=>$data, |
|
130
|
|
|
"headers"=>[ |
|
131
|
|
|
'Content-Type' => 'application/json', |
|
132
|
|
|
], |
|
133
|
|
|
'http_errors' => false, |
|
134
|
|
|
] |
|
135
|
|
|
); |
|
136
|
|
|
$result = json_decode($response->getBody()->getContents(), true); |
|
137
|
|
|
if (!isset($result['status']) |
|
138
|
|
|
|| ((string)$result['status'] !== '0' |
|
139
|
|
|
&& (string)$result['status'] !== '2') |
|
140
|
|
|
|| (string)$result['rrn'] === '0') { |
|
141
|
|
|
throw new InvalidPaymentException("خطای ناشناخته رخ داده است. در صورت کسر مبلغ از حساب حداکثر پس از 72 ساعت به حسابتان برمیگردد", $result['status'] ?? 400); |
|
142
|
|
|
} |
|
143
|
|
|
$refId = $result['rrn']; |
|
144
|
|
|
$receipt = new Receipt('pna', $refId); |
|
145
|
|
|
$receipt->detail([ |
|
146
|
|
|
'cardNumberMasked'=>$result['cardNumberMasked'], |
|
147
|
|
|
'token'=>$result['token'], |
|
148
|
|
|
]); |
|
149
|
|
|
return $receipt; |
|
150
|
|
|
} |
|
151
|
|
|
} |