Pārlūkot izejas kodu

创建支付订单增加支付宝,微信支付

locky 3 gadi atpakaļ
vecāks
revīzija
a77ca48df1
1 mainītis faili ar 88 papildinājumiem un 31 dzēšanām
  1. 88 31
      Controller/AiController.py

+ 88 - 31
Controller/AiController.py

@@ -99,7 +99,7 @@ class AiView(View):
                 return response.json(tko.code)
             userID = tko.userID
             if operation == 'createpayorder':  # 创建支付订单
-                return self.do_create_pay_order(request_dict, userID, response)
+                return self.do_create_pay_order(request_dict, request, userID, response)
             elif operation == 'changeaistatus':  # 修改AI开关状态
                 return self.do_change_ai_status(userID, request_dict, response)
             elif operation == 'getAiStatus':  # 获取AI开关状态
@@ -418,7 +418,7 @@ class AiView(View):
             values('uid', 'use_status', 'endTime', 'bucket__content')
         return response.json(0, [ai_service_qs[0]])
 
-    def do_create_pay_order(self, request_dict, userID, response):
+    def do_create_pay_order(self, request_dict, request, userID, response):
         uid = request_dict.get('uid', None)
         channel = request_dict.get('channel', None)
         pay_type = int(request_dict.get('pay_type', 1))
@@ -446,45 +446,102 @@ class AiView(View):
                 values('currency', 'price', 'content', 'effective_day', 'title')
             if not ai_sm_qs.exists():
                 return response.json(173)
+            title = ai_sm_qs[0]['title']
+            content = ai_sm_qs[0]['content']
             currency = ai_sm_qs[0]['currency']
             price = ai_sm_qs[0]['price']
-            content = ai_sm_qs[0]['content']
             day = ai_sm_qs[0]['effective_day']
 
+            nowTime = int(time.time())
             orderID = CommonService.createOrderID()
             price = round(float(price), 2)
-            if pay_type == 1:
-                # 创建PayPal支付
-                cancel_url = CommonService.get_payment_status_url(lang, 'fail')
-                call_sub_url = "{}AiService/doPayPalCallBack?orderID={}&lang={}".format(SERVER_DOMAIN_SSL, orderID, lang)
-
-                paypalrestsdk.configure(PAYPAL_CRD)
-                payment = paypalrestsdk.Payment({
-                    "intent": "sale",
-                    "payer": {"payment_method": "paypal"},
-                    "redirect_urls": {"return_url": call_sub_url, "cancel_url": cancel_url},
-                    "transactions": [{
-                        "item_list": {"items": [
-                            {"name": "Cloud video", "sku": "1", "price": price, "currency": "USD", "quantity": 1}]},
-                        "amount": {"total": price, "currency": currency},
-                        "description": content}]})
-                if not payment.create():        # 创建失败
-                    return response.json(10, payment.error)
-                paymentID = payment['id']       # 获取paymentID
-                nowTime = int(time.time())
-                for link in payment.links:
-                    if link.rel == "approval_url":
-                        pay_url = str(link.href)
-                        Order_Model.objects.create(orderID=orderID, UID=uid, channel=channel, userID_id=userID,
-                                                   desc=content, payType=pay_type, payTime=nowTime, price=price,
-                                                   currency=currency, addTime=nowTime, updTime=nowTime, pay_url=pay_url,
-                                                   paymentID=paymentID, ai_rank_id=ai_meal_id, rank_id=1, order_type=1)
-                        return response.json(0, {"redirectUrl": pay_url, "orderID": orderID})
-                return response.json(10, 'create_ai_order_failed')
+
+            order_dict = {
+                'orderID': orderID,
+                'UID': uid,
+                'channel': channel,
+                'userID_id': userID,
+                'desc': content,
+                'payType': pay_type,
+                'payTime': nowTime,
+                'price': price,
+                'currency': currency,
+                'addTime': nowTime,
+                'updTime': nowTime,
+                'ai_rank_id': ai_meal_id,
+                'rank_id': 1,
+                'order_type': 1,
+            }
+
+            if pay_type == 1:       # PayPal支付
+                order_dict['paymentID'], order_dict['pay_url'] = self.create_paypal_payment(lang, orderID, price, currency, content, response)
+                res_data = {'redirectUrl': order_dict['pay_url'], 'orderID': orderID}
+            elif pay_type == 2:     # 支付宝
+                order_dict['pay_url'] = self.create_alipay_payment(lang, orderID, price, title, content, response)
+                res_data = {'redirectUrl': order_dict['pay_url'], 'orderID': orderID}
+            elif pay_type == 3:     # 微信支付
+                ip = CommonService.get_ip_address(request)
+                order_dict['pay_url'], sign_params = self.create_wechat_payment(orderID, ip, response)
+                res_data = {'redirectUrl': order_dict['pay_url'], 'orderID': orderID, 'result': sign_params}
+            Order_Model.objects.create(**order_dict)
+            return response.json(0, res_data)
         except Exception as e:
             print(e)
             return response.json(500, repr(e))
 
+    @staticmethod
+    def create_paypal_payment(lang, orderID, price, currency, content, response):
+        cancel_url = CommonService.get_payment_status_url(lang, 'fail')
+        call_sub_url = "{}AiService/doPayPalCallBack?orderID={}&lang={}".format(SERVER_DOMAIN_SSL, orderID, lang)
+
+        paypalrestsdk.configure(PAYPAL_CRD)
+        payment = paypalrestsdk.Payment({
+            "intent": "sale",
+            "payer": {"payment_method": "paypal"},
+            "redirect_urls": {"return_url": call_sub_url, "cancel_url": cancel_url},
+            "transactions": [{
+                "item_list": {"items": [
+                    {"name": "Cloud video", "sku": "1", "price": price, "currency": "USD", "quantity": 1}]},
+                "amount": {"total": price, "currency": currency},
+                "description": content}]})
+        if not payment.create():  # 创建失败
+            return response.json(10, payment.error)
+        paymentID = payment['id']  # 获取paymentID
+        for link in payment.links:
+            if link.rel == "approval_url":
+                pay_url = str(link.href)
+                return paymentID, pay_url
+        return response.json(10, 'create_ai_order_failed')
+
+    @staticmethod
+    def create_alipay_payment(lang, orderID, price, title, content, response):
+        aliPayObj = AliPayObject()
+        alipay = aliPayObj.conf()
+        subject = title + content
+        order_string = alipay.api_alipay_trade_wap_pay(
+            out_trade_no=orderID,
+            total_amount=price,
+            subject=subject,
+            return_url="{}web/paid2/success.html".format(SERVER_DOMAIN_SSL),
+            notify_url="{}AiService/doAlipayCallBack".format(SERVER_DOMAIN_SSL),
+            quit_url="{}web/paid2/fail.html".format(SERVER_DOMAIN_SSL),
+            passback_params=quote("lang=" + lang)
+        )
+        if not order_string:
+            return response.json(10, '生成订单错误.')
+        return aliPayObj.alipay_prefix + order_string
+
+    @staticmethod
+    def create_wechat_payment(lang, orderID, price, ip, content, response):
+        pay = WechatPayObject()
+        pay_url = "{}AiService/doWechatCallBack".format(SERVER_DOMAIN_SSL)
+        # 统一调用接口
+        pay.get_parameter(orderID, content, float(price) * 100, ip, pay_url, quote("lang=" + lang))
+        sign_params = pay.re_finall(orderid=orderID)
+        if not sign_params:
+            return response.json(10, '生成订单错误.')
+        return pay_url, sign_params
+
     def do_pay_by_paypal_callback(self, request_dict, response):
         logger = logging.getLogger('info')
         logger.info('AI订单---paypal支付回调')