收藏
回答

微信支付V3接口调用报400,把所有参数加上了还是报400是怎么回事?

微信支付V3

回答关注问题邀请回答
收藏

2 个回答

  • 大稳·杨
    大稳·杨
    2021-02-26

    楼主解决了吗?什么原因

    2021-02-26
    有用
    回复
  • 我不懂
    我不懂
    2020-12-28
    SortedMap<String, Object> sortedMap = new TreeMap<String, Object>();
    sortedMap.put("appid", wxPayConfigEntity.getApppid()); //应用ID
    sortedMap.put("mchid", wxPayConfigEntity.getMch_id()); //商户号
    
    sortedMap.put("description", "珠宝纯定制-" + weiXinPayPo.getBody()); //商品描述
    sortedMap.put("out_trade_no", out_trade_no); //商户订单号
    sortedMap.put("notify_url", wxPayConfigEntity.getNotify_url()); //通知回调地址
    //总金额
    int totalFee = weiXinPayPo.getTotal_fee().multiply(new BigDecimal(100)).intValue();//总金额  需要乘以100 ,微信支付默认单位分
    Map<String, Object> map1 = new HashMap<>();
    map1.put("total",totalFee);
    map1.put("currency","CNY");
    sortedMap.put("amount",map1);
    JSONObject json = new JSONObject(sortedMap);
    String reqdata = json.toJSONString();
    //校验支付路径
    HttpUrl httpurl = HttpUrl.parse(wxPayConfigEntity.getPayurl());
    // 请求body参数
    log.info(reqdata);
    
    String Sign = null;
    PrivateKey privateKey = null;//商户私钥
    AutoUpdateCertificatesVerifier verifier = null;
    try {
        Sign = wxPayUtil.getToken(nonceStr,timestamp,POST,httpurl,reqdata);
        //======设置请求头========
        privateKey = wxPayUtil.getPrivateKey(wxPayConfigEntity.getKeyPath());
        //不需要传入微信支付证书,将会自动更新
        verifier = new AutoUpdateCertificatesVerifier(
                new WechatPay2Credentials(wxPayConfigEntity.getMch_id(), new PrivateKeySigner(wxPayConfigEntity.getSerialNo(), privateKey)),
                wxPayConfigEntity.getApiV3Key().getBytes("utf-8"));
    } catch (IOException e) {
        e.printStackTrace();
    } catch (SignatureException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }
    //通过WechatPayHttpClientBuilder构造的HttpClient,会自动的处理签名和验签
    CloseableHttpClient httpClient = WechatPayHttpClientBuilder.create()
            .withMerchant(wxPayConfigEntity.getMch_id(), wxPayConfigEntity.getSerialNo(), privateKey)
            .withValidator(new WechatPay2Validator(verifier)).build();
    HttpPost httpPost = new HttpPost(wxPayConfigEntity.getPayurl());
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build();
    httpPost.setConfig(requestConfig);
    httpPost.addHeader("Content-Type", "application/json");
    httpPost.addHeader("Accept","application/json");
    httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
    String authType   ="WECHATPAY2-SHA256-RSA2048";
    String authorization = wxPayUtil.getAuthorization(Sign, authType);
    httpPost.addHeader("Authorization",authorization);
    StringEntity entity = new StringEntity(reqdata,"UTF-8");
    httpPost.setEntity(entity);
    //======发起请求========
    CloseableHttpResponse response = null;
    String result = null;
    try {
         response = httpClient.execute(httpPost);
         result = EntityUtils.toString(response.getEntity());
    } catch (IOException e) {
        e.printStackTrace();
    }
    int statusCode = response.getStatusLine().getStatusCode();
    

    参数列表里面六个必填的参数都传了,打印处理的json也没问题,请求报400

    2020-12-28
    有用
    回复 2
登录 后发表内容
问题标签