1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
<?php
class Espay extends AbstractPaymentGateway {
public $key; protected $password; protected $signaturekey; protected $invoice; protected $payment_method = 'ESPAY';
public $signature_js; protected $signature_js_sandbox = "https://sandbox-kit.espay.id/public/signature/js"; protected $signature_js_production = "https://kit.espay.id/public/signature/js";
private $bypassValidation = false;
protected $rest_merchantinfo_url = "http://116.90.162.170:10809/rest/merchant/merchantinfo";
public function __construct() { // Your Credentials provided by Espay Team $this->password = MasterSetting::get('ESPAY_PASSWORD'); $this->signaturekey = MasterSetting::get('ESPAY_SIGNATUREKEY'); $this->key = MasterSetting::get('ESPAY_KEY');
$this->signature_js = $this->signature_js_production; $this->payment_currency = "IDR"; // or USD }
public function sandbox() { // $this->bypassValidation = true; $this->signature_js = $this->signature_js_sandbox; return $this; }
// public function setInvoice(Invoice $invoice) // { // $this->invoice = $invoice; // }
public function paymentParam($args) { // if (!$this->invoice->id) { // return ['error' => 'Invoice Not Found']; // } // vdump($this->invoice->id, $args['invoice_id']); global $site_info; return [ 'paymentId' => $this->invoice->id ?: $args['invoice_id'], 'key' => $this->key, 'backUrl' => "{$site_info['url']}/espay/ConfirmOrder.php", // 'display' => 'select', // select, tab, radio 'bankCode' => '008', 'bankProduct' => 'MANDIRIIB', ]; }
protected function generateCustomizedData($payment_method) { $data = []; return $data; }
public function getMerchantinfo() { $products_json = self::_excute($this->rest_merchantinfo_url, "key={$this->key}"); $products = json_decode($products_json); return $products; }
public function paymentVerify($data, &$messages = array()) { // maybe move validate password, signature, invoice here }
// the string will have to be converted to UPPERCASE before hashing is done. protected function sign($arg) { return hash('sha256', strtoupper($arg)); }
protected function validatePassword($password) { // vdump("pw = {$this->password}", $password); if ($this->bypassValidation) { return true; } return ($password == $this->password); }
protected function validateSignature($data, $signature) { // vdump("data = {$data}", $this->sign($data)); if ($this->bypassValidation) { return true; } return ($signature == $this->sign($data)); }
protected function processInquiry($request) { $signatureFromEspay = (!empty($request['signature']) ? $request['signature'] : ''); $rq_datetime = (!empty($request['rq_datetime']) ? $request['rq_datetime'] : ''); $order_id = (!empty($request['order_id']) ? $request['order_id'] : ''); $passwordServer = (!empty($request['password']) ? $request['password'] : '');
$data = "##{$this->signaturekey}##{$rq_datetime}##{$order_id}##INQUIRY##";
if (!$this->validatePassword($passwordServer)) { $response = '1;Merchant Failed to Identified;;;;;'; return $response; }
if (!$this->validateSignature($data, $signatureFromEspay)) { $response = '1;Invalid Signature Key;;;;;'; return $response; }
$invoice = Invoice::find($order_id); if (!$invoice->id) { $response = '1;Order Id Does Not Exist;;;;;'; // if order id not exist show plain reponse return $response; }
// if order id truly exist get order detail from database // and give the response, format: error_code;error_message;order_id;amount;ccy;description;trx_date // convert amount to IDR $amount = get_currency_price($invoice->amount, null, $this->payment_currency, 3); // vdump($invoice);
if (count($invoice->invoiceItems) == 1) { $payment_description = "[Buyer]: {$invoice->customer->fullname('en')}. <br/>[Seller]: {$invoice->invoiceItems->first()->invoiceable->orderItem->supplier->name_en}. <br/>[Order]: {$invoice->code}"; } else { $payment_description = "Payment Order {$invoice->code} by {$invoice->customer->fullname('en')}";
}
// show response // see TSD for more detail // required to change tx time to indonesia timezone? $response = '0;Success;' . $order_id . ';' . $amount . ';' . $this->payment_currency . ';' . str_replace(';', '.', $payment_description) . ';' . date('Y/m/d H:i:s') . ''; return $response; }
protected function processPaymentReport($request) { $signatureFromEspay = (!empty($request['signature']) ? $request['signature'] : ''); $rq_datetime = (!empty($request['rq_datetime']) ? $request['rq_datetime'] : ''); $member_id = (!empty($request['member_id']) ? $request['member_id'] : ''); $order_id = (!empty($request['order_id']) ? $request['order_id'] : ''); $passwordServer = (!empty($request['password']) ? $request['password'] : ''); $debit_from = (!empty($request['debit_from']) ? $request['debit_from'] : ''); $credit_to = (!empty($request['credit_to']) ? $request['credit_to'] : ''); $product = (!empty($request['product_code']) ? $request['product_code'] : ''); $paidAmount = (!empty($request['amount']) ? $request['amount'] : ''); $paymentfee = (!empty($request['payment_fee']) ? $request['payment_fee'] : 0); $payment_ref = (!empty($request['payment_ref']) ? $request['payment_ref'] : '');
$data = "##{$this->signaturekey}##{$rq_datetime}##{$order_id}##PAYMENTREPORT##"; // exit("data = {$data}");
if (!$this->validatePassword($passwordServer)) { $response = '1;Merchant Failed to Identified;;;;;'; return $response; }
if (!$this->validateSignature($data, $signatureFromEspay)) { $response = '1;Invalid Signature Key;;;;;'; return $response; }
$invoice = Invoice::find($order_id); if (!$invoice->id) { $response = '1;Order Id Does Not Exist;;;;;'; // if order id not exist show plain reponse return $response; } // vdump($invoice);
$paymentRecord = PaymentRecord::create([ 'payment_type' => 'ESPAY', 'payment_method' => $this->payment_method, 'code' => $payment_ref, 'session_id' => session_id(), 'order_id' => $invoice->order_id, 'invoice_id' => $invoice->id, 'payable_type' => 'Invoice', 'payable_id' => $invoice->id, 'currency_code' => $request['ccy'], 'currency_rate' => get_currency_rate(0, $request['ccy'])['rate'] ?: 1, 'amount' => $paidAmount, 'status' => 'PAID', 'nvpstr' => json_encode($request), 'nvpstr2' => $request['message'], 'paystatus' => 1, ]);
// Flag your transaction to be success here // same as paymentVerify() // update order, invoice and payment status try { $postData = [ 'currency_code' => $this->payment_currency, 'currency_rate' => get_currency_rate(false, $this->payment_currency)['rate'], ]; $invoiceConfirmResponse = $invoice->confirm($postData); $response = '0,Success,' . $paymentRecord->id . ',' . $order_id . ',' . date('Y-m-d H:i:s') . ''; } catch (OrderNumberException $e) { vdump('error: ' . $e->getMessage()); $response = '1;Unable to complete the order ' . $e->getMessage() . ';;;;;'; // } catch (InvoiceAlreadyCompletedException $e){ http_response_code(406); // not acceptable $response = '1;Unable to complete the order ' . $e->getMessage() . ';;;;;'; // } // dq(1,1);
// if done, show the response, format: success_flag,error message,reconcile_id, order_id,reconcile_datetime return $response; }
// http://localhost/b2b2c/html/espay/api.php?action=INQUIRY // http://localhost/b2b2c/html/espay/api.php?action=PAYMENTREPORT public function processNotification($request) { $apiLog = ApiLog::create([ 'type' => get_class($this), 'endpoint' => "{$_SERVER['REQUEST_URI']}", 'request' => $request, // 'response' => $response, // 'status' => substr($response, 0, 1), 'desc_en' => json_encode($_SERVER, JSON_PRETTY_PRINT), ]);
switch ($request['action']) { case 'INQUIRY': $response = $this->processInquiry($request); break;
case 'PAYMENTREPORT': $response = $this->processPaymentReport($request); break;
default: $response = "1,Unexpected API: {$request['action']}"; // exit("Unexpected API: {$request['action']}"); break; }
$apiLog->fill([ 'response' => $response, 'status' => substr($response, 0, 1), ])->save();
return $response; }
protected static function _excute($endpoint, $param) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $endpoint); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
curl_setopt($ch, CURLOPT_HEADER, false); //curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//NOTE: skip SSL certificate verification (this allows sending request to hosts with self signed certificates, but reduces security) curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//enable ssl version 3 //this is added because mandiri ecash case that ssl version that have been not supported before curl_setopt($ch, CURLOPT_SSLVERSION, 1);
curl_setopt($ch, CURLOPT_VERBOSE, true); //save to temporary file (php built in stream), cannot save to php://memory // $verbose = fopen('php://temp', 'rw+'); // curl_setopt($ch, CURLOPT_STDERR, $verbose);
// curl_setopt($ch, CURLOPT_POSTFIELDS, $request); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); //use http 1.1 $output = curl_exec($ch); curl_close($ch);
// $output = self::_xml_pre_process($output);
ApiLog::create([ 'type' => get_class($this), 'endpoint' => $endpoint, 'request' => $param, 'response' => $output, // 'status' => $output_obj->status ?: $output_obj->pstatus, ]);
return $output; }
// public static function resolve_pstatus($v_pstatus, $translation_fn=false){ // switch ($v_pstatus) { // case '': // case '0': // $description = "Not paid"; // break; // case '1': // case '20': // $description = "Paid"; // break; // case '2': // $description = "Not confirmed Payment"; // break; // case '3': // case '30': // $description = "Rejected Payment"; // break; // case '4': // $description = "Refund in progress"; // break; // case '5': // $description = "Refund is completed"; // break; // case '6': // $description = "Fail to refund"; // break; // case '7': // $description = "Card holder rejected"; // break; // case '8': // $description = "Abnormal refund"; // break; // default: // $description = "Invalid"; // break; // } // return is_callable($translation_fn) ? call_user_func($translation_fn, $description) : $description; // }
}
|