阿里的支付也就是支付寶,官網以及出了Python的SDK了~因此要先下載~~html
pip install alipay-sdk-python
支付寶沙箱環境的地址 https://openhome.alipay.com/platform/appDaily.htm?tab=infopython
注意: django
若是安裝alipay報json
須要先下載一個安裝環境:api
pip install alipay-sdk-python
按照他要求的方式生成商戶的公鑰和私鑰~把公鑰放入網站~私鑰本身留着簽名用~~服務器
下面是商戶信息以及賣家信息~下載一個支付寶的沙箱環境~app
而後按照下面的帳號和密碼登陸和支付~~只支持安卓的~dom
仔細閱讀文檔~按照文檔的參數要求去向接口發請求~~ide
from django.conf.urls import url from django.contrib import admin from demo.views import AliPayView, PayHandlerView urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^pay$', AliPayView.as_view()), url(r'^alipay_handler', PayHandlerView.as_view()), ]
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) # 初始化各類配置信息 # 阿里提供服務的接口 alipay_config.server_url = "https://openapi.alipaydev.com/gateway.do" # 申請的沙箱環境的app_id alipay_config.app_id = "2016091800540924" # 商戶的私鑰 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) 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 = 8888 # 商品標題 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") 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") views.py
注意: 咱們的回調url的測試要放在公網IP上~因此測試的時候放入本身服務器post