标签:
项目开源地址:http://git.oschina.net/javen205/weixin_guide
微信扫码支付(模式一)你有测试成功吗?如果你看到了这篇文章,我想你是在测试中遇到问题了。OK 那现在我们来看看微信扫码支付中的坑。
解决问题
/** * * @author Javen * 2016年5月14日 * 扫码支付获取二维码URL(模式一) */ public String getCodeUrl(){ String url="weixin://wxpay/bizpayurl?sign=XXXXX&appid=XXXXX&mch_id=XXXXX&product_id=XXXXX&time_stamp=XXXXX&nonce_str=XXXXX"; String product_id="001"; String timeStamp=Long.toString(System.currentTimeMillis() / 1000); String nonceStr=Long.toString(System.currentTimeMillis()); Map<String, String> packageParams = new HashMap<String, String>(); packageParams.put("appid", appid); packageParams.put("mch_id", partner); packageParams.put("product_id",product_id); packageParams.put("time_stamp", timeStamp); packageParams.put("nonce_str", nonceStr); String packageSign = PaymentKit.createSign(packageParams, paternerKey); return StringUtils.replace(url, "XXXXX", packageSign,appid,partner,product_id,timeStamp,nonceStr); }
Enumeration<String> en=getParaNames(); while (en.hasMoreElements()) { Object o= en.nextElement(); System.out.println(o.toString()+"="+getPara(o.toString())); }
以上代码中输出的参数都为NULL
HttpServletRequest request = getRequest(); /** * 获取用户扫描二维码后,微信返回的信息 */ 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");
result结果为
<xml> <appid><![CDATA[wx5e9360a3f46f64cd]]></appid> <openid><![CDATA[o_pncsidC-pRRfCP4zj98h6slREw]]></openid> <mch_id><![CDATA[1322117501]]></mch_id> <is_subscribe><![CDATA[Y]]></is_subscribe> <nonce_str><![CDATA[y4dGR6JxuybgUWUo]]></nonce_str> <product_id><![CDATA[001]]></product_id> <sign><![CDATA[89951BAE8796DE7D32E9741E42C35FB4]]></sign> </xml>
如果以上都没有出现问题,下面就要根据product_id来生成(统计下单接口)预付订单了。
以下是正确返回的XML数据
<xml>
<return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[OK]]></return_msg> <appid><![CDATA[wx5e9360a3f46f64cd]]></appid> <mch_id><![CDATA[1322117501]]></mch_id> <nonce_str><![CDATA[lr3cCuO2KxMEBHIW]]></nonce_str> <sign><![CDATA[842C1857EDD009D67519527BCF3AFA4C]]></sign> <result_code><![CDATA[SUCCESS]]></result_code> <prepay_id><![CDATA[wx201605151311568e801d50fb0555050106]]></prepay_id> <trade_type><![CDATA[NATIVE]]></trade_type> <code_url><![CDATA[weixin://wxpay/bizpayurl?pr=Gj3ZF2b]]></code_url> </xml>
如果返回的 return_code result_code 不为SUCCESS 而回调的接口没有返回任何数据或者返回的数据不合法就会出现以下错误
/** * @author Javen * 2016年5月14日 * 扫码支付回调(模式一) */ public void wxpay(){ try { HttpServletRequest request = getRequest(); /** * 获取用户扫描二维码后,微信返回的信息 */ 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"); System.out.println("callback>>>>"+result); /** * 获取返回的信息内容中各个参数的值 */ Map<String, String> map = PaymentKit.xmlToMap(result); for (String key : map.keySet()) { System.out.println("key= "+ key + " and value= " + map.get(key)); } String appid=map.get("appid"); String openid = map.get("openid"); String mch_id = map.get("mch_id"); String is_subscribe = map.get("is_subscribe"); String nonce_str = map.get("nonce_str"); String product_id = map.get("product_id"); String sign = map.get("sign"); Map<String, String> packageParams = new HashMap<String, String>(); packageParams.put("appid", appid); packageParams.put("openid", openid); packageParams.put("mch_id",mch_id); packageParams.put("is_subscribe",is_subscribe); packageParams.put("nonce_str",nonce_str); packageParams.put("product_id", product_id); String packageSign = PaymentKit.createSign(packageParams, paternerKey); // 统一下单文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1 Map<String, String> params = new HashMap<String, String>(); params.put("appid", appid); params.put("mch_id", mch_id); params.put("body", "测试扫码支付"); String out_trade_no=Long.toString(System.currentTimeMillis()); params.put("out_trade_no", out_trade_no); int price=((int)(Float.valueOf(10)*100)); params.put("total_fee", price+""); params.put("attach", out_trade_no); String ip = IpKit.getRealIp(getRequest()); if (StrKit.isBlank(ip)) { ip = "127.0.0.1"; } params.put("spbill_create_ip", ip); params.put("trade_type", TradeType.NATIVE.name()); params.put("nonce_str", System.currentTimeMillis() / 1000 + ""); params.put("notify_url", notify_url); params.put("openid", openid); String paysign = PaymentKit.createSign(params, paternerKey); params.put("sign", paysign); String xmlResult = PaymentApi.pushOrder(params); System.out.println("prepay_xml>>>"+xmlResult); /** * 发送信息给微信服务器 */ Map<String, String> payResult = PaymentKit.xmlToMap(xmlResult); String return_code = payResult.get("return_code"); String result_code = payResult.get("result_code"); if (StrKit.notBlank(return_code) && StrKit.notBlank(result_code) && return_code.equalsIgnoreCase("SUCCESS")&&result_code.equalsIgnoreCase("SUCCESS")) { // 以下字段在return_code 和result_code都为SUCCESS的时候有返回 String prepay_id = payResult.get("prepay_id"); Map<String, String> prepayParams = new HashMap<String, String>(); prepayParams.put("return_code", "SUCCESS"); prepayParams.put("appId", appid); prepayParams.put("mch_id", mch_id); prepayParams.put("nonceStr", System.currentTimeMillis() + ""); prepayParams.put("prepay_id", prepay_id); String prepaySign = null; if (sign.equals(packageSign)) { prepayParams.put("result_code", "SUCCESS"); }else { prepayParams.put("result_code", "FAIL"); prepayParams.put("err_code_des", "订单失效"); //result_code为FAIL时,添加该键值对,value值是微信告诉客户的信息 } prepaySign = PaymentKit.createSign(prepayParams, paternerKey); prepayParams.put("sign", prepaySign); String xml = PaymentKit.toXml(prepayParams); log.error(xml); renderText(xml); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
标签:
原文地址:http://www.cnblogs.com/zyw-205520/p/5495115.html