zoukankan      html  css  js  c++  java
  • 支付宝沙箱测试

    支付宝沙箱测试

    本文主要介绍PC端浏览器访问网站时的支付宝支付功能,首先了解一下网站PC端支付宝支付需要进行的流程:

    网站如果需要支持支付宝在线支付功能可以先对支付功能进行沙箱测试,测试成功换用支付宝的真是支付接口即可。

    沙箱测试步骤如下

    1.安装SDK,由于支付宝的python版官方SDK使用比较繁琐,这里使用非官方的python-alipay-sdk,该SDK目前已经支持了大部分应用场景下的支付宝支付功能GitHub链接为python-alipay-sdk

    # 安装python-alipay-sdk
    pip install python-alipay-sdk --upgrade
    #安装pyopenssl
    pip install pyopenssl
    

    2.支付测试

    from alipay import AliPay
    
    # 公钥私钥配置
    app_private_key_string = """-----BEGIN RSA PRIVATE KEY-----
    MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCUcOQ4Art/U4kqvBroL10A33lDNnRXg0mIBUM4GoT9HwlC2J5wBlIY9XlN9RR1AwLBQGthfTnJF7WvkLzNlxK5bapGalGuqQdxYagMTftqoR8qC9PCmIMWIjGuD9MSRctmUHYySn3u9y+AJx98fW5EaLVmwFeuhRtGpkI6VBjTKJorr0T40OkX/WrAdJJ/2WpqOhM+XF+TxmER6JTlHABxu3eJKNXZg5IPzrZmj9DqlheKvMh8fvsEMVGQnmrUimJ6G8fXCdmVrISXM7JeOcWTwUbtMkk9iIUeRIHgMRYZCOEnld26BzUxqn5+N81c0NBQsCe4Y2KUyFxHNEZZCPwrAgMBAAECggEAQ8Qx+/wLWvpo8XCEnCEbRU3AT1Xp0RXnmMhGQrOgqOnlUXYRBrVr/2b5ZQSQvUaNmGnvW8dOfSZs1GSwPO9OJgcRqCnbdnBGLJXXoR5su+YokwppNzpnBdDAe2VQB9SDIgdznkvxqAmCzu+NaUjd8S8YOXtvzAWKSTuXmodP+f2pY9Oz/3/UupoeWLtl88WvwkfgwDM7jSB6/aZ5syfPlPS2uBStH+BU9p8hq7OUt4rD4O9HGqr2MP6hivhmK8QM48nTY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/DqzKwf7F9m8hyGqJM97af5IRkEdVCvFI5U1Y8xZsR8mWj+YhIU9rv48zZn81uJ7OqkkWXc/ENCMqGTiEe4tKPniLibTdpaIgPNn9c3QSa03psvJI8v/n5+0rs+KKXxN8UwLcmMMN5Zfy8Ejvq/rax9EXepxLqSP7xQ8DXHRBCkFHUY6W2vdIKQZFc8wqMqglRjGjfN8OgYwaN2F6TPPPHdoVbpjduEx1RlACItapHNWv21YTr0PYx+edb3Oj+Tjfinzuyb9S0uXDEHOOGeLrerOSJr3rVwDJpFKye6Lojz9H7aV+gki1Mp4W2qykyefYEmkDtYwIDAQAB
    -----END PUBLIC KEY-----"""
    
    # 生成alipay对象(完成配置)
    alipay = AliPay(
        appid="2016093000631832",
        app_notify_url=None,  # the default notify path
        app_private_key_string=app_private_key_string,
        # alipay public key, do not use your own public key!
        alipay_public_key_string=alipay_public_key_string,
        sign_type="RSA2",  # RSA or RSA2 加密算法
        debug=True  # False by default debug为True是沙箱模式
    )
    
    # 生成订单信息
    import time
    order_string = alipay.api_alipay_trade_page_pay(
        out_trade_no='%s' % time.time(),#订单号,这里用时间戳实现,真实环境建议使用其他算法生成
        total_amount=1000000,#支付金额
        subject='小飞机',#订单名称
        return_url="http://127.0.0.1:8080",#同步回调的前端url,这里一定要填否则生成的支付链接无法正常放问
        notify_url="https://example.com/notify"  # this is optional,#异步回调的后台url链接,用于确认支付成功,防止用户在支付过程中非法更改前端页面向后端发送虚假支付信息
    )
    
    #支付宝网关,去掉dev就是真实支付环境
    gateway = 'https://openapi.alipaydev.com/gateway.do'
    # 拼接得到支付链接,链接中的?是必须要加的
    pay_url = '%s?%s' % (gateway, order_string)
    
    print(pay_url)
    
    

    阿里公钥指的是支付宝公钥,我们需先在电脑上安装”支付宝开放平台开发助手“生成”应用私钥“和”应用公钥“,然后使用应用公钥去下图

    点击“设置/查看”,复制到对应的弹框生成支付宝公钥,具体如下图:

    测试成功后,我们可以对支付功能封装成包,以方便之后的使用。

    结构

    libs
        ├── aliPay  					  # aliapy二次封装包
        │   ├── __init__.py 				# 包文件
        │   ├── pay.py						# 支付文件
        └── └── settings.py  				# 应用配置  
    

    settings.py

    #应用id
    APP_ID = "20161017007089"
    
    #是否采用沙箱环境
    DEBUG = True
    
    #签名算法
    SIGN = 'RSA2'
    
    #支付宝网关
    if DEBUG:
        GATEWAY = 'https://openapi.alipaydev.com/gateway.do?'
    
    else:
        GATEWAY = 'https://openapi.alipay.com/gateway.do?'
    
    
    
    # 应用私钥:首行尾行是固定的,中间是一行私钥字符串(不能有其他字符,否则报错)
    APP_PRIVATE_KEY_STRING = """-----BEGIN RSA PRIVATE KEY-----
    MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCzIlk75nL5bNF/7rm3nGFoxeZM6yoJ1+bK4Gw5FBjUZZxgH0LBzblshlQsYPy1vjvXp+linCYU1K3tv7SdR9VaBEuSBrabsoeAyv/IvWdIfMogUJqSEJcO8tdijJXyyC2E3k1KesTA3wyMnBIfjviemAdp327JdFVxpq2A8zXtKMKr9htf9LPdQQTaMxP65oiKeDn3QElCthZMuMPgwoqYP+ShtyktFCqRqzpm7yXeQurrdAH4XRGtbLJkM2Zg0L3z3PXUAO0k4dEQI5tLJopT9iQgsI+XM74N+EhH7ouFYLyPQoRDFRx4Ln10YqdadTqL+AzfHpYm6S8zPgodHrEfAgMBAAECggEAYj8sgdXaqUIthOUBptp18mjX7sfrvopTYmHD+Wo0VAmGwpFio7iZQhRKhiyLbYJwf87wCc1qdXJQEl1aSgjj0wMYD3bvIGXCO7kaoJaA1xjaIu8SV5oRv3kg+EPy2b1AeecSrQ64aq/qyhOvPO/FG/Z12nNY59SOKE8qSdcadH9CeLr32XgYfNGA5Oah39zAQb5hBhz3/YYbifAzwxHjnDk90ufYwsVJ9FtiNONAc9h48W6X2pWQHTcG4jvK/xxh/GAAyzAhjD+LkJZooEkbIk4X2ZOaRL+6iW4Wcs3IiSyEsYyhZK41QcCtFySb9mnq/OYkD1SPb9P506NRjs2nQQKBgQDsaJ0q6it3aexlQ4+gBPd83at1VYdc+HXxaU5fB+zAzxADOrZJph0H8q5mLlUqLwZR/HqWPxz34z8M+MphbK14NjMJjzDkKyUwzOikWnSB0fiNlf3l3cGmfe6Iow1yypYi6dwhXrnxLIYixdTYExEsoiqgsUOWtwMC+b/VBunk5QKBgQDB+qmO8KFH7MreN9TN4cYFXHimSaY/dZ++q5ujjlNDGr6JoJ/Qd2ylbgGC6YjoqROYsSgwr8DoXta57CkNponwJ73ines8kOPxHN7SxM93XJrGEDqG6JvclSlfO1OnlGjXiZXidJGuzMiOBRc3o+FlGOowfBTIIz1zVdTGtMfBswKBgG5DUOLeO43Dur10aH3DM0drAHiGfs8vF5uUeTg7swIu0ZIgBTuOncXCO/NusswtCK6eqvJ+110LAbJkK8iK7tG9vco8nhodwYfkWrGleue97kbjaXuYFaU44VObcT9UG/iGyORLkUBaeumnCeIJ6p29uVIfu52FGIN/xyhtxBO9AoGAYGYqh3+sFutlcgO6/Mnbt+RZjQIC3Xyd84qi4Sgk1q9ey1SIt3fyBVBi7eUTBdSoiXRY7cEaFCC1g1YKnN8Lo86MPieOtN2oebWIQqROGhpqTE3aWBsXAgsasVnxW9GUPLECTLTzhTP94x5CiIMnyy8Tix7O0Rs6OawI/urubqMCgYEA45vernjpiB0hb/vP6Q1Q4B6L8ulXrCKfVbL3dehmp/hsM8+c80xhwLQInm7b1mWGc66XLggr48r5zYwRGj0ZlGIuFK4emGr4m3dSBJ9vGU8QtL56vLC1sP1BrJO/vnrcWXnFIom6tn01qnmUrnh4vnr+LxelwjjIYdVh+jZweVE=
    -----END RSA PRIVATE KEY-----"""
    
    # 阿里公钥:首行尾行是固定的,中间是一行公钥字符串(不能有其他字符,否则报错)
    APLIPAY_PUBLIC_KEY_STRING = """-----BEGIN PUBLIC KEY-----
    MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAi2LCKoe5lEntKcmyF7jkPAlxDlUV1k77jHvps4TW3rUR6Ylj74RXmSTd3bCUrUuw/fNl5bnNrd/tiV3wbljwkDP7DFKKiuUCWlQPzEaoqf9mOaWQyT+ov6+8XFK+s/2jpeA5v1XVjz2KCmk8XGswZqG057/gQVX2ZjW8lhp8kobGmY+b17331KyGcAkCIOwcSeu//Pafgqpsk8sjNtOWCXDw1e4WW8GjODXc+ztYk8IPGGxdpXH5038ug41m0+yPioRSOP+pGN76ZLNc9nKImVCw5dGrsW2h2/KvmNvfn0PsjFo34Xs4K2kL4YWshc/89s6yaThi2Ww6Xox9GZ/pgwIDAQAB
    -----END PUBLIC KEY-----"""
    
    
    

    pay.py

    from alipay import AliPay
    from .settings import *
    
    alipay = AliPay(
        # 应用ID,可以为真实应用,对应debug设置False,可以为沙箱应用,debug设置True
        appid=APP_ID,
        # 该通知接口一般都设置None
        app_notify_url=None,
        # 应用私钥
        app_private_key_string=APP_PRIVATE_KEY_STRING,
        # 阿里pay公钥
        alipay_public_key_string=ALIPAY_PUBLIC_KEY_STRING,
        # 签名算法,采用RSA2
        sign_type=SIGN,
        # 是否是沙箱环境
        debug=DEBUG
    )
    

    __init__.py

    from .settings import GATEWAY as alipay_gateway
    
    from .pay import alipay
    

    使用测试:

    from libs.aliPay import alipay,alipay_gateway
    
    import time
    
    order_string = alipay.api_alipay_trade_page_pay(
        out_trade_no="%s"%time.time(),
        total_amount=10000,
        subject='test1',
        #前端同步回调链接
        return_url="http://127.0.0.1:8080",
        #后端异步回调链接
        notify_url="https://example.com/notify" # 可选, 不填则使用默认notify url
    )
    
    #支付链接
    pay_url = alipay_gateway + order_string
    print(pay_url)
    
    
  • 相关阅读:
    Mysql查漏补缺
    RabbitMQ学习笔记
    memcache学习笔记
    Redis问题整理
    JedisCluster获取key所在的节点
    JavaSE编程题
    IDEA快捷键 日常整理
    Idea 常用快捷键列表
    【C++】 构造函数为什么不能声明为虚函数,析构函数可以
    【算法笔记】买卖股票问题--DP/贪心算法
  • 原文地址:https://www.cnblogs.com/ghylpb/p/12194073.html
Copyright © 2011-2022 走看看