环境准备
1.登录支付宝的开发平台-沙箱环境
https://openhome.alipay.com/platform/appDaily.htm?tab=info
2.下载秘钥生成工具,然后把自己的公钥保存到信息配置里面的公钥
3.下载安装沙箱版钱包
4.点左上角的沙箱账号,并用账号登录
配置Django项目
urls.py
urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^$', AliPayView.as_view()), ]
views.py
from django.shortcuts import render, redirect from django.http import HttpResponse from rest_framework.views import APIView from rest_framework.response import Response from alipay.aop.api.DefaultAlipayClient import DefaultAlipayClient from alipay.aop.api.AlipayClientConfig import AlipayClientConfig from alipay.aop.api.domain.AlipayTradePrecreateModel import AlipayTradePrecreateModel from alipay.aop.api.request. AlipayTradePrecreateRequest import AlipayTradePrecreateRequest from alipay.aop.api.domain.AlipayTradePagePayModel import AlipayTradePagePayModel from alipay.aop.api.request.AlipayTradePagePayRequest import AlipayTradePagePayRequest import time # Create your views here. # 沙箱环境地址:https://openhome.alipay.com/platform/appDaily.htm?tab=info # "https://openapi.alipaydev.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2" def ali_pay(): # 为阿里支付实例化一个配置信息对象 alipay_config = AlipayClientConfig(sandbox_debug=True)#测试环境开启debug模式 # 初始化各种配置信息 # 阿里提供服务的接口 alipay_config.server_url = "https://openapi.alipaydev.com/gateway.do" # 申请的沙箱环境的app_id alipay_config.app_id = "2016092300575239" # 商户的私钥 with open("keys/app_private_key.txt") as f: alipay_config.app_private_key = f.read() # 阿里的公钥 with open("keys/alipay_public_key.txt") as f: alipay_config.alipay_public_key = f.read() # 实例化一个支付对象并返回 alipay_client = DefaultAlipayClient(alipay_client_config=alipay_config) # print(alipay_client) return alipay_client class AliPayView(APIView): def get(self, request): return render(request, "pay.html") # 生成支付宝自带页面的API def post(self, request): # 得到阿里支付的实例化对象 client = ali_pay() # 为API生成一个模板对象 初始化参数用的 model = AlipayTradePagePayModel() # 订单号 model.out_trade_no = "pay" + str(time.time()) # 金额 model.total_amount = 88888 # 商品标题 model.subject = "保时捷卡宴一辆" # 商品详细内容 model.body = "保时捷卡宴白菜价大放送" # 销售产品码,与支付宝签约的产品码名称 model.product_code = "FAST_INSTANT_TRADE_PAY" # 实例化一个请求对象 request = AlipayTradePagePayRequest(biz_model=model) # get请求 用户支付成功后返回的页面请求地址 request.return_url = "http://140.143.63.45:8000/alipay_handler" # post请求 用户支付成功通知商户的请求地址 request.notify_url = "http://140.143.63.45:8000/alipay_handler" # 利用阿里支付对象发一个获得页面的请求 参数是request response = client.page_execute(request, http_method="GET") print(response) return redirect(response) class PayHandlerView(APIView): def get(self, request): # return_url的回调地址 print(request.data) # 用户支付成功之后回到哪 return HttpResponse("return_url测试") def post(self, request): print(request.data) # 用户支付成功 在这里修改订单状态以及优惠券贝里等等情况 return HttpResponse("notify_url")
支付宝的支付
1, 客户端的配置信息对象AlipayClientConfig
2, 实例化客户端对象 DefaultAlipayClient(config)
3, 为你的API接口请求生成一个模板对象
存放商品的各种信息以及订单号等等~~~
4, 生成一个API接口请求对象 把模板对象当成参数
AlipayTradePagePayRequest(biz_model=model)
5, 调用客户端的发送请求的方法
response = client.page_execute(request, http_method="GET")
6, 根据response做自己的操作
# get请求 用户支付成功后返回的页面请求地址
request.return_url = "http://140.143.63.45:8000/alipay_handler"
# post请求 用户支付成功通知商户的请求地址
request.notify_url = "http://140.143.63.45:8000/alipay_handler"