前期由于项目需要,需要用到微信支付的相关内容,于是自己在网上查阅了很多资料,花了一周左右时间,将微信用到的二维码支付、H5支付、公众号内支付和APP支付统一进行了整理,希望跟大家多交流交流。
前期准备工作:
开发之前,需要提前注册好微信公众平台账号和商户平台账号,并完成绑定和签约,因为配置需要用到相关信息。此处不再说明。
下面开始正式的开发工作。
首先创建WeixinPayConfig类,用于存放微信支付的相关配置信息。
相关代码:
public class WeixinPayConfig {
public static final String appid = "wx…..";//appid
public static final String mch_id = "…..";//商户号
public static final String device_info = "WEB";//设备号
public static final String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//统一下单地址
public static final String order_url = "https://api.mch.weixin.qq.com/pay/orderquery";//查询订单
public static final String tx_url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";//提现到个人账户
public static final String notify_url = "http://可访问地址/WechatPay/notify_url.do";//异步跳转地址
public static final String key = "32位密钥";//密钥
public static final String scene_info = "{\"h5_info\": {\"type\":\"Wap\",\"wap_url\": \"https://pay.qq.com\",\"wap_name\": \"腾讯充值\"}}";
}
接着创建生成二维码接口,前台调用该接口在页面显示付款二维码。
// 生成付款二维码
@SuppressWarnings({ "unchecked", "rawtypes" })
@RequestMapping("loadPayImage.do")
@ResponseBody
public void loadPayImage(HttpServletRequest request, HttpServletResponse response, Map map1) throws Exception {
//
String mch_id = WeixinPayConfig.mch_id;//商户id
String orderNo = DateUtil.getCurrentDateStr();// 生成订单号
String nonce_str = StringUtil.getRandomString(30);
String ip = WxpayUtil.getRemortIP(request);
int total_fee = 1;
Map<String, Object> map = new HashMap<String, Object>();
map.put("appid", WeixinPayConfig.appid);// appid
map.put("mch_id", mch_id);// 商户号
map.put("device_info", WeixinPayConfig.device_info);// 设备名
map.put("nonce_str", nonce_str);// 随机字符串
map.put("notify_url", WeixinPayConfig.notify_url);// 异步跳转地址
map.put("trade_type", "NATIVE");// 支付类型
map.put("out_trade_no", orderNo);// 订单号
map.put("body", "测试商品");// 商品描述
map.put("total_fee", total_fee);// 总金额(分)
map.put("spbill_create_ip", ip);// 用户ip地址
// map.put("spbill_create_ip", "127.0.0.1");// 用户ip地址
map.put("sign", WxpayUtil.getSign(map));// 签名
String xml = XmlUtil.genXml(map);// 生成xml
InputStream content = HttpClientUtil.sendXMLDataByPost(WeixinPayConfig.url, xml).getEntity().getContent();// 发送xml消息
String code_url = WxpayUtil.getElementValue(content, "code_url");
if (null != code_url) {
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
Map hints = new HashMap();
BitMatrix bitMatrix = null;
try {
bitMatrix = multiFormatWriter.encode(code_url, BarcodeFormat.QR_CODE, 100, 100, hints);
BufferedImage image = WxpayUtil.toBufferedImage(bitMatrix);
// 输出二维码图片流
boolean write = ImageIO.write(image, "png", response.getOutputStream());
if (write) {
//逻辑处理,可以保存一条未支付的订单到数据库,支付成功后更改支付状态
}
} catch (WriterException e1) {
e1.printStackTrace();
}
}
}
用到的工具类:
根据时间生成订单号---DateUtil.java
public class DateUtil {
/**
* 根据时间生成时间戳
*/
public static String getCurrentDateStr(){
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddhhmmssSSS");
String time = simpleDateFormat.format(date);
return time;
}
public static void main(String[] args) {
String currentDateStr = getCurrentDateStr();
System.out.println(currentDateStr);
}
}
随机生成指定长度的字符串---StringUtil.java
public class StringUtil {
/**
* 判断是否是空
* @param str
* &#