前言:支付宝沙箱环境(Beta)是协助开发者进行接口功能开发及主要功能联调的辅助环境。沙箱环境模拟了开放平台部分产品的主要功能和主要逻辑。在开发者应用上线审核前,开发者可以根据自身需求,先在沙箱环境中了解、组合和调试各种开放接口,进行开发调通工作,从而帮助开发者在应用上线审核完成后,能更快速、更顺利的进行线上调试和验收工作。
一、开通支付宝沙箱环境
沙箱环境地址:https://openhome.alipay.com/platform/appDaily.htm?tab=info
访问上面url,支付宝扫码登录,实名认证,根据提示创建应用,生成相应的沙箱应用环境。生成并上传RSA2(SHA256)的应用公钥,详见生成RSA密钥;
二、python实现的支付宝PC端支付接口
from datetime import datetime
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA256
from urllib.parse import quote_plus
from base64 import decodebytes, encodebytes
import json
class AliPay(object):
"""
支付宝支付接口(PC端支付接口)
"""
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.importKey(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",
# "qr_pay_mode":4
}
biz_content.update(kwargs)
data = self.build_body("alipay.trade.page.pay", biz_content, self.return_url)
return self.sign_data(data)
def build_body(self, method, biz_content, return_url=None):
data = {
"app_id": self.appid,
"method": method,
"charset": "utf-8",
"sign_type": "RSA2",
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"version": "1.0",
"biz_content": biz_content
}
if return_url is not None:
data["notify_url"] = self.app_notify_url
data["return_url"] = self.return_url
return data
def sign_data(self, data):
data.pop("sign", None)
# 排序后的字符串
unsigned_items = self.ordered_data(data)
unsigned_string = "&".join("{0}={1}".format(k, v) for k, v in unsigned_items)
sign = self.sign(unsigned_string.encode("utf-8"))
# ordered_items = self.ordered_data(data)
quoted_string = "&".join("{0}={1}".format(k, quote_plus(v)) for k, v in unsigned_items)
# 获得最终的订单信息字符串
signed_string = quoted_string + "&sign=" + quote_plus(sign)
return signed_string
def ordered_data(self, data):
complex_keys = []
for key, value in data.items():
if isinstance(value, dict):
complex_keys.append(key)
# 将字典类型的数据dump出来
for key in complex_keys:
data[key] = json.dumps(data[key], separators=(',', ':'))
return sorted([(k, v) for k, v in data.items()])
def sign(self, unsigned_string):
# 开始计算签名
key = self.app_private_key
signer = PKCS1_v1_5.new(key)
signature = signer.sign(SHA256.new(unsigned_string))
# base64 编码,转换为unicode表示并移除回车
sign = encodebytes(signature).decode("utf8").replace("\n", "")
return sign
def _verify(self, raw_content, signature):
# 开始计算签名
key = self.alipay_public_key
signer = PKCS1_v1_5.new(key)
digest = SHA256.new()
digest.update(raw_content.encode("utf8"))
if signer.verify(digest, decodebytes(signature.encode("utf8"))):
return True
return False
def verify(self, data, signature):
if "sign_type" in data:
sign_type = data.pop("sign_type")
# 排序后的字符串
unsigned_items = self.ordered_data(data)
message = "&".join(u"{}={}".format(k, v) for k, v in unsigned_items)
return self._verify(message, signature)
这里面的Crypto包不是很好找,需要的可以百度云下载https://pan.baidu.com/s/1aAVe4PIOp2YVhWnPpDtOOg提取码 tmmy
三、支付接口测试的逻辑代码框架
from django.shortcuts import render, redirect, HttpResponse
from utils.pay import AliPay //导入支付宝的PC端支付接口类
import time
def ali():
# 沙箱环境地址:https://openhome.alipay.com/platform/appDaily.htm?tab=info
app_id = "2016092100559183"
# POST请求,用于最后的检测(检测是否支付成功,生成订单)
notify_url = "http://www.wangjifei.com:8804/page2/"
# GET请求,用于页面的跳转展示(获取订单状态,显示给用户)
return_url = "http://www.wangjifei.com:8804/page2/"
# 用户的私钥
merchant_private_key_path = "keys/app_private_2048.txt"
# 支付宝的公钥
alipay_public_key_path = "keys/alipay_public_2048.txt"
alipay = AliPay(
appid=app_id,
app_notify_url=notify_url,
return_url=return_url,
app_private_key_path=merchant_private_key_path,
alipay_public_key_path=alipay_public_key_path, # 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥
debug=True, # 默认False,
)
return alipay
def page1(request):
if request.method == "GET":
return render(request, 'page1.html')
else:
money = float(request.POST.get('money'))
alipay = ali()
# 生成支付的url
query_params = alipay.direct_pay(
subject="Django课程", # 商品简单描述
out_trade_no="x2" + str(time.time()), # 商户订单号
total_amount=money, # 交易金额(单位: 元 保留俩位小数)
)
# 支付宝网关,带上订单参数才有意义
pay_url = "https://openapi.alipaydev.com/gateway.do?{}".format(query_params)
# POST请求重定向到支付宝提供的网关,跳转到支付宝支付界面
return redirect(pay_url)
def page2(request):
alipay = ali()
if request.method == "POST":
# 检测是否支付成功
# 去请求体中获取所有返回的数据:状态/订单号
from urllib.parse import parse_qs
body_str = request.body.decode('utf-8')
post_data = parse_qs(body_str)
post_dict = {}
for k, v in post_data.items():
post_dict[k] = v[0]
print(post_dict)
sign = post_dict.pop('sign', None)
status = alipay.verify(post_dict, sign)
print('POST验证', status)
if status:
# 修改订单状态
pass
return HttpResponse('POST返回')
else:
params = request.GET.dict()
sign = params.pop('sign', None)
status = alipay.verify(params, sign)
print('GET验证', status)
if status:
# 获取订单状态,显示给用户
return HttpResponse('支付成功')
urls.py
from django.conf.urls import url
from API.views import text
urlpatterns = [
url(r'^page1/', text.page1),
url(r'^page2/', text.page2),
]
page1.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="dist/css/bootstrap.css">
</head>
<body>
<form method="POST">
{% csrf_token %}
<input type="text" name="money">
<input type="submit" value="去支付" />
</form>
<script></script>
</body>
</html>
app_private_2048.txt
-----BEGIN RSA PRIVATE KEY-----
用户私钥
-----END RSA PRIVATE KEY-----
alipay_public_2048.txt
-----BEGIN PUBLIC KEY-----
支付宝公钥
-----END PUBLIC KEY-----
四、支付测试
写好这些代码就可以做简单的支付宝支付测试了,代码看着不少,其实真的不多,支付宝接口不是我们写的,直接拿来用就好,逻辑代码前面是沙箱环境的参数配置,后面是简单的page1,page2逻辑代码框架。
访问http://127.0.0.1:8000/page1/进入支付页面,其实就是一个input标签。
填写支付金额,点击支付,界面就会自动跳转到支付宝的支付界面,生成订单的基本信息和一个付款的二维码
-
扫码的时候不能用正式版的支付宝 app 来扫,必须用沙箱环境版的支付宝 app 来扫。当然,可以使用登录帐密来支付,但是一定要注意只能用和应用 ID 对应的沙箱帐号来登录。
4.支付完成后,界面会自动跳转到配置好的 notify_url(检测是否支付成功,生成订单) 和return_url(获取订单状态,显示给用户)路由。
注意:最后的这一步在本地测试时是看不出任何效果的,因为支付宝支付完成后找不到我们本地局域网的url,可以搬到服务器上用外网就可以了