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**********¬ify_url=+https%3A%2F%*********Fpub%2Fapi%2Fv1%2F********allback1&version=1.0&app_id=20********57&sign_type=R***×tamp=2019-0******55&alipay_sdk=al*******.49.ALL&format=json">
<input type="hidden" name="biz_content" value="{"body":"","enable_pay_channels":"balance,moneyFund,bankPay,debitCardExpress,creditCardExpress,creditCard,pcredit,pcreditpayInstallment,coupon,point,voucher,mdiscount,honeyPay","out_trade_no":"132ecf937ad84487aa6cbaeb2ec157fe","product_code":"13","subject":"SpringBoot 2.x微信支付在線教育網站項目實戰","timeout_express":"20m","total_amount":"98"}">
<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