SpringBoot整合微信支付V3版本APP支付

话不多说直接上代码。

一、下单

1、支付相关参数配置,根据自己相关参数配置

/**
     * 商户号
     */
    private String mchId;
    /**
     * 商户证书序列号
     */
    private String mchSerialNo;

    /**
     * apiV3密钥
     */
    private String apiV3key;
    /**
     * APP ID
     */
    private String  appId;

    /**
     * 回调通知路径
     */
    private String  notifyUrl;

    /**
     * 支付url
     */
    private  String  payUrl;

    /**
     * 查询支付Url
     */
    private  String  queryPayUrl;

2、支付方法(获取支付连接可以封装成工具类)

2、计算签名方法

/**
     * 计算签名
     * @param message
     * @param yourPrivateKey
     * @return
     */
 private     String sign(byte[] message, PrivateKey yourPrivateKey)  {

        try{
            Signature sign = Signature.getInstance("SHA256withRSA");
            sign.initSign(yourPrivateKey);
            sign.update(message);
            return Base64.getEncoder().encodeToString(sign.sign());
        }catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (SignatureException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }

     return "";
    }

3、支付回调通知

4、验证签名

5、解密回调通知密文

/**
     * 解密回调通知密文
     * @param body
     * @return
     */
    private     String   decryptOrder(String  body){

        try {
            AesUtil aesUtil  = new AesUtil(emsWeChatPayConfig.getApiV3key().getBytes("utf-8"));
            ObjectMapper objectMapper  = new ObjectMapper();
            JsonNode jsonNode = objectMapper.readTree(body);
            JsonNode resource = jsonNode.get("resource");
            //数据密文
            String ciphertext = resource.get("ciphertext").textValue();
            String associatedData = resource.get("associated_data").textValue();
            String nonce = resource.get("nonce").textValue();

            return aesUtil.decryptToString(associatedData.getBytes("utf-8"),nonce.getBytes("utf-8"),ciphertext);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }

        return  "";
    }
经验分享 程序员 微信小程序 职场和发展