Luffy Xuecheng project - to pay - payment interface

 ############### payment interface ################

"" " 
Paid logic 
a front-end transmission parameters, 
click on paid immediately, what to do? Need to figure out this business, 
what you need to pass the tip? Here's front-end data needs to be passed, 
1, information does not need to send coupons, inside it are placed in redis, 
2, must pass the final amount due, can not say that you saw earlier 900, the background of the 1000 final payment is calculated to verify front-end and back-end consistent, 
3, only Alipay, so I do not requires payment, 
so the payment immediately after clicking, the amount need to pass Berry, and the total amount of 
{ 
    Beili: 100 
    Money: 900 
} 

Second, the back-end processing; 
1, pass over the front end of data acquisition, and has the total amount Bailey, 
2 , data validation, 
    2.1 requires both values are greater than or equal to 0, 
    whether personal accounts 100 2.2 Berry, lack of balance can not go down, 
3, to the settlement center for course information, course information may be multiple, need to loop course information 
payment_userid_courseid: { 
    COURSE_ID:. 1 
    title 
    IMG 
    price_policy_id:. 1 
    The coupon_id: { 
            } 
                . 1: {}
                2: {} 
    default_coupoon: 
    period: 
    period_display: 
    . Price: 
    } 
payment_global_coupon_ User ID: { 
  'coupon': { 
      2: { 'money_equivalent_value':} 200 is, # coupon ID: {coupon information} 
  }, 
  'default_coupon': 0 
} 
    3.1 check program to check whether the program is still valid, or on-line state, 
    3.2 pricing strategy is legitimate, whether in the presence of 
    3.3 verify the legality of the coupon, 
    3.4 to obtain the original price and coupon type, 
        if it is stand by, is the original price - Offer amount, and if the result is negative, that is, direct 0. 
        If a full cut, whether or not meet the minimum limit, 
        if a discount is a percentage off the original price *, 
4, GM coupon processing: 
    4.1 General coupon is legitimate 
        if is stand by, is the original price - discount amount, and if the result is negative, that is, direct 0. 
        If a full cut, whether or not meet the minimum limit, 
        if a discount is a percentage off the original price *, 
    this step after that is the amount actually paid, and 
5, processing beli
    4.1 calculates whether sufficient beli 
    4.2 after the price calculation beli 
6, check the total amount 
     actually paid - amounts payable Berry is a need to pass over the front and paymoney determine whether the consistent 
     calculation of the final amount, to calculate the original price of each course - = discount coupons, and discounts for all courses together, and then go - universal coupons to get the final price, 
7, generate orders, if it is an order to buy a three course 
there is a transaction-based to operate of either succeed or fail, 
    the order table to generate data, 
    the Order Details table to generate three data, and generate the corresponding recorded three courses of registration, 
    registration form, you need to generate record, 
    if there is Berry, the first need Berry generated transaction records, second, balance field to be deducted, 
    the state of coupon updates 
    Note: If the payment amount is 0, the interface directly without calling Alipay, the order status is paid, 
    Note: If the payment amount is greater than 0, you must call interface Alipay, and 
        generate url, containing the order number, 
        the callback function Used to update the order status, 

"" "

 

 ############### payment interface ################

from django.core.exceptions Import ObjectDoesNotExist
 from utils.pay Import AliPay
 Import Time
 class PaymentView (APIView):
     # front end needs to pass what? The following data is the need to pass the distal end, 
    # { 
    # pay_money: 800 
    # course_info: [ 
    #                  { 
    #                  COURSE_ID:. 1 
    #                  price_policy_id:. 1 
    #                  coupon_record_id:. 1 
    #                    }, 
    #                  { 
    #                  COURSE_ID: 2 
    #                 price_policy_id:5
    #                 coupon_record_id :2
    #                   }
    # ]
    # global_coupon_id :1
    # beli:1000
    # }

    authentication_classes = [TokenAuth, ]
    def post(self,request):
        now = datetime.datetime.utcnow()
        res = BaseException()
        # 1,获取前端传过来的数据
        user = request.user
        courses_info = request.data.get('courses_info')
        global_coupon_id = request.data.get('global_coupon_id')
        beli = request.data.get('beli')
        pay_money = request.data.get('pay_money')
        print(courses_info,global_coupon_id,beli,pay_money)
        # 2,循环课程信息
        course_price_list = []
        try:
            for course_info in courses_info:
                course_id = course_info.get('course_id')
                price_policy_id Course_info.get = ( ' price_policy_id ' ) 
                The coupon_id = course_info.get ( ' coupon_record_id ' )
                 Print (COURSE_ID, price_policy_id, The coupon_id)
             #      2.1 check data 
            #      2.2 check whether there course, 
                course_obj models.Course.objects.get = ( = the above mentioned id COURSE_ID)
             #      2.3 pricing strategy is legitimate, 
                iF price_policy_id not  in [obj.pk for obj in course_obj.price_policy.all ()]:
                     The raise CommentException (1051, 'Price policy does not exist ' ) 

            #      2.4 verify the coupon is legitimate 
                coupon_record = models.CouponRecord.objects.filter ( 
                    PK = The coupon_id,
                     # the Account = request.user, 
                    # Status = 0, 
                    # coupon__valid_begin_date__lte = now, 
                    # coupon__valid_end_date__gte = now, 
                    # coupon__object_id = COURSE_ID 
                ) .first ()
                 Print (coupon_record)
                 IF  not coupon_record:
                     The raise CommentException (1052, ' coupons have a problem ')
             #      2.5 After calculating the price discount coupon programs 
                # should be calculated according to different types of coupons 
                course_price = models.PricePolicy.objects.get (PK = price_policy_id) .price
                 Print (course_price) 
                coupon_price = self.coupon_price (course_price, coupon_record ) 
                course_price_list.append (coupon_price) 
                Print (course_price_list)
             # 3, GM coupon processing: 
            #      3.1 generic coupon is legitimate 
            global_coupon_record = models.CouponRecord.objects.filter ( 
                PK = global_coupon_id, 
                the Account =request.user, 
                Status = 0, 
                coupon__valid_begin_date__lte = now, 
                coupon__valid_end_date__gte = now, 
                coupon__object_id = None 
            ) .first () 
            IF  not global_coupon_record:
                 The raise CommentException (1053, ' Universal coupons in question ' )
             #      after the general computing coupon price 
            global_coupon_price = self.coupon_price (SUM (course_price_list), global_coupon_record)
             Print (global_coupon_price) 

            #4, the processing Beli 
            #      4.1 Beli calculate whether a sufficient 
            IF Beli> request.user.beli:
                 The raise CommentException (1054, ' insufficient number Beli ' )
             #      price calculated after 4.2 Beli 
            final_price = global_coupon_price - Beli / 10 Print (final_price)
             IF final_price < 0: 
                final_price = 0
             # 5, whether the comparison parameters and paymoney consistent iF final_price =! pay_money:
                 The raise CommentException (1055, ' the price actually paid and the price parameters inconsistent ' )
            
            

            # 7, generate orders 
            # Orders table 
            # Order Details table 

            # 8, to build two-dimensional code page url Alipay, 

            alipay = self.get_alipy () 

            query_params = alipay.direct_pay ( 
                Subject = " inflatable Han " ,   # simple commodity description 
                out_trade_no = " X2 " + str (time.time ()),   # merchant order number 
                TOTAL_AMOUNT = pay_money,   # transaction amount (unit: RMB reserved previous two decimal places) 
            ) 

            pay_url = " https://openapi.alipaydev.com/ gateway.do? {}" .Format (query_params) 

            res.data = ' order to create a successful ' 
            res.url = pay_url
         the except ObjectDoesNotExist AS E: 
            res.code = 1050 
            res.error = ' Course information does not exist ' 
        the except CommentException AS E: 
            res.code = E .code 
            res.error = e.msg
         the except Exception AS E: 
            res.code = 500 
            res.error = STR (E)
         return the Response (res.dict)


    def coupon_price(self,price,coupon_record):
        coupon_type = coupon_record.coupon.coupon_type
        money_equivalent_value = coupon_record.coupon.money_equivalent_value
        off_percent = coupon_record.coupon.off_percent
        minimum_consume = coupon_record.coupon.minimum_consume

        rebate_price = 0
        if coupon_type==0:
            rebate_price = price-money_equivalent_value
            if rebate_price<0:
                rebate_price=0
        elif coupon_type ==. 1 :
             IF . price < minimum_consume:
                 The raise CommentException ( ' does not satisfy the minimum consumption ' , 1054 )
         the else : 
            rebate_price * off_percent / 100
         return rebate_price 


    DEF get_alipy (Self): 

        obj = AliPay ( 
            AppID = settings.APPID, 
            app_notify_url = settings.NOTIFY_URL,   # if successful payment, Alipay will send a POST request to the address (check whether the payment has been completed) 
            return_url = settings.RETURN_URL,   #If the payment is successful, redirected back to the address of your website. 
            = settings.PUB_KEY_PATH alipay_public_key_path,   # Alipay public 
            app_private_key_path = settings.PRI_KEY_PATH,   # application private 
            Debug = True,   # default False, 
        )
         return obj

 

 ############### payment interface ################

 

 ############### payment interface ################

 ############### payment interface ################

 

Guess you like

Origin www.cnblogs.com/andy0816/p/12302097.html