微信支付之扫码支付相关代码(Java)
Posted bjlf_1989
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微信支付之扫码支付相关代码(Java)相关的知识,希望对你有一定的参考价值。
最近开发网站过程,需要引入支付过程,第三方支付中最火的莫过于支付宝支付和微信支付,下边借助微信支付官网上的文档,写一下接入微信支付之扫码支付的流程
相对支付宝支付而言,微信支付的开发文档写的相当的low,demo写的一点都不简洁,下边写一下微信扫码支付的过程,这一过程中,需要注意的所涉及的实际业务是怎样的,根据实际情况结合业务进行引入,在进入正式开发之前,要申请微信支付的相关内容按照官网的操作进行即可,审核成功后,会得到appId,商户号,商户平台登录账号和密码
登录微信支付官网
(1)商户后台系统根据用户选购的商品生成订单。
(2)用户确认支付后调用微信支付【统一下单API】生成预支付交易;
(3)微信支付系统收到请求后生成预支付交易单,并返回交易会话的二维码链接code_url。
(4)商户后台系统根据返回的code_url生成二维码。
(5)用户打开微信“扫一扫”扫描二维码,微信客户端将扫码内容发送到微信支付系统。
(6)微信支付系统收到客户端请求,验证链接有效性后发起用户支付,要求用户授权。
(7)用户在微信客户端输入密码,确认支付后,微信客户端提交授权。
(8)微信支付系统根据用户授权完成支付交易。
(9)微信支付系统完成支付交易后给微信客户端返回交易结果,并将交易结果通过短信、微信消息提示用户。微信客户端展示支付交易结果页面。
(10)微信支付系统通过发送异步消息通知商户后台系统支付结果。商户后台系统需回复接收情况,通知微信后台系统不再发送该单的支付通知。
(11)未收到支付通知的情况,商户后台系统调用【查询订单API】。
(12)商户确认订单已支付后给用户发货。
开发的流程,首先根据自己的业务流程,生成订单保存到数据库当中,这一过程包含了商品的信息,需要支付的钱数,商品名称等,下边开始进入统一下单的方法,这部分是微信统一下单入口(controller方法)
/** * 微信扫码支付统一下单 */ @RequestMapping(value = "/WxPayUnifiedorder", method = RequestMethod.GET) @ResponseBody public Object WxPayUnifiedorder(String out_trade_no) throws Exception{ HashMap<String,Object> map = new HashMap<String,Object>(); String codeUrl = wxPayService.weixin_pay(out_trade_no); map.put("codeUrl",codeUrl); return map; }下边是微信支付统一下单具体实现方法
/** * 微信支付统一下单接口 * @param out_trade_no * @return * @throws Exception */ public String weixin_pay(String out_trade_no) throws Exception { // 账号信息 String appid = PayConfigUtil.getAppid(); // appid //String appsecret = PayConfigUtil.APP_SECRET; // appsecret // 商业号 String mch_id = PayConfigUtil.getMchid(); // key String key = PayConfigUtil.getKey(); String currTime = PayCommonUtil.getCurrTime(); String strTime = currTime.substring(8, currTime.length()); String strRandom = PayCommonUtil.buildRandom(4) + ""; //随机字符串 String nonce_str = strTime + strRandom; // 价格 注意:价格的单位是分 //String order_price = "1"; // 商品名称 //String body = "企嘉科技商品"; //查询订单数据表获取订单信息 PayOrder payOrder = payOrderDao.get(PayOrder.class,out_trade_no); // 获取发起电脑 ip String spbill_create_ip = PayConfigUtil.getIP(); // 回调接口 String notify_url = PayConfigUtil.NOTIFY_URL; String trade_type = "NATIVE"; String time_start = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); Calendar ca = Calendar.getInstance(); ca.setTime(new Date()); ca.add(Calendar.DATE, 1); String time_expire = new SimpleDateFormat("yyyyMMddHHmmss").format(ca.getTime()); 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",payOrder.getBody()); packageParams.put("out_trade_no", out_trade_no); //packageParams.put("total_fee", "1"); packageParams.put("total_fee", payOrder.getTotalFee()); packageParams.put("spbill_create_ip", spbill_create_ip); packageParams.put("notify_url", notify_url); packageParams.put("trade_type", trade_type); packageParams.put("time_start", time_start); packageParams.put("time_expire", time_expire); String sign = PayCommonUtil.createSign("UTF-8", packageParams,key); packageParams.put("sign", sign); String requestXML = PayCommonUtil.getRequestXml(packageParams); System.out.println("请求xml::::"+requestXML); String resXml = HttpUtil.postData(PayConfigUtil.PAY_API, requestXML); System.out.println("返回xml::::"+resXml); Map map = XMLUtil.doXMLParse(resXml); //String return_code = (String) map.get("return_code"); //String prepay_id = (String) map.get("prepay_id"); String urlCode = (String) map.get("code_url"); System.out.println("打印调用统一下单接口生成二维码url:::::"+urlCode); return urlCode; }这一方法中,我们从工具类中获取微信官网提供的appId以及商户号,以及在商户平台上设置的支付密钥,处理一些统一下单接口需要携带的参数,回调链接(微信发起的本地调用,并返回成功或错误信息),totalFee的单位为分,官网的接口文档有说明,trade_type写死指定为NATIVE,另外的两个量time_start与time_expire是指定订单的有效期,
可以根据的自己的业务需求具体指定时间,(此处是24小时的订单有效期),如果无需指定,直接注释掉即可,sign是签名操作,借助工具类将packageParams转化为XML借助postData方法请求微信统一下单接口
public static String PAY_API = "https://api.mch.weixin.qq.com/pay/unifiedorder";resXml作为调用统一下单的返回值,当然返回的也是xml文件,再借助xmlUtil将返回值转化为map,返回值中有生成二维码的code_url,接下来我们需要处理的是,解析二维码url在前端网页中生成二维码图片,在前端使用ajax调用统一下单接口方法,接收返回值code_url,接收返回值后在调用后台接口方法解析code_url
common.get("/*****/wxpay/WxPayUnifiedorder?out_trade_no="+orderId,function(data){ var codeUrl = data.codeUrl; if(codeUrl!=null && codeUrl!=""){ $("#id_wxtwoCode").attr('src',"******/wxpay/qr_codeImg?code_url="+codeUrl); } });
html中img标签
<div class="weimg"> <!-- <img src="../common/img/wei_06.png"> --> <img id="id_wxtwoCode"src=""/> <p>打开手机端微信<br>扫一扫继续支付</p> </div>
下边是解析二维码过程
/** * 生成二维码图片并直接以流的形式输出到页面 * @param code_url * @param response */ @RequestMapping("qr_codeImg") @ResponseBody public void getQRCode(String code_url,HttpServletResponse response){ wxPayService.encodeQrcode(code_url, response); }
/** * 生成二维码图片 不存储 直接以流的形式输出到页面 * @param content * @param response */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static void encodeQrcode(String content,HttpServletResponse response){ if(content==null || "".equals(content)) return; MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); Map hints = new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); //设置字符集编码类型 BitMatrix bitMatrix = null; try { bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 300, 300,hints); BufferedImage image = toBufferedImage(bitMatrix); //输出二维码图片流 try { ImageIO.write(image, "png", response.getOutputStream()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (WriterException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }这里的300,300确定了二维码图片的大小
二维码图片生成后,接下来的流程就是扫码进行支付操作,这里还存在一个问题,需要在扫码后支付成功,需要实现页面的跳转,这一过程需要添加js的定时器,定时查看订单的支付状态是否发生了改变是否为支付成功,成功即可跳转后续流程,失败跳转失败提示
//定时器,每隔1s查询订单支付状态,订单状态改变,清除页面定时器,页面跳转 function checkOrder(orderId) { console.log("定时器方法"+orderId); common.get("/*******/order/checkOrder?out_trade_no="+orderId,function(data){ var isorder = data.isorder; var paystatus = data.paystatus; if(isorder=="1"){ if(paystatus=="1"){ //支付成功跳转 window.location.href="******/order/wxpaySuccess?out_trade_no="+orderId; clearInterval(time); }else if(paystatus=="2"){ //支付失败 alert("支付失败!"); clearInterval(time); } } }); }
isorder查看是否存在订单,存在订单时判断支付状态,进行后续流程
支付成功后,微信服务端开始回调方法,
@RequestMapping(value = "/weixinNotify", method = RequestMethod.POST) @ResponseBody public void weixinNotify(HttpServletRequest request,HttpServletResponse response) throws Exception{ System.out.println("支付回调方法开始!"); HashMap<String,Object> map = new HashMap<String,Object>(); wxPayService.weixin_notify(request, response); System.out.println("支付回调方法结束!"); }
/** * 微信支付回调方法 * @param request * @param response * @throws Exception */ public void weixin_notify(HttpServletRequest request,HttpServletResponse response) throws Exception{ //读取参数 InputStream inputStream ; StringBuffer sb = new StringBuffer(); inputStream = request.getInputStream(); String s ; BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); while ((s = in.readLine()) != null){ sb.append(s); } in.close(); inputStream.close(); //解析xml成map Map<String, String> m = new HashMap<String, String>(); m = XMLUtil.doXMLParse(sb.toString()); //过滤空 设置 TreeMap SortedMap<Object,Object> packageParams = new TreeMap<Object,Object>(); Iterator it = m.keySet().iterator(); while (it.hasNext()) { String parameter = (String) it.next(); String parameterValue = m.get(parameter); String v = ""; if(null != parameterValue) { v = parameterValue.trim(); } packageParams.put(parameter, v); } // 账号信息 String key = PayConfigUtil.getKey(); // key String out_trade_no = (String)packageParams.get("out_trade_no"); //logger.info(packageParams); //判断签名是否正确 if(PayCommonUtil.isTenpaySign("UTF-8", packageParams,key)) { //------------------------------ //处理业务开始 //------------------------------ String resXml = ""; if("SUCCESS".equals((String)packageParams.get("result_code"))){ // 这里是支付成功 //////////执行自己的业务逻辑//////////////// String mch_id = (String)packageParams.get("mch_id"); String openid = (String)packageParams.get("openid"); String is_subscribe = (String)packageParams.get("is_subscribe"); String bank_type = (String)packageParams.get("bank_type"); String total_fee = (String)packageParams.get("total_fee"); String transaction_id = (String)packageParams.get("transaction_id"); System.out.println("mch_id:"+mch_id); System.out.println("openid:"+openid); System.out.println("is_subscribe:"+is_subscribe); System.out.println("out_trade_no:"+out_trade_no); System.out.println("total_fee:"+total_fee); System.out.println("bank_type:"+bank_type); System.out.println("transaction_id:"+transaction_id); //成功回调后需要处理预生成订单的状态和一些支付信息
//查询数据库中订单,首先判定订单中金额与返回的金额是否相等,不等金额被纂改
//判定订单是否已经被支付,不可重复支付
//正常处理相关业务逻辑
} else { System.out.println("支付失败,错误信息:" + packageParams.get("err_code")+ "-----订单号:::"+out_trade_no+"*******支付失败时间::::" +new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); String err_code = (String)packageParams.get("err_code"); resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> "; } //------------------------------ //处理业务完毕 //------------------------------ BufferedOutputStream out = new BufferedOutputStream( response.getOutputStream()); out.write(resXml.getBytes()); out.flush(); out.close(); } else{ System.out.println("通知签名验证失败---时间::::"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); } }
至此,微信支付流程完毕
下边是相关工具类
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.URL; import java.net.URLConnection; import com.entplus.log.entity.Log; public class HttpUtil { //private static final Log logger = Logs.get(); private final static int CONNECT_TIMEOUT = 5000; // in milliseconds private final static String DEFAULT_ENCODING = "UTF-8"; public static String postData(String urlStr, String data){ return postData(urlStr, data, null); } public static String postData(String urlStr, String data, String contentType){ BufferedReader reader = null; try { URL url = new URL(urlStr); URLConnection conn = url.openConnection(); conn.setDoOutput(true); conn.setConnectTimeout(CONNECT_TIMEOUT); conn.setReadTimeout(CONNECT_TIMEOUT); if(contentType != null) conn.setRequestProperty("content-type", contentType); OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), DEFAULT_ENCODING); if(data == null) data = ""; writer.write(data); writer.flush(); writer.close(); reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), DEFAULT_ENCODING)); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line); sb.append("\r\n"); } return sb.toString(); } catch (IOException e) { //logger.error("Error connecting to " + urlStr + ": " + e.getMessage()); System.out.println("Error connecting to " + urlStr + ": " + e.getMessage()); } finally { try { if (reader != null) reader.close(); } catch (IOException e) { } } return null; } }
import java.text.SimpleDateFormat; import java.util.Date; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.SortedMap; public class PayCommonUtil { /** * 是否签名正确,规则是:按参数名称a-z排序,遇到空值的参数不参加签名。 * @return boolean */ public static boolean isTenpaySign(String characterEncoding, SortedMap<Object, Object> packageParams, String API_KEY) { StringBuffer sb = new StringBuffer(); Set es = packageParams.entrySet(); Iterator it = es.iterator(); while(it.hasNext()) { Map.Entry entry = (Map.Entry)it.next(); String k = (String)entry.getKey(); String v = (String)entry.getValue(); if(!"sign".equals(k) && null != v && !"".equals(v)) { sb.append(k + "=" + v + "&"); } } sb.append("key=" + API_KEY); //算出摘要 String mysign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toLowerCase(); String tenpaySign = ((String)packageParams.get("sign")).toLowerCase(); //System.out.println(tenpaySign + " " + mysign); return tenpaySign.equals(mysign); } /** * @author * @date 2016-4-22 * @Description:sign签名 * @param characterEncoding * 编码格式 * @param parameters * 请求参数 * @return */ public static String createSign(String characterEncoding, SortedMap<Object, Object> packageParams, String API_KEY) { StringBuffer sb = new StringBuffer(); Set es = packageParams.entrySet(); Iterator it = es.iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); String k = (String) entry.getKey(); String v = (String) entry.getValue(); if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) { sb.append(k + "=" + v + "&"); } } sb.append("key=" + API_KEY); String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase(); return sign; } /** * @author * @date 2016-4-22 * @Description:将请求参数转换为xml格式的string * @param parameters * 请求参数 * @return */ public static String getRequestXml(SortedMap<Object, Object> parameters) { StringBuffer sb = new StringBuffer(); sb.append("<xml>"); Set es = parameters.entrySet(); Iterator it = es.iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); String k = (String) entry.getKey(); String v = (String) entry.getValue(); if ("attach".equalsIgnoreCase(k) || "body".equalsIgnoreCase(k) || "sign".equalsIgnoreCase(k)) { sb.append("<" + k + ">" + "<![CDATA[" + v + "]]></" + k + ">"); } else { sb.append("<" + k + ">" + v + "</" + k + ">"); } } sb.append("</xml>"); return sb.toString(); } /** * 取出一个指定长度大小的随机正整数. * * @param length * int 设定所取出随机数的长度。length小于11 * @return int 返回生成的随机数。 */ public static int buildRandom(int length) { int num = 1; double random = Math.random(); if (random < 0.1) { random = random + 0.1; } for (int i = 0; i < length; i++) { num = num * 10; } return (int) ((random * num)); } /** * 获取当前时间 yyyyMMddHHmmss * * @return String */ public static String getCurrTime() { Date now = new Date(); SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMddHHmmss"); String s = outFormat.format(now); return s; } }
package com.entplus.wxpay.util; public class PayConfigUtil { //这个就是自己要保管好的私有Key了(切记只能放在自己的后台代码里,不能放在任何可能被看到源代码的客户端程序中) // 每次自己Post数据给API的时候都要用这个key来对所有字段进行签名,生成的签名会放在Sign这个字段,API收到Post数据的时候也会用同样的签名算法对Post过来的数据进行签名和验证 // 收到API的返回的时候也要用这个key来对返回的数据算下签名,跟API的Sign数据进行比较,如果值不一致,有可能数据被第三方给篡改 private static String key = ""; //微信分配的公众号ID(开通公众号之后可以获取到) private static String appID = ""; //微信支付分配的商户号ID(开通公众号的微信支付功能之后可以获取到) private static String mchID = ""; //机器IP private static String ip = ""; //以下是几个API的路径: //1)被扫支付API //public static String PAY_API = "https://api.mch.weixin.qq.com/pay/micropay"; public static String PAY_API = "https://api.mch.weixin.qq.com/pay/unifiedorder"; //2)被扫支付查询API public static String PAY_QUERY_API = "https://api.mch.weixin.qq.com/pay/orderquery"; //3)退款API public static String REFUND_API = "https://api.mch.weixin.qq.com/secapi/pay/refund"; //4)退款查询API public static String REFUND_QUERY_API = "https://api.mch.weixin.qq.com/pay/refundquery"; //5)撤销API public static String REVERSE_API = "https://api.mch.weixin.qq.com/secapi/pay/reverse"; //6)下载对账单API public static String DOWNLOAD_BILL_API = "https://api.mch.weixin.qq.com/pay/downloadbill"; //7) 统计上报API public static String REPORT_API = "https://api.mch.weixin.qq.com/payitil/report"; //回调地址 //public static String NOTIFY_URL = ""; //测试 public static String HttpsRequestClassName = "com.entplus.wxpay.util.HttpsRequest"; public static void setKey(String key) { PayConfigUtil.key = key; } public static void setAppID(String appID) { PayConfigUtil.appID = appID; } public static void setMchID(String mchID) { PayConfigUtil.mchID = mchID; } public static void setIp(String ip) { PayConfigUtil.ip = ip; } public static String getKey(){ return key; } public static String getAppid(){ return appID; } public static String getMchid(){ return mchID; } public static String getIP(){ return ip; } public static void setHttpsRequestClassName(String name){ HttpsRequestClassName = name; } }
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; public class XMLUtil { /** * 解析xml,返回第一级元素键值对。如果第一级元素有子节点,则此节点的值是子节点的xml数据。 * @param strxml * @return * @throws JDOMException * @throws IOException */ public static Map doXMLParse(String strxml) throws JDOMException, IOException { strxml = strxml.replaceFirst("encoding=\".*\"", "encoding=\"UTF-8\""); if(null == strxml || "".equals(strxml)) { return null; } Map m = new HashMap(); InputStream in = new ByteArrayInputStream(strxml.getBytes("UTF-8")); SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(in); Element root = doc.getRootElement(); List list = root.getChildren(); Iterator it = list.iterator(); while(it.hasNext()) { Element e = (Element) it.next(); String k = e.getName(); String v = ""; List children = e.getChildren(); if(children.isEmpty()) { v = e.getTextNormalize(); } else { v = XMLUtil.getChildrenText(children); } m.put(k, v); } //关闭流 in.close(); return m; } /** * 获取子结点的xml * @param children * @return String */ public static String getChildrenText(List children) { StringBuffer sb = new StringBuffer(); if(!children.isEmpty()) { Iterator it = children.iterator(); while(it.hasNext()) { Element e = (Element) it.next(); String name = e.getName(); String value = e.getTextNormalize(); List list = e.getChildren(); sb.append("<" + name + ">"); if(!list.isEmpty()) { sb.append(XMLUtil.getChildrenText(list)); } sb.append(value); sb.append("</" + name + ">"); } } return sb.toString(); } }
import java.security.MessageDigest; public class MD5Util { private static String byteArrayToHexString(byte b[]) { StringBuffer resultSb = new StringBuffer(); for (int i = 0; i < b.length; i++) resultSb.append(byteToHexString(b[i])); return resultSb.toString(); } private static String byteToHexString(byte b) { int n = b; if (n < 0) n += 256; int d1 = n / 16; int d2 = n % 16; return hexDigits[d1] + hexDigits[d2]; } public static String MD5Encode(String origin, String charsetname) { String resultString = null; try { resultString = new String(origin); MessageDigest md = MessageDigest.getInstance("MD5"); if (charsetname == null || "".equals(charsetname)) resultString = byteArrayToHexString(md.digest(resultString .getBytes())); else resultString = byteArrayToHexString(md.digest(resultString .getBytes(charsetname))); } catch (Exception exception) { } return resultString; } private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; }
开发过程中借助了网络资料,欢迎大家指正。
以上是关于微信支付之扫码支付相关代码(Java)的主要内容,如果未能解决你的问题,请参考以下文章
微信支付之扫码支付公众号支付H5支付小程序支付相关业务流程分析总结