H5调起支付宝支付

1.H5调起支付宝支付,我这里是的方法是H5通过访问后端接口,由后台生成一个隐藏的form表单,将form表单作为一个字符串返回给H5,H5将form表单渲染到页面上,通过提交form表单调起支付宝支付。

form表单大致是这样的,这里我从别处找到

<form name="punchout_form" method="post" action="https://openapi.alipay.com/gateway.do?charset=UTF-8&method=alipay.trade.wap.pay&sign=DEpMkI**********************eWUs6EW3QKlt9OHYv%2FqkporO8Sr5%2Bay5VA9dpx3pAbIiPPajQ2gEcWHvz5bm*******kxH8ZvHUXahQL9S69p9wKNXpXOxYadlsxE8QKGUc4cO5rrgGq08%2BpiOq%2FOz4fLogEBWANXILUMWXNzJn8YryNifZ416Pd%2BxkKgXs%2Fo%2FQDcqEUg*********VXXPRq7IGRGQg%2FpZkOhxCH%2Fq%2B9hnWEncAfQLlAXfPqjdcQTNJ0TJdVr1X1ENOdAr5LQkydWw2EQ8g%3D%3D&return_url=+https%3A%2F%2**********&notify_url=+https%3A%2F%*********Fpub%2Fapi%2Fv1%2F********allback1&version=1.0&app_id=20********57&sign_type=R***&timestamp=2019-0******55&alipay_sdk=al*******.49.ALL&format=json">
    <input type="hidden" name="biz_content" value="{&quot;body&quot;:&quot;&quot;,&quot;enable_pay_channels&quot;:&quot;balance,moneyFund,bankPay,debitCardExpress,creditCardExpress,creditCard,pcredit,pcreditpayInstallment,coupon,point,voucher,mdiscount,honeyPay&quot;,&quot;out_trade_no&quot;:&quot;132ecf937ad84487aa6cbaeb2ec157fe&quot;,&quot;product_code&quot;:&quot;13&quot;,&quot;subject&quot;:&quot;SpringBoot 2.x微信支付在线教育网站项目实战&quot;,&quot;timeout_express&quot;:&quot;20m&quot;,&quot;total_amount&quot;:&quot;98&quot;}">
    <input type="submit" value="立即支付" style="display:none" >
</form>
<script>document.forms[0].submit();</script>

2.如何生成form表单并返回给H5,这里只是使用了支付宝sdk里面的方法 生成form表单,不用请求支付宝后台,与微信统一下单接口有所区别。需要注意返回值类型Map<String,StringBuffer>,使用StringBuffer返回给H5可以正常收到,如果使用String类型,map被包装类包装后序列化到H5可能接收不到。

	/**
	 * 支付宝下单参数	        json.put("out_trade_no", ""); //商户订单号
					json.put("total_amount", "0.01");  //总金额,元为单位
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public static  Map<String,StringBuffer> aliPay(Map<String,String> map) throws Exception 
                //这里采用RSA2 加密方式
		AlipayClient client = new DefaultAlipayClient(AliPayProperties.URL, aliPayProperties.getAppid(), Configs.getPrivateKey(), AliPayProperties.FORMAT, AliPayProperties.CHARSET, Configs.getAlipayPublicKey(),AliPayProperties.SIGNTYPE);
		AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
		
		JSONObject json = new JSONObject();
		json.putAll(map); //包含商户订单号,金额
		json.put("product_code", "QUICK_WAP_WAY"); 
		json.put("subject", aliPayProperties.getSubject());  //商品的标题/交易标题/订单标题/订单关键字等。
		
		request.setBizContent(json.toString());
		request.setNotifyUrl(aliPayProperties.getNotifyUrl()); //设置回调地址
		request.setReturnUrl(aliPayProperties.getReturnUrl()); //设置返回地址
		Map<String,StringBuffer> resmap = null;
		AlipayTradeWapPayResponse response;
		try {
			response = client.pageExecute(request);
			StringBuffer sb = new StringBuffer();
			String data =response.getBody() ; //获取form表单字符串
			log.info("支付宝下单参数拼接:"+data);
			sb.append(data);
			resmap = new HashMap<String,StringBuffer>();
			resmap.put("from", sb);
		} catch (AlipayApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return resmap;
	}

3.在上述代码中会设置支付宝支付成功回调地址,与支付成功后的返回地址。后面我会说一下支付宝回调。

支付宝回调:https://blog.csdn.net/qq_38669394/article/details/106673016

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值