文档&博客
接入步骤
- 申请H5支付
- 调用统一下单接口
- 通过
mweb_url
唤起微信客户端支付 - 服务端异步通知处理
常见异常
常见异常以及解决方案请参考博客-微信H5支付
代码示例
https://gitee.com/javen205/IJPay-Demo https://github.com/javen205/IJPay-Demo
/**
* 微信H5 支付
* 注意:必须再web页面中发起支付且域名已添加到开发配置中
*/
@RequestMapping(value ="/wapPay",method = {RequestMethod.POST,RequestMethod.GET})
public void wapPay(HttpServletRequest request,HttpServletResponse response){
String ip = IpKit.getRealIp(request);
if (StrKit.isBlank(ip)) {
ip = "127.0.0.1";
}
H5ScencInfo sceneInfo = new H5ScencInfo();
H5 h5_info = new H5();
h5_info.setType("Wap");
//此域名必须在商户平台--"产品中心"--"开发配置"中添加
h5_info.setWap_url("https://pay.qq.com");
h5_info.setWap_name("腾讯充值");
sceneInfo.setH5_info(h5_info);
Map<String, String> params = WxPayApiConfigKit.getWxPayApiConfig()
.setAttach("IJPay H5支付测试 -By Javen")
.setBody("IJPay H5支付测试 -By Javen")
.setSpbillCreateIp(ip)
.setTotalFee("520")
.setTradeType(TradeType.MWEB)
.setNotifyUrl(notify_url)
.setOutTradeNo(String.valueOf(System.currentTimeMillis()))
.setSceneInfo(h5_info.toString())
.build();
String xmlResult = WxPayApi.pushOrder(false,params);
log.info(xmlResult);
Map<String, String> result = PaymentKit.xmlToMap(xmlResult);
String return_code = result.get("return_code");
String return_msg = result.get("return_msg");
if (!PaymentKit.codeIsOK(return_code)) {
log.error("return_code>"+return_code+" return_msg>"+return_msg);
throw new RuntimeException(return_msg);
}
String result_code = result.get("result_code");
if (!PaymentKit.codeIsOK(result_code)) {
log.error("result_code>"+result_code+" return_msg>"+return_msg);
throw new RuntimeException(return_msg);
}
// 以下字段在return_code 和result_code都为SUCCESS的时候有返回
String prepay_id = result.get("prepay_id");
String mweb_url = result.get("mweb_url");
log.info("prepay_id:"+prepay_id+" mweb_url:"+mweb_url);
try {
response.sendRedirect(mweb_url);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 微信H5 支付
* 注意:必须再web页面中发起支付且域名已添加到开发配置中
*/
public void wapPay(){
String ip = IpKit.getRealIp(getRequest());
if (StrKit.isBlank(ip)) {
ip = "127.0.0.1";
}
H5ScencInfo sceneInfo = new H5ScencInfo();
H5 h5_info = new H5();
h5_info.setType("Wap");
//此域名必须在商户平台--"产品中心"--"开发配置"中添加
h5_info.setWap_url("https://pay.qq.com");
h5_info.setWap_name("腾讯充值");
sceneInfo.setH5_info(h5_info);
Map<String, String> params = WxPayApiConfigKit.getWxPayApiConfig()
.setAttach("IJPay H5支付测试 -By Javen")
.setBody("IJPay H5支付测试 -By Javen")
.setSpbillCreateIp(ip)
.setTotalFee("520")
.setTradeType(TradeType.MWEB)
.setNotifyUrl(notify_url)
.setOutTradeNo(String.valueOf(System.currentTimeMillis()))
.setSceneInfo(h5_info.toString())
.build();
String xmlResult = WxPayApi.pushOrder(false,params);
log.info(xmlResult);
Map<String, String> result = PaymentKit.xmlToMap(xmlResult);
String return_code = result.get("return_code");
String return_msg = result.get("return_msg");
if (!PaymentKit.codeIsOK(return_code)) {
ajax.addError(return_msg);
renderJson(ajax);
return;
}
String result_code = result.get("result_code");
if (!PaymentKit.codeIsOK(result_code)) {
ajax.addError(return_msg);
renderJson(ajax);
return;
}
// 以下字段在return_code 和result_code都为SUCCESS的时候有返回
String prepay_id = result.get("prepay_id");
String mweb_url = result.get("mweb_url");
System.out.println("prepay_id:"+prepay_id+" mweb_url:"+mweb_url);
redirect(mweb_url);
}