Bootstrap

python支付宝支付pc和h5代码封装

coding=utf-8

from alipay.aop.api.AlipayClientConfig import AlipayClientConfig
from alipay.aop.api.DefaultAlipayClient import DefaultAlipayClient
from alipay.aop.api.domain.AlipayTradeWapPayModel import AlipayTradeWapPayModel
from alipay.aop.api.request.AlipayTradeWapPayRequest import AlipayTradeWapPayRequest
from alipay.aop.api.domain.AlipayTradePagePayModel import AlipayTradePagePayModel
from alipay.aop.api.request.AlipayTradePagePayRequest import AlipayTradePagePayRequest

def alipay_config(order_no, pay_way, method):
# pay_way:支付方式,1代表pc支付,2代表h5支付
    clientConfig = AlipayClientConfig()                                             # 支付宝封装的配置文件,默认是RSA2加密,这个类可以自己定义
    clientConfig.app_id = "appid,开发者应用id"                                       # 开发者应用id
    clientConfig.app_private_key = app_private_key.replace("\r\n", "\n")            # 开发者应用私钥
    # clientConfig.alipay_public_key = public_key                                   # 蚂蚁金服开放平台公钥,可以不传
    if pay_way == 1:
        model = AlipayTradePagePayModel()                                           # 业务参数初始化
        model.product_code = “FAST_INSTANT_TRADE_PAY”                               # 销售产品码,商家和支付宝签约的产品码
    else:
        model = AlipayTradeWapPayModel()                                            # 业务参数初始化
        model.product_code = “QUICK_WAP_WAY”                                        # 销售产品码,商家和支付宝签约的产品码
    model.subject = “男士裤子”                                                       # 商品的标题/交易标题
    model.body = “具体大小等等”                                                       # 对一笔交易的具体描述信息
    model.out_trade_no = order_no                                                   # 商户网站唯一订单号
    model.timeout_express = “90m”                                                   # 订单最晚交易时间,超过关闭
    model.total_amount = “49.99”                                                    # 订单交易金额
    if pay_way == 1:
        ali_request = AlipayTradePagePayRequest(biz_model=model)                    # 获取pc站支付请求
        ali_request.notify_url = “”                                                 # 服务器通知地址
        ali_request.return_url =  “”                                                # 页面通知地址
    else:
        ali_request = AlipayTradeWapPayRequest(biz_model=model)                     # 获取wap站支付请求
        ali_request.notify_url =  “”                                                                               # 服务器通知地址
        ali_request.return_url =  “” l                                                                            # 页面通知地址
    client = DefaultAlipayClient(alipay_client_config=clientConfig)
    response_url = client.page_execute(ali_request, http_method=method)
    return response_url
    ```
    调用时候传
    alipay_config(order_no, pay_way, method)
    order_no:订单号,随机生成
    pay_way:支付方式,1代表pc支付,2代表h5支付
    method:请求方式
;