/var/www/hkosl.com/littleark/webadmin/models/FinpayCreditCard.php


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
<?php
/**
 * Finnet Finpay Credit Card
 * Refer to: Credit Card API - Merchant Integration Guide V.1.0.docx
 * @author Garrick Lam
 */

class FinpayCreditCard extends Finpay
{
    protected 
$payment_method 'FINPAY_CREDITCARD';

    public 
$getLink_url;
    protected 
$getLink_url_sandbox    "https://finpaydev.finnet-indonesia.com/payment/getLink.php";
    protected 
$getLink_url_production "https://finpaydev.finnet-indonesia.com/payment/getLink.php";

    
// public $payment_url;
    // protected $payment_url_sandbox    = "https://finpaydev.finnet-indonesia.com/payment/form/redirectLink.php?access=";
    // protected $payment_url_production = "https://finpaydev.finnet-indonesia.com/payment/form/redirectLink.php?access=";

    // public $transaction_url;
    // protected $transaction_url_sandbox    = "https://finpaydev.finnet-indonesia.com/transaction/TransactionMain.do";
    // protected $transaction_url_production = "https://finpay.finnet-indonesia.com/transaction/TransactionMain.do";

    
public function __construct()
    {
        
// Your Credentials provided by Finnet
        
$this->merchant_id MasterSetting::get('FINPAY_CC_MERCHANT_ID');
        
$this->password    MasterSetting::get('FINPAY_CC_PASSWORD');

        
$this->payment_url      $this->payment_url_production;
        
$this->getlink_url      $this->getlink_url_production;
        
$this->transaction_url  $this->transaction_url_production;
        
$this->payment_currency "IDR"// or USD
    
}

    public function 
sandbox()
    {
        
$isSandbox true;
        
// $this->bypassValidation   = true;
        
$this->payment_url     $this->payment_url_sandbox;
        
$this->getLink_url     $this->getLink_url_sandbox;
        
$this->transaction_url $this->transaction_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='')
    {
        
// vdump(__FUNCTION__);
        
global $site_info;

        
$endpoint $this->getLink_url;
        
$customer $this->invoice->customer->decrypt();

        
$args = [
            
'merchant_id'    => $this->merchant_id,
            
'cust_id'        => $customer->id,
            
'cust_msisdn'    => $customer->tel,
            
'cust_name'      => trim(preg_replace("/[^a-zA-Z ]/"""$customer->fullname('en')) ?: 'Customer'),
            
'cust_email'     => $customer->email,
            
'self_form'      => 'N',
            
'threeDS'        => 'Y',
            
// 'save_card'      => 1,
            
'trx_prod'       => 'ecom',
            
// 'cust_status'    => '1',
            
'trx_type'       => 'Pay',
            
'trx_amount'     => (int) get_currency_price($this->invoice->amountnull$this->payment_currency3),
            
'trx_invoice'    => $this->invoice->id,
            
'trx_date'       => /*(float)*/date('YmdHis'),
            
'trx_returnurl'  => "{$site_info['url']}/finpay/api.php?payment_method={$this->payment_method}",
            
'trx_successurl' => "{$site_info['url']}/order_success.php?order_code={$this->invoice->order->code}",
            
'trx_failedurl'  => "{$site_info['url']}/confirm_order.php?fail=1",
            
// 'trx_failedurl'  => "{$site_info['url']}/finpay/fail.php",
            // 'fin_name'       => $this->fin_name,
            // 'fin_cardno'     => $this->fin_cardno,
            // 'fin_cardcvv'    => $this->fin_cardcvv,
            // 'fin_cardexp'    => $this->fin_cardexp,
            // 'fin_cardmask'   => $this->fin_cardmask,
            // 'fin_sesid'      => $this->fin_sesid,
            // 'fin_securehash' => $this->fin_securehash,
        
];

        
$args array_merge($args$this->generateCustomizedData($payment_type));
        
// ksort($args);
        
$args array_merge($args$this->generateSignature($args));

        
ApiLog::create([
            
'type'     => get_class($this),
            
'endpoint' => $endpoint,
            
'request'  => $args,
        ]);

        return [
            
"endpoint" => $endpoint,
            
"method"   => "POST",
            
"args"     => $args,
        ];
    }

    public function 
getLink()
    {
        
$param $this->paymentParam();
        
// vdump($param['endpoint'], $param['args']); //exit;
        
$result $this->_excute($param['endpoint'], $param['args']);
        
$result json_decode($result);
        
// vdump("result: ", $result); exit;

        
$success true;
        if (
$result->fin_rc !== self::SUCCESS) {
            return 
false;
        }

        if (
$success) {
            return 
$result->redirecturl;
        }

        return 
false;
    }

    protected function 
generateCustomizedData($args)
    {
        
$data = [];
        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''fin_securehash']) && $param) {
                
$data_str .= "{$param}%";
            }
        }

        return [
            
"fin_securehash" => $this->sign($data_str),
        ];
    }

    protected function 
validateSignature($data)
    {
        if (
$this->bypassValidation) {
            return 
true;
        }

        
$expected_signature $this->generateSignature($data);
        
// var_dump(__FUNCTION__, "expected:{$expected_signature['fin_securehash']}, actual:{$data['fin_securehash']}");
        
return ($data['fin_securehash'] == $expected_signature['fin_securehash']);
    }

    protected function 
processPaymentNotification($request)
    {
        
// var_dump('FinpayCreditCard::processPaymentNotification');
        // TODO: move to FinpayCode

        // finpay/api.php?payment_method=FINPAYCREDITCARD&merchant_id=wifiid001&cust_id=29&cust_msisdn=6266660000&trx_prod=ecom&trx_type=Pay&trx_amount=18225800&trx_invoice=389&trx_date=20170120101141&fin_name=Tagaki&fin_cardmask=443426******0008&fin_rc=00&fin_desc=Success&fin_responsecode=&fin_securehash=762fbb835f3e2b96421790a65c1aa8a99a637256005167f10268a53e2b85b595

        
if ($request['fin_rc'] != self::SUCCESS) {
            
$response $result['result_desc'] ?: "Unable to complete the order";
            return 
$response;
        }

        if (!
$this->validateSignature($request)) {
            
$response self::INVALID_SIGNATURE;
            return 
$response;
        }
        
// exit('signature ok');

        // $invoice = Invoice::find($request['invoice']);
        
$invoice Invoice::find($request['trx_invoice']);
        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['trx_date'],
            
'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;
    }

}