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

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐