Try the Python LINE Pay SDK

A memo written by Mr. Kagawa, a LINE Pay teacher, to help you understand and use the SDK created by @ sumihiro3. https://github.com/sumihiro3/line-pay-sdk-python/tree/master/examples

I made a starter for one-time general payment only, so please use here.

Prerequisite knowledge

What is SDK

The SDK is a package of programs, APIs, sample code, etc. so that you can develop applications with less effort. The SDK allows developers to implement new features in their applications without having to understand the detailed mechanics of the technology that enables them.

LINE Pay API (v3) payment flow

line_pay_api02.jpg Quote: https://dev.classmethod.jp/articles/devio-cafe-line/

line_pay_v3_flow.png Quote: https://dev.classmethod.jp/articles/line-pay-v3-lambda/

It's a little confusing, but SP is a service provider and we are a business operator.

① Have Pay issue a payment URL using the Request API ② Ask the user to throw the URL and tap it to approve it on the payment screen in the example. ③ Then, we will receive an approved notification from our business operator (app and system), which is the SP, so we will complete the payment using the Confirm API.

Normally, this will confirm sales, and we don't have to use the Capture API. However, there seems to be a case where you want to take one step between the completion of payment and the confirmation of sales, in which case you have to perform the sales confirmation process yourself using the Capture API as shown below.

If "options.payment.capture" of Request API is set to false, the settlement and sales confirmation will be separated, and the payment status will remain in the sales confirmation waiting (authorization) state even if the payment is completed. To confirm the sales, you need to call the Capture API to confirm the sales.

request_options = {
//abridgement
    "options": {
        "payment": {
            "capture": False
        }
    },
//abridgement
response = api.request(request_options)

ldgq20200720ハンズオン.png

This is the basic payment flow of LINE Pay API v3, but from the user's point of view, we are beginner engineers because there are only three steps: "Pay with LINE Pay" → "Go to the payment screen" → "Press the payment button". I think it's hard to imagine that the above processing is done behind the scenes. It's a crazy SDK, so I think it's okay to use it even if you don't know this area.

Advance preparation

--As a prerequisite, the Python version of your PC should be 3.6 or higher. --If you do not have a LINE Pay member store ID or do not make an actual payment, please obtain a member store account in the Sandbox environment.

However, if you do it in the sandbox, the behavior of Pay will be different from the actual behavior, and the display of automatic payment (subscription) will be the same as general payment, so it will be complicated. I wonder if LINE will create an account that can be tested with the same behavior.

Transaction confirmation in sandbox

Even if you log in to the member store MyPage with your Sandbox account, no transaction history will be recorded on that screen. After logging in, click the Sandbox link in the upper right to see the transaction history settled in Sandbox (the first screen seems to have no use other than getting the interlocking key ...).

スクリーンショット 2020-11-02 19.15.53.png

スクリーンショット 2020-11-02 19.17.21.png

First of all, SDK clone or download

First, clone the GitHub repository or Download and unzip it.

Go to this directory in your terminal

$ cd ine-pay-sdk-python-master

Install the required libraries with pip install

$ pip install -r requirements.txt

Create a .env file by copying the .env.sample file

$ cp ./.env.sample ./.env

Make port 8000 accessible by external URL with ngrok

$ ngrok http 8000

Edit the .env file with any editor such as VSCode, Atom, vim, emacs Enter the domain issued by ngrok and the Channes_ID and Channel Secret Key that can be confirmed from the payment interlocking management> interlocking key of LINE Pay member store MyPage.

HOST_NAME=xxxxx.ngrok.io
LINE_PAY_CHANNEL_ID=YOUR_LINE_PAY_CHANNEL_ID
LINE_PAY_CHANNEL_SECRET=YOUR_LINE_PAY_CHANNEL_SECRET

General payment

基本的にこのSDKは試したい決済流れの.pyファイルをRunさせて、xxxxx.ngrok.io/requestにアクセスすることでその流れを試せる作りになっているようだ。

General payment → refund

$ python request-confirm-refund.py

If you access https://xxxxx.ngrok.io/request in this state

When you press paymentUrl (because it is a sandbox), you will be taken to the screen below, so log in to LINE (when you try to access the above URL on mobile or try to make a payment by scanning the QR code, it fails for some reason ...).

スクリーンショット 2020-11-02 19.26.59.png

The sandbox mock payment screen opens, so press PAY NOW

スクリーンショット 2020-11-02 19.27.22.png

When the payment is completed, you will be redirected to the screen that displays the payment result.

スクリーンショット 2020-11-02 19.27.50.png

This time it is a py file that you can try refunding, so if you press the REFUND link at the bottom, you can process the refund and you will be redirected to the next screen.

The transaction breakdown on the Sandbox screen of the member store MyPage at this time is as follows, and the upper No. 1 is recorded as the settlement process and the lower No. 2 is recorded as the settlement cancellation process.

スクリーンショット 2020-11-02 19.38.09.png

スクリーンショット 2020-11-02 19.38.39.png

General payment → capture

By setting options.payment.capture in request_options to False, you can dare to wait for sales confirmation (authorization) after payment, and later try sales confirmation (capture) using Capture API ʻauthorizations-capture.py` file Run.

authorizations-capture.py


        "options": {
            "payment": {
                "capture": False
            }
        },
$ python authorizations-capture.py

Similarly, if you access https://xxxxx.ngrok.io/request and make a payment, you will be redirected to the screen with the capture link.

The transaction breakdown of the member store MyPage at this time is as follows.

Transaction breakdown after settlement ↓ スクリーンショット 2020-11-02 20.02.56.png

Transaction breakdown after capture ↓ スクリーンショット 2020-11-02 20.04.47.png

The sales confirmation process should be possible from this screen as well, but for some reason there is no sales confirmation button when using this SDK. Is it because it's a sandbox?

Checkout

Checkout is an API that allows you to make payments including the shipping address and shipping fee when making payments with EC etc., but this API cannot be used in the Sandbox environment, and official member stores are also reviewing its use. So it seems that this SDK does not have a py file to try.

Automatic payment

$ python request-confirm-pre_approved.py

If you access https://xxxxx.ngrok.io/request in the same way and make a payment, you can get the RegKey for automatic payment after the payment. It seems that the first payment and the acquisition of RegKey are performed at the same time as using LINE Pay (Sandbox does not reproduce the check in the original automatic payment flow ...).

You can make automatic payments using RegKey and the Pay Preapproved API by clicking the link below. However, the transaction breakdown of the member store MyPage does not record the transaction by automatic settlement. Is this also a sandbox?

The flow of formal LINE Pay automatic payment is as follows. https://youtu.be/KdQLm88nl2Q

Why are all access URLs / request?

This may be an amateur opinion, but I was wondering why I couldn't access each HTML file in the file. However, in the case of Python (or Flask), the route URL is determined in the py file as shown below, so you can only access here. The HTML file under templates is an HTML file for the program to call like `return render_template ("request.html", result = response) `in the py file.

xxx.py


@app.route("/request", methods=['GET'])
def pay_request():
    #abridgement
    return render_template("request.html", result=response)

reference

--RegKey is valid for 180 days --There is an API to check if RegKey is valid, but there is no way to know the expiration date --Users can expire from the settings screen --In the sandbox environment, the expiration date is set to 1 day

The user's expire is probably here

Launch LINE Pay from the LINE app wallet (launch as a LIFF app) Press automatic payment at the bottom If you subscribe in the production environment, you can manage it here. Note that no Reg Key in Sandbox is displayed.

Recommended Posts

Try the Python LINE Pay SDK
Try LINE Notify in Python
Try python
Try a similar search for Image Search using the Python SDK [Search]
Try using the Python Cmd module
Load the remote Python SDK in IntelliJ
Try using the Wunderlist API in Python
Try using the Kraken API in Python
Try using Dialogflow (formerly API.AI) Python SDK #dialogflow
Working with OpenStack using the Python SDK
[Python] Try pydash of the Python version of lodash
Read the file line by line in Python
Read the file line by line in Python
Python amateurs try to summarize the list ①
Try hitting the YouTube API in Python
[Python] Read the specified line in the file
Python> try: / except:
LINE heroku python
Try to solve the Python class inheritance problem
[Cloudian # 7] Try deleting the bucket in Python (boto3)
Try to solve the man-machine chart with Python
Try using the BitFlyer Ligntning API in Python
Upgrade the Azure Machine Learning SDK for Python
Python: Try using the UI on Pythonista 3 on iPad
Try using the Python web framework Tornado Part 1
Try the free version of Progate [Python I]
Try using LINE Notify for the time being
Try CIing the pushed python code on GitHub.
Try using the collections module (ChainMap) of python3
Try using the Python web framework Tornado Part 2
Try implementing the Monte Carlo method in Python
Call Polly from the AWS SDK for Python
Try accessing the YQL API directly from Python 3
Try using the DropBox Core API in Python
Find the maximum Python
Try scraping with Python.
Try to solve the programming challenge book with python3
python try ~ except ~ else
[AWS IoT] Register things in AWS IoT using the AWS IoT Python SDK
Try translating with Python while maintaining the PDF layout
Try to solve the internship assignment problem with Python
Try touching the micro: bit with VS Code + Python
the zen of Python
Try Debian + Python 3.4 + django1.7 ...
Try gRPC in Python
[Python] Split the date
Try 9 slices in Python
Try using Tweepy [Python2.7]
Python try / except notes
Try translating the Python Data Science Handbook into Japanese
Try scraping the data of COVID-19 in Tokyo with Python
Try hitting the Twitter API quickly and easily with Python
Try to get the function list of Python> os package
A note about hitting the Facebook API with the Python SDK
Try using the Python web framework Django (2) --Look at setting.py
[Hyperledger Iroha] Notes on how to use the Python SDK
Create a Twitter BOT with the GoogleAppEngine SDK for Python
[Python] I asked LINE BOT to answer the weather forecast.
Try to automate the operation of network devices with Python
"Let Python do the boring things" exercise ~ Command line mailer ~
Google search for the last line of the file in Python