其他分享
首页 > 其他分享> > 45 支付宝支付(二次封装)

45 支付宝支付(二次封装)

作者:互联网

支付宝支付

一、入门

"""
1)支付宝API:六大接口
https://docs.open.alipay.com/270/105900/

2)支付宝工作流程(见下图):
https://docs.open.alipay.com/270/105898/

3)支付宝8次异步通知机制(支付宝对我们服务器发送POST请求,索要 success 7个字符)
https://docs.open.alipay.com/270/105902/
"""
# 1、在沙箱环境下实名认证:https://openhome.alipay.com/platform/appDaily.htm?tab=info

# 2、电脑网站支付API:https://docs.open.alipay.com/270/105900/

# 3、完成RSA密钥生成:https://docs.open.alipay.com/291/105971      #点在线生成(不需要下载)

# 4、在开发中心的沙箱应用下设置应用公钥:填入生成的公钥文件中的内容

# 5、Python支付宝开源框架:https://github.com/fzlee/alipay       #由于电脑网站支付没有 python sdk ,对着API列表里的找又比较麻烦,我们用第三方的sdk(也是扣着API做的封装,本身也是调用了API)
# >: pip install python-alipay-sdk --upgrade

# 7、公钥私钥设置
"""
# alipay_public_key.pem
-----BEGIN PUBLIC KEY-----
支付宝公钥
-----END PUBLIC KEY-----

# app_private_key.pem
-----BEGIN RSA PRIVATE KEY-----
用户私钥
-----END RSA PRIVATE KEY-----
"""

# 8、支付宝链接
"""
开发:https://openapi.alipay.com/gateway.do
沙箱:https://openapi.alipaydev.com/gateway.do
"""

二、支付流程

 

 

三、支付宝支付测试
https://openhome.alipay.com/platform/appDaily.htm?tab=info
# 沙箱环境
    -商户号
    -用户号
# 非对称加密
    -公钥,私钥:
    -加密用公钥加密,解密用私钥解密
# 对称加密:加密和解密的密码一样
# 第三方支付宝sdk
# 生成公钥私钥
    -在本地使用命令生成
    -支付网站,软件生成:https://docs.open.alipay.com/291/105971     点链接,然后点web在线加密(如下图)
    -支付宝支付的话,仅有公钥私钥不行,还需要支付宝公钥---》把咱们的公钥填入---》生成支付宝公钥
 

 

 复制上面的公钥,配置在下图,把咱们的公钥填入---》生成支付宝公钥

 

 

 

 

 

 

 

 pri

-----BEGIN RSA PRIVATE KEY-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QS45jRReFXSL9on830mrGMkdijLEIDB7VnQKKAoZCEtk7IPexWOx/m7rKiM93h3Au2teSEi8asYv08eJ+d5ycLFqmom690Op3gsgqLPWooldocliKIkz7YHa50+NNT10oVh5fE90BLa1V8vsnef0LOBATGwvv6da/94f7/BECgYEA+dZLkbo/Crq4Xn7+++Iq4SG02UDFW9DgXH+E9fEgzq2hAEawXuuJ0wl+igwwG/mHAjEOZoc01WtRsWzbE/z7vxmoT3c8Xt9A/xcs4bU8JBDhc7wW7sREtRx21/ccHdkJ6IGeXodhLZuIRk5WXsr3tcOyP2XtEDwOn7wjh5fFEjkCgYEA21hGsayYAsXxmNTSfCPto9m4pasWRO5uteeGCViJ6S4yoSiIFro9/390Ds62ZQti6SObrTlBg2LW6x5tjbU3LkVbRkaZzlTfgbkOmH56d2500Kv6xoWhowelXCEjjwg5OqsfJsn6Sfmef8oOAH636XNfJuMXtD8gqAJYUqW+8GMCgYARm7vpwnGtBOn3pwW7pP4WrfR8F2eRiBA4FLi/SMZiKrSdO1404m7tU876Vc+SOSz9x41XRpI/tHemhKCQMRVhK6LELwgvO5buI6kDI23gsNvi90+6dlTNWvs5T25I4EIlg/UwfmQn/b1GD0FhHvcEdY17kGh762+wbGcoqFa4aQKBgQDAgLgChRQgEvMXC2jzdGDClXkdRIrV0mAtm1UibKcUk+vxTm4pbCivTH9h439mLNkQ/aRKId7iXWN7lAfX/3u/SzVw9tNTp19FLVGm+hGnMbSp0tXTV0Jln3c75QWdKT14xOghFdBpDf9R7dXeD1h/NTcEHPgXw/N4IB694iMyOwKBgQCzeJ//qo0FZzcKhbpk1zeW+s2wHh984f2DEDUhCdBRNM7s5k9zLyrYhQGToA+d0WRFjak5DJKMah2V8nowu7j4XO3Xy1lfAxk1xCUcjIK/3ZAtW+zHA8gVq7ZBfHnNjSoHVA7b0VCs8wGJO17Nl6R158QTw/fMj+8RiJvFcllCmQ==
-----END RSA PRIVATE KEY-----

alipay/main.py

import os
# 应用私钥
APP_PRIVATE_KEY_STRING = open('./pri').read()

# 支付宝公钥
ALIPAY_PUBLIC_KEY_STRING = open('./pub').read()

from alipay import AliPay
pay=AliPay(
    appid=2021000119627825,
    app_notify_url=None,
    app_private_key_string=APP_PRIVATE_KEY_STRING ,
    alipay_public_key_string=ALIPAY_PUBLIC_KEY_STRING,
    sign_type='RSA2',
    debug=True)



res=pay.api_alipay_trade_page_pay(
            out_trade_no='123456',
            total_amount=float(99.99),  # 只有生成支付宝链接时,不能用Decimal
            subject='精品内衣',
            return_url='http://127.0.0.1:8080/home',
            notify_url='http://127.0.0.1:8080/home',
)
# print(res)
pay_url='https://openapi.alipaydev.com/gateway.do?'+res
print(pay_url)

 

 

 

 

 

 

aliapy二次封装包

GitHub开源框架
https://github.com/fzlee/alipay
依赖
>: pip install python-alipay-sdk --upgrade
# 如果抛ssl相关错误,代表缺失该包
>: pip install pyopenssl
结构

alipay_public_key.pem
-----BEGIN PUBLIC KEY-----
拿应用公钥跟支付宝换来的支付宝公钥
-----END PUBLIC KEY-----
app_private_key.pem
-----BEGIN RSA PRIVATE KEY-----
通过支付宝公钥私钥签发软件签发的应用私钥
-----END RSA PRIVATE KEY-----
setting.py
import os
# 应用私钥
APP_PRIVATE_KEY_STRING = open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'pem', 'app_private_key.pem')).read()

# 支付宝公钥
ALIPAY_PUBLIC_KEY_STRING = open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'pem', 'alipay_public_key.pem')).read()

# 应用ID
APP_ID = '2016093000631831'

# 加密方式
SIGN = 'RSA2'

# 是否是支付宝测试环境(沙箱环境),如果采用真是支付宝环境,配置False
DEBUG = True

# 支付网关
GATEWAY = 'https://openapi.alipaydev.com/gateway.do' if DEBUG else 'https://openapi.alipay.com/gateway.do'
pay.py
from alipay import AliPay
from . import settings

# 支付对象
alipay = AliPay(
    appid=settings.APP_ID,
    app_notify_url=None,
    app_private_key_string=settings.APP_PRIVATE_KEY_STRING,
    alipay_public_key_string=settings.ALIPAY_PUBLIC_KEY_STRING,
    sign_type=settings.SIGN,
    debug=settings.DEBUG
)

# 支付网关
gateway = settings.GATEWAY
_init_.py
# 包对外提供的变量
from .pay import gateway, alipay
补充:在自己项目的配置文件中配置支付宝回调接口:settings.py | dev.py
# 上线后必须换成公网地址
# 后台基URL
BASE_URL = 'http://127.0.0.1:8000'
# 前台基URL
LUFFY_URL = 'http://127.0.0.1:8080'
# 支付宝同步异步回调接口配置
# 后台异步回调接口
NOTIFY_URL = BASE_URL + "/order/success/"
# 前台同步回调接口,没有 / 结尾
RETURN_URL = LUFFY_URL + "/pay/success"

 

标签:alipay,支付宝,公钥,封装,45,-----,KEY,com
来源: https://www.cnblogs.com/Haier123/p/15961530.html