')
class Meta:
- verbose_name = _('orderinfo')
- verbose_name_plural = _('orderinfo')
+ verbose_name = _(u'orderinfo')
+ verbose_name_plural = _(u'orderinfo')
def __unicode__(self):
return u'{0.pk}'.format(self)
@@ -10,7 +10,6 @@ from pywe_pay import WeChatPay |
||
| 10 | 10 |
from pywe_pay_notify import check_pay_notify |
| 11 | 11 |
from pywe_response import WXPAY_NOTIFY_FAIL, WXPAY_NOTIFY_SUCCESS |
| 12 | 12 |
from pywe_sign import check_signature |
| 13 |
-from pywe_xml import xml_to_dict |
|
| 14 | 13 |
from TimeConvert import TimeConvert as tc |
| 15 | 14 |
|
| 16 | 15 |
from account.models import UserIncomeExpensesInfo, UserInfo |
@@ -22,7 +21,7 @@ from utils.error.errno_utils import (GroupPhotoStatusCode, OrderStatusCode, User |
||
| 22 | 21 |
from utils.error.response_utils import response |
| 23 | 22 |
from utils.redis.rbrief import set_brief_info |
| 24 | 23 |
from utils.redis.rorder import set_lensman_order_record |
| 25 |
-from utils.wechat_utils import get_user_openid |
|
| 24 |
+from utils.wx_utils import get_trade_type, get_user_openid |
|
| 26 | 25 |
|
| 27 | 26 |
|
| 28 | 27 |
WECHAT = settings.WECHAT |
@@ -52,14 +51,14 @@ def wx_order_create_api(request): |
||
| 52 | 51 |
return response(GroupPhotoStatusCode.GROUP_PHOTO_NOT_FOUND) |
| 53 | 52 |
|
| 54 | 53 |
# 判断是否重复购买 |
| 55 |
- if OrderInfo.objects.filter(photo_id=photo_id, photo_type=photo_type_int, from_uid=user_id, pay_status=OrderInfo.PAID).exists(): |
|
| 54 |
+ if OrderInfo.objects.filter(photo_id=photo_id, photo_type=photo_type_int, from_uid=user_id, pay_status=OrderInfo.PAID, status=True).exists(): |
|
| 56 | 55 |
return response(OrderStatusCode.WX_ORDER_PAID_ALREADY_EXISTS) |
| 57 | 56 |
|
| 58 | 57 |
body = request.POST.get('body', '') # 商品描述
|
| 59 | 58 |
total_fee = int(request.POST.get('total_fee', 0)) # 总金额,单位分
|
| 60 | 59 |
|
| 61 | 60 |
# 金额校验 |
| 62 |
- # if int(r.get(LENSMAN_PHOTO_PRICE % (user_id, photo_id, photo_type)) or 0) != total_fee: |
|
| 61 |
+ # if r.getint(LENSMAN_PHOTO_PRICE % (user_id, photo_id, photo_type)) != total_fee: |
|
| 63 | 62 |
# return response(OrderStatusCode.FEE_CHECK_FAIL) |
| 64 | 63 |
|
| 65 | 64 |
# 获取 from_uid, to_uid |
@@ -70,9 +69,9 @@ def wx_order_create_api(request): |
||
| 70 | 69 |
trade_type = request.POST.get('trade_type', '')
|
| 71 | 70 |
|
| 72 | 71 |
# 根据 trade_type 获取 wechat 配置 |
| 73 |
- wechat = WECHAT.get(trade_type, {})
|
|
| 72 |
+ wxcfg = WECHAT.get(trade_type, {})
|
|
| 74 | 73 |
# WeChatPay 初始化 |
| 75 |
- wxpay = WeChatPay(wechat.get('appID'), wechat.get('apiKey'), wechat.get('mchID'))
|
|
| 74 |
+ wxpay = WeChatPay(wxcfg.get('appID'), wxcfg.get('apiKey'), wxcfg.get('mchID'))
|
|
| 76 | 75 |
|
| 77 | 76 |
# 生成订单 |
| 78 | 77 |
order = OrderInfo.objects.create( |
@@ -93,7 +92,7 @@ def wx_order_create_api(request): |
||
| 93 | 92 |
notify_url=settings.API_DOMAIN + '/wx/notify_url', |
| 94 | 93 |
out_trade_no=order.order_id, |
| 95 | 94 |
total_fee=total_fee, |
| 96 |
- trade_type=trade_type if trade_type != 'MINIAPP' else 'JSAPI', |
|
| 95 |
+ trade_type=get_trade_type(trade_type), |
|
| 97 | 96 |
openid=get_user_openid(user, trade_type), # 可选,用户在商户appid下的唯一标识。trade_type=JSAPI,此参数必传 |
| 98 | 97 |
) |
| 99 | 98 |
except WeChatPayException as e: |
@@ -194,7 +193,7 @@ def wx_order_query_api(request): |
||
| 194 | 193 |
transaction_id = request.POST.get('transaction_id', '')
|
| 195 | 194 |
|
| 196 | 195 |
try: |
| 197 |
- order = OrderInfo.objects.select_for_update().get(order_id=order_id) |
|
| 196 |
+ order = OrderInfo.objects.select_for_update().get(order_id=order_id, status=True) |
|
| 198 | 197 |
except OrderInfo.DoesNotExist: |
| 199 | 198 |
return response(OrderStatusCode.WX_ORDER_NOT_FOUND) |
| 200 | 199 |
|
@@ -204,14 +203,14 @@ def wx_order_query_api(request): |
||
| 204 | 203 |
return response(OrderStatusCode.WX_ORDER_PAY_FAIL) |
| 205 | 204 |
|
| 206 | 205 |
# 根据 trade_type 获取 wechat 配置 |
| 207 |
- wechat = WECHAT.get(order.trade_type, {})
|
|
| 206 |
+ wxcfg = WECHAT.get(order.trade_type, {})
|
|
| 208 | 207 |
# WeChatPay 初始化 |
| 209 |
- wxpay = WeChatPay(wechat.get('appID'), wechat.get('apiKey'), wechat.get('mchID'))
|
|
| 208 |
+ wxpay = WeChatPay(wxcfg.get('appID'), wxcfg.get('apiKey'), wxcfg.get('mchID'))
|
|
| 210 | 209 |
|
| 211 | 210 |
# 订单查询 |
| 212 | 211 |
data = wxpay.order.query(transaction_id, order_id) |
| 213 | 212 |
# 签名校验 |
| 214 |
- if not check_signature(data, wechat.get('apiKey')):
|
|
| 213 |
+ if not check_signature(data, wxcfg.get('apiKey')):
|
|
| 215 | 214 |
return response(OrderStatusCode.SIGN_CHECK_FAIL) |
| 216 | 215 |
|
| 217 | 216 |
order.notify_msg = data |
@@ -277,11 +276,10 @@ def wx_notify_url_api(request): |
||
| 277 | 276 |
if not success: |
| 278 | 277 |
return HttpResponse(WXPAY_NOTIFY_FAIL) |
| 279 | 278 |
|
| 280 |
- out_trade_no = data.get('out_trade_no', '')
|
|
| 281 | 279 |
try: |
| 282 |
- order = OrderInfo.objects.get(order_id=out_trade_no) |
|
| 280 |
+ order = OrderInfo.objects.select_for_update().get(order_id=data.get('out_trade_no', ''), status=True)
|
|
| 283 | 281 |
except OrderInfo.DoesNotExist: |
| 284 |
- return response(OrderStatusCode.WX_ORDER_NOT_FOUND) |
|
| 282 |
+ return HttpResponse(WXPAY_NOTIFY_FAIL) |
|
| 285 | 283 |
|
| 286 | 284 |
order.notify_msg = request.body |
| 287 | 285 |
order.transaction_id = data.get('transaction_id', '')
|
@@ -320,21 +318,22 @@ def wx_balance_withdraw_api(request): |
||
| 320 | 318 |
return response(WithdrawStatusCode.BALANCE_NOT_ENOUGH) |
| 321 | 319 |
|
| 322 | 320 |
# 根据 trade_type 获取 wechat 配置 |
| 323 |
- wechat = WECHAT.get(trade_type, {})
|
|
| 321 |
+ wxcfg = WECHAT.get(trade_type, {})
|
|
| 324 | 322 |
# WeChatPay 初始化 |
| 325 |
- wxpay = WeChatPay(wechat.get('appID'), wechat.get('apiKey'), wechat.get('mchID'), mch_cert=wechat.get('mch_cert'), mch_key=wechat.get('mch_key'))
|
|
| 323 |
+ wxpay = WeChatPay(wxcfg.get('appID'), wxcfg.get('apiKey'), wxcfg.get('mchID'), mch_cert=wxcfg.get('mch_cert'), mch_key=wxcfg.get('mch_key'))
|
|
| 326 | 324 |
|
| 327 | 325 |
if withdraw_type == 'TRANSFER': |
| 328 | 326 |
ret_data = wxpay.transfer.transfer(user.openid, amount, u'摄影师余额提现,企业付款', check_name='NO_CHECK') |
| 329 | 327 |
elif withdraw_type == 'PACKET': |
| 328 |
+ wxrpk = wxcfg.get('redpack', {})
|
|
| 330 | 329 |
ret_data = wxpay.redpack.send( |
| 331 | 330 |
user.openid, |
| 332 | 331 |
amount, |
| 333 |
- send_name=wechat.get('redpacket', {}).get('SEND_NAME'),
|
|
| 334 |
- nick_name=wechat.get('redpacket', {}).get('NICK_NAME'),
|
|
| 335 |
- act_name=wechat.get('redpacket', {}).get('ACT_NAME'),
|
|
| 336 |
- wishing=wechat.get('redpacket', {}).get('WISHING'),
|
|
| 337 |
- remark=wechat.get('redpacket', {}).get('REMARK'),
|
|
| 332 |
+ send_name=wxrpk.get('SEND_NAME'),
|
|
| 333 |
+ nick_name=wxrpk.get('NICK_NAME'),
|
|
| 334 |
+ act_name=wxrpk.get('ACT_NAME'),
|
|
| 335 |
+ wishing=wxrpk.get('WISHING'),
|
|
| 336 |
+ remark=wxrpk.get('REMARK'),
|
|
| 338 | 337 |
) |
| 339 | 338 |
|
| 340 | 339 |
# 根据 ret_data 判断是否提现成功, 成功则减余额, 失败则提示 |
@@ -1,13 +0,0 @@ |
||
| 1 |
-# -*- coding: utf-8 -*- |
|
| 2 |
- |
|
| 3 |
- |
|
| 4 |
-def get_user_openid(user, trade_type): |
|
| 5 |
- if trade_type == 'MINIAPP': |
|
| 6 |
- openid = user.openid_miniapp |
|
| 7 |
- elif trade_type == 'JSAPI': |
|
| 8 |
- openid = user.openid_oauth |
|
| 9 |
- elif trade_type == 'APP': |
|
| 10 |
- openid = None |
|
| 11 |
- else: |
|
| 12 |
- openid = None |
|
| 13 |
- return openid |
@@ -1,6 +1,10 @@ |
||
| 1 | 1 |
# -*- coding: utf-8 -*- |
| 2 | 2 |
|
| 3 | 3 |
|
| 4 |
+def get_trade_type(trade_type): |
|
| 5 |
+ return trade_type if trade_type != 'MINIAPP' else 'JSAPI' |
|
| 6 |
+ |
|
| 7 |
+ |
|
| 4 | 8 |
def get_user_openid(user, trade_type): |
| 5 | 9 |
if trade_type == 'MINIAPP': |
| 6 | 10 |
openid = user.openid_miniapp |