代扣解约挽留业务-获取解约挽留信息(JSON)
当用户关闭协议时,协议所关联的模板在获取范围内,则主动的获取挽留信息。 官方文档
请求头(headers)
Wechatpay-Nonce: 3d980fb850fdce97f6bfb3d248597f16
Wechatpay-Serial: 7132d72a03e93cddf8c03bbd1f37eedf********
Wechatpay-Signature: i48r5y8IQw1qpTO+ywoV...
Wechatpay-Signature-Type: WECHATPAY2-SHA256-RSA2048
Wechatpay-Timestamp: 1710048759
Request-ID: 08F78BB5AF0610D302189F99DD5C20BA56F89845-0
请求报文(body)
json
{
"id":"EV-2018022511223320873",
"create_time":"20180225112233",
"resource_type":"encrypt-resource",
"event_type":"ENTRUST.TERMINATE_RETENTION",
"summary": "获取解约挽留信息",
"resource" : {
"original_type":"entrust",
"algorithm":"AEAD_AES_256_GCM",
"ciphertext": "...",
"nonce": "...",
"associated_data": ""
}
}
resource.ciphertext 解密后的明文格式
json
{
"appid": "wxd678efh567hg6787",
"contract_id": "123124412412423431",
"mchid": "1900000109",
"openid": "o-MYE42l80oelYMDE34nYD456Xoy",
"out_contract_code": "wxwtdk20200910100000",
"plan_id": 12535
}
处理程序
js
const { Formatter, Rsa, Aes } = require('wechatpay-axios-plugin')
const {
'wechatpay-nonce': wechatpayNonce,
'wechatpay-serial': wechatpaySerial,
'wechatpay-signature': wechatpaySignature,
'wechatpay-timestamp': wechatpayTimestamp,
} = headers
let code = 'SUCCESS', message = undefined
if (Math.abs(Formatter.timestamp() - wechatpayTimestamp) > MAXIMUM_CLOCK_OFFSET) {
code = 'FAIL'
message = 'Over clock offset'
}
else
if (!Object.hasOwn(platformCertificates, wechatpaySerial)) {
code = 'FAIL'
message = 'platform certificate not exists'
}
else
if (!Rsa.verify(
Formatter.joinedByLineFeed(wechatpayTimestamp, wechatpayNonce, json),
wechatpaySignature,
platformCertificates[wechatpaySerial]
)) {
code = 'FAIL'
message = 'sign mismatched'
}
// do your business
// ...
// ...
const {
id,
create_time,
resource_type,
event_type,
resource: {
ciphertext,
nonce,
associated_data
},
} = JSON.parse(json)
const {
appid,
contract_id,
mchid,
openid,
out_contract_code,
plan_id,
} = JSON.parse(Aes.AesGcm.decrypt(nonce, apiv3Key, ciphertext, associated_data))
// do your business
// ...
// ...
const response = { code, message, retention_type, coupon_info: { state, coupon_id } }
正常应答头(headers)
Status: 200
正常应答报文(body)
json
{
"code": "SUCCESS",
"message": "",
"retention_type": "COUPON",
"coupon_info": {
"state": "SEND_COUPON",
"coupon_id": "9867041"
}
}
注意:
- 对后台交互时,如果微信收到应答不是成功或超时(当前是1秒的超时),微信认为获取失败,不会展示挽留信息。
- 特别提醒:商户系统对于开启结果通知的内容一定要做签名验证,并校验通知的信息是否与商户侧的信息一致,防止数据泄露导致出现“假通知”,造成资金损失。