登录  /  注册

微信扫码支付模式

PHPz
发布: 2017-04-04 11:30:11
原创
6585人浏览过

微信扫码支付(模式一)

微信扫码支付遇到的问题

  • 原生支付URL参数错误

  • 回调接口URL有回调,但是接收不到参数

  • 商户后台返回的数据字段结构不合法

  • 获取商户订单信息超时或者商户返回的httpcode非200

解决问题

  • 原生支付URL参数错误
    这个错误一般会出现在获取到二维码URL之后生成二维码微信扫码的时候。如果你出现此类型的问题请检查

1、生成二维码所需参数列表中参数是否有错误(区分大小写)
2、参数中签名sign时候正确 签名算法   签名校验工具


以下是生成二维码URL的代码

/**
 * 
 * @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> packageParams = new HashMap<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);
}</string></string>
登录后复制
  • 回调接口URL有回调,但是接收不到参数

    Enumeration  en=getParaNames();

      while (en.hasMoreElements()) {
          Object o= en.nextElement();
          System.out.println(o.toString()+"="+getPara(o.toString()));
      }
    登录后复制

以上代码中输出的参数都为NULL

由于官方的文档描述不是很清楚,大家都以为回调请求将带productid和用户的openid等参数是以普通的参数一样,其实这个回调返回的参数是一个XML输入流

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结果为









</prepay_id><br><trade_type></trade_type><br><code_url>![CDATA[weixin://wxpay/bizpayurl?pr=Gj3ZF2b]]</code_url><br></xml></p> <p>如果返回的 return_code  result_code 不为SUCCESS 而回调的接口没有返回任何数据或者返回的数据不合法就会出现以下错误</p> <ul class=" list-paddingleft-2"> <li><p>商户后台返回的数据字段结构不合法(返回的数据包格式不正确)</p></li> <li><p>获取商户订单信息超时或者商户返回的httpcode非200(没有返回的数据包)</p></li> </ul> <p>如果以上都没有问题,就剩下最后一个步骤了  商户后台系统将prepay_id返回给微信支付系统  以下是详细的代码</p> <pre class="brush:php;toolbar:false">/**  * @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&gt;&gt;&gt;&gt;"+result);
        /**
         * 获取返回的信息内容中各个参数的值
         */
        Map<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> packageParams = new HashMap<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> params = new HashMap<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&gt;&gt;&gt;"+xmlResult);

        /**
         * 发送信息给微信服务器
         */
        Map<string> payResult = PaymentKit.xmlToMap(xmlResult);

        String return_code = payResult.get("return_code");
        String result_code = payResult.get("result_code");

        if (StrKit.notBlank(return_code) &amp;&amp; StrKit.notBlank(result_code) &amp;&amp; return_code.equalsIgnoreCase("SUCCESS")&amp;&amp;result_code.equalsIgnoreCase("SUCCESS")) {
            // 以下字段在return_code 和result_code都为SUCCESS的时候有返回
            String prepay_id = payResult.get("prepay_id");

            Map<string> prepayParams = new HashMap<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();
    }

}</string></string></string></string></string></string></string></string>
登录后复制

以上就是微信扫码支付模式的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号