Răsfoiți Sursa

Merge remote-tracking branch 'origin/test'

locky 3 ani în urmă
părinte
comite
2e26d21426

+ 1 - 0
AdminController/ServeManagementController.py

@@ -647,6 +647,7 @@ class serveManagement(View):
                 "channel",
                 "desc",
                 "price",
+                "refunded_amount",
                 "currency",
                 "addTime",
                 "updTime",

+ 1 - 0
Ansjer/urls.py

@@ -244,6 +244,7 @@ urlpatterns = [
 
     #AI服务
     url(r'^AiService/(?P<operation>.*)$', AiController.AiView.as_view()),
+    url('^ai/updateUnusedAi', AiController.updateUnusedAi),
 
     #新增解密的接口
     url(r'^v3/account/changePwd$', UserController.v3ChangePwdView.as_view()),

+ 25 - 1
Controller/AiController.py

@@ -117,6 +117,8 @@ class AiView(View):
                 return self.do_querylist(userID, request_dict, response)
             elif operation == 'updateJpushTime':  # 查询订单
                 return self.update_jpush_time(userID, request_dict, response)
+            elif operation == 'updateJpushTime':  # 查询订单
+                return self.update_jpush_time(userID, request_dict, response)
             else:
                 return response.json(414)
 
@@ -452,11 +454,13 @@ class AiView(View):
             has = AiService.objects.filter(uid=uid, channel=channel, use_status=1)
             if has.exists():
                 use_status = 0
+                endTime = 0
             else:
                 use_status = 1
+                endTime = nowTime+(day*86400)
             # return response.json(0)
             AiService.objects.create(orders_id=orderID, uid=uid, channel=channel, detect_status=1,
-                                       endTime=nowTime+(day*86400), addTime=nowTime, updTime=nowTime,
+                                       endTime=endTime, addTime=nowTime, updTime=nowTime,
                                      use_status=use_status, detect_group='1')
             return response.json(0, {"orderID": orderID})
         return response.json(10, 'generate_order_false')
@@ -991,3 +995,23 @@ class AiView(View):
         except Exception as e:
             print(e)
             return response.json(500, repr(e))
+
+# 如果ai套餐过期,更新未使用的关联套餐
+def updateUnusedAi(request):
+    response = ResponseObject()
+    now_time = int(time.time())
+    expired_ai_services = AiService.objects.filter(endTime__lte=now_time,use_status=1).values("id","uid")[0:200]
+    for expired_ai_service in expired_ai_services:
+        try:
+            with transaction.atomic():
+                AiService.objects.filter(id=expired_ai_service['id']).update(use_status=2)
+                hasUnUse = AiService.objects.filter(uid=expired_ai_service['uid'],use_status=0).order_by('addTime')[0:1]
+                if hasUnUse.exists():
+                    hasUnUse = hasUnUse.values('id','orders__ai_rank__effective_day')
+                    endTime = now_time + (hasUnUse[0]['orders__ai_rank__effective_day'] * 86400)
+                    AiService.objects.filter(id=hasUnUse[0]['id']).update(use_status=1, endTime=endTime)
+        except Exception as e:
+            # return HttpResponse("line={line},error={error}".format(line=e.__traceback__.tb_lineno, error=repr(e)))
+            continue
+    # UID_Bucket.objects.filter(id__in=list(id_list)).update(use_status=2)  # 更新过期云存关联套餐状态
+    return response.json(0)

+ 42 - 17
Controller/CloudStorage.py

@@ -1948,35 +1948,53 @@ class CloudStorageView(View):
 
     def do_refund(self, request_dict, response):
         orderID = request_dict.get('orderID', None)    # 商户订单号
-        if not orderID:
+        refund_amount = request_dict.get('refund_amount', None)  # 退款金额
+        if not all([orderID, refund_amount]):
             return response.json(444)
         try:
             order_qs = Order_Model.objects.filter(orderID=orderID).\
-                values('status', 'payType', 'price', 'currency', 'paymentID')
+                values('status', 'payType', 'price', 'refunded_amount', 'currency', 'paymentID')
             if not order_qs.exists():
                 return response.json(173)
 
-            # 支付状态不为支付成功和退款失败
+            # 支付状态不为支付成功和退款失败和部分退款
             status = order_qs[0]['status']
-            if status != 1 and status != 4:
+            if status != 1 and status != 4 and status != 6:
                 return response.json(805)
 
+            refund_amount = float(refund_amount)
             now_time = int(time.time())
             payType = order_qs[0]['payType']
-            refund_amount = order_qs[0]['price']    # 退款金额
-            currency = order_qs[0]['currency']      # 货币
-            paymentID = order_qs[0]['paymentID']    # 退款id
+            price = order_qs[0]['price']                        # 订单金额
+            refunded_amount = order_qs[0]['refunded_amount']    # 已退款金额
+            currency = order_qs[0]['currency']                  # 货币
+
+            # 判断是否符合付款条件
+            if refunded_amount == float(price):                 # 已全额退款
+                return response.json(805)
+            if refund_amount + refunded_amount > float(price):  # 退款金额超出订单价格
+                return response.json(805)
+            # 部分退款标识
+            is_partial_refund = True if(float(price) > refund_amount + refunded_amount) else False
+
             out_request_no = str(time.strftime('%Y%m%d%H%M%S', time.localtime(now_time)))    # 退款请求号
             # 根据支付类型处理退款
             if payType == 1:    # PayPal
+                paymentID = order_qs[0]['paymentID']  # PayPal PAYMENT_ID
+                if not paymentID:
+                    return response.json(805)
                 paypalrestsdk.configure(PAYPAL_CRD)
                 payment = paypalrestsdk.Payment.find(paymentID)
-                print(payment)
+                print('payment', payment)
                 related_resources = payment['transactions'][0]['related_resources']
+                print('related_resources: ', related_resources)
                 if not related_resources:
                     return response.json(805)
                 sale = related_resources[0]['sale']
-                if sale['state'] != 'completed':
+                print('sale: ', sale)
+                # 没退款过 'state' 不为 'completed' 或 已退款过但 'state' 不为 'partially_refunded'
+                if (refunded_amount == 0 and sale['state'] != 'completed') or \
+                        (refunded_amount != 0 and sale['state'] != 'partially_refunded'):
                     return response.json(805)
                 sale_id = sale['id']
                 paypalSale = paypalrestsdk.Sale.find(sale_id)
@@ -1986,23 +2004,30 @@ class CloudStorageView(View):
                         'currency': currency
                     }
                 })
-                status = 5 if refund.success() else 4
+                refund_success = True if refund.success() else False
             elif payType == 2:  # 支付宝
                 aliPayObj = AliPayObject()
                 alipay = aliPayObj.conf()
                 refund_response = alipay.api_alipay_trade_refund(refund_amount=refund_amount, out_trade_no=orderID,
                                                                  out_request_no=out_request_no)
-                # 退款成功,修改订单支付状态为'退款成功',否则改为'退款失败'
-                status = 5 if refund_response['code'] == '10000' else 4
+                refund_success = True if(refund_response['code'] == '10000') else False
             elif payType == 3:  # 微信
                 wechatPayObj = WechatPayObject()
-                refund_amount = int(float(refund_amount) * 100)  # 退款金额,单位为分,只能为整数
-                refund_success = wechatPayObj.refund(out_trade_no=orderID, out_refund_no=out_request_no,
-                                                     total_fee=refund_amount, refund_fee=refund_amount)
-                status = 5 if refund_success else 4
+                refund_fee = int(refund_amount * 100)  # 退款金额,单位为分,只能为整数
+                success = wechatPayObj.refund(out_trade_no=orderID, out_refund_no=out_request_no,
+                                              total_fee=refund_fee, refund_fee=refund_fee)
+                refund_success = True if success else False
             else:   # 不支持退款的支付类型
                 return response.json(805)
-            order_qs.update(status=status, updTime=now_time)    # 更新订单状态
+
+            # 更新订单状态和已退款金额
+            update_dict = {'updTime': now_time}
+            if refund_success:
+                update_dict['status'] = 6 if is_partial_refund else 5
+                update_dict['refunded_amount'] = refunded_amount + refund_amount
+            else:
+                update_dict['status'] = 4
+            order_qs.update(**update_dict)
             return response.json(0)
         except Exception as e:
             print(e)

+ 2 - 2
Model/models.py

@@ -665,11 +665,12 @@ class Order_Model(models.Model):
     channel = models.SmallIntegerField(default=0, verbose_name=u'通道数')
     desc = models.CharField(max_length=50, default='', verbose_name='商品描述')
     price = models.CharField(default='', max_length=16, verbose_name='价格')
+    refunded_amount = models.FloatField(default=0, verbose_name=u'已退款金额')
     currency = models.CharField(blank=True, default='$', max_length=32, verbose_name=u'货币')
     addTime = models.IntegerField(verbose_name='添加时间', default=0)
     updTime = models.IntegerField(verbose_name='更新时间', default=0)
     isSelectDiscounts = models.SmallIntegerField(default=0, verbose_name=u'用户是否选择了第二年优惠 [0=否,1是]')
-    # 0: 待支付, 1:支付成功, 2: 取消支付, 4: 退款失败, 5: 退款成功, 9:处理中, 10:支付失败
+    # 0: 待支付, 1:支付成功, 2: 取消支付, 4: 退款失败, 5: 全额退款, 6: 部分退款, 9:处理中, 10:支付失败
     status = models.SmallIntegerField(default=0, verbose_name='付款状态')
     # 1: PayPal, 2: 支付宝, 3: 微信, 10: 免费体验, 11: 激活码
     payType = models.SmallIntegerField(default=0, verbose_name='支付方式')
@@ -690,7 +691,6 @@ class Order_Model(models.Model):
 
     # 备用字段
     spare_4 = models.CharField(default='', blank=True, max_length=64, db_index=True, verbose_name=u'备用字段4')
-    spare_2 = models.CharField(default='', blank=True, max_length=64, verbose_name=u'备用字段2')
 
     def __str__(self):
         return self.orderID

+ 1 - 1
Object/ResponseObject.py

@@ -63,7 +63,7 @@ class ResponseObject(object):
             801: 'The refund amount cannot be greater than the order price',
             802: 'The order has been completed and cannot be cancelled',
             804: 'Refund, please do not repeat the operation',
-            805: 'No refund',
+            805: 'Could not refund',
             900: 'There is no information about this version!',
             901: 'Getting URL failure!',
             902: 'No update!',