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