支付宝沙箱的使用
https://opendocs.alipay.com/open/200/105311
OpenSSL> genrsa -out app_private_key.pem 2048 # 私钥RSA2
OpenSSL> rsa -in app_private_key.pem -pubout -out app_public_key.pem # 导出公钥
开发者将私钥保留,将公钥提交给支付宝配置到开发平台,用于验证签名。
将公钥文件去除头尾、换行和空格,转成一行字符串。把该字符串提供给支付宝账号管理者,登录开放平台上传应用公钥并获取支付宝公钥
# 参考网址
https://opendocs.alipay.com/open/291/106130
如果需要使用文件方式(如PHP/.NET版本)读取支付宝公钥,需要在头尾加入标示后保存至文件
例如:
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDI6d306Q8fIfCOaTXyiUeJHkrIvYISRcc73s3vF1ZT7XN8RNPwJxo8pWaJMmvyTn9N4HQ632qJBVHf8sxHi/fEsraprwCtzvzQETrNRwVxLO5jVmRGi60j8Ue1efIlzPXV9je9mkjzOmdssymZkh2QhUrCmZYI/FCEa3/cNMW0QIDAQAB
-----END PUBLIC KEY-----
import json
from base64 import decodebytes, encodebytes
from datetime import datetime
from urllib. parse import quote_plus
from Crypto. Hash import SHA256
from Crypto. PublicKey import RSA
from Crypto. Signature import PKCS1_v1_5
class AliPay ( object ) :
"""
支付宝支付接口
"""
def __init__ ( self, appid, app_notify_url, app_private_key_path,
alipay_public_key_path, return_url, debug= False ) :
self. appid = appid
self. app_notify_url = app_notify_url
self. app_private_key_path = app_private_key_path
self. app_private_key = None
self. return_url = return_url
with open ( self. app_private_key_path) as fp:
self. app_private_key = RSA. importKey( fp. read( ) )
self. alipay_public_key_path = alipay_public_key_path
with open ( self. alipay_public_key_path) as fp:
self. alipay_public_key = RSA. import_key( fp. read( ) )
if debug is True :
self. __gateway = "https://openapi.alipaydev.com/gateway.do"
else :
self. __gateway = "https://openapi.alipay.com/gateway.do"
def direct_pay ( self, subject, out_trade_no, total_amount, return_url= None , ** kwargs) :
biz_content = {
"subject" : subject,
"out_trade_no" : out_trade_no,
"total_amount" : total_amount,
"product_code" : "FAST_INSTANT_TRADE_PAY" ,
}
biz_content. update( kwargs)
data = self. build_body( "alipay.trade.page.pay" , biz_content, self. return_url)
return self.