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 get_pay_url(cls, order_id, total_amount): # 获取内容 with open(ALIPAY_KEY_PATH) as f: alipay_public_key = f.read() with open(APP_KEY_PRIVATE_PATH) as f: app_private_key = f.read() # 设置配置,包括支付宝网关地址、app_id、应用私钥、支付宝公钥; config = AlipayClientConfig() config.server_url = settings.SERVER_URL config.app_id = settings.APP_ID config.app_private_key = app_private_key config.alipay_public_key = alipay_public_key # 得到 client = DefaultAlipayClient(alipay_client_config=config) model = AlipayTradePagePayModel() model.out_trade_no = order_id model.total_amount = str(total_amount) model.subject = "天天生鲜-%s" % order_id model.product_code = "FAST_INSTANT_TRADE_PAY" model.body = '天天生鲜' request = AlipayTradePagePayRequest(biz_model=model) # 得到构造的请求,如果http_method是GET,则是一个带完成请求参数的url response_url = client.page_execute(request, http_method="GET") return response_url
def __init__(self): # 得到阿里支付的实例化对象 self.client = alipayconfig() # 为API生成一个模版对象,初始化参数用的 self.model = AlipayTradePagePayModel() # 实例化一个请求对象 self.request = AlipayTradePagePayRequest(biz_model=self.model)
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 alipay_trade_page(order): """ 页面接口示例:alipay.trade.page.pay,--统一收单下单 """ client = alipay_base_config() # 对照接口文档,构造请求对象 model = AlipayTradePagePayModel() # 必填 model.out_trade_no = order.order_id model.total_amount = str(order.total_price) model.subject = "天天生鲜测试" model.product_code = "FAST_INSTANT_TRADE_PAY" # 选填中有些必填 settle_detail_info = SettleDetailInfo() settle_detail_info.amount = str(order.total_price) # 必填 settle_detail_info.trans_in_type = "userId" # 必填 settle_detail_info.trans_in = settings.ALIPAY_UID # 必填 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 = "2088301300153242" # model.sub_merchant = sub_merchant request = AlipayTradePagePayRequest(biz_model=model) # 得到构造的请求,如果http_method是GET,则是一个带完成请求参数的url,如果http_method是POST,则是一段HTML表单片段 response = client.page_execute(request, http_method="GET") return response
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': '参数不完整'}) 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': '无效的订单id'}) """ 设置配置,包括支付宝网关地址、app_id、应用私钥、支付宝公钥等,其他配置值可以查看AlipayClientConfig的定义。 """ alipay_client_config = AlipayClientConfig() alipay_client_config.server_url = 'https://openapi.alipaydev.com/gateway.do' alipay_client_config.app_id = '2016100100641374' app_private_key = '' with open(os.path.join(settings.BASE_DIR, 'apps/order/app_private_key.pem'), 'r') as f: for line in f: app_private_key += line alipay_client_config.app_private_key = app_private_key alipay_public_key = '' with open(os.path.join(settings.BASE_DIR, 'apps/order/alipay_public_key.pem'), 'r') as f: for line in f: alipay_public_key += line alipay_client_config.alipay_public_key = alipay_public_key """ 得到客户端对象。 注意,一个alipay_client_config对象对应一个DefaultAlipayClient,定义DefaultAlipayClient对象后,alipay_client_config不得修改,如果想使用不同的配置,请定义不同的DefaultAlipayClient。 logger参数用于打印日志,不传则不打印,建议传递。 """ client = DefaultAlipayClient(alipay_client_config=alipay_client_config, logger=logger) total_pay = order.transit_price + order.total_price total_pay = round(float(total_pay), 2) model = AlipayTradePagePayModel() model.out_trade_no = order_id model.total_amount = total_pay model.subject = "天天生鲜{0}".format(order_id) model.product_code = "FAST_INSTANT_TRADE_PAY" request = AlipayTradePagePayRequest(biz_model=model) response = client.page_execute(request, http_method="GET") # 访问支付页面 return JsonResponse({'res': 3, 'response': 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 create_token_params(config: GatewayConfig, token_config: TokenConfig) -> dict: # params = {} # customer_id = token_config.customer_id # if customer_id and config.store_customer: # params["customer_id"] = customer_id model = AlipayTradePagePayModel() model.out_trade_no = "202005020000226" model.total_amount = 50 model.subject = "测试" model.body = "支付宝测试" model.product_code = "FAST_INSTANT_TRADE_PAY" request = AlipayTradePagePayRequest(biz_model=model) return request
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): # 获得支付宝客户端 client = AlipayView.sign() # 构造请求参数 model = AlipayTradePagePayModel() model.out_trade_no = order_id model.total_amount = str(order.total_price) model.subject = "天天生鲜订单支付" model.timeout_express = str(settings.ALIPAY_TIMEOUT_MINUTE) + 'm' pay_request = AlipayTradePagePayRequest(biz_model=model) url = 'http://121.40.139.86:8000' + reverse('order:alipay_result') pay_request.notify_url = url # 支付后回调地址 pay_url = client.page_execute(pay_request, http_method='GET') # 获取支付链接 return JsonResponse({'res': 0, 'msg': '正在支付中...', 'url': pay_url})
def response_page_pay(trade_no: str, price) -> str: ''' 处理电脑页面的支付请求,返回跳转到支付宝的页面地址 :传入参数: trade_no: 字符串格式的订单号 price: 价格 :返回: 字符串格式的支付地址 ''' model = AlipayTradePagePayModel() model.subject = "course" model.total_amount = price model.out_trade_no = trade_no model.body = "Courses" model.product_code = "FAST_INSTANT_TRADE_PAY" request = AlipayTradePagePayRequest(biz_model=model) return CLIENT.page_execute(request, http_method="GET")
def get_url_request(): app_alipay_sanbox_private_path = r'C:\Users\jerry\Desktop\app_private_key.pem' alipay_sanbox_public_path = r'C:\Users\jerry\Desktop\alipay_public_key.pem' alipay_client_config = AlipayClientConfig() alipay_client_config.server_url = "https://openapi.alipaydev.com/gateway.do" alipay_client_config.app_id = "20160923420591565" alipay_client_config.app_private_key = get_key( app_alipay_sanbox_private_path) alipay_client_config.alipay_public_key = get_key(alipay_sanbox_public_path) alipay_client_config.charset = 'utf-8' client = DefaultAlipayClient(alipay_client_config=alipay_client_config) model = AlipayTradePagePayModel() model.out_trade_no = "pay201805020000227" model.total_amount = 50 model.subject = "test" model.body = "alipaytest" 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 = "2088302300165605" # # 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 = "208830234153246" # # model.sub_merchant = sub_merchant request = AlipayTradePagePayRequest(biz_model=model) response = client.page_execute(request, http_method="GET") return 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_user=user, belong=article) 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) # 得到构造的请求,如果http_method是GET,则是一个带完成请求参数的url,如果http_method是POST,则是一段HTML表单片段 # pay_request.notify_url = settings.ALIPAY_NOTIFY_URL # pay_request.return_url = settings.ALIPAY_RETURN_URL response = client.page_execute(pay_request, http_method='GET') print(response) pay_link = response return Response({'pay_link': pay_link})
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+OFXVDPmoKJyljRsD2NYRz2iV6nuvRZ4inU31lpy6wNWwgqAZi6tIKMr1ElOFWH59ReCtkTyY0N2u4Z2TopiZgpCnZ3tQAoSEOslyefIH4w/I/dm2zxH3FF7bRuEp5jkueem5gfzmarGRClNp0wIDAQABAoIBABTfaDJ4tMghgQF0fEYEK6IcR8SWU/vSjJg7UJ61laXhc90Kp6XZQnz/8ZYmQLoHj0+/IgeEQSa5RCIACQtimkr2mfkmDsxy/Nmrrdq8eNVNY7r8wLc5/nnW9KMPYmCV81AVmI0BWWu+qhSpdF68Kxox4kCCPPJfdVyNu9olBGCyAtPKp2J/+lD/qCG9uxo7ltT5L4nsoR9gciLqce4B4VImHroUatDcOrDeMniRhNHkNa2YRkV4ddncAHtexAjA8yesTO1bO1D/TyLOHRnNElTBSDJbJaVzifgHM0Om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FqAodVHwpPRSemj8mwBtzEGGSyOT4Ilbv4KOz+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 init_app(self, app): config = app.config['ALIPAY'] if not hasattr(app, "extensions"): app.extensions = {} app.extensions['alipay'] = self alipay_client_config = AlipayClientConfig(config['DEBUG']) alipay_client_config.app_id = config['ALIPAY_APP_ID'] alipay_client_config.app_private_key = open(config['ALIPAY_PRIVATE_KEY_FILE'], "r").read() alipay_client_config.app_public_key = open(config['ALIPAY_PUB_KEY_FILE'], "r").read() self.alipay_client_config = alipay_client_config self.model = AlipayTradePagePayModel() self.model.product_code = "FAST_INSTANT_TRADE_PAY" self.return_url = config['ALIPAY_RETURN_URL'] self.notify_url = config['ALIPAY_NOTIFY_URL'] self.logger = self.get_logger(config['LOG_PATH']) self.logger.debug(self.notify_url) self.logger.debug(self.return_url) self.client = DefaultAlipayClient(alipay_client_config=alipay_client_config, logger=self.logger)
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 get_payment_page(request, houseid): # https://opendocs.alipay.com/apis/api_1/alipay.trade.page.pay model = AlipayTradePagePayModel() # 产品订单号 model.out_trade_no = '202003051646520001' # 订单总金额 model.total_amount = 200 # 订单主题 model.subject = '租房订金' # model.body = '支付宝测试' # 销售产品码,与支付宝签约的产品码名称。 注:目前仅支持FAST_INSTANT_TRADE_PAY model.product_code = 'FAST_INSTANT_TRADE_PAY' # 结算详细信息 settle_detail_info = SettleDetailInfo() settle_detail_info.amount = 200 # 结算收款方的账户类型 settle_detail_info.trans_in_type = 'cardAliasNo' # 结算收款方 settle_detail_info.trans_in = '6216603100004601162' 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 = '2088102180149774' # model.sub_merchant = sub_merchant request = AlipayTradePagePayRequest(biz_model=model) url = alipay_client.page_execute(request, http_method='GET') # 此处应该补充生成交易流水的代码 ---> Model # 将用户信息、房源信息、交易编号、交易金额、交易时间、交易状态、……写入数据库 # 还需要一个设计一个接口作为接收通知的接口其中包括对订单交易结果的查询 # 最后还要更新之前的交易流水信息 # request = AlipayTradeQueryRequest(biz_model=model) # content = alipay_client.page_execute(request) return DefaultResponse(data={'url': url})
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 alipay(request): total_price = request.GET.get('total_price') # 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 = "pay201805020000526" model.total_amount = total_price model.subject = "测试" model.body = "支付宝测试" model.product_code = "FAST_INSTANT_TRADE_PAY" request = AlipayTradePagePayRequest(biz_model=model) # 得到构造的请求,如果http_method是GET,则是一个带完成请求参数的url,如果http_method是POST,则是一段HTML表单片段 response = client.page_execute(request, http_method="GET") # print("alipay.trade.page.pay response:" + response) return redirect(response)
def payorders(self, order_id, total_amount): model = AlipayTradePagePayModel() model.product_code = 'FAST_INSTANT_TRADE_PAY' model.total_amount = str(total_amount) model.subject = '天天生鲜订单支付-天天艹楼雅倩' model.out_trade_no = order_id request = AlipayTradePagePayRequest(biz_model=model) response_content = self.client.page_execute(request, http_method='GET') if not response_content: return "caolouyaqianshibai" # response = AlipayTradePagePayResponse() # response.parse_response_content(response_content) # # 响应成功的业务处理 # if response.is_success(): # # 如果业务成功,可以通过response属性获取需要的值 # print("get response trade_no:" + response.trade_no) # # 响应失败的业务处理 # else: # # 如果业务失败,可以从错误码中可以得知错误情况,具体错误码信息可以查看接口文档 # print(response.code + "," + response.msg + "," + response.sub_code + "," + response.sub_msg) return response_content
def StartCaolouyaqian(): alipay_client_config = AlipayClientConfig() alipay_client_config.server_url = '' alipay_client_config.app_id = '' alipay_client_config.app_private_key = '' alipay_client_config.alipay_public_key = '' client = DefaultAlipayClient(alipay_client_config) # 构造请求参数对象 model = AlipayTradePagePayModel() model.out_trade_no = "20150320010101002" model.product_code = 'FAST_INSTANT_TRADE_PAY' model.total_amount = "88.88" model.subject = "Iphone6 16G" request = AlipayTradePagePayRequest(biz_model=model) # 执行API调用 try: response_content = client.page_execute(request) print(response_content) return response_content except Exception as e: print(traceback.format_exc())
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/TeESyHbF48ssS9CiBS6YUASiDa1bDv5A94GWos4FgFM4a8fKx4fc2QXhSY4rG+zIJAVSctwKnDTt0S8VtycS7GBu9uTAthLz4wZKNQexi1W6dSLkj7SDlYapR2zlmF+3NSB8ID52k7cQuPFR+tJSa0SMt0Q6w1kzjJmPDBW8tNo2lqGq9OguefZz4+wnGacaTjzB+e/P1Ni5oIc8L9JJyXfu3qUTop1QgogIOjCRAtM4RpQIp+GIGs72kljnosPqlUfn8kjBo9Ndj7SfzDoBpcQIDAQABAoIBAQCijsLDT9DuHWr9CpH/TAwNV2UpAOyD0HlpZVnsSIek/rF/59gHWI1DiY/3BMyN6p3wt34+MtZ5/kwaBnFry0jUVS3KI3Jely5ODsyhxcOXB8V6FofOBjR6XMPgrSA2FBnJRAZd5CkEJ1yzg78OUkU5VINZSC1UKXLECa2TTkP9nWDpnbWz37vvyZtZ+JtJvNcxwr7Bjw+HkZ7TxAqCMOqe6Avs7KkSCJXBOPXP+t/GyEgFKnwexVFuhUr4XcgzLrCo5+lQk0cXxTfn4rMC9h90y7onUZxxCL02dvVJ9q2Y5cklCnvkqvRG2k2tqPNQx4Fz4YuHtLuUuoYm9FhKJyoFAoGBAOfUVMGcSKzotZ9NuxTiwiPEKymhbPfSlR9E9pjiaEfylq9TLwN6omKZxSV4IZufXE319dHYDNiOG5UsZ5f9/ZouXxNuLCYqkB2i70wPNlaMVo3PcJ12f24puVXV2FxGu4wrdF7Xi/BFM68qy6xCRa08PFapstD+wy3//zQEPz5/AoGBAON04k5cZlwziNgh5gQFeu5OsyZcqNF2jGHAfuzfgV6GzuyJe5Df24tOSQqCMAucVIaJyHfF3ydnkKq99uGxJlcApNIluAz/7P3lAbgtGqSLBdwaERighfyiWF+dN4HGpiszBgAnJxvDdPbzA0eQxWum6eA/jaif10lEZ2XC1EAPAoGBAKy5KhzGD++jwg3aoBRDjlmPgokV2FYb8zbE7uWhagiQ2WvvEgwJhorn/laYVpHX/bGoG2opCgked7aAyv4XIqeI/RVA9GvLMmyiRJ/qhZWeIa6uaz0mWHL/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+eU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HRMs1ulcpUPosCfzIucJUgX1IQZxJmAyTHmXzSoJVtY/1WbpLsP/zQgp+cUkfFi3CFDGxz6ex3w1U2q9SxemiQvUYbnn1EC3Yv3E0APloRrsAm+XIvPKLMacSbfHcZJw9EcaF3uNfNYtKzHGObigAzF1EEZlRbwG5LeOQ/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): # 获取用户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/RppUa3Qh7bfQaCJU4cMndksibgTSM7S1xkDILWJNtaSjJTpIpHhQtWAoa1FYWPmZQWanbETBBHvxTJiJlxXbBcfFweVl86PCSNPD6uUHcZHNNiR0Dh3siedJVv77GQurp6fs54C0FvVV3ocCU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OIKv+Ivt/Smu1qVWI1S4kJE/WUmntELLSQr7qrCWmcqqCHZ9HyH3185CO+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)
alipay_client_config.app_private_key = app_private_key alipay_public_key = '' with open(os.path.join(settings.BASE_DIR, 'apps/order/alipay_public_key.pem'), 'r') as f: for line in f: alipay_public_key += line alipay_client_config.alipay_public_key = alipay_public_key """ 得到客户端对象。 一个alipay_client_config对象对应一个DefaultAlipayClient,定义DefaultAlipayClient对象后,alipay_client_config不得修改,如果想使用不同的配置,请定义不同的DefaultAlipayClient。 logger参数用于打印日志,不传则不打印,建议传递。 """ client = DefaultAlipayClient(alipay_client_config=alipay_client_config) total_pay = transit_price + total_price model = AlipayTradePagePayModel() model.out_trade_no = order_id model.total_amount = 0.01 model.subject = "天天生鲜{0}".format(order_id) model.product_code = "FAST_INSTANT_TRADE_PAY" request = AlipayTradePagePayRequest(biz_model=model) response = client.page_execute(request, http_method="GET") print(response) # request = AlipayTradeQueryRequest(biz_model=model) # response = client.page_execute(request, http_method="GET") # data = requests.get(response) # print(json.loads(data.text)) # data = json.loads(data.text) # print(data.get('alipay_trade_query_response').get('code'))
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+uM0hlIzSW5pcohL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49GZTTwsSJ0kX6SSBWLkGZOs3RpaLsrgOtkuT1DIXzvoTDe5Q88Hf/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 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': '参数不完整'}) 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': '无效的订单id'}) """ 设置配置,包括支付宝网关地址、app_id、应用私钥、支付宝公钥等,其他配置值可以查看AlipayClientConfig的定义。 """ alipay_client_config = AlipayClientConfig() alipay_client_config.server_url = 'https://openapi.alipaydev.com/gateway.do' alipay_client_config.app_id = '2016100100641374' app_private_key = '' with open(os.path.join(settings.BASE_DIR, 'apps/order/app_private_key.pem'), 'r') as f: for line in f: app_private_key += line alipay_client_config.app_private_key = app_private_key alipay_public_key = '' with open(os.path.join(settings.BASE_DIR, 'apps/order/alipay_public_key.pem'), 'r') as f: for line in f: alipay_public_key += line alipay_client_config.alipay_public_key = alipay_public_key """ 得到客户端对象。 注意,一个alipay_client_config对象对应一个DefaultAlipayClient,定义DefaultAlipayClient对象后,alipay_client_config不得修改,如果想使用不同的配置,请定义不同的DefaultAlipayClient。 logger参数用于打印日志,不传则不打印,建议传递。 """ client = DefaultAlipayClient(alipay_client_config=alipay_client_config, logger=logger) total_pay = order.transit_price + order.total_price total_pay = round(float(total_pay), 2) model = AlipayTradePagePayModel() model.out_trade_no = order_id model.total_amount = total_pay model.subject = "天天生鲜{0}".format(order_id) model.product_code = "FAST_INSTANT_TRADE_PAY" while True: request = AlipayTradeQueryRequest(biz_model=model) response = client.page_execute(request, http_method="GET") data = requests.get(response) data = json.loads(data.text) code = data.get('alipay_trade_query_response').get('code') trade_status = data.get('alipay_trade_query_response').get('trade_status') print(code, trade_status) if code == '10000' and trade_status == 'TRADE_SUCCESS': # 支付成功 # 获取支付宝交易号 trade_no = data.get('alipay_trade_query_response').get('code') # 更新订单状态 order.trade_no = trade_no order.order_status = 4 order.save() # 返回结果 return JsonResponse({'res': 3, 'message': '支付成功'}) elif code=='40004' or (code == '10000' and trade_status == 'WAIT_BUYER_PAY'): # 等待卖家付款 import time time.sleep(5) continue else: # 支付出错 return JsonResponse({'res': 4, 'errmsg': '支付失败'})