订单状态
订单状态的保存
创建支付状态视图
编辑子应用 payment 目录下的 views.py 文件,添加支付状态试图:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
...
from django.conf import settings
...
class PaymentStatusView(APIView): def put(self, request): data = request.query_params.dict() signature = data.pop('sign')
app_private_key_string = open('/data/gitlab/python3-django-small_haoke/haoke/haoke/apps/payment/keys/app_private_key.pem').read() alipay_public_key_string = open('/data/gitlab/python3-django-small_haoke/haoke/haoke/apps/payment/keys/alipay_public_key.pem').read() alipay = AliPay( appid = settings.ALIPAY_APPID, app_notify_url = None, app_private_key_string = app_private_key_string, alipay_public_key_string = alipay_public_key_string, sign_type = 'RSA2', debug = settings.ALIPAY_DEBUG )
success = alipay.verify(data, signature)
if success: order_id = data.get('out_trade_no') trade_id = data.get('trade_no')
Payment.object.create( order_id= order_id, trade_id = trade_id )
OrderInfo.objects.filter(order_id=order_id, status=OrderInfo.ORDER_STATUS_ENUM['UNPAID']).update(status=OrderInfo.ORDER_STATUS_ENUM['UNSEND'])
return Response({'trade_id': trade_id})
|
创建支付状态访问路由
编辑子应用 payment 目录下的 urls.py 文件,添加访问路由:
1 2 3 4 5 6 7
| from django.urls import path, re_path from .import views
urlpatterns = [ re_path('orders/(?P<order_id>\d+)/payment/', views.PaymentView.as_view()), path('payment/status/', views.PaymentStatusView.as_view()), ]
|