Django 表单的在线支付和订单处理
Django 可以通过第三方支付平台来实现在线支付和订单处理。常用的支付平台有支付宝、微信支付、银联支付等。
以下是 Django 实现支付宝支付的步骤和代码演示:
-
注册支付宝开放平台账号,并创建应用获取应用ID和密钥。
-
在 settings.py 中配置支付宝相关参数:
ALI_APP_ID = 'xxxxxxxxxx' # 应用ID ALI_APP_PRIVATE_KEY_PATH = '应用私钥路径' #应用私钥路径 ALI_ALIPAY_PUBLIC_KEY_PATH = '支付宝公钥路径' # 支付宝公钥路径 ALI_NOTIFY_URL = 'https://www.pidancode.com/notify_url' # 支付成功后的回调URL ALI_RETURN_URL = 'https://www.pidancode.com/return_url' # 支付成功后的跳转URL
- 定义支付表单类:
from django import forms class PaymentForm(forms.Form): subject = forms.CharField(max_length=128) out_trade_no = forms.CharField(max_length=64) total_amount = forms.DecimalField(max_digits=8, decimal_places=2) body = forms.CharField(max_length=256)
- 编写视图函数和支付逻辑:
from django.shortcuts import render from django.http import HttpResponseRedirect from django.urls import reverse from django.conf import settings from django.core import signing from Alipay import AliPay from .forms import PaymentForm def payment(request): if request.method == 'POST': form = PaymentForm(request.POST) if form.is_valid(): subject = form.cleaned_data['subject'] out_trade_no = form.cleaned_data['out_trade_no'] total_amount = form.cleaned_data['total_amount'] body = form.cleaned_data['body'] # 生成订单号码并保存在 session 中,用于支付后的校验 order_id = signing.dumps(out_trade_no) request.session['order_id'] = order_id alipay = AliPay( appid=settings.ALI_APP_ID, app_notify_url=settings.ALI_NOTIFY_URL, app_private_key_path=settings.ALI_APP_PRIVATE_KEY_PATH, alipay_public_key_path=settings.ALI_ALIPAY_PUBLIC_KEY_PATH, sign_type="RSA2", # RSA 或者 RSA2 debug=True # 默认 False ) # 手机网站支付,需要 out_trade_no,total_amount,subject,seller_id # 将订单信息传给支付宝,生成支付链接 order_string = alipay.api_alipay_trade_wap_pay( out_trade_no=out_trade_no, total_amount=total_amount, subject=subject, return_url=settings.ALI_RETURN_URL, notify_url=settings.ALI_NOTIFY_URL, # 可选参数,不填则使用默认notify url ) # 将支付链接返回给用户 return HttpResponseRedirect(order_string) else: form = PaymentForm() return render(request, 'payment.html', {'form': form})
- 在回调(notify_url 和 return_url)函数中处理支付结果:
from django.http import HttpResponse from django.core import signing from django.conf import settings from Alipay import AliPay def notify(request): # 验证支付宝异步通知 alipay = AliPay( appid=settings.ALI_APP_ID, app_notify_url=settings.ALI_NOTIFY_URL, app_private_key_path=settings.ALI_APP_PRIVATE_KEY_PATH, alipay_public_key_path=settings.ALI_ALIPAY_PUBLIC_KEY_PATH, sign_type="RSA2", debug=True ) # 取出 session 中保存的订单号码 order_id = request.session.get('order_id', None) if order_id: out_trade_no = signing.loads(order_id) else: out_trade_no = '' # 验证支付通知是否合法 if request.method == 'POST': form = request.POST.dict() signature = form.pop('sign', None) if signature: is_verify = alipay.verify(form, signature) if is_verify and form['trade_status'] == 'TRADE_SUCCESS': # 校验通过,处理订单状态,并返回 success 响应给支付宝 # code ... return HttpResponse('success') # 验证失败,返回 error 响应给支付宝 return HttpResponse('error')
以上就是 Django 实现支付宝支付和订单处理的基本流程和代码演示。实现微信支付和银联支付也大同小异,只需要用相应的 SDK 和 API 进行调用和处理即可。
相关文章