支付寶介面的二次封裝
阿新 • • 發佈:2020-07-29
安裝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