1
|
|
|
<?php |
2
|
|
|
namespace Shetabit\Multipay\Drivers\Atipay; |
3
|
|
|
|
4
|
|
|
use GuzzleHttp\Client; |
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
|
|
|
require_once 'Core/fn.atipay.php'; |
15
|
|
|
|
16
|
|
|
class Atipay extends Driver |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Atipay 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
|
|
|
* Atipay constructor. |
41
|
|
|
* Construct the class with the relevant settings. |
42
|
|
|
* |
43
|
|
|
* @param Invoice $invoice |
44
|
|
|
* @param $settings |
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
|
|
|
* Retrieve data from details using its name. |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
private function extractDetails($name) |
59
|
|
|
{ |
60
|
|
|
return empty($this->invoice->getDetails()[$name]) ? null : $this->invoice->getDetails()[$name]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Purchase Invoice. |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
* |
68
|
|
|
* @throws PurchaseFailedException |
69
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
70
|
|
|
*/ |
71
|
|
|
public function purchase() |
72
|
|
|
{ |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
$amount = $this->invoice->getAmount(); |
76
|
|
|
if ($this->settings->currency == 'T'){ //convert amount to rial, payment gateways need rial |
77
|
|
|
$amount = $amount * 10; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$order_id = $this->invoice->getUuid(); |
81
|
|
|
$mobile = $this->extractDetails('mobile'); |
82
|
|
|
$description = $this->extractDetails('description'); |
|
|
|
|
83
|
|
|
$apikey = $this->settings->apikey; |
84
|
|
|
$redirectUrl = $this->settings->callbackUrl; |
85
|
|
|
|
86
|
|
|
$token_params = array('apiKey'=>$apikey, |
87
|
|
|
'redirectUrl'=>$redirectUrl, |
88
|
|
|
'invoiceNumber'=>$order_id, |
89
|
|
|
'amount'=>$amount, |
90
|
|
|
'cellNumber'=>$mobile, |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
$r = fn_atipay_get_token($token_params); |
94
|
|
|
if ($r['success'] == 1) { |
95
|
|
|
$token = $r['token']; |
96
|
|
|
$this->invoice->transactionId($token); |
97
|
|
|
return $this->invoice->getTransactionId(); |
98
|
|
|
}else{ |
99
|
|
|
$error_message = $r['errorMessage']; |
100
|
|
|
throw new PurchaseFailedException($error_message); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Pay the Invoice |
107
|
|
|
* |
108
|
|
|
* @return RedirectionForm |
109
|
|
|
*/ |
110
|
|
|
public function pay(): RedirectionForm |
111
|
|
|
{ |
112
|
|
|
$token = $this->invoice->getTransactionId(); |
113
|
|
|
$payUrl = $this->settings->atipayRedirectGatewayUrl; |
114
|
|
|
return $this->redirectWithForm($payUrl, ['token'=>$token], 'POST'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Verify payment |
119
|
|
|
* |
120
|
|
|
* @return ReceiptInterface |
121
|
|
|
* |
122
|
|
|
* @throws InvalidPaymentException |
123
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
124
|
|
|
*/ |
125
|
|
|
public function verify(): ReceiptInterface |
126
|
|
|
{ |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
|
130
|
|
|
$result = fn_check_callback_data($params); |
|
|
|
|
131
|
|
|
$payment_id = $params['reservationNumber']; |
|
|
|
|
132
|
|
|
if ($result['success'] == 1) { //will verify here |
133
|
|
|
$apiKey = $this->setting->apikey; |
|
|
|
|
134
|
|
|
$amount = $this->invoice->getAmount(); |
135
|
|
|
if ($this->settings->currency == 'T'){ //convert amount to rial, payment gateways need rial |
136
|
|
|
$amount = $amount * 10; |
137
|
|
|
} |
138
|
|
|
$verify_params = array('apiKey' => $apiKey, |
139
|
|
|
'referenceNumber' => $params['referenceNumber'] |
140
|
|
|
); |
141
|
|
|
|
142
|
|
|
$r = fn_atipay_verify_payment($verify_params, $amount); |
143
|
|
|
if ($r['success'] == 0) { //veriy failed |
144
|
|
|
$error_message = $r['errorMessage']; |
145
|
|
|
throw new InvalidPaymentException($error_message); |
146
|
|
|
} else { //success |
147
|
|
|
$receipt = $this->createReceipt($data['referenceNumber']); |
|
|
|
|
148
|
|
|
$receipt->detail([ |
149
|
|
|
'referenceNo' => $params['referenceNumber'], |
150
|
|
|
'rrn' => Request::input('rrn'), |
151
|
|
|
'pan' => $params['maskedPan'] |
152
|
|
|
]); |
153
|
|
|
|
154
|
|
|
return $receipt; |
155
|
|
|
} |
156
|
|
|
} else { |
157
|
|
|
$error_message = $result['error']; |
158
|
|
|
throw new InvalidPaymentException($error_message); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
return $this->createReceipt($body['transId']); |
|
|
|
|
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Generate the payment's receipt |
167
|
|
|
* |
168
|
|
|
* @param $referenceId |
169
|
|
|
* |
170
|
|
|
* @return Receipt |
171
|
|
|
*/ |
172
|
|
|
protected function createReceipt($referenceId) |
173
|
|
|
{ |
174
|
|
|
$receipt = new Receipt('Atipay', $referenceId); |
175
|
|
|
|
176
|
|
|
return $receipt; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
} |
181
|
|
|
?> |
|
|
|
|
182
|
|
|
|