标签:vat spl 没有 onclick self log cat bsp note
1 import datetime 2 3 from django.core.exceptions import ObjectDoesNotExist 4 5 from rest_framework.views import APIView 6 from rest_framework.response import Response 7 8 from app01.utils.auth import LoginAuth 9 from app01.utils.response import BaseResponse 10 from app01.utils.exception import CommonException 11 from app01.utils.pay import AliPay 12 from app01.models import * 13 14 15 class PaymentView(APIView): 16 ‘‘‘ 17 模拟请求数据格式: 18 19 { 20 is_beli:true, 21 course_list=[ 22 { course_id:1 23 default_price_policy_id:1, 24 coupon_record_id:2 25 }, 26 { course_id:2 27 default_price_policy_id:4, 28 coupon_record_id:6 29 } 30 ], 31 global_coupon_id:3, 32 pay_money:298 33 34 } 35 36 37 状态码: 38 1000: 成功 39 1001: 课程不存在 40 1002: 价格策略不合法 41 1003: 加入购物车失败 42 1004: 获取购物车失败 43 1005: 贝里数有问题 44 1006: 优惠券异常 45 1007: 优惠券未达到最低消费 46 1008: 支付总价格异常 47 48 ‘‘‘ 49 authentication_classes = [LoginAuth] 50 51 52 def get_alipay(self): 53 # 沙箱环境地址:https://openhome.alipay.com/platform/appDaily.htm?tab=info 54 app_id = "2016091100486897" 55 # POST请求,用于最后的检测 56 notify_url = "http://47.94.172.250:8804/page2/" 57 # notify_url = "http://www.wupeiqi.com:8804/page2/" 58 # GET请求,用于页面的跳转展示 59 return_url = "http://47.94.172.250:8804/page2/" 60 # return_url = "http://www.wupeiqi.com:8804/page2/" 61 merchant_private_key_path = "keys/app_private_2048.txt" 62 alipay_public_key_path = "keys/alipay_public_2048.txt" 63 64 alipay = AliPay( 65 appid=app_id, 66 app_notify_url=notify_url, 67 return_url=return_url, 68 app_private_key_path=merchant_private_key_path, 69 alipay_public_key_path=alipay_public_key_path, # 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥 70 debug=True, # 默认False, 71 ) 72 return alipay 73 74 def cal_coupon_price(self, price, coupon_record_obj): 75 coupon_type = coupon_record_obj.coupon.coupon_type 76 money_equivalent_value = coupon_record_obj.coupon.money_equivalent_value 77 off_percent = coupon_record_obj.coupon.off_percent 78 minimum_consume = coupon_record_obj.coupon.minimum_consume 79 rebate_price = 0 80 if coupon_type == 0: # 立减券 81 rebate_price = price - money_equivalent_value 82 if rebate_price <= 0: 83 rebate_price = 0 84 elif coupon_type == 1: # 满减券 85 if minimum_consume > price: 86 raise CommonException(1007, "优惠券未达到最低消费") 87 else: 88 rebate_price = price - money_equivalent_value 89 elif coupon_type == 2: 90 rebate_price = price * off_percent / 100 91 92 return rebate_price 93 94 def post(self, request): 95 print(request.data) 96 response = BaseResponse() 97 # 1.获取数据 98 99 user_id = request.user.pk 100 global_coupon_id = request.data.get("global_coupon_id") 101 pay_money = request.data.get("pay_money") 102 course_list = request.data.get("course_list") 103 is_beli = request.data.get("is_beli") 104 now = datetime.datetime.now() 105 106 try: 107 # 2 校验数据 108 # 2.2 校验课程 109 110 course_price_list = [] 111 for course_dict in course_list: 112 # 2.2.1 校验课程id 113 course_id = course_dict.get("course_id") 114 course_obj = Course.objects.get(pk=course_id) 115 # 2.2.2 价格策略id 116 if course_dict.get("default_price_policy_id") not in [obj.pk for obj in course_obj.price_policy.all()]: 117 raise CommonException(1002, "价格策略异常") 118 # 2.2.3 课程优惠券id 119 price_policy_obj = PricePolicy.objects.get(pk=course_dict.get("default_price_policy_id")) 120 course_dict["original_price"] = price_policy_obj.price 121 course_dict["valid_period_display"] = price_policy_obj.get_valid_period_display() 122 course_dict["valid_period"] = price_policy_obj.valid_period 123 coupon_record_id = course_dict.get("coupon_record_id") 124 if coupon_record_id: 125 coupon_record_list = CouponRecord.objects.filter(account=request.user, 126 status=0, 127 coupon__valid_begin_date__lte=now, 128 coupon__valid_end_date__gte=now, 129 coupon__content_type_id=9, 130 coupon__object_id=course_id 131 ) 132 print("coupon_record_id", coupon_record_id) 133 print([obj.pk for obj in coupon_record_list]) 134 # 默认获取没有选择的优惠券和筛选后优劵 135 if coupon_record_id not in [obj.pk for obj in coupon_record_list]: 136 raise CommonException(1006, "课程优惠劵异常") 137 # 计算循环课程的课程优惠券优惠后的价格 138 coupon_record_obj = CouponRecord.objects.get(pk=coupon_record_id) 139 rebate_price = self.cal_coupon_price(price_policy_obj.price, coupon_record_obj) 140 course_price_list.append(rebate_price) 141 course_dict["rebate_price"] = rebate_price 142 else: 143 course_price_list.append(price_policy_obj.price) 144 145 # 2.3 校验通用优惠券id 146 global_coupon_record_list = CouponRecord.objects.filter(account=request.user, 147 status=0, 148 coupon__valid_begin_date__lte=now, 149 coupon__valid_end_date__gte=now, 150 coupon__content_type_id=9, 151 coupon__object_id=None 152 ) 153 print("global_coupon_record_list", global_coupon_record_list) 154 155 156 157 if global_coupon_id: 158 if global_coupon_id not in [obj.pk for obj in global_coupon_record_list]: 159 raise CommonException("通用优惠券异常", 1006) 160 global_coupon_record_obj = CouponRecord.objects.get(pk=global_coupon_id) 161 final_price = self.cal_coupon_price(sum(course_price_list), global_coupon_record_obj) 162 else: 163 final_price = sum(course_price_list) 164 # 2.4 计算实际支付价格与money 校验 165 if is_beli: 166 final_price = final_price - request.user.beli / 10 167 if final_price < 0: 168 final_price = 0 169 print(final_price) 170 171 if final_price != float(pay_money): 172 raise CommonException("支付总价格异常!", 1008) 173 174 # 4.调用alipay 接口 175 alipay = self.get_alipay() 176 query_params = alipay.direct_pay( 177 subject="路飞学成", 178 out_trade_no="x345" + str(datetime.datetime.now()), 179 total_amount=pay_money, 180 ) 181 pay_url = "https://openapi.alipaydev.com/gateway.do?{}".format(query_params) 182 response.data = { 183 "url": pay_url 184 } 185 186 187 188 except ObjectDoesNotExist as e: 189 response.code = 1001 190 response.msg = "课程不存在!" 191 except CommonException as e: 192 response.code = e.code 193 response.msg = e.msg 194 # except Exception as e: 195 # response.code = 500 196 # response.msg = str(e) 197 198 return Response(response.dict)
标签:vat spl 没有 onclick self log cat bsp note
原文地址:https://www.cnblogs.com/zhangqing979797/p/10148391.html