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
|
<?php /** * Finnet Finpay T-Cash * Refer to: Finpay-TCASH Integration Doc v.02.pdf * @author Garrick Lam */
class FinpayTCash extends Finpay { protected $payment_method = 'FINPAY_TCASH';
public $getLink_url; protected $getLink_url_sandbox = "https://hsact.finnet-indonesia.com/dev/tcash.php"; protected $getLink_url_production = "https://hsact.finnet-indonesia.com/prod/tcash.php"; // protected $getLink_url_production = "http://localhost/echo.php";
const PAYMENT_FAILED = '11';
public function __construct() { // Your Credentials provided by Finnet $this->merchant_id = MasterSetting::get('FINPAY_TCASH_MERCHANT_ID'); $this->password = MasterSetting::get('FINPAY_TCASH_PASSWORD');
$this->getLink_url = $this->getLink_url_production; $this->payment_currency = "IDR"; // or USD }
public function sandbox() { $isSandbox = true; // $this->bypassValidation = true; $this->getLink_url = $this->getLink_url_sandbox; return $this; }
/** * 3.1. * Initiate transaction to Get Link, Member & Non Member with 3DS support, Member & Returning Customer with non 3DS support Client Side */ public function paymentParam($payment_method) { global $site_info;
$args = [ 'merchant_id' => $this->merchant_id, 'invoice' => $this->invoice->id, 'amount' => number_format(get_currency_price($this->invoice->amount, null, $this->payment_currency, 3), 2, '.', ''), // 'amount' => number_format($v_amount, 2, '.', ''), 'return_url' => "{$site_info['url']}/finpay/api.php?payment_method={$this->payment_method}", // server-to-server response ];
$args = array_merge($args, $this->generateCustomizedData($args)); $args = array_merge($args, $this->generateSignature($args));
ApiLog::create([ 'type' => get_class($this), 'endpoint' => $this->getLink_url, 'request' => $args, ]);
return [ "endpoint" => $this->getLink_url, "method" => "POST", "args" => $args, ];
}
public function getLink() { $param = $this->paymentParam(); // vdump($param['endpoint'], $param['args']); //exit; $result = $this->_excute($param['endpoint'], $param['args']);
if (preg_match("/^\s*0[1|2]/", $result, $matches)) { // error // var_dump($matches[0]); throw new Exception("Error Processing TCash Response: {$result}", 1); // var_dump($result); } else { // success html return $result; } }
protected function generateCustomizedData($ar) { $data = [];
// iterate invoice.invoiceItems foreach ($this->invoice->invoiceItems as $invoiceItem) { // vdump($invoiceItem); $data['pname'][] = $invoiceItem->invoiceable->orderItem->proProductSku->getProductName('en'); $data['price'][] = (int) get_currency_price(round($invoiceItem->discounted_price / $invoiceItem->quantity, 2), null, $this->payment_currency, 3); // IDR $data['qty'][] = (int) $invoiceItem->quantity; }
return $data; }
protected function generateSignature($data) { // ksort($data);
// concat data to a string $data_str = ''; foreach ($data as $key => $param) { if (!in_array($key, ['payment_method', 'mer_signature']) && $param) { $data_str .= "{$param}%"; } } // var_dump($data_str); return [ "mer_signature" => $this->sign($data_str), ]; }
protected function validateSignature($data) { if ($this->bypassValidation) { return true; }
$expected_signature = $this->generateSignature($data); // var_dump(__FUNCTION__, "expected:{$expected_signature['mer_signature']}, actual:{$data['mer_signature']}"); return ($data['mer_signature'] == $expected_signature['mer_signature']); }
protected function processPaymentNotification($request) { // var_dump('FinpayTCash::processPaymentNotification');
// finpay/api.php?payment_method=FINPAY_TCASH&invoice=100&ref_no=thisismyref&result_code=00&result_desc=success&mer_signature=646f77c16130aec55a5896918153ad415fb6c90eece1ad16413180a4230e426b&merchant_id=PAYEASE
if ($request['result_code'] != self::SUCCESS) { $response = $request['result_desc'] ?: "Unable to complete the order"; http_response_code(402); // payment required return $response; }
if (!$this->validateSignature($request)) { $response = self::INVALID_SIGNATURE; http_response_code(406); // not acceptable return $response; } // exit('signature ok');
$invoice = Invoice::find($request['invoice']); if (!$invoice->id) { $response = 'Invoice Does Not Exist'; // if order id not exist show plain reponse http_response_code(406); // not acceptable return $response; } // vdump($invoice->id, $request); exit; $paymentRecord = PaymentRecord::create([ 'payment_type' => 'FINPAY', 'payment_method' => $request['payment_method'], 'code' => $request['ref_no'], 'session_id' => session_id(), 'order_id' => $invoice->order_id, 'invoice_id' => $invoice->id, 'payable_type' => 'Invoice', 'payable_id' => $invoice->id, 'currency_code' => $this->payment_currency, 'currency_rate' => get_currency_rate(false, $this->payment_currency)['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 = self::SUCCESS; } catch (OrderNumberException $e) { http_response_code(406); // not acceptable $response = 'Unable to complete the order ' . $e->getMessage() . ''; // vdump('error: ' . $e->getMessage()); } catch (InvoiceAlreadyCompletedException $e){ http_response_code(406); // not acceptable $response = 'Error: ' . $e->getMessage() . ''; } // dq(1,1);
return $response; } }
|