java接入新版公钥微信支付接口

该文章已生成可运行项目,
Map<String, String> map = new HashMap<>(1);
        try {
            PreRechargeOrders preRechargeOrders = preRechargeOrdersDao.queryById(id);
            PreUsers preUsers = preUsersDao.queryById(preRechargeOrders.getUserId());
            String productDesc = "xx充值";
            String timeExpire = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX").format(new Date().getTime() + 1000 * 60 * 3);
            int money_fen = changY2F(preRechargeOrders.getPrice().floatValue());
            Config config =
                    new RSAPublicKeyConfig.Builder()
                            .merchantId(mchId) //微信支付的商户号
                            .privateKeyFromPath(keyPath) // 商户API证书私钥的存放路径
                            .publicKeyFromPath(pub_key) //微信支付公钥的存放路径
                            .publicKeyId(publicKey) //微信支付公钥ID
                            .merchantSerialNumber(apiKey) //商户API证书序列号
                            .apiV3Key(apiKey3) //APIv3密钥
                            .build();
            JsapiServiceExtension service = new JsapiServiceExtension.Builder().config(config).build();
            com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest prepayRequest = new com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest();
            Amount amount = new Amount();
            amount.setTotal(money_fen);
            prepayRequest.setAmount(amount);
            prepayRequest.setAppid(appId);
            prepayRequest.setMchid(mchId);
            prepayRequest.setDescription(productDesc);
            prepayRequest.setOutTradeNo(preRechargeOrders.getRechargeNo());
            prepayRequest.setTimeExpire(timeExpire);
            prepayRequest.setAttach("xx充值");
            prepayRequest.setNotifyUrl(domain.concat("/recharge/taocanRecharge"));
            Payer payer = new Payer();
            payer.setOpenid(preUsers.getOpenId());
            prepayRequest.setPayer(payer);
            SettleInfo settleInfo = new SettleInfo();
            settleInfo.setProfitSharing(false);
            prepayRequest.setSettleInfo(settleInfo);
            PrepayWithRequestPaymentResponse prepayWithRequestPaymentResponse = service.prepayWithRequestPayment(prepayRequest);

            map.put("code", "200");
            map.put("appId", prepayWithRequestPaymentResponse.getAppId());
            map.put("timeStamp", prepayWithRequestPaymentResponse.getTimeStamp());
            map.put("nonceStr", prepayWithRequestPaymentResponse.getNonceStr());
            map.put("package", prepayWithRequestPaymentResponse.getPackageVal());
            map.put("signType", prepayWithRequestPaymentResponse.getSignType());
            map.put("paySign", prepayWithRequestPaymentResponse.getPaySign());
        } catch (Exception e) {
            e.printStackTrace();
        }

引入jar

        <dependency>
            <groupId>com.github.wechatpay-apiv3</groupId>
            <artifactId>wechatpay-java</artifactId>
            <version>0.2.17</version>
        </dependency>

回调接入:

  public Map<String, String> rechargeNotify(HttpServletRequest request, HttpServletResponse response) { 
Map<String, String> map = new HashMap<>(12);
String timestamp = request.getHeader("Wechatpay-Timestamp");
            String nonce = request.getHeader("Wechatpay-Nonce");
            String serialNo = request.getHeader("Wechatpay-Serial");
            String signature = request.getHeader("Wechatpay-Signature");
            log.info("timestamp:{} nonce:{} serialNo:{} signature:{}" + timestamp + nonce + serialNo + signature);
            String result = HttpKit.readData(request);
            log.info("支付通知密文 {}" + result);
            NotificationConfig config = new RSAPublicKeyNotificationConfig.Builder()
                    .publicKeyFromPath(pub_key)
                    .publicKeyId(publicKey)
                    .apiV3Key(apiKey3)
                    .build();
            NotificationParser parser = new NotificationParser(config);
            RequestParam requestParam = new RequestParam.Builder()
                    .serialNumber(publicKey)
                    .nonce(nonce)
                    .signature(signature)
                    .timestamp(timestamp)
                    .body(result)
                    .build();
            Transaction transaction = parser.parse(requestParam, Transaction.class);

            JSONObject jsonObject = JSONUtil.parseObj(transaction);
            log.info("---解析数据:" + jsonObject);
}

本文章已经生成可运行项目
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值