urls.py
from django.conf.urls import url from django.contrib import admin from app01.views import user,banner,goods,categroy,order urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^user/wxapp/login', user.Login.as_view()), url(r'^banner/list', banner.List.as_view()), url(r'^hotgoods/list', goods.HoteGoods.as_view()), url(r'^category/all', categroy.List.as_view()), url(r'^goods/list', goods.List.as_view()), url(r'^goods/detail', goods.Detail.as_view()), url(r'^order/create', order.Create.as_view()), # 支付 url(r'^order/notify/<paymethod>', order.Notify.as_view()), # 支付回调 ]
views/order.py
import importlib class Create(APIView): def post(self,request): ... # pay_method = 'Wxpay' 假设用户传入的是微信支付方法 try: # pay_file是对象 pay_file = importlib.import_module(f'app01.Pay.{pay_method}') # 此方法将字符串作为路径导入 pay_class = getattr(pay_file,pay_method) data = pay_class().pay(order_data) except: return Response({'code':201,'msg':'未知支付方式'}) # 1对接小程序支付 # 2我们要用celery去定时检查,改订单在指定时间内有没有支付,没有支付,取消订单,回滚库存 return Response({'code':200,'msg':'ok','data':data}) class Notify(APIView): # 处理支付回调,因为每种支付方式返回数据不同,处理方式放在Pay文件下的各个类中 def post(self,request,paymethod): pay_file = importlib.import_module(f'app01.Pay.{paymethod}') pay_class = getattr(pay_file,paymethod) data = pay_class().notify(request.data) if data['status']=='success': models.Order.objects.filter(order_id=data['order_id']).update(pay_status=1) return Response(data['print'])
Pay/Alipay.py
class Alipay(): def pay(self,order_data): # 统一下单方法 pass # 回调地址: notify_url = 'http://127.0.0.1:8000/order/notify/<Alipay>' def notify(self,notify_data): if notify_data['success']: # notify_data['order_id']表示商城订单 data = {'status':'success','order_id':notify_data['order_id'],'print':'0000'}#自定义返回参数 return data