Line pay deposit api with php

  • March 28, 2023
  • 555

Các bước tích hợp line pay deposit api sử dụng php. Người dùng đăng nhập line, line trả về access token, từ access token gọi api lấy reference number, sử dụng reference number của người dùng gọi api deposit line pay thế là xong. Chú ý api get reference number không có môi trường sandbox, nên khi test api deposit với môi trường sandbox cần lấy các reference number sandbox mà line pay cung cấp. 

API Authentication

Muốn gọi các api của line pay cần sự dụng auth theo document mà line cung cấp.

Tạo auth signature sử dụng thuật toán HMAC-SHA256 như sau:


HTTP Method : GET
Signature = Base64(HMAC-SHA256(Your ChannelSecret, (Your ChannelSecret + URL Path + Query String + nonce))) Query String : A query string except ? (Example: Name1=Value1&Name2=Value2...)

HTTP Method : POST
Signature = Base64(HMAC-SHA256(Your ChannelSecret, (Your ChannelSecret + URL Path + RequestBody + nonce)))

Get reference number

Api get reference number không có môi trường sandbox, nên chỉ khi run production mới cần gọi api này, nếu môi trường sandbox thì lấy các reference number được cung cấp sẵn bởi line. Để gọi api cần tạo auth signature như sau:


$channelSecret = 'LINE_PAY_CHENNEL_SECRET_KEY';
$uri = '/v3/payments/user-referenceNo/get';
$params = [
    'channelAccessToken' => 'LINE_ACCESS_TOKEN',
    'agreeType' => 'Y',
];
$requestBody = json_encode($params);
$nonce = uniqid(); // or uuid
$authMacText = $channelSecret . $uri . $requestBody . $nonce;

$signature = base64_encode(hash_hmac('sha256', $authMacText, $channelSecret, true));

Khi có được signature chúng ta sẽ call api lấy reference number như sau:


// Run command curl in terminal
curl -X POST \
-H "Content-Type: application/json" \
-H "X-LINE-ChannelId: CHANNEL_ID" \
-H "X-LINE-Authorization-Nonce: $nonce" \
-H "X-LINE-Authorization: $signature" \ -d '$requestBody' \ https://api-pay.line.me/v3/payments/user-referenceNo/get // Call api with http client $url = 'https://api-pay.line.me/v3/payments/user-referenceNo/get'; $params = [ 'channelAccessToken' => 'LINE_ACCESS_TOKEN', 'agreeType' => 'Y', ]; $channelId = 'CHANNEL_ID'; $client = new Client(); $client->post($url, json_encode($params), [ 'headers' => [ 'type' => 'json', 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'X-LINE-Authorization' => $signature, 'X-LINE-Authorization-Nonce' => $nonce,
'X-LINE-ChannelId' => $channelId, ], ]);

Handle response từ api line trả về


public function getReferenceNo(array $data): ?string
{
        $client = new Client();
        $response = $client->post($url, json_encode($params), [
            'headers' => [
                'type' => 'json',
                'Content-Type' => 'application/json',
                'Accept' => 'application/json',
                'X-LINE-Authorization' => $signature,
                'X-LINE-Authorization-Nonce' => $nonce,
                'X-LINE-ChannelId' => $channelId,
            ],
        ]);
        if ($response->getStatusCode() != '200') {
            return null;
        }

        $res = json_decode($response->body(), true);
        if (Hash::get($res, 'returnCode') !== '0000') {
            return null;
        }

        return Hash::get($res, 'info.referenceNo');
}

Line deposit api

Khi có reference number chúng ta có thể gọi api deposit.

Tạo signature cho api deposit


$channelSecret = 'LINE_PAY_CHENNEL_SECRET_KEY';
$uri = '/v1/partner-deposits';
$params = [
    'referenceNo' => $referenceNo,
    'amount' => $amount,
    'currency' => 'JPY',
    'orderId' => 'order' . time(),
];
$requestBody = json_encode($params);
$nonce = uniqid(); // or uuid
$authMacText = $channelSecret . $uri . $requestBody . $nonce; $signature = base64_encode(hash_hmac('sha256', $authMacText, $channelSecret, true));

Call api deposit with line payment


// Run command curl in terminal
curl -X POST \
-H "Content-Type: application/json" \
-H "X-LINE-ChannelId: CHANNEL_ID" \
-H "X-LINE-Authorization-Nonce: $nonce" \
-H "X-LINE-Authorization: $signature" \ -d '$requestBody' \ https://api-pay.line.me/v3//v1/partner-deposits // Call api with http client $url = 'https://api-pay.line.me/v1/partner-deposits'; $params = [ 'referenceNo' => $referenceNo, 'amount' => $amount, 'currency' => 'JPY', 'orderId' => 'order' . time(), ]; $channelId = 'CHANNEL_ID'; $client = new Client(); $client->post($url, json_encode($params), [ 'headers' => [ 'type' => 'json', 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'X-LINE-Authorization' => $signature, 'X-LINE-Authorization-Nonce' => $nonce, 'X-LINE-ChannelId' => $channelId, ], ]);

Muốn test api deposit ở môi trường sandbox chỉ cần đổi sang link sandbox là được https://sandbox-api-pay.line.me/v1/partner-deposits
Response api trả vê khi deposit thành công


{
    "returnCode":"0000",
    "returnMessage":"success",
    "info": {
        "transactionId":2019011804956338240,
        "transactionDate":"2019-01-18T23:19:52Z"
    }
}

Tổng kết

Như vậy là chúng ta đã sử dụng được api deposit của line pay. Hi vọng bài viết sẽ giúp ích cho mọi người. Thanks for reading...