springboot 微信支付里的app支付坑注意,requestPayment,支付报错,"errMsg":"requestPayment:fail:[payment微信:-1]General errors

时间:2019-12-24 18:38:35   收藏:0   阅读:895

 

 

 

  @RequestMapping(value="/toPay4App",method= {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "去支付. 用于app")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "money", value = "金额", required = false, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "type",value = "类型6=一年会员", required = true,paramType = "query",dataType = "int"),
            @ApiImplicitParam(name = "memberid", value = "用户id", required = true, paramType = "query", dataType = "string"),
    })
    public BasicMapDataRet toPay4App(
            HttpServletRequest request,
            @RequestParam(required = false)BigDecimal money, Integer type,@RequestParam(required = false) Integer num,Integer memberid
    ) throws RestMainException {
        try {
            BasicMapDataRet ret = new BasicMapDataRet();
            //业务逻辑和校验

            Map map = doUnifiedOrder(request,trade,hrRechargeOrder);

            ret.setResult(BasicRet.SUCCESS);
            ret.getData().put("result",map);
            return ret;
        }catch (Exception e){
            throw new RestMainException(e);
        }
    }

 

 /**
     * 微信app端支付专用的 下单
     * @return
     * @throws Exception
     */
    public Map doUnifiedOrder(HttpServletRequest request,Trade trade,HrRechargeOrder hrRechargeOrder) throws Exception {
        try {
            //WXConfigUtil config = new WXConfigUtil();
            //System.out.println("wxPayAppConfigtoString:"+wxPayAppConfig.toString()+"字符串"+wxPayAppConfig.getKey()+"appid"+wxPayAppConfig.getAppID());
            WXPay wxpay = new WXPay(wxPayAppConfig);
            Map<String, String> data = new HashMap<>();
            data.put("appid", wxPayAppConfig.getAppID());
            //System.out.println("打印appid"+data.get("appid"));
            data.put("mch_id", wxPayAppConfig.getMchID());
            //System.out.println("打印mch_id"+data.get("mch_id"));
            data.put("nonce_str", WXPayUtil.generateNonceStr());
            //System.out.println("打印nonce_str"+data.get("nonce_str"));
            data.put("body", trade.getBody());
            //System.out.println("打印body"+data.get("body"));
            data.put("out_trade_no", trade.getOutTradeNo());
            //System.out.println("打印out_trade_no"+data.get("out_trade_no"));

            data.put("total_fee",hrRechargeOrder.getMoney().multiply(new BigDecimal(100)).toString());
            //data.put("total_fee",hrRechargeOrder.getMoney().toString());
            //System.out.println("打印total_fee和money"+data.get("total_fee"));
            //自己的服务器IP地址
            data.put("spbill_create_ip", request.getRemoteAddr());
            //System.out.println("打印spbill_create_ip"+data.get("spbill_create_ip"));
            //异步通知地址(请注意必须是外网)
            data.put("notify_url", webConfBean.getProjectDomain()+"/rest/wxpay/notify");
            //System.out.println("打印notify_url"+data.get("notify_url"));
            //交易类型
            data.put("trade_type", "APP");
            //System.out.println("打印trade_type"+data.get("trade_type"));
            //附加数据,在查询API和支付通知中原样返回,该字段主要用于商户携带订单的自定义数据
            //data.put("attach", "");
            //System.out.println("进入sign前"+data.toString()+"wxPayAppConfig.getKey()"+"0ba2e528d876e0ef66ca5344debb3eac");
            //System.out.println("进入sign前再次打印data"+data.toString());

            data.put("sign", WXPayUtil.generateSignature(data, wxPayAppConfig.getKey(),
                    WXPayConstants.SignType.MD5));
            //System.out.println("请求前最后打印data"+data.toString());
            //使用官方API请求预付订单
            Map<String, String> response = wxpay.unifiedOrder(data);
            //System.out.println("打印response.get(\"return_code\")"+response.get("return_code"));
            //System.out.println("打印response"+response.toString());
            if ("SUCCESS".equals(response.get("return_code"))) {//主要返回以下5个参数
                //System.out.println("进入success");
                Map<String, String> param = new HashMap<>();
                param.put("appid",wxPayAppConfig.getAppID());
                param.put("partnerid",response.get("mch_id"));
                param.put("prepayid",response.get("prepay_id"));
                param.put("package","Sign=WXPay");
                param.put("noncestr",WXPayUtil.generateNonceStr());
                param.put("timestamp",System.currentTimeMillis()/1000+"");
                param.put("sign",WXPayUtil.generateSignature(param, wxPayAppConfig.getKey(),
                        WXPayConstants.SignType.MD5));
                //System.out.println("进入第二次sign"+param.get("sign"));
                return param;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException("下单失败");
        }
        throw new MyException("下单失败");
    }

最大的坑在加红的位置,引入用的@Value 不用
@Component
@ConfigurationProperties("mod.wechat.pay")
①原因在于不支持驼峰的会空指针
②另外一个坑在于 app支付不同于PC和H5的JSAPI和NATIVE它需要二次验签名,而H5和PC的可以直接request和result
 

 

参考博客 https://www.jianshu.com/p/59baa5a280b7

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!