springboot 微信支付第四步,支付成功回调通知解密

         前面三篇文章,我们记录了微信支付的第一步、第二步、第三步,接下来是最后一步,完成支付后,拿到微信推送的支付报文,进行解密,做本地数据处理。

         微信支付成功回调通知信息如下图, 微信支付成功回调通知官方文档点击直达

    这里我们只需要解密数据,拿到解密数据里面自己系统内部的订单号,个人是觉得没必要进行验签的,如何解密,我们通用采用官方的解密代码

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class AesUtil {
  static final int KEY_LENGTH_BYTE = 32;
  static final int TAG_LENGTH_BIT = 128;
  private final byte[] aesKey;
  public AesUtil(byte[] key) {
    if (key.length != KEY_LENGTH_BYTE) {
      throw new IllegalArgumentException("无效的ApiV3Key,长度必须为32个字节");
    }
      this.aesKey = key;
  }
  public String decryptToString(byte[] associatedData, byte[] nonce, String ciphertext)
  throws GeneralSecurityException, IOException {
    try {
      Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
      SecretKeySpec key = new SecretKeySpec(aesKey, "AES");
      GCMParameterSpec spec = new GCMParameterSpec(TAG_LENGTH_BIT, nonce);
      cipher.init(Cipher.DECRYPT_MODE, key, spec);
      cipher.updateAAD(associatedData);
      return new String(cipher.doFinal(Base64.getDecoder().decode(ciphertext)), "utf-8");
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
      throw new IllegalStateException(e);
    } catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
      throw new IllegalArgumentException(e);
    }
  }
}

 上面的解密代码,来自官方,点击直达

这里解密只需要v3key , 拿到我们内部系统的订单号,然后做后续逻辑处理

AesUtil aesUtil = new AesUtil(WECHAT_APIV3KEY.getBytes(StandardCharsets.UTF_8));

        String decryptedData = aesUtil.decryptToString(backDTO.getResource().getAssociated_data().getBytes(StandardCharsets.UTF_8), backDTO.getResource().getNonce().getBytes(StandardCharsets.UTF_8),
                backDTO.getResource().getCiphertext());

        log.info("微信解密的字符串信息:{}",  decryptedData  );

        JSONObject jsonInfo = (JSONObject) JSONObject.parse(decryptedData);

        String  outTradeNo = jsonInfo.getString("out_trade_no");

backDTO

@Data
public class WeCallBackDTO {

    /**
     * 事件的唯一标识符。
     */
    private String id;

    /**
     * 事件的创建时间。
     */
    private String create_time;

    /**
     * 资源类型,指示涉及的资源类型。
     */
    private String resource_type;

    /**
     * 事件类型,指示发生的事件类型。
     */
    private String event_type;

    /**
     * 事件的简要描述。
     */
    private String summary;

    /**
     * 关联的资源信息。
     */
    private WeResourcedDTO resource;
}

@Data
public class WeResourcedDTO {

    /**
     * 原始资源的类型。
     */
    private String original_type;

    /**
     * 使用的加密算法。
     */
    private String algorithm;

    /**
     * 加密后的密文。
     */
    private String ciphertext;

    /**
     * 附加数据,用于验证完整性。
     */
    private String associated_data;

    /**
     * 随机数,用于加密过程。
     */
    private String nonce;
}

 OK, 到这里微信支付的流程,就全部走完了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值