文章彙整

Line Pay 金流串接

Astral WebBy Astral Web 3 weeks agoNo Comments
首頁  /  電子商務  /  Line Pay 金流串接

註冊為LINE Pay的商家,可以吸引全球的LINE用戶作為自己的客戶。此外,通過LINE擴展商家的銷售管道,可以預見銷售額的迅速成長。

上次我們已經說過如何申請Sand Box與帳號,現在我們要來串接付款的程式。

Sandbox       Url : https://sandbox-api-pay.line.me

Production   Url : https://api-pay.line.me

LINE Pay的Authentication方式為

GET 為 Channel Secret + URI + Query String + nonce

POST Channel Secret + URI + Request Body + nonce

Channel Secret 請登入SandBox拿取,管理付款連結>管理連結金鑰 輸入密碼便會顯示。

Nonce 請用Timestamp

Base64(HMAC-SHA256(Your ChannelSecret, (Your ChannelSecret + URI + Query String + nonce)))

先行呼叫Request API取得付款URL

<?php
$sandBox = 'https://sandbox-api-pay.line.me';
$uri = '/v3/payments/request';
$channelId = 'yourChannelId';
$channelSecret = ‘yourChannelSecret’;
$Nonce = date('c') . uniqid('-');
$isSandbox = false;

$qyery = [
    'amount' => 250,
    'currency' => 'TWD',
    'orderId' => '000001',
    'packages' => [
        [
            'id' => '000001',
            'amount' => 250,
            'name' => 'test store',
            'products' => [
                [
                    'name' => 'test product',
                    'quantity' => 1,
                    'price' => 250
                ],
            ],
        ],
    ],
    'redirectUrls' => [
        'confirmUrl' => 'https://test.astralweb.com/confirm.php',
        'cancelUrl' => 'https://test.astralweb.com/cancel.php',
    ],
];

$authMacText = $channelSecret . $uri . json_encode($qyery) . $Nonce;
$Authorization = base64_encode(hash_hmac('sha256', $authMacText, $channelSecret, true));


$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => $sandBox.$uri,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS =>json_encode($qyery),
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json',
        'X-LINE-ChannelId: '.$channelId,
        'X-LINE-Authorization-Nonce: '.$Nonce,
        'X-LINE-Authorization: '.$Authorization
    ),
));

$response = curl_exec($curl);

curl_close($curl);

$data = json_decode($response);

header("Location: ".$data->info->paymentUrl->web);
?>

使用回應得paymentUrl進行跳轉到LINE付款頁面

輸入帳號或者是使用手機掃描付款

按下付款後便會跳轉當初所設定的confirmUrl,https://test.astralweb.com/line-pay/confirm?transactionId=2021100100691484310&orderId=000001

並帶上transactionId與orderId,這時授權完成 , 現在可以呼叫Confirm API

<?php
$sandBox = 'https://sandbox-api-pay.line.me';
$uri = '/v3/payments/'.$_GET['transactionId'].'/confirm';
$channelId = ''yourChannelId'';
$channelSecret = 'yourChannelSecret';
$Nonce = date('c') . uniqid('-');
$isSandbox = false;

$qyery = [
    'amount' => 250,
    'currency' => 'TWD'
];

$authMacText = $channelSecret . $uri . json_encode($qyery) . $Nonce;
$Authorization = base64_encode(hash_hmac('sha256', $authMacText, $channelSecret, true));

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => $sandBox.$uri,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS =>json_encode($qyery),
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json',
        'X-LINE-ChannelId: '.$channelId,
        'X-LINE-Authorization-Nonce: '.$Nonce,
        'X-LINE-Authorization: '.$Authorization
    ),
));

$response = curl_exec($curl);

curl_close($curl);

echo $response;
?>

這邊是回來的資料,到這裡你已完成付款的流程,由於LINEPAY是預設options.payment.capture所以我們不須使用Capture API向LINEPAY請款,如果需要要向LINE申請。

{
    "returnCode": "0000", 
    "returnMessage": "Success.", 
    "info": {
        "transactionId": 2021100100691493400, 
        "orderId": "000001", 
        "payInfo": [
            {
                "method": "CREDIT_CARD", 
                "amount": 250, 
                "maskedCreditCardNumber": "************1111"
            }
        ], 
        "packages": [
            {
                "id": "000001", 
                "amount": 250, 
                "userFeeAmount": 0, 
                "products": [
                    {
                        "name": "test product", 
                        "quantity": 1, 
                        "price": 250
                    }
                ]
            }
        ]
    }
}

到這裡我們已經完成LINE Pay的付款所有流程,請注意confirmUrl有可能會因為網路問題或使用者等因素導致沒有回傳,必須建立主動檢查機制,註冊為LINE Pay的商家,可以吸引全球的LINE用戶作為自己的客戶。此外,通過LINE擴展商家的銷售管道,可以預見銷售額的迅速成長。

 

以上是本篇的Line Pay金流串接分享,對於經營電子商務的商家來說,金流是很重要的一個環節,喜歡歐斯瑞文章的讀者們,記得追蹤我們的fb粉絲團IG,也別忘了訂閱電子報,隨時掌握第一手最新消息呦!若有問題也歡迎聯繫我們

參考範例

https://pay.line.me/jp/developers/apis/onlineApis?locale=zh_TW

 000

歐斯瑞

歐斯瑞 - 一個對網路世界充滿熱情的團隊

歐斯瑞提供免費電商知識,並協助各大類型之企業 電商諮詢建置MagentoShopify 相關的電商專案。

立即聯繫我們

推薦文章

Categories:
  電子商務後端開發網站技術與設計

留下回應

你的電子郵件地址不會被公開.