微信native支付也叫扫码原生支付,场景就是用户用微信的扫一扫,扫描一个商品的二维码,扫码成功后会出现支付界面进行付款。然而扫码原生支付又分为两种:静态native支付和动态native支付。这两种又有什么区别呢?通俗来讲,静态native支付就是我们可以将一个商品的信息做成一个二维码,比如一瓶可口可乐,该商品的信息变成二维码后多个用户都可以扫描该二维码进行付款购买商品。那么动态native支付又是什么呢?其实动态native支付也是将一个商品信息变成二维码,与静态native支付的区别就是,该二维码信息中有一个订单号的信息在里面,一个用户付款成功后,该二维码就不能被其他用户扫描支付。知道这两种扫码原生支付区别,我们可以根据我们自己的需求进行选择开发。下面我们来进行静态native支付。
开发之前,我们需要到登陆自己的服务号,进行设置原生支付URL。
步骤请参考官方文档:http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=6_3
该原生支付URL的作用就是接收用户扫码二维码后微信支付系统返回的信息,根据返回的信息进行支付操作。
具体详细流程和参数意义请参考官方文档:http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=6_4
java代码如下 一个普通的spirngMVC的controller:
package test.concroller; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.Date; import java.util.Map; import java.util.SortedMap; import java.util.TreeMap; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * V型知识库 * www.vxzsk.com * * 静态native支付 */ public class StaticNativeController { /** * @author V型知识库 www.vxzsk.com * @Desription:native静态支付生成二维码 * @param args * @throws Exception */ public static void main(String[] args) throws Exception { SortedMap<Object,Object> parameters = new TreeMap<Object,Object>(); parameters.put("appid", ConfigUtil.APPID); parameters.put("mch_id", ConfigUtil.MCH_ID); parameters.put("time_stamp", Long.toString(new Date().getTime())); parameters.put("nonce_str", PayCommonUtil.CreateNoncestr()); parameters.put("product_id", "No.201401051607001");//商品号要唯一 String sign = PayCommonUtil.createSign("UTF-8", parameters); parameters.put("sign", sign); String url = "weixin://wxpay/bizpayurl?sign=SIGN&appid=APPID&mch_id=MCHID&product_id=PRODUCTID&time_stamp=TIMESTAMP&nonce_str=NOCESTR"; String result = url.replace("SIGN", sign). replace("APPID", ConfigUtil.APPID). replace("MCHID", ConfigUtil.MCH_ID). replace("PRODUCTID", (String)parameters.get("product_id")). replace("TIMESTAMP", (String)parameters.get("time_stamp")). replace("NOCESTR", (String)parameters.get("nonce_str")); System.out.println("result="+result); //TODO 将result变成二维码 } /** * 通用工具类 * @author V型知识库 www.vxzsk.com * */ public void execute(HttpServletRequest request, HttpServletResponse response) throws Exception{ /** * 获取用户扫描二维码后,微信返回的信息 */ InputStream inStream = request.getInputStream(); ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inStream.read(buffer)) != -1) { outSteam.write(buffer, 0, len); } outSteam.close(); inStream.close(); String result = new String(outSteam.toByteArray(),"utf-8"); /** * 获取返回的信息内容中各个参数的值 */ Map<Object, Object> map = XMLUtil.doXMLParse(result); SortedMap<Object,Object> parameters = new TreeMap<Object,Object>(); parameters.put("appid", map.get("appid")); parameters.put("openid", map.get("openid")); parameters.put("mch_id", map.get("mch_id")); parameters.put("is_subscribe",map.get("is_subscribe")); parameters.put("nonce_str", map.get("nonce_str")); parameters.put("product_id", map.get("product_id")); String sign = PayCommonUtil.createSign("utf-8", parameters); /** * 调用统一接口,返回预付id */ SortedMap<Object,Object> para = new TreeMap<Object,Object>(); para.put("appid", ConfigUtil.APPID); para.put("mch_id", ConfigUtil.MCH_ID); para.put("nonce_str", PayCommonUtil.CreateNoncestr()); para.put("body", "LEO测试NATIVE支付"); para.put("out_trade_no", "20150106003");//商户订单号要唯一 para.put("total_fee", "1"); para.put("spbill_create_ip","xxxip地址");//填写自己的 para.put("notify_url", ConfigUtil.NOTIFY_URL);//支付成功后回调的action,与JSAPI相同 para.put("trade_type", "NATIVE"); para.put("product_id", map.get("product_id")); String nativeSign = PayCommonUtil.createSign("UTF-8", para); para.put("sign", nativeSign); String requestXML = PayCommonUtil.getRequestXml(para); String nativeResult =CommonUtil.httpsRequest(ConfigUtil.UNIFIED_ORDER_URL, "POST", requestXML); System.out.println(nativeResult); Map<Object, Object> resultMap = XMLUtil.doXMLParse(nativeResult); String returnCode = (String) resultMap.get("return_code"); String resultCode = (String) resultMap.get("result_code"); /** * 发送信息给微信服务器 */ SortedMap<Object, Object> toWX = new TreeMap<Object, Object>(); if(returnCode.equalsIgnoreCase("SUCCESS")&&resultCode.equalsIgnoreCase("SUCCESS")){ String prepayId = (String) resultMap.get("prepay_id"); toWX.put("return_code", "SUCCESS"); toWX.put("return_msg", ""); toWX.put("appid", map.get("appid")); toWX.put("nonce_str", PayCommonUtil.CreateNoncestr()); toWX.put("prepay_id", prepayId); String toWXSign =""; if(map.get("sign").equals(sign)){ toWX.put("result_code", "SUCCESS"); toWXSign = PayCommonUtil.createSign("utf-8", toWX); }else {//else的部分 暂测试未通过 toWX.put("result_code", "FAIL"); toWX.put("err_code_des", "订单失效"); //result_code为FAIL时,添加该键值对,value值是微信告诉客户的信息 toWXSign = PayCommonUtil.createSign("utf-8", toWX); } toWX.put("sign", toWXSign); response.getWriter().write(PayCommonUtil.getRequestXml(toWX)); System.out.println(PayCommonUtil.getRequestXml(toWX)); }else { System.out.println("付款失败!"); return ; } } }
Mian方法里是生成二维码给用户扫描,execute方法是我的原生支付URL回调地址。
main方法打印出来的字符串在百度搜索二维码生成器 复制到生成器里面即可 生成图片二维码,当然你也可以自己写个方法生成。
上述代码中用到的工具类如下:(dom.jar自行下载)
package test.concroller; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.ConnectException; import java.net.URL; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import net.sf.json.JSONException; import net.sf.json.JSONObject; /** * 通用工具类 * @author V型知识库 www.vxzsk.com * */ public class CommonUtil { /** * 发送https请求 * @param requestUrl 请求地址 * @param requestMethod 请求方式(GET、POST) * @param outputStr 提交的数据 * @return 返回微信服务器响应的信息 */ public static String httpsRequest(String requestUrl, String requestMethod, String outputStr) { try { // 创建SSLContext对象,并使用我们指定的信任管理器初始化 TrustManager[] tm = { new MyX509TrustManager() }; SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE"); sslContext.init(null, tm, new java.security.SecureRandom()); // 从上述SSLContext对象中得到SSLSocketFactory对象 SSLSocketFactory ssf = sslContext.getSocketFactory(); URL url = new URL(requestUrl); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setSSLSocketFactory(ssf); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); // 设置请求方式(GET/POST) conn.setRequestMethod(requestMethod); conn.setRequestProperty("content-type", "application/x-www-form-urlencoded"); // 当outputStr不为null时向输出流写数据 if (null != outputStr) { OutputStream outputStream = conn.getOutputStream(); // 注意编码格式 outputStream.write(outputStr.getBytes("UTF-8")); outputStream.close(); } // 从输入流读取返回内容 InputStream inputStream = conn.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8"); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String str = null; StringBuffer buffer = new StringBuffer(); while ((str = bufferedReader.readLine()) != null) { buffer.append(str); } // 释放资源 bufferedReader.close(); inputStreamReader.close(); inputStream.close(); inputStream = null; conn.disconnect(); return buffer.toString(); } catch (ConnectException ce) { System.out.println("连接超时:{}"+ce); } catch (Exception e) { System.out.println("https请求异常:{}"+e); } return null; } /** * 获取接口访问凭证 * * @param appid 凭证 * @param appsecret 密钥 * @return */ public static Token getToken(String appid, String appsecret) { Token token = null; String requestUrl = ConfigUtil.TOKEN_URL.replace("APPID", appid).replace("APPSECRET", appsecret); // 发起GET请求获取凭证 JSONObject jsonObject = JSONObject.fromObject(httpsRequest(requestUrl, "GET", null)); if (null != jsonObject) { try { token = new Token(); token.setAccessToken(jsonObject.getString("access_token")); token.setExpiresIn(jsonObject.getInt("expires_in")); } catch (JSONException e) { token = null; // 获取token失败 System.out.println("获取token失败 errcode:{} errmsg:{}"+jsonObject.getInt("errcode")+"===="+jsonObject.getString("errmsg")); } } return token; } public static String urlEncodeUTF8(String source){ String result = source; try { result = java.net.URLEncoder.encode(source,"utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return result; } }
package test.concroller; /** * 通用工具类 * @author V型知识库 www.vxzsk.com * */ public class ConfigUtil { /** * 服务号相关信息 */ public final static String APPID = "";//服务号的应用号 public final static String APP_SECRECT = "";//服务号的应用密码 public final static String TOKEN = "weixinCourse";//服务号的配置token public final static String MCH_ID = "";//商户号 public final static String API_KEY = "";//API密钥 public final static String SIGN_TYPE = "MD5";//签名加密方式 public final static String CERT_PATH = "D:/apiclient_cert.p12";//微信支付证书存放路径地址 //微信支付统一接口的回调action public final static String NOTIFY_URL = "http://14.117.25.80:8016/wxweb/config/pay!paySuccess.action"; //微信支付成功支付后跳转的地址 public final static String SUCCESS_URL = "http://14.117.25.80:8016/wxweb/contents/config/pay_success.jsp"; //oauth2授权时回调action public final static String REDIRECT_URI = "http://14.117.25.80:8016/GoMyTrip/a.jsp?port=8016"; /** * 微信基础接口地址 */ //获取token接口(GET) public final static String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET"; //oauth2授权接口(GET) public final static String OAUTH2_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code"; //刷新access_token接口(GET) public final static String REFRESH_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN"; // 菜单创建接口(POST) public final static String MENU_CREATE_URL = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN"; // 菜单查询(GET) public final static String MENU_GET_URL = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN"; // 菜单删除(GET) public final static String MENU_DELETE_URL = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN"; /** * 微信支付接口地址 */ //微信支付统一接口(POST) public final static String UNIFIED_ORDER_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder"; //微信退款接口(POST) public final static String REFUND_URL = "https://api.mch.weixin.qq.com/secapi/pay/refund"; //订单查询接口(POST) public final static String CHECK_ORDER_URL = "https://api.mch.weixin.qq.com/pay/orderquery"; //关闭订单接口(POST) public final static String CLOSE_ORDER_URL = "https://api.mch.weixin.qq.com/pay/closeorder"; //退款查询接口(POST) public final static String CHECK_REFUND_URL = "https://api.mch.weixin.qq.com/pay/refundquery"; //对账单接口(POST) public final static String DOWNLOAD_BILL_URL = "https://api.mch.weixin.qq.com/pay/downloadbill"; //短链接转换接口(POST) public final static String SHORT_URL = "https://api.mch.weixin.qq.com/tools/shorturl"; //接口调用上报接口(POST) public final static String REPORT_URL = "https://api.mch.weixin.qq.com/payitil/report"; }
package test.concroller; import java.security.MessageDigest; /** * 通用工具类 * @author V型知识库 www.vxzsk.com * */ 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" }; }
package test.concroller; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.X509TrustManager; /** * 通用工具类 * @author V型知识库 www.vxzsk.com * */ public class MyX509TrustManager implements X509TrustManager { // 检查客户端证书 public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } // 检查服务器端证书 public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } // 返回受信任的X509证书数组 public X509Certificate[] getAcceptedIssuers() { return null; } }
package test.concroller; import java.util.Iterator; import java.util.Map; import java.util.Random; import java.util.Set; import java.util.SortedMap; /** * 通用工具类 * @author V型知识库 www.vxzsk.com * */ public class PayCommonUtil { public static String CreateNoncestr(int length) { String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; String res = ""; for (int i = 0; i < length; i++) { Random rd = new Random(); res += chars.indexOf(rd.nextInt(chars.length() - 1)); } return res; } public static String CreateNoncestr() { String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; String res = ""; for (int i = 0; i < 16; i++) { Random rd = new Random(); res += chars.charAt(rd.nextInt(chars.length() - 1)); } return res; } /** * @author V型知识库www.vxzsk.com * @date * @Description:sign签名 * @param characterEncoding 编码格式 * @param parameters 请求参数 * @return */ public static String createSign(String characterEncoding,SortedMap<Object,Object> parameters){ StringBuffer sb = new StringBuffer(); Set es = parameters.entrySet(); Iterator it = es.iterator(); while(it.hasNext()) { Map.Entry entry = (Map.Entry)it.next(); String k = (String)entry.getKey(); Object v = entry.getValue(); if(null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) { sb.append(k + "=" + v + "&"); } } sb.append("key=" + ConfigUtil.API_KEY); String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase(); return sign; } /** * @author V型知识库www.vxzsk.com * @date * @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(); } /** * @author V型知识库www.vxzsk.com * @date * @Description:返回给微信的参数 * @param return_code 返回编码 * @param return_msg 返回信息 * @return */ public static String setXML(String return_code, String return_msg) { return "<xml><return_code><![CDATA[" + return_code + "]]></return_code><return_msg><![CDATA[" + return_msg + "]]></return_msg></xml>"; } }
package test.concroller; /*** * V型知识库 * www.vxzsk.com * * */ public class Token { private String accessToken; private int expiresIn; public String getAccessToken() { return accessToken; } public void setAccessToken(String accessToken) { this.accessToken = accessToken; } public int getExpiresIn() { return expiresIn; } public void setExpiresIn(int expiresIn) { this.expiresIn = expiresIn; } }
package test.concroller; 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; /*** * V型知识库 www.vxzsk.com * * */ 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(); } }
把上述代码复制到工程里,记得要下载依赖的dom.jar,当然你也可以点击下面的案例下载按钮下载源码
感觉本站内容不错,读后有收获?小额赞助,鼓励网站分享出更好的教程