“This is the 22nd day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
Alipay develops detailed processes
1. Formal environment (business license and other information required)
-
Alipay Open Platform – development documents
-
Here we choose the computer website to pay
- To use it, you need a business license, etc. We use sandbox environment here
2. Sandbox environment (simulating real environment)
-
The sandbox environment
2.1 Apply for enabling sandbox Environment
- Sandbox environment application
- After you confirm the information, the following screen is displayed
- After successful registration, two values are obtained:
- APPID: XXXX
- Alipay Gateway
- Openapi.alipaydev.com/gateway.do (…
- Openapi.alipay.com/gateway.do (…
2.2 Generating a Key
- The key is used to encrypt and verify the parameters added to the URL
2.2.1 Downloading the Key Generator
2.2.2 Generating a Key
- A pair of keys is generated, two at the same time
txt
file- Application of public key
- Application of the private key
- We put two files in the project for later use
2.2.3 Upload the application public key and obtain the alipay public key
- Click on the
Save Settings
After will generate alipay public key
- Then put the alipay public key in the project, convenient for later use
- So far, we have obtained three keys:
- Application of public key
- Alipay generated after the public key is useless
- Application of the private key
- After the URL in the incoming data for signature encryption
- Alipay public Key (generated by applying public key)
- When the page jumps back after successful payment, verify the value passed to us by Alipay
- Application of public key
3. Account information and test APP
- Download sandbox Alipay app [Android version only]
- Then view the sandbox account login
- Buyers information
- The seller information
Note: Do not log in using your Own Alipay account
4. SDK & API
There are usually two types of support
SDK, ready-made Python modules [preferred]
1. Install modules 2. Implement desired functions based on modulesCopy the code
API, provides a URL
1. Manually process and encrypt the URLCopy the code
4.1 SDK
- Access documentation is implemented through tools, and we need to implement through code, click download development kit
4.2 API
- Pay the API
- To use the payment function, select it here
Unified receiving, ordering and payment page interface
Parameters of structure
# jump to this address: [gateway? Parameter] compositionThe gateway params = {= https://openapi.alipaydev.com/gateway.do'app_id': 'xxxx'.'method': 'alipay.trade.page.pay'.'format': 'JSON'.'return_url': 'Page address to jump to after payment (GET request)'.'notify_url': 'Jump to the return_url and send a POST request to that address'.'charset': 'utf-8'.'sign_type': 'RSA2'.'sign': 'signature'.'timestamp': 'yyyy-MM-dd HH:mm:ss'.'version': '1.0'.'biz_content': {
'out_trade_no': 'Order Number'.'product_code': 'FAST_INSTANT_TRADE_PAY'.'total_amount': 88.88.'subject': 'Order Title'}}Copy the code
If the server goes down after the payment is successful, what should I do?
Send a request to notify_URL, the payment is successful, the status is updated, the server is down, and Alipay cannot access, the notification will be within 24 hours: The Alipay server will repeatedly send the notification until the notification exceeds 24 hours and 22 minutes. Generally, 8 notifications are completed within 25 hours (the interval frequency is 4m,10m,10m,1h,2h,6h, and 15h). After receiving the alipay request, the returned data is incorrect, ditto above. Return a success
Payment results are notified asynchronously
5. Alipay signature
- The parameters are processed and then spliced with the gateway
- Request the signature using the build key
- Self-implementing signature
5.1 Signature Principles
Params.pop (sign) 2. Sort (params) sort(params) by ASCII increment of the first character's key (params), by ASCII increment of the second character's key (params) The sorted parameters and their corresponding values are combined in the format of parameter = Parameter value, and these parameters are joined with & characters. In this case, the string to be signed is generated. String to be signed = "app_id= XXXX&method = Alipay.trade.page.pay&...." Dumps (XXX, separators=(',',':'))) 4. Dumps (XXX, separators=(',',':')) Sign the signature string using the SHA256WithRSA signature function of the respective language and the merchant (application) private key. Params [sign] = signature params[sign] = signature params[sign] = signature Base64 encoding cannot have a newline signature inside. Replace ('\n', ") 5. Note: do not appear when concatenating urls; Parse import quote_plus' from urllib.parse import quote_plus'Copy the code
5.2 Signature Implementation
# pip install pycrypto
Windows installation may fail.
Pycryptodome can be downloaded and installed
# Pycryptodome.xxx. WHL installation method:
PIP install Pycryptodome.xxx. WHL
Copy the code
Download is also a bit troublesome, I have downloaded several files on the network disk, you need to download by yourself, I put the version of [Py27, PY35, Py36], other versions can be downloaded by yourself
Note: Install according to your version of Python, for example: Py35 in the file name stands for PYTHon3.5
Link: pan.baidu.com/s/1z1kT-Qjd… Extraction code: KJND
# create a dictionary
params = {
'app_id': "2021000117635347".'method': 'alipay.trade.page.pay'.'format': 'JSON'.'return_url': "http://127.0.0.1:8001/pay/notify/".'notify_url': "http://127.0.0.1:8001/pay/notify/".'charset': 'utf-8'.'sign_type': 'RSA2'.'timestamp': datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
'version': '1.0'.'biz_content': json.dumps({
'out_trade_no': xxx,
'product_code': 'FAST_INSTANT_TRADE_PAY'.'total_amount': xxx,
'subject': "tracer payment"
}, separators=(', '.':'))}Get the string to be signed
unsigned_string = "&".join(["{0} = {1}".format(k, params[k]) for k in sorted(params)])
# SHA256WithRSA(corresponding sign_type is RSA2)
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA256
from base64 import decodebytes, encodebytes
# SHA256WithRSA + Uses the private key to sign the signed string
private_key = RSA.importKey(open("Files/Application Private key 204.txt").read())
signer = PKCS1_v1_5.new(private_key)
signature = signer.sign(SHA256.new(unsigned_string.encode('utf-8')))
Base64 encodes the post-signed execution into a string
sign_string = encodebytes(signature).decode("utf8").replace('\n'.' ')
# assign the generated signature to the sign parameter, concatenating it to the request parameter.
from urllib.parse import quote_plus
result = "&".join(["{0} = {1}".format(k, quote_plus(params[k])) for k in sorted(params)])
result = result + "&sign=" + quote_plus(sign_string)
gateway = "https://openapi.alipaydev.com/gateway.do"
pay_url = "{}? {}".format(gateway, result)
Copy the code
Finally, welcome to pay attention to my personal wechat public account “Little Ape Ruochen”, get more IT technology, dry goods knowledge, hot news