Python access to the paypal process:
Step one:
First of all, you need to download a python sdk, this sdk is not in the developer documentation of Alipay, but the powerful python programmers have developed a set of python Alipay payment sdk by themselves.
Download: pip install python-alipay-sdk ,github link is/fzlee/alipay
Step two:
After downloading the sdk, you need to make some relevant configurations for Alipay, the environment of Alipay is divided into two kinds, one is the formal environment, that is, the environment after the program is online.
The second is the sandbox environment, which is the test environment that developers use when developing projects.
The formal environment requires the creation of the application, and then Alipay back-end for review, through which an appid will be assigned as a unique flag. As shown in the figure for the creation of the application.
Here I use the sandbox environment, so do not need to wait for Alipay's audit, click into the developer center - & gt; R & D services - & gt; sandbox application. As shown in the figure it will automatically assign you an appid, you only need to this is a private key and public key can be.
Step Three:
You need to generate your own server-side private key and public key, now Alipay supports encryption methods sha256, sha1,
Recommended to use sha256. Alipay provides the method and steps to generate the secret key (/291/105971/ )
Open the link in brackets to see the detailed method and steps to generate the secret key. After the public key private key is generated, you have to upload the public key to the Alipay key settings. Copy the public key of Alipay locally, save your own public key private key and the public key of Alipay (this is very important), usually saved to the local project file.
Step Four:
Understand and familiarize with Alipay development process. As shown in the figure, the client through the back-end program and Alipay back-end interaction flow chart
1、Users choose to use Alipay payment
2、Users use the app to initiate a request to the merchant server to obtain the signed order information
3. The merchant server returns 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=¬ify_url=http%3A%2F%%2F&return_url=http%3A%2F%2F47.92.87.172%3A8000%2F&sign_type=RSA2×tamp=2015+15%3A19%3A15&version=1.0&sign=BSzk6LzwNp
4、Merchant app call 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 to synchronize the payment result, and the server verifies the signature and parses the payment result.
10. Return the payment result.
11. The merchant app displays payment results.
12. Alipay server sends payment notification asynchronously to merchant server.
13. The merchant server receives the payment notification and returns the response to the Alipay server.
Step Five:
The python code is as follows
# Business Processing: Calling Alipay's payment interface using python sdk # Initialization from alipay import AliPay alipay = AliPay( app, 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, ) # Calling the 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='testtext%s' % 1, return_url=None, notify_url=None, ) # Return answer pay_url = "/?" + order_string print(pay_url)
This code implements the function of generating a signed url, which is the url to access the Alipay payment
where the parameter in the initialization section: appid is the unique flag id of the paypal.
app_notify_url is the URL address for asynchronous notification to the merchant's backend server when payment is completed in the Alipay backend.
app_private_key_path is the self-generated private key.
alipay_public_key_path is the public key of paypal. sign_type is the encryption method of the signature.
When debug is true, the url of paypal payment is informal environment, if it is online environment you can change it to false.
Call interface part of the parameters: out_trade_no for the order number this is the merchant backend server generated by the order number.
total_amount is the payment amount (must be of string type), subject is the title, return_url is the page to which the payment page will be redirected after successful payment.
notify_url is the url for asynchronous notifications.
Step Six:
Running this code will result in a signed url.
Visit this url and see the results as:
You can download a paypal for the sandbox environment on your phone, or you can click on the right side to log in to your account to make a payment. The account number and password used to make the payment are provided in the sandbox application.
Step Seven:
Login to complete payment
Instance Extension:
Python docking paypal payment since the implementation of the function
# -*- coding: utf-8 -*- import base64 import json import from datetime import datetime import requests from import default_backend from import hashes from import serialization from import padding class AliPayException(Exception): def __init__(self, data): super(AliPayException, self).__init__() = data def __str__(self): return "alipay - {}".format() def __unicode__(self): return u"alipay - {}".format() class AliPayVerifyException(AliPayException): def __init__(self, msg, data): super(AliPayVerifyException, self).__init__('alipay verify except - {}:{}'.format(msg, data)) class AliPay: def __init__(self, **kwargs): """ :param kwargs:
This article on the python access to Alipay example of the operation of the article is introduced to this, more related python how to access the content of Alipay please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!