[Python] LinePay串接Online APIs – 初步了解流程及使用postman測試api v2 (2/5)

因有些人只想了解linepay的運作方式, 提供一些簡單可使用的方式。

以下測試環境及工具使用:

  • Sandbox
  • postman
  • linePay文檔
  • Online APIs 文檔:網址
  • 按下此處 查看以前的API文件(v2)

本次postman的簡易流程
簡易流程圖網址

基本設定

請先下載postman

domain(v2測試用)

https://sandbox-api-pay.line.me/v2/payments

在現有的Headers添加

Content-Type:application/json
X-LINE-ChannelId:商戶的Channel ID
X-LINE-ChannelSecret:商戶的Channel Secret

request 請求訂單

API:

POST {domain}/request

Request Body:

{
    "amount": 1,
    "currency": "TWD", 
    "productName": "MyProd",
    "productImageUrl": "https://cimg.cnyes.cool/prod/news/4556115/l/79b7b76238dcaa28d626ec007bff576f.jpg",
    "confirmUrl": "http://127.0.0.1:3000",
    "orderId": "Order202203110011"
}

Payment Reserve Response:

{
    "returnCode": "0000",
    "returnMessage": "Success.",
    "info": {
        "paymentUrl": {
            "web": "https://sandbox-web-pay.line.me/web/payment/wait?transactionReserveId=WVZub2tRakxRbzk0NWVsa1VMZ1pyTzhOSk00QXJqNXgvbTZIeXZ2NklpL2ZiMHlBUGN4SnJrTG4xVWh6bFA1cg",
            "app": "line://pay/payment/WVZub2tRakxRbzk0NWVsa1VMZ1pyTzhOSk00QXJqNXgvbTZIeXZ2NklpL2ZiMHlBUGN4SnJrTG4xVWh6bFA1cg"
        },
        "transactionId": 2022031100707222710,
        "paymentAccessToken": "785453599850"
    }
}

取得info.paymentUrl.web後, 使用瀏覽器打開如下:

點擊付款:

付款成功如下:

補充:
為什麼結帳後會原本的網頁會出現下圖?

文檔內的說明如下

Merchant's URL that the buyer is redirected to after selecting a payment method and entering the payment password in LINE Pay.
On the redirected URL, Merchant can call Confirm Payment API and complete the payment
LINE Pay passes an additional parameter, "transactionId"

Reference Detailed Explanation and Exceptional Case of ConfirmUrl

簡單來說就是:想讓結帳完的顧客看到什麼畫面。

confirm 確認

API:

POST {domain}/{transactionId}/confirm

範例:

https://sandbox-api-pay.line.me/v2/payments/2022031400707322810/confirm

Headers添加

X-LINE-MerchantDeviceType: POS
X-LINE-MerchantDeviceProfileId: DUMMY

Request Body:

{
    "amount": 1,
    "currency": "TWD"
}

Response成功的Json如下:
(補充:此次訂單是重新發起的新的訂單,所以transactionId與orderId會與上面請求訂單時的資料不一樣, 主要目的是呈現成功的結果)

{
    "returnCode": "0000",
    "returnMessage": "Success.",
    "info": {
        "transactionId": 2022031400707322810,
        "orderId": "dev202203140002",
        "payInfo": [
            {
                "method": "CREDIT_CARD",
                "amount": 1,
                "maskedCreditCardNumber": "************1111"
            }
        ]
    }
}

若失敗或是orderId有重複, 則會告知錯誤。

Check Payment Status 查看訂單狀態

API

GET {domain}?transactionId={transactionId}

範例:

https://sandbox-api-pay.line.me/v2/payments?transactionId=2022031400707322810

Response成功的Json如下:

{
    "returnCode": "0000",
    "returnMessage": "Success.",
    "info": [
        {
            "transactionId": 2022031400707322810,
            "transactionDate": "2022-03-13T16:11:08Z",
            "transactionType": "PAYMENT",
            "productName": "MyProd",
            "currency": "TWD",
            "payInfo": [
                {
                    "method": "CREDIT_CARD",
                    "amount": 1
                }
            ],
            "orderId": "dev202203140002"
        }
    ]
}

注意:
正常來說是要先檢查是否付款後才執行confirm這支API,但因為v2是需要先進行confirm這支API才有辦法使用,否則會出現:

{
    "returnCode": "1150",
    "returnMessage": "Transaction record not found."
}

若是使用v3則可正常先進行Check Payment Status 後再進行確認。

refurnd 退款

取消已付款的交易。

API:

POST {dimain}/{transactionId}/refund

Headers添加

X-LINE-MerchantDeviceType: POS
X-LINE-MerchantDeviceProfileId: DUMMY

refundAmount: 退款金額

{
    "refundAmount": 1
}

Response:

{
    "returnCode": "0000",
    "returnMessage": "Success.",
    "info": {
        "refundTransactionId": 2022031400707327211,
        "refundTransactionDate": "2022-03-13T17:05:50Z"
    }
}

發佈留言

內容索引