java 微信报关 V2

  • 申报接口
//这个类请转至:https://blog.csdn.net/qq_40506288/article/details/110918650
@Autowired
private MyWxPayConfig myWxPayConfig;

public Result<Object> wechatDeclareRequest(String id) {
    String xmlStr = null;
    String errCodeDes = null;
    try {
        //PayDeclaration 给自己自定义的类
        PayDeclaration payDeclaration = baseMapper.selectById(id);
        if (null == payDeclaration) {
            return Result.error("支付单不存在");
        }
        if (null == payDeclaration.getTransactionId()) {
            return Result.error("数据异常【微信订单号为空】");
        }
        if (null == payDeclaration.getOutTradeNo()) {
            return Result.error("数据异常【商户订单号为空】");
        }
        //微信报关数据设置
        String wechatDeclareValue = wechatDeclareValue(payDeclaration);

        CloseableHttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(PayConfig.url);
        post.setEntity(new StringEntity(wechatDeclareValue));
        HttpResponse response = client.execute(post);
        HttpEntity entity = response.getEntity();
        //微信报关返回的xml字符串
        xmlStr = EntityUtils.toString(entity);
        //微信配置信息初始化,微信SDK提供的,地址:https://pay.weixin.qq.com/wiki/doc/api/external/jsapi.php?chapter=11_1
        WXPay wxPay = new WXPay(myWxPayConfig);

        //xml格式转map,并对sign签名认证,认证失败会返回异常信息,如果不需要签名认证可以忽略这个步骤,自行处理XML,上一行代码微信配置初始化也可以忽略掉
        Map<String, String> map = wxPay.processResponseXml(xmlStr);
        //异常错误信息
        errCodeDes = map.get("err_code_des");
        String returnCode = map.get("return_code");
        String resultCode = map.get("result_code");
        if (!returnCode.equals("SUCCESS") || !resultCode.equals("SUCCESS")) {
            return Result.error("申报失败【" + errCodeDes + "】");
        }
        logger.info(JSONUtil.parseFromMap(map).toString());
        return Result.ok("申报成功");
    } catch (Exception e) {
        e.printStackTrace();
        //申报记录,失败
        return Result.error("申报失败【" + errCodeDes + "】");
    }

}

  • 申报参数设置
    public String wechatDeclareValue(PayDeclaration payDeclaration) throws Exception {
        //封装签名字段
        Map<String, String> params = new TreeMap<String, String>();
        params.put("appid", payDeclaration.getAppid());
        params.put("customs", payDeclaration.getCustoms());
        params.put("mch_customs_no", payDeclaration.getMerchantCustomsNo());
        params.put("mch_id", payDeclaration.getMchid());
        params.put("out_trade_no", payDeclaration.getOutTradeNo());
        params.put("transaction_id", payDeclaration.getTransactionId());
        //支付人的信息
        //params.put("cert_id", payDeclaration.getCertId());
        //params.put("cert_type", payDeclaration.getCertType());
        //params.put("name", payDeclaration.getName());
        //如果是拆单才需要传递下面的几个参数
        //params.put("order_fee", payDeclaration.getOrderFee().toString());
        //params.put("product_fee", payDeclaration.getProductFee().toString());
        //params.put("transport_fee", null != payDeclaration.getTransportFee() ? payDeclaration.getTransportFee().toString() : "0");
        //params.put("fee_type", payDeclaration.getFeeType());
        //params.put("sub_order_no", payDeclaration.getSubOrderNo());
        //微信的SDK方法
        return WXPayUtil.generateSignedXml(params, PayConfig.apiSecretKey);
    }

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值