Example operation of python access Alipay

**Python access Alipay process: **

first step:

First, download a python SDK. This SDK is not available in Alipay’s developer documentation, but powerful python programmers have developed a set of Alipay payment SDKs belonging to python.

Download method: pip install python-alipay-sdk, the link to github is https://github.com/fzlee/alipay

**Step 2: **

After downloading the SDK, you need to configure Alipay. Alipay's environment is divided into two types, one is the formal environment, which is the environment after the program is online.

The second is the sandbox environment, that is, the test environment for developers when developing projects.

The formal environment needs to create an application, and then the Alipay background will review it, and after passing it, an appid will be assigned as a unique sign. The picture shows creating an application.

Here I use the sandbox environment, so I don’t need to wait for Alipay’s review. Click to enter the Developer Center-R&D Service-Sandbox Application. As shown in the picture, it will automatically assign you an appid, you only need this to be the private key and public key.

third step:

You need to generate your own private key and public key of the server. Now Alipay supports sha256, sha1,

Sha256 is recommended. Alipay provides methods and steps to generate secret keys (https://docs.open.alipay.com/291/105971/)

Open the link in brackets to see the detailed method and steps for generating the secret key. After the public key and private key are generated, upload the public key to Alipay key settings. Copy Alipay's public key to the local, save your own public key and private key and Alipay's public key (this is very important), generally save them to the file of the local project.

the fourth step:

Understand and be familiar with the development process of Alipay. The picture shows the interaction flowchart between the client and Alipay's back-end through the back-end program

1、 The user chooses to use Alipay to pay

2、 The user uses the app to initiate a request to the merchant server to obtain the signed order information

3、 The merchant server returns the signed order information (query string or url), for example:

app_id=201609239&biz_content=%7B%22subject%22%3A%22%5Cu6d4b%5Cu8bd5%5Cu8ba2%5Cu5%22out_trade_no%22%3A%22201702021111%22%2C%22total_amount%22%3A1000%2C%22product_code%22%3A%22FAST_INSTANT_TRADE_PAY%22%7D&charset=utf-8&method=alipay.trade.page.pay&notify_url=http%3A%2F%dus.com%2F&return_url=http%3A%2F%2F47.92.87.172%3A8000%2F&sign_type=RSA2&timestamp=2015+15%3A19%3A15&version=1.0&sign=BSzk6LzwNp

4、 Merchant app calls payment interface

5- 8、 The Alipay server receives the request and returns the payment result.

9、 The merchant app initiates a request to the merchant server, synchronizes the payment result, the server verifies the signature, and parses the payment result.

10、 Return the payment result.

11、 The merchant app displays the payment result.

12、 The Alipay server asynchronously sends a payment notification to the merchant server.

13、 The merchant server receives the payment notification and returns a response to the Alipay server.

the fifth step:

The python code is as follows

# Business processing:Use pythonsdk to call Alipay's payment interface
# initialization
fromalipayimportAliPay
alipay=AliPay(
appid="2016092300574189",
app_notify_url=None,
app_private_key_path=r"D:\python source code\alipay_keys\private_keys",
alipay_public_key_path=r"D:\python source code\alipay_keys\public_key",
sign_type="RSA2",
debug=True,)
# Call interface
# total_pay=order.total_price+order.transit_price
total_pay=12311
order_string=alipay.api_alipay_trade_page_pay(
out_trade_no=1231231312313,
total_amount=str(total_pay),
subject='Test text%s'%1,
return_url=None,
notify_url=None,)
# Return response
pay_url="https://openapi.alipaydev.com/gateway.do?"+order_string
print(pay_url)

The function of this code is to generate a signed url, which is the url to access Alipay payment

The parameter of the initialization part: appid is the unique identification id of Alipay,

app_notify_url is the URL address of the back-end server that is asynchronously notified to the merchant when the Alipay back-end payment is completed.

app_private_key_path is the private key generated by yourself,

alipay_public_key_path is the public key of Alipay. sign_type is the encryption method of the signature,

When debug is true, the payment URL of Alipay is an informal environment, if it is an online environment, it can be changed to false.

The parameters of the calling interface part: out_trade_no is the order number. This is the order number generated by the merchant's back-end server.

total_amount is the payment amount (must be a string type), subject is the title, and return_url is the page to be redirected to after the payment is successful.

notify_url is the url of asynchronous notification.

**Step 6: **

Running this code will get a signed URL.

Visit this url and see the result:

You can download Alipay in a sandbox environment on your mobile phone, or click the login account on the right to pay. The account and password for payment are provided in the sandbox application.

**Step Seven: **

Login to complete payment

Example extension:

Python docking Alipay payment self-implementation function

# - *- coding: utf-8-*-import base64
import json
import urllib.parse
from datetime import datetime
import requests
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import padding
classAliPayException(Exception):
 def __init__(self, data):super(AliPayException, self).__init__()
 self.data = data
 def __str__(self):return"alipay - {}".format(self.data)
 def __unicode__(self):return u"alipay - {}".format(self.data)classAliPayVerifyException(AliPayException):
 def __init__(self, msg, data):super(AliPayVerifyException, self).__init__('alipay verify except - {}:{}'.format(msg, data))classAliPay:
 def __init__(self,**kwargs):"""
 : param kwargs:

Recommended Posts

Example operation of python access Alipay
The operation of python access hdfs
Example of feature extraction operation implemented in Python
Python negative modulus operation example
Python preliminary implementation of word2vec operation
Python calculation of information entropy example
Python list comprehension operation example summary
Some examples of python operation redis
Example of python calculating derivative and plotting
7 features of Python3.9
Python file operation
Python object-oriented example
python operation kafka
Analysis of common methods of Python operation Jira library
Python implementation of hand drawing effect example sharing
Python3.7 debugging example method
Basics of Python syntax
Python operation SQLite database
Basic syntax of Python
Detailed explanation of the attribute access process of Python objects
Basic knowledge of Python (1)
Example of how to automatically download pictures in python
Prettytable module of python
Python operation yaml instructions
Python interpolate interpolation example
09. Common modules of Python3
Python-openCV open operation example
Learn the hard core operation of Python in one minute
Python automated operation and maintenance 2
Consolidate the foundation of Python (4)
Python operation Excel merge cells
Consolidate the foundation of Python(7)
In-depth understanding of python list (LIST)
Subscripts of tuples in Python
Python analysis of wav files
Consolidate the foundation of Python(6)
Analysis of JS of Python crawler
python king of glory wallpaper
Consolidate the foundation of Python(5)
Python3 logging log package example
Python implementation of gomoku program
Analysis of Python Sandbox Escape
Some new features of Python 3.10
Python regular expression example code
Deep understanding of Python multithreading
Python output mathematical symbols example
Python automated operation and maintenance 1
Analysis of Python object-oriented programming
Python iterable object de-duplication example
Python version of OpenCV installation
Python one-dimensional two-dimensional interpolation example
Quick start Python file operation
Python draw bar graph (bar graph) example
9 feature engineering techniques of Python
Python right alignment example method
matplotlib of python drawing module
Python method of parameter passing
Consolidate the foundation of Python (3)
Collection of Python Common Modules