微信支付扫码支付(模式二)的支付流程是:生成订单 ,统一下单,统一下单后返回code_url,根据code_url 生成一个支付二维码,然后是支付回调。流程大体是这样的。给个文档地址:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_5
前端界面 index.html,先预下单返回code_url 然后把code_url 去请求生成二维码,这里是用的img标签去接的。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<body>
页面
<button id="send">微信支付,扫码支付,</button>
<img alt="" src="">
<script type="text/javascript">
var urlCode="";
$("#send").click(function(){
$.ajax({
type: "GET",
url: "/wxPay",
data: {},
dataType: "json",
success: function(data){
console.log(data);
console.log(data.urlCode)
urlCode=data.urlCode;
$("img").attr("src",`codewxPay?urlCode=${urlCode}`)
},error:function(jqXHR){
console.log("失败")
console.log(jqXHR)
}
});
})
</script>
</body>
</html>
Controller请求 的接口:PaymentController,生成订单,统一下单,并且返回code_url
/***
* 微信支付接口 预下单返回 code_url
* @param pc
* @return
* @throws Exception
*/
@RequestMapping("/wxPay")
public Object wxPay(WeChatParams weChatParams,HttpServletResponse response){
//初始化数据
weChatParams.setBody("测试商品3");
weChatParams.setTotal_fee("1");
weChatParams.setOut_trade_no("hw54095507921");
weChatParams.setAttach("xiner");
weChatParams.setMemberid("888");
String urlCode= WeixinPay.getCodeUrl(weChatParams);
Map<String, Object> map=new HashMap<String, Object>();
map.put("urlCode", urlCode);
return map;
}
拿到了url_code,把url_code去请求 生成二维码 的接口
/***
* 微信支付接口 根据 code_url 生成二维码
* @param pc
* @return
* @throws IOException
* @throws Exception
*/
@RequestMapping("/codewxPay")
public void codewxPay(String urlCode,HttpServletResponse response) throws IOException{
Map<String, Object> map=new HashMap<String, Object>();
BitMatrix url= WeixinPay.encodeQrcode(urlCode, response);
map.put("url", url);
MatrixToImageWriter.writeToStream(url, "png", response.getOutputStream());
}
逻辑处理,统一下单,和生成图片二维码的代码都写在了这里:WeixinPay
package com.WeChat;
import java.awt.image.BufferedImage;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.servlet.http.HttpServletResponse;
import com.config.WeChatConfig;
import com.entity.WeChatParams;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.util.HttpUtil;
import com.util.PayForUtil;
import com.util.XMLUtil;
import org.apache.commons.lang.StringUtils;
/****
* 扫码支付
* @author admin
*
*/
public class WeixinPay {
private static final int BLACK = 0xff000000;
private static final int WHITE = 0xFFFFFFFF;
public static String getCodeUrl(WeChatParams ps) {
try {
/**
* 账号信息
*/
String appid = WeChatConfig.APPID;//微信服务号的appid
String mch_id = WeChatConfig.MCHID; //微信支付商户号
String key = WeChatConfig.APIKEY; // 微信支付的API密钥
String notify_url = WeChatConfig.WECHAT_NOTIFY_URL_PC;//回调地址【注意,这里必须要使用外网的地址】
String ufdoder_url=WeChatConfig.UFDODER_URL;//微信下单API地址
String trade_type = "NATIVE"; //类型【网页扫码支付】
/**
* 时间字符串
*/
String currTime = PayForUtil.getCurrTime();
String strTime = currTime.substring(8, currTime.length());
String strRandom = PayForUtil.buildRandom(4) + "";
String nonce_str = strTime + strRandom;
SortedMap<Object,Object> packageParams = new TreeMap<Object,Object>();
packageParams.put("appid", appid);
packageParams.put("mch_id", mch_id);
packageParams.put("nonce_str", nonce_str);//随机字符串
packageParams.put("body", ps.body);//支付的商品名称
packageParams.put("out_trade_no", ps.out_trade_no+nonce_str);//商户订单号【备注:每次发起请求都需要随机的字符串,否则失败。】
packageParams.put("total_fee", ps.total_fee);//支付金额
packageParams.put("spbill_create_ip", PayForUtil.localIp());//客户端主机
packageParams.put("notify_url", notify_url);
packageParams.put("trade_type", trade_type);
packageParams.put("attach", ps.attach);//额外的参数【业务类型+会员ID+支付类型】
String sign = PayForUtil.createSign("UTF-8", packageParams,key); //获取签名
packageParams.put("sign", sign);
String requestXML = PayForUtil.getRequestXml(packageParams);//将请求参数转换成String类型
System.err.println("微信支付请求参数的报文"+requestXML);
String resXml = HttpUtil.postData(ufdoder_url,requestXML); //解析请求之后的xml参数并且转换成String类型
Map map = XMLUtil.doXMLParse(resXml);
String urlCode = (String) map.get("code_url");
System.err.println("微信支付响应参数的报文"+resXml);
System.err.println("返回的地址 ===================");
System.err.println(urlCode);
return urlCode;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
/**
* 将路径生成二维码图片
* @author chenp
* @param content
* @param response
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static BitMatrix encodeQrcode(String content,HttpServletResponse response){
if(StringUtils.isBlank(content))
return null;
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
Map hints = new HashMap();
BitMatrix bitMatrix = null;
try {
bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 250, 250,hints);
return bitMatrix;
} catch (WriterException e1) {
e1.printStackTrace();
}
return null;
}
/**
* 类型转换
* @author chenp
* @param matrix
* @return
*/
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) == true ? BLACK : WHITE);
}
}
return image;
}
// 特殊字符处理
public static String UrlEncode(String src) throws UnsupportedEncodingE