| @@ -19,10 +19,10 @@ WECHAT = settings.WECHAT | ||
| 19 | 19 | @logit | 
| 20 | 20 | @transaction.atomic | 
| 21 | 21 | def get_userinfo_api(request): | 
| 22 | -    wechat = WECHAT.get('MINIAPP', {}) | |
| 22 | +    wxcfg = WECHAT.get('MINIAPP', {}) | |
| 23 | 23 |  | 
| 24 | -    appid = wechat.get('appID') | |
| 25 | -    secret = wechat.get('appsecret') | |
| 24 | +    appid = wxcfg.get('appID') | |
| 25 | +    secret = wxcfg.get('appsecret') | |
| 26 | 26 |  | 
| 27 | 27 |      code = request.POST.get('code', '') | 
| 28 | 28 |      encryptedData = request.POST.get('encryptedData', '') | 
| @@ -212,7 +212,7 @@ WECHAT = { | ||
| 212 | 212 | 'apiKey': '', | 
| 213 | 213 | 'mch_cert': '', | 
| 214 | 214 | 'mch_key': '', | 
| 215 | -        'redpacket': { | |
| 215 | +        'redpack': { | |
| 216 | 216 |  | 
| 217 | 217 | } | 
| 218 | 218 | }, | 
| @@ -53,15 +53,15 @@ class OrderInfo(CreateUpdateMixin): | ||
| 53 | 53 |  | 
| 54 | 54 | order_id = ShortUUIDField(_(u'order_id'), max_length=255, help_text=u'订单唯一标识', db_index=True) | 
| 55 | 55 |  | 
| 56 | -    prepay_id = models.CharField(_('prepay_id'), max_length=255, blank=True, null=True, help_text=u'预支付交易会话标识') | |
| 57 | -    transaction_id = models.CharField(_('transaction_id'), max_length=255, blank=True, null=True, help_text=u'微信订单号') | |
| 56 | + prepay_id = models.CharField(_(u'prepay_id'), max_length=255, blank=True, null=True, help_text=u'预支付交易会话标识') | |
| 57 | + transaction_id = models.CharField(_(u'transaction_id'), max_length=255, blank=True, null=True, help_text=u'交易单号') | |
| 58 | 58 |  | 
| 59 | 59 | group_id = models.CharField(_(u'group_id'), max_length=255, blank=True, null=True, help_text=u'群组唯一标识', db_index=True) | 
| 60 | 60 | session_id = models.CharField(_(u'session_id'), max_length=255, blank=True, null=True, help_text=u'照片组唯一标识,同 PhotosInfo 表', db_index=True) | 
| 61 | 61 | photo_id = models.CharField(_(u'photo_id'), max_length=255, blank=True, null=True, help_text=u'飞图唯一标识', db_index=True) | 
| 62 | 62 | lensman_photo_id = models.CharField(_(u'lensman_photo_id'), max_length=255, blank=True, null=True, help_text=u'摄影师照片唯一标识,同 PhotosInfo 表', db_index=True) | 
| 63 | 63 |  | 
| 64 | -    photo_type = models.IntegerField(_('photo_type'), choices=PHOTO_TYPE, default=NOMARK, help_text=u'购买照片类型', db_index=True) | |
| 64 | + photo_type = models.IntegerField(_(u'photo_type'), choices=PHOTO_TYPE, default=NOMARK, help_text=u'购买照片类型', db_index=True) | |
| 65 | 65 | photo_status = models.IntegerField(_(u'photo_status'), choices=PHOTO_STATUS, default=WANTED, help_text=_(u'购买照片状态, 标识用户是否已经获得照片'), db_index=True) | 
| 66 | 66 |  | 
| 67 | 67 | from_uid = models.CharField(_(u'from_uid'), max_length=255, help_text=u'付款用户唯一标识', db_index=True) | 
| @@ -70,7 +70,7 @@ class OrderInfo(CreateUpdateMixin): | ||
| 70 | 70 | body = models.CharField(_(u'body'), max_length=255, blank=True, null=True, help_text=u'商品描述') | 
| 71 | 71 | total_fee = models.IntegerField(_(u'total_fee'), default=0, help_text=u'总金额') | 
| 72 | 72 |  | 
| 73 | -    trade_type = models.CharField(_('trade_type'), max_length=255, blank=True, null=True, help_text=u'支付方式') | |
| 73 | + trade_type = models.CharField(_(u'trade_type'), max_length=255, blank=True, null=True, help_text=u'支付方式') | |
| 74 | 74 |  | 
| 75 | 75 | pay_status = models.IntegerField(_(u'pay_status'), choices=PAY_STATUS, default=WAITING_PAY, help_text=u'支付状态', db_index=True) | 
| 76 | 76 | paid_at = models.DateTimeField(_(u'paid_at'), blank=True, null=True, help_text=_(u'支付时间')) | 
| @@ -81,11 +81,11 @@ class OrderInfo(CreateUpdateMixin): | ||
| 81 | 81 | # 微信统一下单 | 
| 82 | 82 | unifiedorder_result = models.TextField(_(u'unifiedorder_result'), blank=True, null=True, help_text=_(u'统一下单结果')) | 
| 83 | 83 | # 微信支付回调 | 
| 84 | -    notify_msg = models.TextField(_('notify_msg'), blank=True, null=True, help_text=u'回调信息') | |
| 84 | + notify_msg = models.TextField(_(u'notify_msg'), blank=True, null=True, help_text=u'回调信息') | |
| 85 | 85 |  | 
| 86 | 86 | class Meta: | 
| 87 | -        verbose_name = _('orderinfo') | |
| 88 | -        verbose_name_plural = _('orderinfo') | |
| 87 | + verbose_name = _(u'orderinfo') | |
| 88 | + verbose_name_plural = _(u'orderinfo') | |
| 89 | 89 |  | 
| 90 | 90 | def __unicode__(self): | 
| 91 | 91 |          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 |