python - alipay sdk 使用及注意点

1. 在 https://openhome.alipay.com/platform/appDaily.htm?tab=info 这里拿到自己的 appid  和  支付宝公钥 , 如果想要得到支付宝的公钥就需要 获取 应用的公钥 具体获取方式 : https://alipay.open.taobao.com/docs/doc.htm?treeId=291&articleId=105971&docType=1 这里下载

2. 导入模块

  分别下载:  

pip install Crypto / pip install pycryptodome
pip install alipay-sdk-python

3. 上代码

views:

from alipay import AliPay

def Alipay():
    alipay = AliPay(
        appid='2016092800613180',   # 你的 appid
        app_notify_url='https://127.0.0.1:8099/aliapy_back_url/',  # 默认回调url
        app_private_key_path='app_test/app_private_2048.txt',   # 应用私钥
        # 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥,
        alipay_public_key_path='app_test/alipay_public_2048.txt',   # 支付宝公钥
        sign_type="RSA2",  # RSA 或者 RSA2    # 注意: 2018年1月5日后创建的应用只支持RSA2的格式; 
        debug=True,  # 默认False  设置 True 则为测试模式
    )
    return alipay

def index(request):
    if request.method == 'GET':
        return render(request,'index.html')
    alipay = Alipay()

    order_string = alipay.api_alipay_trade_page_pay(
        out_trade_no="2002",    # 商品标识
        total_amount=0.01,  # 商品价格
        subject='001',  # 商品名称
        return_url="https://example.com",
        notify_url="https://example.com/notify"  # 可选, 不填则使用默认notify url
    )
    print('order_string : ',order_string)
    pay_url = "https://openapi.alipaydev.com/gateway.do?{}".format(order_string)    # 调用支付宝支付接口
    return redirect(pay_url)
alipay 加密导入: 
from datetime import datetime
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA256,SHA
from urllib.parse import quote_plus
from urllib.parse import urlparse, parse_qs
from base64 import decodebytes, encodebytes
import json

猜你喜欢

转载自www.cnblogs.com/chaoqi/p/10441064.html