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
|
<?php /** * Finnet Finpay Code * Refer to: FinPay Code Integration Guide v.1.12.pdf * @author Garrick Lam */
class FinpayCode extends Finpay { protected $payment_method = 'FINPAY_CODE';
public $gateway_url; protected $gateway_url_production = [ 'PAYMENT_CODE_195' => "https://billhosting.finnet-indonesia.com/prepaidsystem/195/response-insert-ex.php", 'PAYMENT_CODE_20111' => "https://billhosting.finnet-indonesia.com/prepaidsystem/195/va-request-02111.php", 'VA_PERMATA_NET' => "https://billhosting.finnet-indonesia.com/prepaidsystem/195/va-pnet.php", 'PAYMENT_STATUS' => "https://billhosting.finnet-indonesia.com/prepaidsystem/195/check-status-ex.php", 'CANCEL_TRANSACTION' => "https://billhosting.finnet-indonesia.com/prepaidsystem/195/cancel-transaction-ex.php", 'CHANGE_TRANSACTION' => "https://billhosting.finnet-indonesia.com/prepaidsystem/195/change-transaction.php", ]; protected $gateway_url_sandbox = [ 'PAYMENT_CODE_195' => "https://sandbox.finpay.co.id/servicescode/response-insert-ex.php", 'PAYMENT_CODE_20111' => "https://sandbox.finpay.co.id/servicescode/va-request-02111.php", 'VA_PERMATA_NET' => "https://sandbox.finpay.co.id/servicescode/va-pnet.php", 'PAYMENT_STATUS' => "https://sandbox.finpay.co.id/servicescode/check-status-ex.php", 'CANCEL_TRANSACTION' => "https://sandbox.finpay.co.id/servicescode/cancel-transaction-ex.php", 'CHANGE_TRANSACTION' => "https://sandbox.finpay.co.id/servicescode/change-transaction.php", ];
protected $timeout = 24 * 60; //1 day protected $bypassValidation = false;
// public function __construct() // { // // Your Credentials provided by Finnet // $this->merchant_id = MasterSetting::get('FINPAY_MERCHANT_ID'); // $this->password = MasterSetting::get('FINPAY_PASSWORD'); // // FINPAY_VAPERMATA_MERCHANT_ID // // FINPAY_VAPERMATA_PASSWORD // // FINPAY_TCASH_MERCHANT_ID // // FINPAY_TCASH_PASSWORD // // FINPAY_VA_MERCHANT_ID // // FINPAY_VA_PASSWORD // // FINPAY_CC_MERCHANT_ID // // FINPAY_CC_PASSWORD
// $this->gateway_url = $this->gateway_url_production; // $this->payment_currency = "IDR"; // or USD // }
// public function sandbox() // { // // $this->bypassValidation = true; // $this->gateway_url = $this->gateway_url_sandbox; // return $this; // }
public function getCode($payment_method) { $param = $this->paymentParam($payment_method); $result = $this->_excute($param['endpoint'], $param['args']); parse_str(trim($result), $result); // vdump($param['endpoint'], $param['args'], $result); exit;
$success = true; if (!$this->validateSignature($result)) { throw new Exception("Error Processing Response Signature", 1); $success = false; }
if ($success) { // url arguemnt to array return $result['payment_code'];
// write a record to PaymentRecord with status = 0 }
return false; }
public function paymentParam($payment_method) { global $site_info;
if ($payment_method && array_key_exists($payment_method, $this->gateway_url)) { $endpoint = $this->gateway_url[$payment_method]; } else { throw new Exception("Error Processing Payment Method [{$payment_method}]", 1); }
$args = [ 'merchant_id' => $this->merchant_id, 'invoice' => $this->invoice->code, 'amount' => get_currency_price($this->invoice->amount, null, $this->payment_currency, 3), 'add_info1' => $args['add_info1'] ?: "Silkroad ebelt", 'add_info2' => $args['add_info2'] ?: "", 'add_info3' => $args['add_info3'] ?: "", 'add_info4' => $args['add_info4'] ?: "", 'add_info5' => $args['add_info5'] ?: "", 'timeout' => $this->timeout, 'return_url' => "{$site_info['url']}/finpay/api.php?payment_method={$payment_method}", ];
$args = array_merge($args, $this->generateCustomizedData($args, $payment_method)); $args = array_merge($args, $this->generateSignature($args));
ApiLog::create([ 'type' => get_class($this), 'endpoint' => $endpoint, 'request' => $args, ]);
return [ "endpoint" => $endpoint, "method" => "POST", "args" => $args, ];
}
protected function generateCustomizedData($args, $payment_method) { global $site_info;
$data = [];
if (in_array($payment_method, [/*'VA_PERMATA',*/'VA_PERMATA_NET'])) { $data['success_page'] = "{$site_info['url']}/finpay/ConfirmOrder.php"; // Only for permatanet and not being part of signature component Merchant’s page success url $data['failed_page'] = "{$site_info['url']}/confirm_order.php?fail=1&payment_method={$payment_method}"; // Only for permatanet and not being part of signature component Merchant’s page }
return $data; }
/** * 1. Parameter sent * 2. Join the string : 0843%2319AB9323%10000%IRA%Info3%Info4% Info5%30%http://www.merchant.com/get_response.php * 3. Uppercase the string : 0843%2319AB9323%10000%IRA%INFO3%INFO4% INFO5%30%HTTP://WWW.MERCHANT.COM/GET_RESPONSE.PHP * 4. Add password (without uppercase), for example the password is “kata-Andi” : 0843%2319AB9323%10000%IRA%INFO3%INFO4% INFO5%30%HTTP://WWW.MERCHANT.COM/GET_RESPONSE.PHP%kata-Andi * 5. Hashing 256, so the final result is : FE053B4EC17E09A78C4CF6BBBC9CDDE1D0880C0E2ACF88326335B271B3C71E04 * Note : If the parameter’s value is null, it’s unnecessary to put into hashing part. * @param array $data arguemnts in request for generate signature hash * @return array signature hash in an array */ protected function generateSignature($data) { // $arg = array_filter($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}%"; } }
return [ "mer_signature" => $this->sign($data_str), ]; }
protected function validateSignature($data) { // vdump($data, $this->sign($data)); exit; if ($this->bypassValidation) { return true; }
// concat data to a string $data_str = ''; foreach ($data as $key => $param) { if (!in_array($key, ['payment_method', 'mer_signature'])) { $data_str .= "{$param}%"; } }
$expected_signature = $this->sign($data_str); // var_dump(__FUNCTION__, "expected:{$expected_signature}, actual: {$data['mer_signature']}", $data_str, $data); exit; return ($data['mer_signature'] == $this->sign($data_str)); }
public function processNotification($request) { switch ($request['result_code']) { case self::SUCCESS: $response = $this->processPaymentNotification($request); break;
case self::EXPIRED: $response = $this->processExpiredCodeNotification($request); break;
default: $response = "Unexpected Result Code: {$request['result_code']}"; break; } }
// 7.4. Response – payment notification : notification sent to merchant’s return url when the payment code is already paid by customer protected function processPaymentNotification($request) { // finpay/api.php?payment_method=FINPAYCODE&mer_signature=8b207bed9ce881f648e7fd517d6f8af17a77fb45bd89579514e58b622cebbed4&trax_type=Payment&merchant_id=PAYEA509&invoice=STIDN20170117121713-D282&payment_code=021113013364&result_code=00&result_desc=Success&log_no=4189554521&payment_source=webdev
if ($request['result_code'] != self::SUCCESS) { $response = $result['result_desc'] ?: "Unable to complete the order"; return $response; }
if (!$this->validateSignature($request)) { $response = self::INVALID_SIGNATURE; // Invalid Signature return $response; }
// $invoice = Invoice::find($request['invoice']); $invoice = Invoice::where('code', $request['invoice'])->first(); if (!$invoice->id) { $response = 'Invoice Does Not Exist'; // if order id not exist show plain reponse return $response; } // vdump($invoice->id, $request);
$paymentRecord = PaymentRecord::create([ 'payment_type' => 'FINPAY', 'payment_method' => $request['payment_method'], 'code' => $request['log_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) { vdump('error: ' . $e->getMessage()); $response = 'Unable to complete the order ' . $e->getMessage() . ''; // } catch (InvoiceAlreadyCompletedException $e){ http_response_code(406); // not acceptable $response = 'Unable to complete the order ' . $e->getMessage() . ''; // } // dq(1,1);
return $response; }
// 7.3. Response – expired code notification : notification sent to merchant’s return url when the payment code is expired protected function processExpiredCodeNotification($request) { $response = self::SUCCESS;
return $response; } }
|