增强行业凭证发货回调日志
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import crypto from 'node:crypto'
|
import crypto from 'node:crypto'
|
||||||
|
|
||||||
import { logInfo, logWarn } from '../../../utils/logger.js'
|
import { logIntegration } from '../../../utils/logger.js'
|
||||||
import { getKuaishouIndustryConfig } from './config.js'
|
import { getKuaishouIndustryConfig } from './config.js'
|
||||||
import { ensureKuaishouIndustryAccessToken } from './token-service.js'
|
import { ensureKuaishouIndustryAccessToken } from './token-service.js'
|
||||||
|
|
||||||
@@ -32,7 +32,7 @@ export async function sendCallback(input: SendCallbackInput): Promise<{ success:
|
|||||||
let config = getKuaishouIndustryConfig()
|
let config = getKuaishouIndustryConfig()
|
||||||
|
|
||||||
if (!config.enabled) {
|
if (!config.enabled) {
|
||||||
logInfo('[kuaishou-industry/send-callback]', '行业电子凭证配置未启用,跳过发码回调')
|
logIntegration('[kuaishou-industry/send-callback]', '行业电子凭证配置未启用,跳过发货回调')
|
||||||
return { success: true }
|
return { success: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,15 +45,15 @@ export async function sendCallback(input: SendCallbackInput): Promise<{ success:
|
|||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const message = error instanceof Error ? error.message : String(error)
|
const message = error instanceof Error ? error.message : String(error)
|
||||||
logWarn('[kuaishou-industry/send-callback]', 'accessToken 刷新失败,无法发起发码回调', {
|
logIntegration('[kuaishou-industry/send-callback]', 'accessToken 刷新失败,无法发起发货回调', {
|
||||||
error: message,
|
error: message,
|
||||||
})
|
}, { level: 'warn' })
|
||||||
return { success: false, error: message }
|
return { success: false, error: message }
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!config.accessToken) {
|
if (!config.accessToken) {
|
||||||
const message = 'accessToken 未配置,无法发起发码回调'
|
const message = 'accessToken 未配置,无法发起发货回调'
|
||||||
logWarn('[kuaishou-industry/send-callback]', message)
|
logIntegration('[kuaishou-industry/send-callback]', message, undefined, { level: 'warn' })
|
||||||
return { success: false, error: message }
|
return { success: false, error: message }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,13 +102,14 @@ export async function sendCallback(input: SendCallbackInput): Promise<{ success:
|
|||||||
}
|
}
|
||||||
|
|
||||||
const url = `${resolveKuaishouOpenApiBaseUrl(config.baseUrl)}/integration/callback/virtual/eticket/send`
|
const url = `${resolveKuaishouOpenApiBaseUrl(config.baseUrl)}/integration/callback/virtual/eticket/send`
|
||||||
|
const requestLog = buildSendCallbackRequestLog({
|
||||||
logInfo('[kuaishou-industry/send-callback]', `发起发码回调 oid=${input.oid}`, {
|
|
||||||
url,
|
url,
|
||||||
oid: input.oid,
|
signParams,
|
||||||
sendNum: input.sendNum,
|
bizParams,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
logIntegration('[kuaishou-industry/send-callback]', `发起电子凭证发货回调 oid=${input.oid}`, requestLog)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const startedAt = Date.now()
|
const startedAt = Date.now()
|
||||||
const res = await fetch(url, {
|
const res = await fetch(url, {
|
||||||
@@ -124,26 +125,56 @@ export async function sendCallback(input: SendCallbackInput): Promise<{ success:
|
|||||||
const ok = res.ok && Number(json.result) === 1
|
const ok = res.ok && Number(json.result) === 1
|
||||||
|
|
||||||
if (ok) {
|
if (ok) {
|
||||||
logInfo('[kuaishou-industry/send-callback]', `发码回调成功 oid=${input.oid}`, {
|
logIntegration('[kuaishou-industry/send-callback]', `电子凭证发货回调成功 oid=${input.oid}`, {
|
||||||
durationMs,
|
durationMs,
|
||||||
|
status: res.status,
|
||||||
|
request: requestLog,
|
||||||
response: json,
|
response: json,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
logWarn('[kuaishou-industry/send-callback]', `发码回调失败 oid=${input.oid}`, {
|
logIntegration('[kuaishou-industry/send-callback]', `电子凭证发货回调失败 oid=${input.oid}`, {
|
||||||
durationMs,
|
durationMs,
|
||||||
status: res.status,
|
status: res.status,
|
||||||
|
request: requestLog,
|
||||||
response: json,
|
response: json,
|
||||||
})
|
}, { level: 'warn' })
|
||||||
}
|
}
|
||||||
|
|
||||||
return { success: ok, response: json }
|
return { success: ok, response: json }
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const message = err instanceof Error ? err.message : String(err)
|
const message = err instanceof Error ? err.message : String(err)
|
||||||
logWarn('[kuaishou-industry/send-callback]', `发码回调异常 oid=${input.oid}`, resolveCallbackErrorDetail(err))
|
logIntegration('[kuaishou-industry/send-callback]', `电子凭证发货回调异常 oid=${input.oid}`, {
|
||||||
|
...resolveCallbackErrorDetail(err),
|
||||||
|
request: requestLog,
|
||||||
|
}, { level: 'warn' })
|
||||||
return { success: false, error: message }
|
return { success: false, error: message }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function buildSendCallbackRequestLog({
|
||||||
|
url,
|
||||||
|
signParams,
|
||||||
|
bizParams,
|
||||||
|
}: {
|
||||||
|
url: string
|
||||||
|
signParams: JsonObject
|
||||||
|
bizParams: JsonObject
|
||||||
|
}): JsonObject {
|
||||||
|
return {
|
||||||
|
url,
|
||||||
|
method: 'POST',
|
||||||
|
contentType: 'application/x-www-form-urlencoded',
|
||||||
|
apiMethod: signParams.method,
|
||||||
|
appkey: signParams.appkey,
|
||||||
|
access_token: signParams.access_token,
|
||||||
|
version: signParams.version,
|
||||||
|
timestamp: signParams.timestamp,
|
||||||
|
signMethod: signParams.signMethod,
|
||||||
|
sign: signParams.sign,
|
||||||
|
param: bizParams,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function signPayload(params: JsonObject, signSecret: string): string {
|
function signPayload(params: JsonObject, signSecret: string): string {
|
||||||
const entries = Object.entries(params)
|
const entries = Object.entries(params)
|
||||||
.filter(([key]) => key !== 'sign' && key !== 'signSecret')
|
.filter(([key]) => key !== 'sign' && key !== 'signSecret')
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { listTasksByOrderId } from '../../../repositories/task-repo.js'
|
|||||||
import { upsertKuaishouIndustryVoucher } from '../../../repositories/kuaishou-industry-voucher-repo.js'
|
import { upsertKuaishouIndustryVoucher } from '../../../repositories/kuaishou-industry-voucher-repo.js'
|
||||||
import { OPEN_91_PLATFORM, OPEN_91_PROVIDER } from '../../open-91/config.js'
|
import { OPEN_91_PLATFORM, OPEN_91_PROVIDER } from '../../open-91/config.js'
|
||||||
import { normalizeTimestampIso } from '../../../utils/time.js'
|
import { normalizeTimestampIso } from '../../../utils/time.js'
|
||||||
import { logWarn } from '../../../utils/logger.js'
|
import { logIntegration } from '../../../utils/logger.js'
|
||||||
import {
|
import {
|
||||||
getKuaishouIndustryConfig,
|
getKuaishouIndustryConfig,
|
||||||
assertMatchingAppKey,
|
assertMatchingAppKey,
|
||||||
@@ -119,28 +119,47 @@ function fireSendCallback(input: {
|
|||||||
num: Number(e.num) || 1,
|
num: Number(e.num) || 1,
|
||||||
validStartTime: Number(e.validStartTime) || 0,
|
validStartTime: Number(e.validStartTime) || 0,
|
||||||
validEndTime: Number(e.validEndTime) || 0,
|
validEndTime: Number(e.validEndTime) || 0,
|
||||||
|
goodsValue: Number(e.goodsValue) || 0,
|
||||||
}))
|
}))
|
||||||
|
const sendNum = eticketItems.reduce((sum, e) => sum + e.num, 0)
|
||||||
|
const totalGoodsValue = eticketItems.reduce((sum, e) => sum + e.goodsValue, 0)
|
||||||
|
|
||||||
|
logIntegration('[kuaishou-industry/send-code]', '电子凭证发货回调已调度', {
|
||||||
|
oid: input.oid,
|
||||||
|
sendType: input.sendType,
|
||||||
|
sendNum,
|
||||||
|
totalGoodsValue,
|
||||||
|
etickets: eticketItems,
|
||||||
|
eticketType: input.eticketType || '',
|
||||||
|
})
|
||||||
|
|
||||||
sendCallback({
|
sendCallback({
|
||||||
oid: input.oid,
|
oid: input.oid,
|
||||||
sendType: input.sendType,
|
sendType: input.sendType,
|
||||||
etickets: eticketItems,
|
etickets: eticketItems,
|
||||||
sendNum: eticketItems.reduce((sum, e) => sum + e.num, 0),
|
sendNum,
|
||||||
|
totalGoodsValue,
|
||||||
token: input.token,
|
token: input.token,
|
||||||
...(input.eticketType ? { eticketType: input.eticketType } : {}),
|
...(input.eticketType ? { eticketType: input.eticketType } : {}),
|
||||||
...(input.ext ? { ext: input.ext } : {}),
|
...(input.ext ? { ext: input.ext } : {}),
|
||||||
}).then((result) => {
|
}).then((result) => {
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
logWarn('[kuaishou-industry/send-code]', '发码回调异步执行失败', {
|
logIntegration('[kuaishou-industry/send-code]', '电子凭证发货回调异步执行失败', {
|
||||||
oid: input.oid,
|
oid: input.oid,
|
||||||
error: result.error || '',
|
error: result.error || '',
|
||||||
response: result.response || null,
|
response: result.response || null,
|
||||||
})
|
}, { level: 'warn' })
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logIntegration('[kuaishou-industry/send-code]', '电子凭证发货回调异步执行完成', {
|
||||||
|
oid: input.oid,
|
||||||
|
response: result.response || null,
|
||||||
|
})
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
logWarn('[kuaishou-industry/send-code]', '发码回调异步执行异常', {
|
logIntegration('[kuaishou-industry/send-code]', '电子凭证发货回调异步执行异常', {
|
||||||
oid: input.oid,
|
oid: input.oid,
|
||||||
error: err instanceof Error ? err.message : String(err),
|
error: err instanceof Error ? err.message : String(err),
|
||||||
})
|
}, { level: 'warn' })
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user