an>
+
+ # JSAPI--公众号支付、NATIVE--原生扫码支付、APP--app支付,统一下单接口trade_type的传参可参考这里
+ trade_type = 'JSAPI'
+
+ # 根据 trade_type 获取 wechat 配置
+ wxcfg = WECHAT.get(trade_type, {})
+ # WeChatPay 初始化
+ wxpay = WeChatPay(wxcfg.get('appID'), wxcfg.get('apiKey'), wxcfg.get('mchID'))
+
+ # 生成订单
+ order = RoomOrderInfo.objects.create(
+ user_id=user_id,
+ room_id=room_id,
+ anchor_id=anchor_id,
+ goods_id=goods_id,
+ share_openid=share_openid,
+ amount=amount,
+ total_fee=total_fee,
+ trade_type=trade_type,
+ name=name,
+ phone=phone,
+ address=address,
+ )
+
+ try:
+ prepay_data = wxpay.order.create(
+ body=body,
+ notify_url=settings.API_DOMAIN + 'live/pay/notify_url',
+ out_trade_no=order.order_id,
+ total_fee=total_fee,
+ trade_type=trade_type,
+ openid=user.openid, # 可选,用户在商户appid下的唯一标识。trade_type=JSAPI,此参数必传
+ )
+ except WeChatPayException as e:
+ order.unifiedorder_result = e.args
+ order.save()
+ return response(OrderStatusCode.UNIFIED_ORDER_FAIL)
+
+ prepay_id = prepay_data.get('prepay_id', '')
+ order.prepay_id = prepay_id
+ order.save()
+
+ wxpay_params = wxpay.jsapi.get_jsapi_params(prepay_id)
+
+
+ return response(200, 'Order Create Success', '订单创建成功', {
+ 'order_id': order.order_id,
+ 'prepay_id': prepay_id,
+ 'wxpay_params': wxpay_params,
+ })
+
+def live_order_cancel(request):
+ user_id = request.POST.get('user_id', '')
+ order_id = request.POST.get('order_id', '')
+ prepay_id = request.POST.get('prepay_id', '')
+
+ try:
+ order = RoomOrderInfo.objects.get(user_id=user_id, order_id=order_id, prepay_id=prepay_id)
+ except:
+ return response(400001, 'Order Not Found', description='直播间订单不存在')
+
+ if order.pay_status == RoomOrderInfo.FAIL:
+ return response(200, 'Order Cancel Success', '订单取消成功')
+
+ order.pay_status = RoomOrderInfo.FAIL
+ order.save()
+
+ try:
+ goods_info = RoomGoodsInfo.objects.get(room_id=order.room_id, goods_id=order.goods_id, anchor_id=order.anchor_id)
+ goods_info.inventory += order.amount
+ goods_info.save()
+ except:
+ return response(400001, 'Room Goods Not Found', description='直播间商品不存在')
+
+ return response(200, 'Order Cancel Success', '订单取消成功')
+
+def order_paid_success(order):
+ if order.pay_status == RoomOrderInfo.PAID:
+ return
+
+ order.pay_status = RoomOrderInfo.PAID
+ order.paid_at = tc.utc_datetime()
+ order.save()
+
+ try:
+ goods_info = RoomGoodsInfo.objects.get(room_id=order.room_id, goods_id=order.goods_id, anchor_id=order.anchor_id)
+ goods_info.sale_infos += [order.order_id]
+ goods_info.save()
+ except:
+ return
+
+def order_paid_fail(order):
+ if order.pay_status == RoomGoodsInfo.FAIL:
+ return
+
+ order.pay_status = RoomGoodsInfo.FAIL
+ order.save()
+
+ try:
+ goods_info = RoomGoodsInfo.objects.get(room_id=order.room_id, goods_id=order.goods_id, anchor_id=order.anchor_id)
+ goods_info.inventory += order.amount
+ goods_info.save()
+ except:
+ return
+
+@logit
+@transaction.atomic
+def notify_url_api(request):
+ """ 支付异步通知回调地址 """
+ notify_data, success = check_pay_notify(request.body, wx_configs=settings.WECHAT)
+ if not success:
+ return HttpResponse(WXPAY_NOTIFY_FAIL)
+
+ order = RoomOrderInfo.objects.select_for_update().get(order_id=notify_data.get('out_trade_no', ''), status=True)
+
+ order.notify_msg = request.body
+ order.transaction_id = notify_data.get('transaction_id', '')
+ order.save()
+
+ result_code = notify_data.get('result_code', '')
+ if result_code == 'SUCCESS':
+ live_views.order_paid_success(order)
+ else:
+ live_views.order_paid_fail(order)
+
+ return HttpResponse(WXPAY_NOTIFY_SUCCESS)
@@ -18,8 +18,11 @@ from account.models import UserInfo |
||
| 18 | 18 |
from goods.models import GoodsInfo, PackGoodsInfo, PackGoodsSaleInfo, PackInfo |
| 19 | 19 |
from kol.models import KOLInfo |
| 20 | 20 |
from pay.models import OrderInfo |
| 21 |
+from live.models import RoomOrderInfo |
|
| 21 | 22 |
from utils.error.errno_utils import KOLStatusCode, OrderStatusCode, PackGoodsStatusCode, PackStatusCode, UserStatusCode |
| 22 | 23 |
|
| 24 |
+from live import views as live_views |
|
| 25 |
+ |
|
| 23 | 26 |
|
| 24 | 27 |
WECHAT = settings.WECHAT |
| 25 | 28 |
|
@@ -215,19 +218,22 @@ def wx_notify_url_api(request): |
||
| 215 | 218 |
if not success: |
| 216 | 219 |
return HttpResponse(WXPAY_NOTIFY_FAIL) |
| 217 | 220 |
|
| 221 |
+ #尖货接龙订单 |
|
| 218 | 222 |
try: |
| 219 | 223 |
order = OrderInfo.objects.select_for_update().get(order_id=notify_data.get('out_trade_no', ''), status=True)
|
| 220 |
- except OrderInfo.DoesNotExist: |
|
| 221 |
- return HttpResponse(WXPAY_NOTIFY_FAIL) |
|
| 224 |
+ order.notify_msg = request.body |
|
| 225 |
+ order.transaction_id = notify_data.get('transaction_id', '')
|
|
| 226 |
+ order.save() |
|
| 222 | 227 |
|
| 223 |
- order.notify_msg = request.body |
|
| 224 |
- order.transaction_id = notify_data.get('transaction_id', '')
|
|
| 225 |
- order.save() |
|
| 228 |
+ result_code = notify_data.get('result_code', '')
|
|
| 229 |
+ if result_code == 'SUCCESS': |
|
| 230 |
+ order_paid_success(order) |
|
| 231 |
+ else: |
|
| 232 |
+ order_paid_fail(order) |
|
| 233 |
+ |
|
| 234 |
+ return HttpResponse(WXPAY_NOTIFY_SUCCESS) |
|
| 235 |
+ except: |
|
| 236 |
+ return HttpResponse(WXPAY_NOTIFY_FAIL) |
|
| 226 | 237 |
|
| 227 |
- result_code = notify_data.get('result_code', '')
|
|
| 228 |
- if result_code == 'SUCCESS': |
|
| 229 |
- order_paid_success(order) |
|
| 230 |
- else: |
|
| 231 |
- order_paid_fail(order) |
|
| 232 | 238 |
|
| 233 | 239 |
return HttpResponse(WXPAY_NOTIFY_SUCCESS) |