From 54511665edef6712685d6bd7ea5c417417d80d77 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Tue, 20 Oct 2026 17:13:09 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E9=80=80=E5=9B=9E=E5=AF=B9=E6=8E=A5):=20?= =?UTF-8?q?=E9=80=80=E5=9B=9E=E9=80=9A=E7=9F=A5=E5=A4=8D=E7=94=A8=E6=97=A2?= =?UTF-8?q?=E6=9C=89=20webhook=EF=BC=88X-Signature=EF=BC=89=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E4=BA=8B=E4=BB=B6=20preparations.returned=E2=80=94?= =?UTF-8?q?=E2=80=94=E5=AF=B9=E6=96=B9=E9=9B=B6=E6=96=B0=E5=A2=9E=E5=87=AD?= =?UTF-8?q?=E6=8D=AE/=E7=AB=AF=E7=82=B9=EF=BC=8C=E8=90=BD=E5=94=AE?= =?UTF-8?q?=E5=90=8E+=E7=94=9F=E6=88=90=E8=A1=A5=E9=80=81=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/routes/mainSystemIntegration.ts | 51 ++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/server/src/routes/mainSystemIntegration.ts b/server/src/routes/mainSystemIntegration.ts index 083a856..b9eb401 100644 --- a/server/src/routes/mainSystemIntegration.ts +++ b/server/src/routes/mainSystemIntegration.ts @@ -557,6 +557,57 @@ router.post('/main-system/webhook', async (req: Request, res: Response) => { * · 业务失败(如零价被拦)**返回 200** —— 事件已持久化,重试由我方对账负责, * 返 5xx 只会让主系统重推,而重推解决不了"我方前置未满足"。 */ + /* ── 主系统「商品退回」(2026-10-20)──────────────────────────────────── + * 通道与 `binding_changed` 完全一致:复用既有 webhook URL + `X-Signature` 验签, + * **对方无需新增端点/密钥/账号**;事件名 `preparations.returned`。 + * 我方处理:落售后记录 + 立即生成补送单(幂等键 = main_return_code + sku)。 + * 业务失败返回 200(事件已收,重试交给我方台账轮询兜底)。 + */ + if (event === 'preparations.returned') { + const rawRows = Array.isArray(payload?.data) ? payload.data : [payload]; + const results: Array<{ prepId: string; ok: boolean; duplicated?: boolean; resupplyOrderCode?: string | null; error?: string }> = []; + const { ingestMainSystemReturn, resolveOrderCodeByPrepId } = await import('../services/mainSystemReturnService'); + for (const raw of rawRows) { + const row = (raw || {}) as Record; + const prepId = String(row.id || row.preparation_id || row.prep_id || '').trim(); + if (!prepId) continue; + const ourOrderCode = + String(row.our_order_code || '').trim() || + (branchCode ? await resolveOrderCodeByPrepId({ branchCode, prepId }) : null) || + ''; + if (!ourOrderCode) { + results.push({ prepId, ok: false, error: 'order_not_found' }); + continue; + } + const quantity = Number(row.return_qty ?? row.qty ?? row.quantity); + const skuCode = String(row.sku_code || '').trim(); + if (!(quantity > 0) || !skuCode) { + results.push({ prepId, ok: false, error: 'invalid_payload' }); + continue; + } + try { + const out = await ingestMainSystemReturn({ + branchCode, + mainReturnCode: row.return_code ? String(row.return_code) : null, + mainOrderCode: row.order_code ? String(row.order_code) : String(payload?.order_code || ''), + ourOrderCode, + mainPrepId: prepId, + skuCode, + itemName: row.item_name ? String(row.item_name) : null, + quantity, + unit: row.unit ? String(row.unit) : null, + reason: row.reason ? String(row.reason) : null, + returnedAt: row.returned_at ? String(row.returned_at) : null, + }); + results.push({ prepId, ok: true, duplicated: out.duplicated, resupplyOrderCode: out.resupplyOrderCode }); + } catch (error) { + results.push({ prepId, ok: false, error: (error as Error)?.message || 'failed' }); + } + } + console.log(`[main-system-webhook] ${branchCode} preparations.returned:处理 ${results.length} 行,成功 ${results.filter((r) => r.ok).length}`); + return res.json({ ok: true, results }); + } + if (isFulfillmentEvent(event)) { if (!isFulfillmentEventsEnabled()) { // 开关关闭 ⇒ 与改造前逐字节一致:200 收下但忽略