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 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 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 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): # 获得支付宝客户端 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 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 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 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})
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& biz_content=%7B%22body%22%3A%22%E6%94%AF%E4%BB%98%E5%AE%9D%E6%B5%8B%E8%AF%95%22%2C%22out_trade_no%22%3A%2220150320010101001%22%2C%22product_code%22%3A%22FAST_INSTANT_TRADE_PAY%22%2C%22settle_info%22%3A%7B%22settle_detail_infos%22%3A%5B%7B%22amount%22%3A50%7D%5D%7D%2C%22sub_merchant%22%3A%7B%22merchant_id%22%3A%222088102176051162%22%7D%2C%22subject%22%3A%22%E6%B5%8B%E8%AF%95%22%2C%22total_amount%22%3A50%7D