安装alipay
的sdk
安装
pip install python-alipay-sdk
在项目的libs
下新建一个ali_pay
的包
# 新建pay.py文件
from alipay import AliPay
from . import settings
# alipay实例
alipay = AliPay(
appid=settings.APPID,
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_TYPE,
debug=settings.DEBUG
)
# 支付宝网关接口
gateway = settings.GATEWAY
alipay
的配置文件settings.py
import os
def obtain_key(key_store_path):
with open(key_store_path, mode='r', encoding='utf-8') as fr:
key = fr.read()
return key
# 应用的ID号
APPID = "2021000116664633"
# 应用私钥&支付宝的公钥
APP_PRIVATE_KEY_STRING_PATH = os.path.join(os.path.dirname(__file__), 'key', 'private_key.pem')
ALIPAY_PUBLIC_KEY_STRING_PATH = os.path.join(os.path.dirname(__file__), 'key', 'public_key.pem')
APP_PRIVATE_KEY_STRING = obtain_key(APP_PRIVATE_KEY_STRING_PATH)
ALIPAY_PUBLIC_KEY_STRING = obtain_key(ALIPAY_PUBLIC_KEY_STRING_PATH)
# 签名类型
SIGN_TYPE = 'RSA2'
# 是否启用沙箱测试环境
DEBUG = True # False则启用正式环境
# 支付宝网关接口
GATEWAY = "https://openapi.alipaydev.com/gateway.do?" if DEBUG else "https://openapi.alipay.com/gateway.do?"
封装:
`__init__.py`
from .pay import alipay, gateway
# 外部使用方式
from ali_pay import alipay,gateway