jiaweiqi 3 năm trước cách đây
mục cha
commit
bc54dc74b3
3 tập tin đã thay đổi với 54 bổ sung131 xóa
  1. 23 34
      apps/activity/models.py
  2. 6 3
      apps/api/views.py
  3. 25 94
      util/wechatpay.py

+ 23 - 34
apps/activity/models.py

@@ -123,17 +123,15 @@ class SplitAccount(models.Model):
         self.splictAccountAddReceiver(spc, spr)
         # 分账申请
         receivers = [{'account': spd.account, 'amount': spd.amount, 'description': "支付推荐佣金"}]
-        success, data = spc.splitaccount_order(pay.transaction_id, self.no, receivers)
-        if not success:
-            raise CustomError(u'分账申请失败!')
+        result = spc.splitaccount_order(pay.transaction_id, self.no, receivers)
         # 分账请求成功  结果不一定成功
-        self.order_id = data['order_id']
-        if data['state'] == 'PROCESSING':
+        self.order_id = result.get('order_id')
+        if result.get('state') == 'PROCESSING':
             self.status = SplitAccount.PROCESSING
-        if data['state'] == 'FINISHED':
+        if result.get('state') == 'FINISHED':
             self.status = SplitAccount.FINISHED
         self.save()
-        for item in data['receivers']:
+        for item in result.get('receivers'):
             # 现在只对一个推荐人分账
             if item['account'] == spd.account:
                 if item['result'] == 'PENDING':
@@ -151,23 +149,19 @@ class SplitAccount(models.Model):
     def splictAccountAddReceiver(self, split_account_func, spr):
         '''添加分账接收人'''
         if spr.status != SplitAccountReceiver.ADD:
-            add = split_account_func.splitaccount_addreceiver(spr.account)
-            if not add:
-                raise CustomError(u'添加分账接收方失败!')
+            split_account_func.splitaccount_addreceiver(spr.account)
             spr.status = SplitAccountReceiver.ADD
             spr.save()
 
     def splictAccountQuery(self, split_account_func, transaction_id, out_order_no, detail):
         '''查询分账结果'''
-        success, data = split_account_func.splitaccount_orderquery(transaction_id, out_order_no)
-        if not success:
-            raise CustomError(u'[%s]查询分账结果失败!' % out_order_no)
-        if data['state'] == 'PROCESSING':
+        result = split_account_func.splitaccount_orderquery(transaction_id, out_order_no)
+        if result.get('state') == 'PROCESSING':
             self.status = SplitAccount.PROCESSING
-        if data['state'] == 'FINISHED':
+        if result.get('state') == 'FINISHED':
             self.status = SplitAccount.FINISHED
         self.save()
-        for item in data['receivers']:
+        for item in result.get('receivers'):
             if detail and detail.account == item['account']:
                 if item['result'] == 'SUCCESS':
                     detail.status = SplitAccountDetail.SUCCESS
@@ -188,27 +182,21 @@ class SplitAccount(models.Model):
         spc = SplitAccountFuc(app.authorizer_appid, app.agent_num, app.private_key, app.cert_serial_no, app.apiv3_key, cert_dir=app.cert_dir, proxy=None)
 
         if spr.status != SplitAccountReceiver.ADD:
-            add = spc.splitaccount_addreceiver(spr.account)
-            if not add:
-                # 添加分账接收方失败 不再继续
-                return
+            spc.splitaccount_addreceiver(spr.account)
             spr.status = SplitAccountReceiver.ADD
             spr.save()
 
         receivers = [{'account': spd.account, 'amount': spd.amount, 'description': "支付推荐佣金"}]
-        success, data = spc.splitaccount_order(pay.transaction_id, self.no, receivers)
-        if not success:
-            # 分账请求失败 不再继续
-            return
-         # 分账请求成功  结果不一定成功
-        self.order_id = data['order_id']
-        if data['state'] == 'PROCESSING':
+        result = spc.splitaccount_order(pay.transaction_id, self.no, receivers)
+
+        self.order_id = result.get('order_id')
+        if result.get('state') == 'PROCESSING':
             self.status = SplitAccount.PROCESSING
-        if data['state'] == 'FINISHED':
+        if result.get('state') == 'FINISHED':
             self.status = SplitAccount.FINISHED
         self.save()
-        for item in data['receivers']:
-            # 现在只对一个推荐人分账
+
+        for item in result.get('receivers'):
             if item['account'] == spd.account:
                 if item['result'] == 'PENDING':
                     spd.status = SplitAccountDetail.PENDING
@@ -467,14 +455,15 @@ class Order(models.Model):
         coupons = ActivityCoupon.objects.filter(activity=self.activity, coupon__enabled=True)
         now = datetime.datetime.date(timezone.now())
         for item in coupons:
-            if item.off_type == Coupon.FIXED_DATE:
-                end_date = item.end_date
+            coupon = item.coupon
+            if coupon.off_type == Coupon.FIXED_DATE:
+                end_date = coupon.end_date
             else:
-                end_date = now + datetime.timedelta(days=item.end_days)
+                end_date = now + datetime.timedelta(days=coupon.end_days)
             MemberCoupon.objects.create(
                 activity=self.activity,
                 member=self.member,
-                coupon=item,
+                coupon=coupon,
                 receive_date=now,
                 end_date=end_date
             )

+ 6 - 3
apps/api/views.py

@@ -41,9 +41,12 @@ class WechatNotifyView(APIView):
             with transaction.atomic():
                 pay = Pay.getByNo(no)
                 pay.payConfirm(amount, transaction_id)
-                BizLog.objects.addnew(pay.customer.user, BizLog.INSERT, u'微信支付成功,no=%s' % no, param)
-            # 微信支付成功之后分账
-            pay.split_account(app)
+                BizLog.objects.addnew(pay.customer.user, BizLog.INSERT, u'微信支付成功,no=%s' % no)
+            # 微信支付成功之后分账  分账不要影响支付结果
+            try:
+                pay.split_account(app)
+            except:
+                traceback.print_exc()
         except Exception as e:
             traceback.print_exc()
             return HttpResponse(WechatPayNotify.response_fail())

+ 25 - 94
util/wechatpay.py

@@ -59,26 +59,11 @@ class SplitAccountFuc(object):
 
             params['receivers'].append(receiver_item)
 
-        success = True
-        data = {}
-        try:
-            code, message = self._core.request(path, SplitAccountTool.POST, data=params)
-            result = json.loads(message)
-            if code == 200:
-                data['order_id'] = result.get('order_id')
-                data['state'] = result.get('state')
-                data['receivers'] = result.get('receivers')
-            else:
-                success = False
-                BizLog.objects.addnew('', BizLog.INSERT, u'[%s]分账失败!原因:' % out_order_no + result.get('code'))
-        except CustomError as e:
-            success = False
-            BizLog.objects.addnew('', BizLog.INSERT, u'[%s]分账失败!原因:' % out_order_no + e.get_error_msg())
-        except Exception as e:
-            success = False
-            BizLog.objects.addnew('', BizLog.INSERT, u'[%s]分账失败!原因:' % out_order_no + str(e))
-
-        return success, data
+        code, message = self._core.request(path, SplitAccountTool.POST, data=params)
+        result = json.loads(message)
+        if code != 200:
+            raise CustomError(u'[{}]分账失败!原因:{}'.format(out_order_no, result))
+        return result
 
     def splitaccount_addreceiver(self, account):
         '''添加分账接收方'''
@@ -90,21 +75,10 @@ class SplitAccountFuc(object):
             'relation_type': "USER",  # body子商户与接收方的关系
         }
 
-        success = True
-        try:
-            code, message = self._core.request(path, SplitAccountTool.POST, data=params)
-            result = json.loads(message)
-            if code != 200:
-                success = False
-                BizLog.objects.addnew('', BizLog.INSERT, u'[%s]添加分账接收方失败!原因:' % account + result.get('code'))
-        except CustomError as e:
-            success = False
-            BizLog.objects.addnew('', BizLog.INSERT, u'[%s]添加分账接收方失败!原因:' % account + e.get_error_msg())
-        except Exception as e:
-            success = False
-            BizLog.objects.addnew('', BizLog.INSERT, u'[%s]添加分账接收方失败!原因:' % account + str(e))
-
-        return success
+        code, message = self._core.request(path, SplitAccountTool.POST, data=params)
+        result = json.loads(message)
+        if code != 200:
+            raise CustomError(u'[{}]添加分账接收方失败!原因:{}'.format(account, result))
 
     def splitaccount_deletereceiver(self, account):
         '''删除分账接收方'''
@@ -115,21 +89,10 @@ class SplitAccountFuc(object):
             'account': account,  # 接收人的openid
         }
 
-        success = True
-        try:
-            code, message = self._core.request(path, SplitAccountTool.POST, data=params)
-            result = json.loads(message)
-            if code != 200:
-                success = False
-                BizLog.objects.addnew('', BizLog.INSERT, u'[%s]删除分账接收方失败!原因:' % account + result.get('code'))
-        except CustomError as e:
-            success = False
-            BizLog.objects.addnew('', BizLog.INSERT, u'[%s]删除分账接收方失败!原因:' % account + e.get_error_msg())
-        except Exception as e:
-            success = False
-            BizLog.objects.addnew('', BizLog.INSERT, u'[%s]删除分账接收方失败!原因:' % account + str(e))
-
-        return success
+        code, message = self._core.request(path, SplitAccountTool.POST, data=params)
+        result = json.loads(message)
+        if code != 200:
+            raise CustomError(u'[{}]删除分账接收方失败!原因:{}'.format(account, result))
 
     def splitaccount_orderquery(self, transaction_id, out_order_no):
         '''
@@ -137,33 +100,16 @@ class SplitAccountFuc(object):
         transaction_id  微信支付订单号
         out_order_no  商户分账单号
         '''
-        success = True
-        data = {}
         if transaction_id and out_order_no:
             path = '/v3/profitsharing/orders/%s?transaction_id=%s' % (out_order_no, transaction_id)
         else:
-            success = False
-            BizLog.objects.addnew('', BizLog.INSERT, u'[%s]查询分账结果失败!原因:参数错误!' % out_order_no)
-            return success, data
-
-        try:
-            code, message = self._core.request(path)
-            result = json.loads(message)
-            if code == 200:
-                data['order_id'] = result.get('order_id')
-                data['state'] = result.get('state')
-                data['receivers'] = result.get('receivers')
-            else:
-                success = False
-                # data = result.get('code')
-                BizLog.objects.addnew('', BizLog.INSERT, u'[%s]查询分账结果失败!原因:' % out_order_no + result.get('code'))
-        except CustomError as e:
-            success = False
-            BizLog.objects.addnew('', BizLog.INSERT, u'[%s]查询分账结果失败!原因:' % out_order_no + e.get_error_msg())
-        except Exception as e:
-            success = False
-            BizLog.objects.addnew('', BizLog.INSERT, u'[%s]查询分账结果失败!原因:' % out_order_no + str(e))
-        return success, data
+            raise CustomError(u'[%s]查询分账结果失败!原因:参数错误!' % out_order_no)
+
+        code, message = self._core.request(path)
+        result = json.loads(message)
+        if code != 200:
+            raise CustomError(u'[{}]查询分账结果失败!原因:{}'.format(out_order_no, result))
+        return result
 
     def splitaccount_return(self):
         '''请求分账回退'''
@@ -182,26 +128,11 @@ class SplitAccountFuc(object):
             'description': "解冻资金"
         }
 
-        success = True
-        data = {}
-        try:
-            code, message = self._core.request(path, SplitAccountTool.POST, data=params)
-            result = json.loads(message)
-            if code == 200:
-                data['order_id'] = result.get('order_id')
-                data['state'] = result.get('state')
-                data['receivers'] = result.get('receivers')
-            else:
-                success = False
-                BizLog.objects.addnew('', BizLog.INSERT, u'[%s]解冻剩余资金失败!原因:'% out_order_no + result.get('code'))
-        except CustomError as e:
-            success = False
-            BizLog.objects.addnew('', BizLog.INSERT, u'[%s]解冻剩余资金失败!原因:'% out_order_no + e.get_error_msg())
-        except Exception as e:
-            success = False
-            BizLog.objects.addnew('', BizLog.INSERT, u'[%s]解冻剩余资金失败!原因:'% out_order_no + str(e))
-
-        return success, data
+        code, message = self._core.request(path, SplitAccountTool.POST, data=params)
+        result = json.loads(message)
+        if code != 200:
+            raise CustomError(u'[{}]解冻剩余资金失败!原因:{}'.format(out_order_no, result))
+        return result
 
     def splitaccount_amountquery(self):
         '''查询剩余待分金额'''