【支付寶SDK】沙箱除錯,以及遇到的坑
阿新 • • 發佈:2020-08-18
from rest_framework.views import APIView from alipay import AliPay, DCAliPay, ISVAliPay from django.http import JsonResponse import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) import time class Alipay(APIView): authentication_classes = [] # 跳轉支付 def get(self,request):# 初始化配置 app_private_key_string = open(BASE_DIR + "/payAPI/應用私鑰2048.txt").read() alipay_public_key_string = open(BASE_DIR + "/payAPI/支付寶公鑰.txt").read() alipay = AliPay( appid="你自己的ID", app_notify_url=None, # 預設回撥url app_private_key_string=app_private_key_string,# 支付寶的公鑰,驗證支付寶回傳訊息使用,不是你自己的公鑰, alipay_public_key_string=alipay_public_key_string, sign_type="RSA2", # RSA 或者 RSA2 debug = True # 預設False ) # 電腦網站支付,需要跳轉到https://openapi.alipay.com/gateway.do? + order_string order_string = alipay.api_alipay_trade_page_pay( out_trade_no="訂單號", total_amount=str("總價"), 坑:要轉換成字串型別 否則會報錯 subject="商品標題", return_url=None, notify_url=None # 可選, 不填則使用預設notify url ) message={} message['pay_url'] = 'https://openapi.alipaydev.com/gateway.do?' + order_string return JsonResponse(message) # 查詢訂單支付情況 def post(self,request): # 初始化配置 app_private_key_string = open(BASE_DIR + "/payAPI/應用私鑰2048.txt").read() alipay_public_key_string = open(BASE_DIR + "/payAPI/支付寶公鑰.txt").read() alipay = AliPay( appid="你的APPID", app_notify_url=None, # 預設回撥url app_private_key_string=app_private_key_string, # 支付寶的公鑰,驗證支付寶回傳訊息使用,不是你自己的公鑰, alipay_public_key_string=alipay_public_key_string, sign_type="RSA2", # RSA 或者 RSA2 debug = True # 預設False ) message = {} while True: result = alipay.api_alipay_trade_query("20200823") print(result) print("trade_status",result.get("trade_status")) # 獲取返回結果 code = result.get("code") if code == "10000" and result.get("trade_status") == "TRADE_SUCCESS": # 支付成功 # 獲取支付寶交易號 trade_no = result.get('trade_no') print("支付寶交易號",trade_no) # 更新訂單狀態 # 返回結果 message['code'] = 200 message['message'] = "支付成功" return JsonResponse(message) elif code == "40004" or (code == "10000" and result.get("trade_status") == "WAIT_BUYER_PAY"): # 等待買家付款 time.sleep(5) continue else: # 支付出錯 message['code'] = 444 message['message'] = "支付失敗" return JsonResponse(message)
<!DOCTYPE html> <html> <head> <title>Bootstrap 例項</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <button type="button" class="btn btn-primary" id="pay">立即付款</button> </div> </body> <script> $("#pay").click(function () { $.ajax({ url:"alipay", type:"GET", success:function (data) { window.open(data.pay_url) // 查詢結果 $.ajax({ url:"alipay/result", type:"POST", success:function (data) { if (data.code == 200){ alert("支付成功") }else { alert("支付失敗") } } }) } }) }) </script> </html>
# 支付寶付款測試 url(r'^$', href_index.Index.as_view()), url(r'alipay$',alipay.Alipay.as_view()), # 查詢 url(r'alipay/result$',alipay.Alipay.as_view()), # 查詢結果