LINE Corporation 於2023年10月1日成爲 LY Corporation。LY Corporation 的新部落格在這裏。LY Corporation Tech Blog

Blog


LINE Pay API SDK(v3) Python 套件介紹與使用方法

LINE Pay SDK Python

大家好,我是 LINE 的技術傳教士立花,負責協助開發商使用 LINE API 並在商務上取得成功。

API Expert加川澄廣先生發布日前釋出的 LINE Pay API v3 的 Python 版 SDK ,以下介紹其使用方法。

v3 的變更點

v3 的變更點如下。

  • 認證機制變更
  • 不須登記 White IP
  • 結帳商品的設定更有彈性
  • Checkout (結帳完成時也可以輸入配送地址)

關於詳細內容,請參照以下。

Python 版 SDK之介紹

Repository

Repository 如下所示。

https://github.com/sumihiro3/line-pay-sdk-python

運作環境

Python >= 3.6

安裝

在 PyPI 完成註冊,可透過 pip 直接安裝。

$ pip install line-pay

各 API 的使用方法

專案範本

LINE Pay SDK Python

為了方便每個人都可以自行確認運作, 已經與 kintone 合作,製作可以體驗LINE Pay API功能的程式  ,並公開於 github。使用方法則記載於 README 中,若有需要,請下載參閱。

https://github.com/stachibana/line-pay-v3-python-sdk-sample

另外,個人業者目前只要通過審查,也可以正式使用 LINE Pay API。關於詳情,請參照以下報導。

使用LINE Pay API,在個人開發的Bot中,加入結帳功能 (日文)

Request API

進行結帳的 Request 的時候,透過 options.payment.capture 的値指定False,可將授權與請款分開作業。若將授權與請款分離,則需透過 Capture API 來進行請款。

order_id = str(uuid.uuid4())
amount = 1
currency = "JPY"
request_options = {
    "amount": amount,
    "currency": currency,
    "orderId": order_id,
    "packages": [
        {
            "id": "package-999",
            "amount": 1,
            "name": "Sample package",
            "products": [
                    {
                        "id": "product-001",
                        "name": "Sample product",
                        "imageUrl": "https://placehold.jp/99ccff/003366/150x150.png?text=Sample%20product",
                                    "quantity": 1,
                                    "price": 1
                    }
            ]
        }
    ],
    "options": {
        "payment": {
            "capture": True|False
        }
    },
    "redirectUrls": {
        "confirmUrl": request.host_url.replace('http', 'https') + "confirm",
        "cancelUrl": request.host_url.replace('http', 'https') + "cancel"
    }
}
response = api.request(request_options)

Confirm API

跳轉到 Request API 所提供的結帳用連結,由使用者同意後,此 API 可讓商店完成結帳。

transaction_id = int(request.args.get('transactionId'))

response = api.confirm(
    transaction_id,
    amount,
    currency
)

Capture API

此 API 可取得確定授權狀態的結帳資訊。

response = api.capture(transaction_id, amount, currency)

Void API

此 API 可取消授權。

response = api.void(transaction_id)

Refund API

此 API 可退還已確定的銷售款項。退款可全額退款或指定金額(部份退款)。

# 全額
response = api.refund(transaction_id)
# 指定金額
response = api.refund(transaction_id, 50.0)

Payment Details API

此 API 可查閱交易明細。

details = api.payment_details(transaction_id)

Pay Preapproved API

此 API 可進行自動結帳(此功能可在預先取得使用者同意下,下次不用經過使用者同意,即可進行結帳) 。自動結帳所必須的 RegKey,可藉由在執行 Request API 時的參數 options.payment.payType 中,指定為 PREAPPROVED 來取得。

RegKey取得

# Request
order_id = str(uuid.uuid4())
amount = 1
currency = "JPY"
request_options = {
    "amount": amount,
    "currency": currency,
    "orderId": order_id,
    "packages": [
        {
            "id": "package-999",
            "amount": 1,
            "name": "Sample package",
            "products": [
                    {
                        "id": "product-001",
                        "name": "Sample product",
                        "imageUrl": "https://placehold.jp/99ccff/003366/150x150.png?text=Sample%20product",
                                    "quantity": 1,
                                    "price": 1
                    }
            ]
        }
    ],
    "options": {
        "payment": {
            "capture": True,
            "payType": "PREAPPROVED"
        }
    },
    "redirectUrls": {
        "confirmUrl": request.host_url.replace('http', 'https') + "confirm",
        "cancelUrl": request.host_url.replace('http', 'https') + "cancel"
    }
}
response = api.request(request_options)
# Confirm
response = api.confirm(
    transaction_id,
    amount,
    currency
)
if 'regKey' in response['info']:
    reg_key = response['info']['regKey']
# Pay preapproved
order_id = str(uuid.uuid4())
amount = 1.0
currency = "JPY"
product_name = "訂閱項目"

response = api.pay_preapproved(reg_key, product_name, amount, currency, order_id, True)

Checkout (註: 此功能目前僅開放日本申請)

此 API 可根據使用者指定的配送地址,建議配送方法與運費,並同時結帳。需安裝指定 options.shipping 的値、收件住址、以及送回配送方法的 Inquiry ShippingMethods API。

※ 本 API 無法在Sandbox環境中使用。

# Request
order_id = str(uuid.uuid4())
amount = 1
currency = "JPY"
request_options = {
    "amount": amount,
    "currency": currency,
    "orderId": order_id,
    "packages": [
        {
            "id": "package-999",
            "amount": 1,
            "name": "Sample package",
            "products": [
                    {
                        "id": "product-001",
                        "name": "Sample product",
                        "imageUrl": "https://placehold.jp/99ccff/003366/150x150.png?text=Sample%20product",
                                    "quantity": 1,
                                    "price": 1
                    }
            ]
        }
    ],
    "options": {
        "payment": {
            "capture": True
        },
        "shipping": {
            "type": "SHIPPING",
            "feeInquiryUrl": request.host_url.replace('http', 'https') + "v1/shippings/methods/get/"
        }
    },
    "redirectUrls": {
        "confirmUrl": request.host_url.replace('http', 'https') + "confirm",
        "cancelUrl": request.host_url.replace('http', 'https') + "cancel"
    }
}
response = api.request(request_options)
# Inquiry ShippingMethods API
@app.route('/v1/shippings/methods/get/', methods=["POST"])
def inquiryShippingMethods():
    result = {
        "returnCode": "0000",
        "returnMessage": "Example",
        "info": {
            "shippingMethods": [
                {
                    "id": "hoge",
                    "name": "hogehoge express",
                    "amount": "1000",
                    "toDeliveryYmd": datetime.datetime.now().strftime("%Y%m%d")
                }
            ]
        }
    }
    return jsonify(result)

總結

加川先生感謝您。多虧你分享的 SDK,編寫的程式碼才能如此簡潔。另外,如果還有其他人在寫 SDK,請務必告訴我喔。