One. Preparing the Environment.
1. First need to register in the pay package developer mode and register the sandbox to simulate the payment process.
https://openhome.alipay.com/platform/appDaily.htm?tab=info
2. Then read the developer documentation, familiar with the Alipay payment process:
https://docs.open.alipay.com/270/105898/
3. App for creating on-line, to be set up license /
https://openhome.alipay.com/platform/appManage.htm#/create/1-ab6b-4f9c4e5c01f11487667522647
4. Installation generating an RSA key generator
https://docs.open.alipay.com/291/105971
5. There github open source framework, the development may be used:
https://github.com/fzlee/alipay
Applications need to be installed in python: pip install python-alipay-sdk --upgrade
6. The public and private key set mode:
"" " # Alipay_public_key.pem ----- BEGIN the PUBLIC KEY ----- Alipay public ----- END the PUBLIC KEY ----- # app_private_key.pem ----- BEGIN RSA PRIVATE KEY- ---- user's private key ----- END RSA PRIV
7. Alipay link
"" " Development: https: //openapi.alipay.com/gateway.do sandbox: HTTPS: //openapi.alipaydev.com/gateway.do " ""
two. Analysis of the payment process.
1. First front-end to submit orders and order information (order amount, order names, order of payment)
2. Background to accept data views, generate orders (default unpaid), generates a payment link via order_string, return to the front.
3. After receiving the distal link by way of open jump, the user performs two-dimensional code payment:
then(response =>{ let order_url = response.data.order_url; window.open(order_url,'_self') })
4. After successful payment, the payment will be sent as the front page success, then jump to a specific page rendering (from rendering).
5. asynchronous call back request interface is divided into eight times, prevent server attacks.
three. Write damo and packing:
Test phase:
from alipay Import AliPay app_private_key_string = "" " ----- the BEGIN the RSA PRIVATE KEY ----- ----- ----- the END the RSA PRIVATE KEY " "" alipay_public_key_string = "" " ----- the pUBLIC KEY ----- BEGIN ----- END the pUBLIC KEY ----- "" " alipay = AliPay ( AppID = " 2016101600696171 " , app_notify_url = None, # default callback url app_private_key_string = app_private_key_string, # Alipay public key, verify Alipay return messages using, not your own public key,alipay_public_key_string=alipay_public_key_string, sign_type="RSA2", # RSA 或者 RSA2 debug=True # 默认False ) import time from luffyapi.libs.iPay import PAY_URL,alipay order_string = alipay.api_alipay_trade_page_pay( out_trade_no=str(time.time()), total_amount=0.01, subject='购买商品', return_url="http://localhost:8080/", notify_url="https://example.com/notify " # Alternatively, do not fill the default URL Notify ) order_url = PAY_URL + order_string IF the __name__ == ' __main__ ' : Print (order_url)
Use the application to pay more for the public packet public key and private key configuration together.
The package structure:
libs ├── iPay # aliapy second package package │ ├── __init__ .py # package file │ ├── Keys # key folder │ │ ├── alipay_public_key.pem # Alipay public │ │ └── app_private_key. PEM # application private └── └── settings.py # application configuration
Import os # Alipay application of the above mentioned id APP_ID = ' 2,016,093,000,631,831 ' # default asynchronous callback address, usually set on the line None APP_NOTIFY_URL = None # application private key file path APP_PRIVATE_KEY_PATH = os.path.join (os.path.dirname ( __FILE__ ), ' Keys ' , ' app_private_key.pem ' ) # Alipay public key file path ALIPAY_PUBLIC_KEY_PATH = os.path.join (os.path.dirname ( __FILE__ ), ' Keys ' , ' alipay_public_key.pem ' ) # signatures way sIGN_TYPE =' RSA2 ' # if a test environment - whether Alipay sandbox DEBUG = True # payment connection DEV_PAY_URL = ' https://openapi.alipaydev.com/gateway.do? ' PROD_PAY_URL = ' https://openapi.alipay.com /gateway.do? '
from alipay Import AliPay from .settings Import * # Foreign objects provide payment alipay = AliPay ( AppID = APP_ID, app_notify_url = APP_NOTIFY_URL, app_private_key_path = APP_PRIVATE_KEY_PATH, alipay_public_key_path = ALIPAY_PUBLIC_KEY_PATH, sign_type = sign_type, Debug = DEBUG ) # provide external payment link prefix pay_url DEV_PAY_URL = IF DEBUG the else PROD_PAY_URL
----- BEGIN PUBLIC KEY -----
Alipay public
----- END PUBLIC KEY -----
----- BEGIN RSA PRIVATE KEY -----
use a private key
----- END RSA PRIVATE KEY -----
Occasionally, the back-end configuration of money in dev interfaces and synchronous callback interface, address asynchronous callback interface.
# Foreground and the base_url UP_BASE_URL = ' http://127.0.0.1:8080 ' END_BASE_URL = ' http://127.0.0.1:8000 ' # alipay Callback Interface Configuration # must be replaced with official website address on the line # synchronization callbacks interface (get ) is generally provided when the front page url separating the front and back return_url UP_BASE_URL + = ' / Pay / Success ' # asynchronous callback interface (POST), must be set to a background server interface notify_url END_BASE_URL + = ' / Order / Success / '