首页 >  微信开发 >  JSAPI微信支付回调结果解密

JSAPI微信支付回调结果解密

时间:2024-01-07

当完成JSAPI支付之后,微信会进行回调,回调地址是传给微信官方的notify_url参数,如果正确,则会返回如下JSON数据:

{

    "id": "EV-2018022511223320873",

    "create_time": "2015-05-20T13:29:35+08:00",

    "resource_type": "encrypt-resource",

    "event_type": "TRANSACTION.SUCCESS",

    "summary": "支付成功",

    "resource": {

        "original_type": "transaction",

        "algorithm": "AEAD_AES_256_GCM",

        "ciphertext": "",

        "associated_data": "",

        "nonce": ""

    }

}

一、对结果进行解密,其实对resouce里面的报文数据进行解密,拿到我们想要的订单信息

官方文档:JSAPI支付回调结果

传入resource里面的对应参数,调用以下方法即可进行解密

注意:apiV3Key是商户平台中自己设置V3密钥密钥字符串

private String decrypt(String nonce, String associatedData, String ciphertext) throws Exception {

       byte[] keyBytes = this.apiV3Key.getBytes();

       byte[] nonceBytes = nonce.getBytes();

       byte[] associatedDataBytes = associatedData.getBytes();

       byte[] ciphertextBytes = Base64.getDecoder().decode(ciphertext);

       Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");

       Key secretKey = new SecretKeySpec(keyBytes, "AES");

       GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(128, nonceBytes);

       cipher.init(Cipher.DECRYPT_MODE, secretKey, gcmParameterSpec);

       cipher.updateAAD(associatedDataBytes);

       byte[] decryptedBytes = cipher.doFinal(ciphertextBytes);

       return new String(decryptedBytes);

}