说明:调用商户Native支付下单接口,微信后台系统返回链接参数code_url,在页面将code_url值生成二维码图片,用户使用微信客户端扫码后发起支付(无需配置白名单)
1、统一下单
public Result unifiedOrderNATIVE(Map<String,Object> orderMap, HttpServletRequest request) {
Map<String, String> returnMap = new HashMap<>();
String orderNo = null;
try {
//如果订单不为空
if (orderMap != null){
orderNo = orderMap.get("id").toString();
Double amount = Double.parseDouble(orderMap.get("sfprice").toString());
JSONObject payObj = new JSONObject();
payObj.put("mchid",payConfig.getMchId());
payObj.put("out_trade_no",orderNo);
payObj.put("appid",payConfig.getWxPayAppid());
payObj.put("description","订单支付");
payObj.put("notify_url",payConfig.getCallbackUrl());
//订单总金额,单位为分。
JSONObject amountObj = new JSONObject();
amountObj.put("total",(int) (amount * 100));
amountObj.put("currency","CNY");
payObj.put("amount",amountObj);
JSONObject res = wechatPayV3Utils.sendPost("https://api.mch.weixin.qq.com/v3/pay/transactions/native", payObj); //发起请求
if (res == null || StringUtils.isEmpty(res.getString("code_url"))) {
//@TODO 支付发起失败可以将订单数据回滚
return ResultUtil.error("获取预支付交易会话标识失败"+res);
}
returnMap.put("code_url",res.get("code_url").toString());
}
return ResultUtil.success(returnMap);
} catch (Exception e) {
logger.error("订单号:{},错误信息:{}", orderNo, e.getStackTrace());
return ResultUtil.error("微信支付统一下单失败");
}
}
2、支付页面
<script>
var userId = "${userId}";
var baseUrl = function (n) {
return 'https://test.com/pay' + n
}
//支付方法
function pay(id,price){
url = baseUrl('/unifiedOrderNATIVE');
data = {id: id,userId:userId}
$.ajax({
url: url,
type: "post",
dataType: "json",
contentType: "application/json;charset=utf-8",
data: JSON.stringify(data),
success: function (res) {
if (res.code == 0) {
var code_url = res.data.code_url;
var qrcodeUrl = qrcode(4, 'L');
qrcodeUrl.addData(code_url);
qrcodeUrl.make()
document.getElementById('qrcodes').innerHTML =
qrcodeUrl.createImgTag();
}else{
alert(res.msg);
}
}
})
}
</script>