def post(self, request): # 得到阿里支付的实例化对象 client = ali_pay() # 为API生成一个模板对象 初始化参数用的 model = AlipayTradePagePayModel() # 订单号 model.out_trade_no = "pay" + str(time.time()) # 金额 model.total_amount = 8888 # 商品标题 model.subject = "测试" # 商品详细内容 model.body = "支付宝测试" # 销售产品码,与支付宝签约的产品码名称 model.product_code = "FAST_INSTANT_TRADE_PAY" # 实例化一个请求对象 request = AlipayTradePagePayRequest(biz_model=model) # get请求 用户支付成功后返回的页面请求地址 request.return_url = "http://140.143.63.45:8888/alipay_handler" # post请求 用户支付成功通知商户的请求地址 request.notify_url = "http://140.143.63.45:8888/alipay_handler" # 利用阿里支付对象发一个获得页面的请求 参数是request response = client.page_execute(request, http_method="GET") # print(response) return redirect(response)
def get(self, request): order_id = request.GET.get('order_id') money = request.GET.get('money') # 业务逻辑处理 try: # 调用支付宝流程 alipay_client_config = AlipayClientConfig() # 创建配置对象 alipay_client_config.server_url = settings.ALIPAY_URL # 网关 alipay_client_config.app_id = settings.ALIPAY_APPID # APPID alipay_client_config.app_private_key = settings.APP_PRIVATE_KEY # 应用私钥 client = DefaultAlipayClient( alipay_client_config=alipay_client_config) # 使用配置创建客户端 model = AlipayTradePagePayModel() # 创建网站支付模型 model.out_trade_no = order_id # 商户订单号码 model.total_amount = str(1) # 支付总额 model.subject = '订单标题' # 订单标题 model.body = '订单描述' # 订单描述 model.product_code = 'FAST_INSTANT_TRADE_PAY' # 与支付宝签约的产品码名称,目前只支持这一种。 model.timeout_express = '30m' # 订单过期关闭时长(分钟) pay_request = AlipayTradePagePayRequest( biz_model=model) # 通过模型创建请求对象 pay_request.notify_url = settings.ALIPAY_NOTIFY_URL # 设置回调通知地址(POST) # pay_request.notify_url = None # 设置回调通知地址(POST) pay_request.return_url = settings.ALIPAY_RETURN_URL # 设置回调通知地址(GET) # pay_request.return_url = None # 设置回调通知地址(GET) response = client.page_execute(pay_request, http_method='GET') # 获取支付链接 pay_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') return HttpResponseRedirect(response) # 重定向到支付宝支付页面 except Exception as e: print(e) return JsonResponse({'code': 201, 'message': '支付失败,请重新支付'})
def post(self, request): money = request.data.get('money') if not money.isdigit(): return Response('错误,只能是数字') money = int(money) client = alipayclient() """ 页面接口示例:alipay.trade.page.pay """ # 对照接口文档,构造请求对象 model = AlipayTradePagePayModel() import time model.out_trade_no = 'pay' + str(time.time()) model.total_amount = money model.subject = "测试" model.body = "支付宝测试" model.product_code = "FAST_INSTANT_TRADE_PAY" request = AlipayTradePagePayRequest(biz_model=model) # 得到构造的请求,如果http_method是GET,则是一个带完成请求参数的url,如果http_method是POST,则是一段HTML表单片段 # get请求 用户支付成功后返回的页面请求地址 request.return_url = "http://127.0.0.1:8002/alipay_handler" # post请求 用户支付成功通知商户的请求地址 request.notify_url = "http://127.0.0.1:8002/alipay_handler" response = client.page_execute(request, http_method="GET") print("alipay.trade.page.pay response:" + response) return redirect(response)
def trade_page_pay(self): page_pay = AlipayTradePagePayModel() page_pay.out_trade_no = 91922212229232131 page_pay.product_code = 'FAST_INSTANT_TRADE_PAY' page_pay.total_amount = 10 page_pay.subject = '测试' request = AlipayTradePagePayRequest(biz_model=page_pay) request.return_url = 'http://139.199.123.96:8000/alipay/return/' request.notify_url = 'http://139.199.123.96:8000/alipay/return/' response = self.client.page_execute(request, http_method="GET") print("alipay.trade.page.pay response:" + response) return response
def pay_order(self, order): self.model.out_trade_no = order.order_no self.model.total_amount = order.total_price self.model.subject = order.subject self.model.body = order.subject _request = AlipayTradePagePayRequest(biz_model=self.model) _request.notify_url = self.notify_url _request.return_url = self.return_url # 得到构造的请求,如果http_method是GET,则是一个带完成请求参数的url,如果http_method是POST,则是一段HTML表单片段 _response = self.client.page_execute(_request, http_method="GET") return _response
def get_pay(out_trade_no, total_amount, return_url): model = AlipayTradePagePayModel() model.out_trade_no = out_trade_no model.total_amount = str(total_amount) model.subject = "测试" model.body = "支付宝测试" model.product_code = "FAST_INSTANT_TRADE_PAY" request = AlipayTradePagePayRequest(biz_model=model) request.notify_url = return_url + '?t=' + out_trade_no request.return_url = return_url + '?t=' + out_trade_no response = client.page_execute(request, http_method="GET") return response
def post(self, request): """生成支付宝自带页面的API""" # 实例化阿里支付的对象 client = aliPay() # 为API生成一个模板对象 model = AlipayTradePagePayModel() model.out_trade_no = 'LuffyCityPayment' + str(time.time()) # 订单号 model.total_amount = request.data.get("total_amount", None) # 金额 model.subject = request.data.get("subject", None) # 商品标题 model.body = request.data.get("body", None) # 商品详细内容 # 实例化一个请求对象 request = AlipayTradePagePayRequest(biz_model=model) # get请求 用户支付成功后返回的页面请求地址 request.return_url = "http://localhost:8080/shopping-cart" # post请求 用户支付成功通知商户的请求地址 request.notify_url = "http://127.0.0.1:8000/alipay_handler" # 利用阿里支付对象发一个获得页面的请求 参数是request response = client.page_execute(request, http_method="GET") return redirect(response)
def do_pay_goods(request): user_id = request.session.get('id') id = request.POST.get('id') name = request.POST.get('name') phone = request.POST.get('phone') address = request.POST.get('address') t = str(time.time()).split('.') order = t[0] + t[1] goods = Goods.objects.filter(id=id, is_activate=0) if not goods.exists(): return HttpResponse('该商品已被购买。') goods = goods[0] goods.is_activate = 1 goods.save() o = Orders(order=order, address=address, phone=phone, name=name, goods_id=id, user_id=user_id) o.save() money = goods.price alipay_client_config = AlipayClientConfig() alipay_client_config.server_url = 'https://openapi.alipaydev.com/gateway.do' alipay_client_config.app_id = '2016092100561600' alipay_client_config.app_private_key = 'MIIEpQIBAAKCAQEAxr2mbz2s8RigfJPxqYFht3hjtbEY9k/7C5BNvATGJRXSpI+9XmSCHG58OnYoZvYLgQoSLKRrE/z7ZeDL8gCVRuLWmEUHRYGEeBQ3IraaHqfHewP39yyoZ3M0Z9gIoK+yRIsgMcK4vdYwQGaw+MRypIEmqIC+AUeWOStYVDzK/TtcJPq8NZN+Gc2hzm94NE4F1fE+OFXVDPmoKJyljRsD2NYRz2iV6nuvRZ4inU31lpy6wNWwgqAZi6tIKMr1ElOFWH59ReCtkTyY0N2u4Z2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dVOL5IslU6cfrmg+HhX7FkykTD5kGYyF7m1Ja4/QfwV6658CgYEAq/YNAIzrLkQxQeyS26bbCf7+FT6jLGmKgxyReODIOPKk7S+vaZWbyHX14AW9cDBeOfsiIQobfkIHMRJtVqe5TC49lHh08zAX610SvXSzIkpeTIMfUVKwhr0xfOqCMSEHKNG6mGbTie0AvhGCHYl2ptAWZot9ugNcgwdTu43Nyag=' alipay_client_config.alipay_public_key = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA8OsmAJsaykPLzLIN//XnymU4s13kPE95EyOvZFq8TMPKJtsQ/f7eQB72wYAi3NarGT7RwebfJoAyeaM0KJsYNFqAodVHwpPRSemj8mwBtzEGGSyOT4Ilbv4KOz+HAsOkaLuQQ2ZGD1m7CCrgZCtxJDVqbd4xigSgMwrTFZYywoT0Sswv0oPV2TMl1tg+F7VUuvQpEvIn6qCQfzXa15eX9/P8OknvP9NSl3jOvDcpMliC2KvbfkNF9Qx9FEgtRlfnZdDItgmUj34VmBph+LUrAyV3lZjLpbFMhKlTBUeAbH72JPUKifXTXKsmbAl0n6cPaJQgb3zd+cfW7equoMhIEQIDAQAB' client = DefaultAlipayClient(alipay_client_config=alipay_client_config) model = AlipayTradePagePayModel() model.out_trade_no = o.order model.total_amount = money model.subject = "小动物保护网站-" + goods.name model.body = goods.name model.product_code = "FAST_INSTANT_TRADE_PAY" request = AlipayTradePagePayRequest(biz_model=model) request.return_url = 'http://39.105.195.67:8000/return_url' print('request:', request) # 得到构造的请求,如果http_method是GET,则是一个带完成请求参数的url,如果http_method是POST,则是一段HTML表单片段 response = client.page_execute(request, http_method="GET") print('response:', response) return HttpResponseRedirect(response)
def getAlipayUrl(request): token = request.POST['token'] user_token = Token.objects.filter(key=token) if len(user_token) == 0: return Response('nologin') user = user_token[0].user article_id = request.POST['article_id'] article = Article.objects.get(id=article_id) nowtime = datetime.datetime.now() new_payorder = PayOrder(belong=article, belong_user=user) new_payorder.order = str(nowtime.year) + str( random.randrange(10000000, 99999999)) # print(new_payorder.order) new_payorder.price = '9.9' # new_payorder.save() alipay_client_config = AlipayClientConfig() alipay_client_config.server_url = settings.ALIPAY_URL alipay_client_config.app_id = settings.ALIPAY_APPID alipay_client_config.app_private_key = settings.APP_PRIVATE_KEY alipay_client_config.alipay_public_key = settings.ALIPAY_PUBLIC_KEY client = DefaultAlipayClient(alipay_client_config=alipay_client_config) model = AlipayTradePagePayModel() model.out_trade_no = new_payorder.order model.total_amount = new_payorder.price model.subject = "打赏订单:" + new_payorder.order + '/' + new_payorder.price + '元' model.product_code = 'FAST_INSTANT_TRADE_PAY' model.timeout_express = '5m' # 发送请求 pay_request = AlipayTradePagePayRequest(biz_model=model) pay_request.notify_url = settings.ALIPAY_NOTIFY_URL pay_request.return_url = settings.ALITAY_RETURN_URL response = client.page_execute(pay_request, http_method='GET') print(response) pay_link = response return Response({'pay_link': pay_link})
def get(self,order_id): order = MovieOrder.query.get(order_id) total_price = order.o_price flight = Flight.query.get(order.o_flight_id) movie = Movie.query.get(flight.f_movie_id) # Alipay Client alipay_client_config = AlipayClientConfig() alipay_client_config.server_url = 'https://openapi.alipaydev.com/gateway.do' alipay_client_config.app_id = ALIPAY_APPID alipay_client_config.app_private_key = ALIPAY_PRIVATE_KEY alipay_client_config.alipay_public_key = ALIPAY_PUBLIC_KEY client = DefaultAlipayClient(alipay_client_config=alipay_client_config) model = AlipayTradePagePayModel() model.out_trade_no = "pay2018050200005262" model.total_amount = total_price model.subject = movie.showname model.body = order.o_seat model.product_code = "FAST_INSTANT_TRADE_PAY" pay_request = AlipayTradePagePayRequest(biz_model=model) pay_request.notify_url = 'https://ruoxianer.github.io/' pay_request.return_url = 'https://ruoxianer.github.io/' # 得到构造的请求,如果http_method是GET,则是一个带完成请求参数的url,如果http_method是POST,则是一段HTML表单片段 response = client.page_execute(pay_request, http_method="GET") # print("alipay.trade.page.pay response:" + response) data = { 'msg':'ok', 'status':HTTP_OK, 'data':{ 'pay_url':response, 'order_id':order_id } } return data
def generate_ali_pay_url(order): client = ali_pay() model = AlipayTradePagePayModel() # 订单id model.out_trade_no = order.pk # 订单总价格 model.total_amount = 88.00 # 商品标题 model.subject = "测试订单" # 商品详情 model.body = "肯德基订餐" # 商品码 model.product_code = "FAST_INSTANT_TRADE_PAY" # 实例化一个请求对象 request = AlipayTradePagePayRequest(biz_model=model) # get请求,用户支付成功后返回的页面地址 request.return_url = None # post请求,用户支付成功后通知商户的请求地址 request.notify_url = None # 利用阿里支付对象发一个获得页面的请求,参数是request response = client.page_execute(request, http_method="GET") print("alipay.trade.app.pay response:" + response) return response
def post(self, request): money = request.data.get('money') try: if isinstance(money, str): money = float('%.2f' % float(money)) except Exception as e: print(e) return Response('错误,只能是数字') print(money) client = alipayclient() """ 页面接口示例:alipay.trade.page.pay """ # 对照接口文档,构造请求对象 model = AlipayTradePagePayModel() import time model.out_trade_no = 'pay' + str(time.time()) model.total_amount = money model.subject = "测试" model.body = "支付宝测试" model.product_code = "FAST_INSTANT_TRADE_PAY" request = AlipayTradePagePayRequest(biz_model=model) # 得到构造的请求,如果http_method是GET,则是一个带完成请求参数的url,如果http_method是POST,则是一段HTML表单片段 # get请求 用户支付成功后返回的页面请求地址 request.return_url = "http://127.0.0.1:8000/api/v1/pay/alipay_handler" # request.return_url = "http://localhost:8080/Order" # post请求 用户支付成功通知商户的请求地址 request.notify_url = "http://127.0.0.1:8000/api/v1/pay/alipay_handler" # request.notify_url = "http://localhost:8080/Order" response = client.page_execute(request, http_method="GET") # print("alipay.trade.page.pay response:" + response) return redirect(response)
def alipy(request, order_num, total): # 导入配置类 alipy = AlipayClientConfig() # 支付宝的网关 alipy.server_url = 'https://openapi.alipaydev.com/gateway.do' # 商家APPID alipy.app_id = '2016092000556084' # 支付宝的私钥 alipy.app_private_key = 'MIIEpgIBAAKCAQEAzfsgTn6BAcTMRg4P5cbtcNgN0kQLkOVA2DGhrY+nIcXkzL5IjDdR85ck/dIslv/TeESyHbF48ssS9CiBS6YUASiD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0mWHL/azJkjTrTCFC634z6Ey0EoilUPUMubTNQubPn426LIWhYXw+mSmiJAoGBAMt+wg1LW4S30oFTap9Ea+QS4vK6SYsE3bmC58tydyKjxzMWGUfXD2tFgdF2AhgRKAop8QE/NVPisyoexGbM/7ks6Ujd2BlBSr0oCm7FY6W8f2SYqZz32IfakugQFx3zJK1Xe9HdNX8AUn+xkMiRPB8A+RLYqRsozgpNuZfwQ8Z/AoGBALb+uPBKRsaxC1q6jkL0MaeGNiCqO+StLCVMoFbTjatSrmw2wt386Qo0DYii+cS1KIXFixZJuw1m3vm4WR5JgoRj477xNVJTUFb/A/GMCLcum9IajLnuD+aqdV3vpGmpQdECsi1sbTMalcjaFozbZLkaJIB5akpfl//A+5UIbRbk' # 支付宝的公钥 alipy.alipay_public_key = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzfsgTn6BAcTMRg4P5cbtcNgN0kQLkOVA2DGhrY+nIcXkzL5IjDdR85ck/dIslv/TeESyHbF48ssS9CiBS6YUASiDa1bDv5A94GWos4FgFM4a8fKx4fc2QXhSY4rG+zIJAVSctwKnDTt0S8VtycS7GBu9uTAthLz4wZKNQexi1W6dSLkj7SDlYapR2zlmF+3NSB8ID52k7cQuPFR+tJSa0SMt0Q6w1kzjJmPDBW8tNo2lqGq9OguefZz4+wnGacaTjzB+e/P1Ni5oIc8L9JJyXfu3qUTop1QgogIOjCRAtM4RpQIp+GIGs72kljnosPqlUfn8kjBo9Ndj7SfzDoBpcQIDAQAB' # 设置默认的客户端配置 client = DefaultAlipayClient(alipay_client_config=alipy) # 订单号 order_num = order_num if order_num == "": return HttpResponse("请确认订单号正确") # 判断用户的登录状态 user_id = request.session.get("uid") if user_id == None: return redirect("/users/user_login") # 查询当前用户购买的订单 order_info = Order.objects.filter(order_num=order_num, user_id=user_id).get() # 判断此订单是否有效 # print(order_info) # return HttpResponse("1") if not order_info: return HttpResponse("订单有误") # 请求数据交易层模型 model = AlipayTradePagePayModel() # 订单号 model.out_trade_no = order_info.order_num # 订单的金额 model.total_amount = total # 标题 model.subject = "测试支付金额" # 订单描述 model.body = "这是一个巨款订单" # 销售的产品码 model.product_code = "FAST_INSTANT_TRADE_PAY" # 交易请求 qingqiu = AlipayTradePagePayRequest(biz_model=model) # 发送上一步的请求 第一个参数是qingqiu 第二个是发送的方式 # localhost:8000/cart/aliapy?2343243243&111 # 回调地址 #qingqiu.return_url = "http://47.107.229.163/cart/return_url/" qingqiu.return_url = "http://127.0.0.1:8000/cart/return_url/" xiangying = client.page_execute(qingqiu, http_method="GET") # print(xiangying) # return HttpResponse(1) return redirect(xiangying)
def do_place_order(request): address_id = request.POST.get('address', 0) if address_id == 0: return HttpResponse('请选择收货地址!') user_id = request.session.get('U_userid', 0) if user_id == 0: return HttpResponseRedirect('/user/login') # 根据收货地址的ID查询详情 address_info = user_address.objects.filter(id=address_id, users_id=user_id).first() if address_info == None: return HttpResponse('请选择收货地址!') # 查询购物车内所有的数据,计算金额 car_list = car.objects.filter(users_id=user_id) if not car_list: return HttpResponse('请选择商品!') total_money = 0 manage_id = 0 manage_name = '' goods_name_all = '' for cart in car_list: total_money += cart.number * int(cart.goods.goods_xprice) manage_id = cart.goods.manager_id #多个商品,最后一个覆盖前边的商品 manage_name = cart.goods.manager.shop_name goods_name_all += cart.goods.goods_name goods_info = GoodsInfo.objects.filter(id=cart.goods_id).first() if goods_info.goods_count < cart.number: return message(goods_info.goods_name + "库存不足,请重新选择<a href='%s'>返回购物车</a>" % (reverse('user:car_list'))) #库存减少 for cart in car_list: goods_info = GoodsInfo.objects.filter(id=cart.goods_id).first() goods_info.goods_count -= cart.number result = GoodsInfo.objects.filter(id=cart.goods_id).update( goods_count=goods_info.goods_count) if result: # 创建订单 orderObj = orders() orderObj.order_code = '20181010' + str(random.randint(100000, 999999)) orderObj.money = total_money orderObj.users_id = user_id orderObj.address = address_info.address orderObj.contacts = address_info.name orderObj.phone = address_info.phone orderObj.manage_id = manage_id orderObj.save() # 订单详情表 for cart in car_list: orderinfoObj = order_info() orderinfoObj.order_id = orderObj.id orderinfoObj.number = cart.number orderinfoObj.goods_id = cart.goods_id orderinfoObj.price = cart.goods.goods_xprice orderinfoObj.save() # 重定向到首页 alipay_client_config = AlipayClientConfig() alipay_client_config.server_url = 'https://openapi.alipaydev.com/gateway.do' # #手动设置 alipay_client_config.app_id = '2016092100561600' alipay_client_config.app_private_key = 'MIIEpgIBAAKCAQEA6KVS4YuG3pFvhbQHtBLjsgWXNe/Xjg2CVCc4+SfiwMeakR/O5IqZqVPThEl9allFyEHPFK7Anofebe+OO0BdYdGfCSrW0R+B85lEfH5YL2ggy9yCWB3GPiffA6B+UM2nO8HRMs1ulcpUPosCfzIucJUgX1IQZxJmAyTHmXzSoJVtY/1WbpLsP/zQgp+cUkfFi3CFDGxz6ex3w1U2q9SxemiQvUYbnn1EC3Yv3E0APloRrsAm+XIvPKLMacSbfHcZJw9EcaF3uNfNYtKzHGObigAzF1EEZlRbwG5LeOQ/iPJvbLilMyefFamuwgCZDBDTVQmHwA9QrjWjHUdeLZyW6QIDAQABAoIBAQCTZXtXbwEqLlbMDT38NyOP/L7K64RUefaivp74LO8bWNtzKwX4AmBMydFvNOiC4sC1mgxLfFSJnGeum2Iv5B3GBfuO4Vds81twLSEZByt0DbMJtlHW5jZd1wES2TJum31i/O9AEqwHt0McxRH16KNHrRPvkJzX0O5U+46CjnUcS5rZXir/OSDHwcN/cBxGqNhaEVsSSsWME8YOsfhCrVlKgI3vfW8UrEp/KociczM+N9wH0NbUlDgo2J9WuvH6/QyYkrLyO4KY5Bs05odS3wn+SoItOMEL8qROQ1iF9b2H5zYc7Ahd1kDBKGTSLVMw5iXUGBons0+eUCUoveiQF3+hAoGBAPcczgsFo9YuR1QSAjh2BsM9BEG2FAn5/xg4HzJoHEtd8fM4MAnwCGECWkzvNE866OaFs743ReQga9Nds4eI5Cg8019m1XAifzvxLaAK+nkotO+fz/8cNKrwS9pYFXNq2XRVph05r+Z/sSfMUUt4Iec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5LeOQ/iPJvbLilMyefFamuwgCZDBDTVQmHwA9QrjWjHUdeLZyW6QIDAQAB' """ 得到客户端对象。 注意,一个alipay_client_config对象对应一个DefaultAlipayClient,定义DefaultAlipayClient对象后,alipay_client_config不得修改,如果想使用不同的配置,请定义不同的DefaultAlipayClient。 logger参数用于打印日志,不传则不打印,建议传递。 """ client = DefaultAlipayClient(alipay_client_config=alipay_client_config) """ 页面接口示例:alipay.trade.page.pay """ # 对照接口文档,构造请求对象 model = AlipayTradePagePayModel() model.out_trade_no = orderObj.order_code model.total_amount = total_money model.subject = "Qshop-" + manage_name model.body = goods_name_all model.product_code = "FAST_INSTANT_TRADE_PAY" request = AlipayTradePagePayRequest(biz_model=model) request.return_url = 'http://39.105.195.67:80/user/return_url/' # 得到构造的请求,如果http_method是GET,则是一个带完成请求参数的url,如果http_method是POST,则是一段HTML表单片段 response = client.page_execute(request, http_method="GET") # print("alipay.trade.page.pay response:" + response) # 清空购物车 car.objects.filter(users_id=user_id).delete() return HttpResponseRedirect(response) else: return HttpResponse('操作有误!')
def do_place_order(request): address_id = request.POST.get('address', 0) if address_id == 0: return HttpResponse('请选择收货地址') user_id = request.session.get('U_userid', 0) if user_id == 0: return HttpResponseRedirect('/user/login') # 根据收货地址的ID 查询详细信息 address_info = user_address.objects.filter(id=address_id, users_id=user_id).first() # if address_info == None: return HttpResponse('请选择收货地址') # 查询购物车内所有的数据,计算金额 car_list = car.objects.filter(users_id=user_id) if not car_list: # return HttpResponse('请选择要结算的商品') total_money = 0 # 计算总金额 manage_id = 0 manage_name = '' goods_name_all = '' for cart in car_list: total_money += cart.number * int(cart.goods.goods_xprice) manage_id = cart.goods.manager_id # 多个商品 最后一个覆盖前边的结果 goods_name_all += cart.goods.goods_name goods_info = GoodsInfro.objects.filter(id=cart.goods_id).first() if goods_info.goods_count < cart.number: return message(goods_info.goods_name + "库存不足,请重新选择<a href='%s'>返回购物车</a>" % (reverse('user:car_list'))) # 创建订单 orderObj = orders() orderObj.order_code = '20181010' + str(random.randint(100000, 999999)) orderObj.money = total_money orderObj.users_id = user_id # orderObj.add_time = user_id orderObj.address = address_info.address orderObj.phone = address_info.phone orderObj.contacts = address_info.name orderObj.manage_id = manage_id orderObj.save() # 订单详情表 for cart in car_list: orderinfoObj = order_info() orderinfoObj.order_id = orderObj.id # orderObj.order=orderObj orderinfoObj.number = cart.number orderinfoObj.goods_id = cart.goods_id orderinfoObj.price = cart.goods.goods_xprice orderinfoObj.save() # 清空购物车 car.objects.filter(users_id=user_id).delete() # 支付 alipay_client_config = AlipayClientConfig() alipay_client_config.server_url = 'https://openapi.alipaydev.com/gateway.do' # alipay_client_config.app_id = '2016092100561606' alipay_client_config.app_private_key = 'MIIEowIBAAKCAQEAvAQeli6bj2kPQBlZmqic8bbfPo29J9XIE/NQM1QhcFU/lAcZpleGm38Y+9XnpUN3k2cxIn49GZTTwsSJ0kX6SSBWLkGZOs3RpaLsrgOtkuT1DIXzvoTDe5Q88Hf/5XQJ3GStJnn1ZRnBqokzRUExbTUnuSW+nQftl6Bv/Xh4Ov5L6GPDfesbTG8SQsZYibXREmvoB7cpcQuz8Ci7MJanDASU3aUHTh2jOe27F3F4cPRDE8XNjoDPcW4CqViXwNZvBXH6phNEzFmDpPcPWSp7U5lFI+29BuBx9AJdhii+DNgKR3iongQf206qlhpzotqE1jv6Cztu4g4l/CsAtH7a8QIDAQABAoIBAEuBYAMx/njuWRiF1a09j6GmrirB4zfvK6WPbiLe43roUVsSKuPZfI5P0Wm5coi0+n1w0JOonML8OLqcETknrybU3KdA4tdxtoLNVj1f7sHyPtLjrIpTaOnlE0ADAPpVymv+5mZwTfNnD5Z7+OIUF1RjJ2u8U7teZrUg0ji5u5b8vlmitHVIgun7wCiIfli4zMXuYT1hp4i4ABtxdetfbETthi5Gcj0KHbymhpWfAEiU21bjA8XSpnEddsGALRQpwK1xS0gm538a+uM0hlIzSW5pcohLw+MxfzRLNwgtoRAweyNdXorv4CsV/rJHqeKZmSopu/0/mu1vtHylBeH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Hf/5XQJ3GStJnn1ZRnBqokzRUExbTUnuSW+nQftl6Bv/Xh4Ov5L6GPDfesbTG8SQsZYibXREmvoB7cpcQuz8Ci7MJanDASU3aUHTh2jOe27F3F4cPRDE8XNjoDPcW4CqViXwNZvBXH6phNEzFmDpPcPWSp7U5lFI+29BuBx9AJdhii+DNgKR3iongQf206qlhpzotqE1jv6Cztu4g4l/CsAtH7a8QIDAQAB' """ 得到客户端对象。 注意,一个alipay_client_config对象对应一个DefaultAlipayClient,定义DefaultAlipayClient对象后,alipay_client_config不得修改,如果想使用不同的配置,请定义不同的DefaultAlipayClient。 logger参数用于打印日志,不传则不打印,建议传递。 """ client = DefaultAlipayClient(alipay_client_config=alipay_client_config) """ 页面接口示例:alipay.trade.page.pay """ # 对照接口文档,构造请求对象 model = AlipayTradePagePayModel() model.out_trade_no = orderObj.order_code model.total_amount = total_money #标题 model.subject = "智能硬件购物商城" #订单描述 model.body = goods_name_all model.product_code = "FAST_INSTANT_TRADE_PAY" request = AlipayTradePagePayRequest(biz_model=model) request.return_url = 'http://127.0.0.1:8000/user/return_url/' # 得到构造的请求,如果http_method是GET,则是一个带完成请求参数的url,如果http_method是POST,则是一段HTML表单片段 response = client.page_execute(request, http_method="GET") print("alipay.trade.page.pay response:" + response) return HttpResponseRedirect(response)
def do_place_order(request): # 获取用户id,并验证 user_id = request.session.get('u_id', 0) if user_id == 0: return HttpResponseRedirect(reverse('user:login')) # 获取用户选择的地址的id,并验证 address_id = request.POST.get('address_id', 0) if address_id == 0: return HttpResponse('收货地址无效') # 通过地址id和用户id(防止获取地址时,用户不在线,所以加上用户id), # 获取地址信息的对象,并验证 addressinfoobj = AddressInfo.objects.filter(id=address_id).first() if addressinfoobj is None: return HttpResponse('地址无效') orderinfoobj = OrderInfo() order_code = '20181010' + str(random.randint(100000, 999999)) orderinfoobj.order_code = order_code total_money = 0 manager_id = 0 goods_name_all = '' # 循环获取该用户购物车商品的对象 for carobj in Car.objects.filter(user_id=user_id): # 计算总的钱 s_goods_money = carobj.number * int(carobj.goods.goods_xprice) total_money += s_goods_money goods_name_all += carobj.goods.goods_name # 获取商家id,由于在循环里面,所以获取的是最后一个商家的eid manager_id = carobj.goods.manager_id # 获取每个商品对象,来判断用户购买数量是否大于库存 goods_obj = GoodsInfo.objects.filter(id=carobj.goods_id).first() # print(carobj.number) if carobj.number > goods_obj.goods_count: return message(goods_obj.goods_name + '库存不足,请重新选择。<a href="%s">点击返回购物车</a>' % (reverse('user:car_list'))) orderinfoobj.money = total_money orderinfoobj.manage_id = manager_id orderinfoobj.user_id = user_id orderinfoobj.address = addressinfoobj.address orderinfoobj.save() ordergoodsobj = OrderGoodsInfo() for carobj2 in Car.objects.filter(user_id=user_id): # 循环次数等于商品种类,所以, # 每循环一次,就在订单商品信息表里面添加一条数据 s_goods_money = carobj2.number * int(carobj2.goods.goods_xprice) ordergoodsobj.order_id = orderinfoobj.id ordergoodsobj.goods_id = carobj2.goods_id ordergoodsobj.number = carobj2.number ordergoodsobj.price = s_goods_money ordergoodsobj.save() # 信息确认并保存完成后,清空购物车 Car.objects.filter(user_id=user_id).delete() """ 设置配置,包括支付宝网关地址、app_id、应用私钥、支付宝公钥等,其他配置值可以查看AlipayClientConfig的定义。 """ # 修改配置文件的参数 alipay_client_config = AlipayClientConfig() alipay_client_config.server_url = 'https://openapi.alipaydev.com/gateway.do' alipay_client_config.app_id = '2016092100561552' alipay_client_config.app_private_key = 'MIIEpAIBAAKCAQEAqfV9wGAatzwNFHpoeTb3QZssVEzZd1+EVhMxHxIO9+pR0X/fiELSfOjD53GwIoLssxjj7Eq8+jgRZ9zscNKEobTlH1lVOR+y7m1I5Sz6Jjcs5Fvw1s/RppUa3Qh7bfQaCJU4cMndksibgTSM7S1xkDILWJNtaSjJTpIpHhQtWAoa1FYWPmZQWanbETBBHvxTJiJlxXbBcfFweVl86PCSNPD6uUHcZHNNiR0Dh3siedJVv77GQurp6fs54C0FvVV3ocCU76xoOXYIxS/ruoLLjCpEDUcOcG5QG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ntELLSQr7qrCWmcqqCHZ9HyH3185CO+D8KxZfkCgYAuweBzWLR2al7DWZtFdWR3XDw+MepoWS2OmCyH2Ek11zeUC0crZMHpxoWlB5f3ABrN3URzbUGSsdslZtjBdDqN6s6Dl+7Iu7wgdV60JDBrS6gqnNYRXRdresckQ2goqbACX9AXSHAM57bRJKkYy1mlwAwlJBywlsmlKq3Tqbcw4w==' alipay_client_config.alipay_public_key = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArw4+bgGlsoU52PjJmzVdjSw+r1ApgbPL8cJnEfnkBMZ6BaKFQYioZk1+XufCBJcDN0acIyNI1RYs0zMxbmP+0nFVqoXVFt0kjjIBYnlBVJ9imL/krb0/DaM7uLAoIWdKGlKCuj+3QqlrFI6S8MUBN059eEXsKuscWojbBSUJsuMeJ0UyBZZK74J1quFYfESdjdamti+CaFgOfzzqFyTcvyBqamSTpajy+BK836kaDia76/hNBPlHlwB8CAiZ9kK/XahmE1boCP0ay5s66T7BOstD3CZFzD8JvmtKyOYV/nNa82sQBK1VDxLcaO2g761hIcqa68AM3uANMs1mj4dq6wIDAQAB' """ 得到客户端对象。 注意,一个alipay_client_config对象对应一个DefaultAlipayClient,定义DefaultAlipayClient对象后,alipay_client_config不得修改,如果想使用不同的配置,请定义不同的DefaultAlipayClient。 logger参数用于打印日志,不传则不打印,建议传递。 """ # 根据配置文件生成 客户端对象 client = DefaultAlipayClient(alipay_client_config=alipay_client_config) """ 页面接口示例:alipay.trade.page.pay """ # 对照接口文档,构造请求对象 model = AlipayTradePagePayModel() model.out_trade_no = orderinfoobj.order_code model.total_amount = total_money model.subject = "支付测试" model.body = goods_name_all model.product_code = "FAST_INSTANT_TRADE_PAY" request = AlipayTradePagePayRequest(biz_model=model) request.return_url = 'http://47.93.191.192/user/pay_return_url/' # 得到构造的请求,如果http_method是GET,则是一个带完成请求参数的url,如果http_method是POST,则是一段HTML表单片段 response = client.page_execute(request, http_method="GET") print("alipay.trade.page.pay response:" + response) # 第二种重定向方法,应用名:页面名(找的是路由,更改路径不影响页面跳转) return HttpResponseRedirect(response)
model.product_code = "FAST_INSTANT_TRADE_PAY" settle_detail_info = SettleDetailInfo() settle_detail_info.amount = 50 settle_detail_info.trans_in_type = "userId" settle_detail_info.trans_in = "2088302300165604" settle_detail_infos = list() settle_detail_infos.append(settle_detail_info) settle_info = SettleInfo() settle_info.settle_detail_infos = settle_detail_infos model.settle_info = settle_info sub_merchant = SubMerchant() sub_merchant.merchant_id = "2088102176051162" model.sub_merchant = sub_merchant request = AlipayTradePagePayRequest(biz_model=model) request.return_url = "http://47.94.172.250:8804/page2/" request.notify_url = "http://47.94.172.250:8804/page2/" # 得到构造的请求,如果http_method是GET,则是一个带完成请求参数的url,如果http_method是POST,则是一段HTML表单片段 response = client.page_execute(request, http_method="GET") print("alipay.trade.page.pay response:" + response) import time time.sleep(1000) """ https://openapi.alipaydev.com/gateway.do? timestamp=2018-08-13+15%3A10%3A43& app_id=2016091700533057& method=alipay.trade.page.pay& charset=utf-8& format=json& version=1.0& sign_type=RSA2&sign=kFQhVQ0opjGiSZ34K1CRC9xQSROYVF8gQmfizXJ4MtlnP3pAWYfVjqZpRwxL6%2BFRSZ4OX8ldF345cbLOAV8Sz04ajTqcru7qnU6qj0OBhYMjBgqRCJyam7iyCK31BsgaOgyxSDTGFq9TvD00BEsP%2BIYo2KJLzM%2Fleeo6vCQAmiHnMIonXIwWVd8lTDN0fJYPipz9BsuaxZQ6lngA3jzZ14sMmptOdZwflhin%2F4sWYHeyTnc275jz7BDFqL3zkvvmvWtIgG7zL9X4VVbxY6IJqswvvQxx%2BupUkYwkI6xGFE%2FWif%2BwKdEaiNoGYwsRl4xR012B2RSv7vC8CMHtQlcN5A%3D%3D&
def post(self, request): """订单支付""" # 校验用户是否登录' user = request.user if not user.is_authenticated: return JsonResponse({'res': 0, 'errmsg': '用户未登录'}) # 接收参数 order_id = request.POST.get('order_id') # 校验参数 if not order_id: return JsonResponse({'res': 1, 'errmsg': '无效的订单id'}) try: order = OrderInfo.objects.get(order_id=order_id, user=user, pay_method=3, order_status=1) except OrderInfo.DoesNotExist: return JsonResponse({'res': 2, 'errmsg': '订单错误'}) # todo: 业务处理 使用python的SDK 调用支付宝的支付接口 logging.basicConfig( level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s', filemode='a', ) logger = logging.getLogger('') """ 设置配置,包括支付宝网关地址、app_id、应用私钥、支付宝公钥等,其他配置值可以查看AlipayClientConfig的定义。 """ alipay_client_config = AlipayClientConfig(sandbox_debug=True) alipay_client_config.server_url = 'https://openapi.alipaydev.com/gateway.do' alipay_client_config.app_id = '2016101900724693' # 应用id 这里是沙箱id alipay_client_config.charset = 'utf-8' with open( os.path.join(settings.BASE_DIR, 'apps/order/app_private_key.txt')) as f: alipay_client_config.app_private_key = f.read() with open( os.path.join(settings.BASE_DIR, 'apps/order/alipay_public_key.txt')) as f: alipay_client_config.alipay_public_key = f.read() alipay_client_config.sign_type = 'RSA2' # RSA或RSA2 """ 得到客户端对象。 注意,一个alipay_client_config对象对应一个DefaultAlipayClient,定义DefaultAlipayClient对象后, alipay_client_config不得修改,如果想使用不同的配置,请定义不同的DefaultAlipayClient。 logger参数用于打印日志,不传则不打印,建议传递。 """ client = DefaultAlipayClient(alipay_client_config=alipay_client_config, logger=logger) # 计算商品金额 total_pay = order.total_price + order.transit_price # Decimal类型 # 订单内容:商品信息 alipay_body = '' goods = OrderGoods.objects.filter(order=order) for good in goods: alipay_body = alipay_body + good.sku.desc + ';' # 对照接口文档,构造请求对象 model = AlipayTradePayModel() model.out_trade_no = order_id # 订单id model.total_amount = str(total_pay) # 支付总金额 model.subject = '天天生鲜用户:%s,您正在支付[天天生鲜网站]订单号为%s' % ( user.username, order_id) # 订单标题 model.body = alipay_body model.product_code = 'FAST_INSTANT_TRADE_PAY' # 与支付宝签约的产品码名称,目前只支持这一种 model.timeout_express = '30m' # 订单过期关闭时长(分钟) pay_request = AlipayTradePagePayRequest(biz_model=model) # 通过模型创建请求对象 # get请求 用户支付成功后返回的页面请求地址 pay_request.return_url = '' # post请求 用户支付成功后返回的页面请求地址 pay_request.notify_url = '' # 利用阿里支付对象发一个获取页面的请求 参数为order_string pay_url = client.page_execute(pay_request, http_method='GET') # 返回应答 return JsonResponse({'res': 3, 'pay_url': pay_url})