Django 17 shopping mall project (pay achieve: Alipay sandbox environment)

1, the payment

Here Insert Picture Description

2, Alipay sandbox environment (for payment of testing, not on the real line environment)

https://openhome.alipay.com/platform/appDaily.htm?tab=account

Here Insert Picture Description
Scan to download Alipay sandbox on Android phone, you can log in to this account
Here Insert Picture Description

2.1, python use the PayPal sandbox process

2.1.1, the installation alipay
pip install python-alipay-sdk --upgrade

如果安装出现问题,可以尝试下面代码
pip3 install -i https://pypi.douban.com/simple pycryptodomex
pip3 install -i https://pypi.douban.com/simple python-alipay-sdk

2.1.2, generate keys
先下载工具:https://docs.open.alipay.com/291/105971

上面连接里有安装步骤和生成密钥步骤,注意非中文目录安装

In accordance with the completion of the steps will generate a csr file upload in a sandbox environment

1, after downloading a good tool: generate app key
Here Insert Picture Description
2, the two keys to save themselves, it will automatically generate a txt file in the computer, to find their own look. Then copy the public key to the second application sandbox inside the platform Alipay

Note that this is the second RSA is not the first RSA2
Here Insert Picture Description
Here Insert Picture Description
3, when you are ready Alipay will generate a public key (note the distinction between Alipay public key and a public key applications, not confused, are two things), copy Alipay public key ( where specific copy titles below: 2.1.3, payment interface code)
Here Insert Picture Description
Here Insert Picture Description

2.1.3, the payment interface code

Here I created a new interface code py file write
Here Insert Picture Description
Here Insert Picture Description
code, there are my key and a private key, is probably dead, replaced by your own Alipay public key and a private key applications on OK

from alipay import AliPay


def pay(order_id, price, subject, return_url=None, notify_url=None):
    alipay_public_key_string = """-----BEGIN PUBLIC KEY-----
    MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIgHnOn7LLILlKETd6BFRJ0GqgS2Y3mn1wMQmyh9zEyWlz5p1zrahRahbXAfCfSqshSNfqOmAQzSHRVjCqjsAw1jyqrXaPdKBmr90DIpIxmIyKXv4GGAkPyJ/6FTFY99uhpiq0qadD/uSzQsefWo0aTvP/65zi3eof7TcZ32oWpIDAQAB
    -----END PUBLIC KEY-----"""

    app_private_key_string = """-----BEGIN RSA PRIVATE KEY-----
    MIICXQIBAAKBgQCgaYHnBs4xmVUeJ06Wncybr4OOu0svQbD/g7kfud2aMPoB3UETNWs0lxu2CtzvdlUtLFSQSI2QfAVeB2fVULILUzyh0HCHMa11/hdAuXBYOTP4Xhe5QlGUTNpBJjqLKsFxLc3PmVptHHRtHlhfdF4NOLdbPfBEB8Fw2oyTo2RhbwIDAQABAoGBAJRXvbemBXy8rYhLFVQX7aVztBeEgMzc1RAWAlaijZoP/MNIlutqlQ93Rjsc5J/WMIKr4i/jyHZ7GoOQGaedDmgORxhw2ji29vgKllsuHzAD5/JzFLIr4T1N//bU6gb7DbTml7dHyEHDLg/nsPVZFmKNg7h3SudPMnpobNPfi4eBAkEA3pTFkd7yzs60Bl559y2UHwYmMFPxmgNJspsVtynv4LS+Fb5a4xDJrMeAjJt16UGE4Xm1669jd5ReckHD7ddUTwJBALh/Lh1BYEYdTXANtIjSPOvKh7Jgc44aQ3nCQP92imwmvI3zIVlnXEXIVVNc1k6dzA1Ppgj8jBMAuLYItn+uOOECQQDU4oIcxJqDRpxUwyPwT/2ttpnr+z3HSoHAfChG6atuxjBQZ6JSLwpVYPMIiOA72tiXN2vSIgwGoTe8HD6jSyJtAkAEtmrtIGBfKhxyQkdcP1KDC1dP/Rq2hIE4uPeEDvkWLh8e2Rj++Z7nwWg8iuCGfY1awbASBrFlQt10+OAAfujBAkBJHaKdVyVmdK9Xqdb4IOopO511kOSIQBZwPnfgkFdBpRrPF+Bt4ROxBi9hP5UwKosONAhi508L5QZDggPht2w
    -----END RSA PRIVATE KEY-----"""

    # 实例化支付应用
    alipay = AliPay(
        appid="20161021007289",
        app_notify_url=None,
        app_private_key_string=app_private_key_string,
        alipay_public_key_string=alipay_public_key_string,
        sign_type="RSA"
    )

    # 发起支付请求
    order_string = alipay.api_alipay_trade_page_pay(
        out_trade_no=order_id,  # 订单号,多次请求不能一样
        total_amount=str(price),  # 支付金额
        subject=subject,  # 交易主题
        return_url=return_url,
        notify_url=notify_url
    )

    return "https://openapi.alipaydev.com/gateway.do?" + order_string

3, routing, and view (availability test)

Routing
Here Insert Picture Description
view
Here Insert Picture Description
access: You can pay
Here Insert Picture Description

4, an error may occur

4.1 RSA? RSA2?

Problem:
Here Insert Picture Description
Solution:

如果出现如上图的问题,
可能是你下图这句代码没有写,
也可能是你生成密钥的时候没有生成RSA格式,重新生成并上传到沙箱就好

Here Insert Picture Description
Here Insert Picture Description

Published 87 original articles · won praise 20 · views 1620

Guess you like

Origin blog.csdn.net/a__int__/article/details/103771846