iT邦幫忙

2021 iThome 鐵人賽

DAY 17
0
永豐金融APIs

試著讀懂與串接永豐金融APIs系列 第 17

Day 0x11 - 建立信用卡付款的訂單

0x1 前言

之前都是建立付款方式為 ATM 的訂單,另一個信用卡的流程都沒跑過,今天就是要來跑一下這個流程

0x2 建立訂單 - 信用卡

首先看一下文件,建立信用卡訂單除了 PayType 要為 C 以外,還有沒有而外的參數要輸入
https://ithelp.ithome.com.tw/upload/images/20210926/20141805i1NJUAgMrm.png

PayType=C(信用卡),則 CardParam 中的 AutoBilling 為必填。
因此要把 ATMParam 改成 CardParam,內容 AutoBillingYN
OKay~ 修改一下 create_order

// app/Http/Controllers/Controller.php
public function create_order(Request $request)
{
    $sinopac = $this->initSinopac();
    $order_no = date('YmdHis');
    $amount = random_int(4000, 10000);
    $pay_type = ($request->get('credit', '') === 'Y') ? 'C' : 'A';
    $expire_date = date('Ymd', time() + 604800);

    $data = [
        'ShopNo'        => $sinopac->shop_no,
        'OrderNo'       => $order_no,
        'Amount'        => $amount . '00',
        'CurrencyID'    => 'TWD',
        'PrdtName'      => '大河',
        'ReturnURL'     => 'http://public.ngrok.io/web',
        'BackendURL'    => 'http://public.ngrok.io/api/receive_msg',
        'PayType'       => $pay_type,
    ];

    if ($pay_type === 'C') {
        $data['CardParam'] = ['AutoBilling' => 'Y'];
    } else {
        $data['ATMParam'] = ['ExpireDate' => $expire_date];
    }

    $data = $sinopac->requestDataset('OrderCreate', $data);
    $message = $sinopac->callApi('https://apisbx.sinopac.com/funBIZ/QPay.WebAPI/api/Order', $data);

    $decrypt_message = $this->reply_message_decrypt($sinopac, $message);
    // 這裡的 – 是 \xE2  不是 \x2D
    $description = explode(' – ', $decrypt_message['Description']);
    if ($description[0] !== 'S0000') {
        Log::alert('訂單未建立成功', $decrypt_message);
    }

    $dataset = [
        'customer_id'       => 0,
        'order_no'          => $order_no,
        'total'             => $amount,
        'pay_type'          => $pay_type,
        'expire_date'       => $expire_date,
        'ts_no'             => $decrypt_message['TSNo'],
        'status'            => $decrypt_message['Status'],
        'description'       => $decrypt_message['Description'],
        'mailing_address'   => '',
        'prdt_name'         => '大河',
    ];
    if ($pay_type === 'C') {
        // 信用卡
        $card_param = $decrypt_message['CardParam'];
        $dataset = array_merge($dataset, [
            'card_pay_url'  => $card_param['CardPayURL'],
        ]);
    } else {
        // 虛擬帳號
        $atm_param = $decrypt_message['ATMParam'];
        $dataset = array_merge($dataset, [
            'atm_pay_no'    => $atm_param['AtmPayNo'],
            'web_atm_url'   => $atm_param['WebAtmURL'],
            'otp_url'       => $atm_param['OtpURL'],
        ]);
    }
    $id = sale_order::create($dataset)->id;

    return [
        'dataset' => $dataset,
        'id' => $id
    ];
}

用 Postman 傳送要求來建立一筆信用卡訂單
https://ithelp.ithome.com.tw/upload/images/20210926/20141805bzOZp5fgtM.png

文件有提供測試用卡號
https://ithelp.ithome.com.tw/upload/images/20210926/20141805HFA94JKqPs.png

開啟連結並輸入文件提供的測試卡號
https://ithelp.ithome.com.tw/upload/images/20210926/20141805DsKlrEwTdu.png

付款完成後會跳回 建立訂單時傳送的 ReturnURL
https://ithelp.ithome.com.tw/upload/images/20210926/20141805wJC0uc0rrd.png

Oops,ReturnURL 的網頁還沒做,而且 method 是 POST

0x3 今日結語

花了一些時間在思考如何把 create_order 拆得更好,
但想了老半天很像要等接下來做到再回頭改了,
所以又把改了一堆的程式全部還原,
明天觀察 Return URL 傳進來的資訊,接著把畫面做出來,
明天見


上一篇
Day 0x10 - 整理解密函數與 Webhook api
下一篇
Day 0x12 - 建立 Return URL 的畫面
系列文
試著讀懂與串接永豐金融APIs30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言