multivendor-wallet 0.0.10 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -118,6 +118,24 @@ function createOrderHooks(runtime) {
118
118
  }
119
119
  __name(createOrderHooks, "createOrderHooks");
120
120
 
121
+ // src/errors.ts
122
+ var _WalletError = class _WalletError extends Error {
123
+ constructor(message) {
124
+ super(message);
125
+ this.name = "WalletError";
126
+ }
127
+ };
128
+ __name(_WalletError, "WalletError");
129
+ var WalletError = _WalletError;
130
+ var _HoldNotFoundError = class _HoldNotFoundError extends WalletError {
131
+ constructor(message = "Active hold not found for reference") {
132
+ super(message);
133
+ this.name = "HoldNotFoundError";
134
+ }
135
+ };
136
+ __name(_HoldNotFoundError, "HoldNotFoundError");
137
+ var HoldNotFoundError = _HoldNotFoundError;
138
+
121
139
  // src/hooks/refund.ts
122
140
  function isWalletPayment(paymentMethod) {
123
141
  return String(paymentMethod ?? "").trim().toLowerCase() === "wallet";
@@ -135,6 +153,32 @@ function idempotencyPrefix(ctx) {
135
153
  return ctx.refundReason === "return" ? `return:${scope}` : `refund:${scope}`;
136
154
  }
137
155
  __name(idempotencyPrefix, "idempotencyPrefix");
156
+ function isHoldNotFound(error) {
157
+ if (error instanceof HoldNotFoundError) {
158
+ return true;
159
+ }
160
+ return error instanceof Error && error.name === "HoldNotFoundError";
161
+ }
162
+ __name(isHoldNotFound, "isHoldNotFound");
163
+ async function cancelEscrowHoldOrSkip(client, params) {
164
+ try {
165
+ await client.cancelHold({
166
+ reference: {
167
+ referenceType: "vendor_order",
168
+ referenceId: params.referenceId
169
+ },
170
+ idempotencyKey: params.idempotencyKey,
171
+ ...params.amount != null ? {
172
+ amount: params.amount
173
+ } : {}
174
+ });
175
+ } catch (error) {
176
+ if (!isHoldNotFound(error)) {
177
+ throw error;
178
+ }
179
+ }
180
+ }
181
+ __name(cancelEscrowHoldOrSkip, "cancelEscrowHoldOrSkip");
138
182
  function createRefundHooks(runtime) {
139
183
  const { client, platformOwnerId, flags } = runtime;
140
184
  return {
@@ -148,21 +192,15 @@ function createRefundHooks(runtime) {
148
192
  if (ctx.sellerNetClawbackRupees != null) {
149
193
  const clawbackPaise = toMinorUnit(ctx.sellerNetClawbackRupees);
150
194
  if (clawbackPaise > 0) {
151
- await client.cancelHold({
152
- reference: {
153
- referenceType: "vendor_order",
154
- referenceId: String(ctx.vendorOrderId)
155
- },
195
+ await cancelEscrowHoldOrSkip(client, {
196
+ referenceId: String(ctx.vendorOrderId),
156
197
  idempotencyKey: `${keyPrefix}:cancel_hold`,
157
198
  amount: clawbackPaise
158
199
  });
159
200
  }
160
201
  } else {
161
- await client.cancelHold({
162
- reference: {
163
- referenceType: "vendor_order",
164
- referenceId: String(ctx.vendorOrderId)
165
- },
202
+ await cancelEscrowHoldOrSkip(client, {
203
+ referenceId: String(ctx.vendorOrderId),
166
204
  idempotencyKey: `${keyPrefix}:cancel_hold`
167
205
  });
168
206
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/currency.ts","../../src/orderCustomerDebit.ts","../../src/types.ts","../../src/hooks/order.ts","../../src/hooks/refund.ts","../../src/hooks/settlement.ts","../../src/plugin-instance.ts","../../src/timestamps.ts","../../src/typeorm/entities.ts","../../src/hooks/reconciliation.ts","../../src/hooks/index.ts"],"names":["toMinorUnit","amount","Math","round","fromMinorUnit","CUSTOMER_CHECKOUT_DEBIT_PURPOSE","orderCustomerDebitIdempotencyKey","orderId","WalletAccountType","isWalletOnlyPayment","paymentMethod","String","trim","toLowerCase","createOrderHooks","runtime","client","platformOwnerId","flags","onOrderPaid","ctx","enabled","ledgerWrites","txCtx","nativeTx","platform","getOrCreateAccount","PLATFORM","currency","orderTotalPaise","orderTotalRupees","shouldDebitCustomer","customerId","skipCustomerDebit","debitRupees","min","walletAmountRupees","customer","CUSTOMER","debit","accountId","id","idempotencyKey","reference","referenceType","referenceId","metadata","purpose","paymentNumber","credit","line","lines","vendorNetRupees","sellerNetRupees","Number","isFinite","commissionBaseRupees","commissionRupees","vendor","VENDOR","vendorId","hold","sourceAccountId","targetAccountId","vendorOrderId","onLineDelivered","release","undefined","isWalletPayment","walletCreditRupees","walletRefundRupees","lineTotalRupees","idempotencyPrefix","scope","idempotencyScope","orderProductId","refundReason","createRefundHooks","onRefund","processExternalRefund","keyPrefix","skipEscrow","sellerNetClawbackRupees","clawbackPaise","cancelHold","refundPaise","balance","getBalance","available","creditRupees","createSettlementHooks","_runtime","onSettlementDebit","_ctx","resolveWalletConnection","Error","pad2","value","padStart","nowTimestamp","date","Date","getFullYear","getMonth","getDate","getHours","getMinutes","getSeconds","WalletAccountEntity","ownerType","ownerId","availableBalance","heldBalance","createdBy","createdDate","modifiedBy","modifiedDate","setCreated","setModified","name","length","default","type","nullable","WalletLedgerEntryEntity","entryType","balanceAfterAvailable","balanceAfterHeld","unique","WalletHoldEntity","status","runWalletReconciliation","parityDeltas","findVendorPaymentHoldParityDeltas","cacheMismatches","findCacheLedgerMismatches","rows","query","deltas","row","vendorPaymentRupees","vendorPaymentAmount","holdPaise","holdAmount","holdRupees","delta","abs","holdStatus","push","deltaRupees","connection","accounts","getRepository","find","mismatches","account","lastEntry","findOne","where","order","cachedAvailable","cachedHeld","ledgerAvailable","ledgerHeld","createLifecycleHooks"],"mappings":";;;;;;;;;;AAGO,SAASA,YAAYC,MAAAA,EAAc;AACxC,EAAA,OAAOC,IAAAA,CAAKC,KAAAA,CAAMF,MAAAA,GAAS,GAAA,CAAA;AAC7B;AAFgBD,MAAAA,CAAAA,WAAAA,EAAAA,aAAAA,CAAAA;AAOT,SAASI,cAAcH,MAAAA,EAAc;AAC1C,EAAA,OAAOA,MAAAA,GAAS,GAAA;AAClB;AAFgBG,MAAAA,CAAAA,aAAAA,EAAAA,eAAAA,CAAAA;;;ACVT,IAAMC,+BAAAA,GAAkC,yBAAA;AAKxC,SAASC,iCAAiCC,OAAAA,EAAwB;AACvE,EAAA,OAAO,gBAAgBA,OAAAA,CAAAA,eAA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G,MAAAA,CAAOqG,OAAAA,CAAQ5C,gBAAgB,CAAA;AACvD,IAAA,MAAMkD,UAAAA,GAAa3G,MAAAA,CAAOqG,OAAAA,CAAQ3C,WAAW,CAAA;AAC7C,IAAA,MAAMkD,eAAAA,GAAkB5G,MAAAA,CAAOsG,SAAAA,CAAU9B,qBAAqB,CAAA;AAC9D,IAAA,MAAMqC,UAAAA,GAAa7G,MAAAA,CAAOsG,SAAAA,CAAU7B,gBAAgB,CAAA;AACpD,IAAA,IAAIiC,eAAAA,KAAoBE,eAAAA,IAAmBD,UAAAA,KAAeE,UAAAA,EAAY;AACpET,MAAAA,UAAAA,CAAWN,IAAAA,CAAK;AACd5G,QAAAA,SAAAA,EAAWmH,OAAAA,CAAQlH,EAAAA;AACnBuH,QAAAA,eAAAA;AACAC,QAAAA,UAAAA;AACAC,QAAAA,eAAAA;AACAC,QAAAA;OACF,CAAA;AACF,IAAA;AACF,EAAA;AACA,EAAA,OAAOT,UAAAA;AACT;AAhCenB,MAAAA,CAAAA,yBAAAA,EAAAA,2BAAAA,CAAAA;;;AC5DR,SAAS6B,qBAAqBrJ,OAAAA,EAAoB;AACvD,EAAA,OAAO;AACL,IAAA,GAAGD,iBAAiBC,OAAAA,CAAAA;AACpB,IAAA,GAAG2E,sBAAsB3E,CAAAA;AACzB,IAAA,GAAG8D,kBAAkB9D,OAAAA;AACvB,GAAA;AACF;AANgBqJ,MAAAA,CAAAA,oBAAAA,EAAAA,sBAAAA,CAAAA","file":"index.cjs","sourcesContent":["/**\n * Convert decimal currency amount (e.g. rupees) to smallest unit (paise).\n */\nexport function toMinorUnit(amount: number): number {\n return Math.round(amount * 100);\n}\n\n/**\n * Convert smallest currency unit (paise) back to decimal amount (rupees).\n */\nexport function fromMinorUnit(amount: number): number {\n return amount / 100;\n}\n","export const CUSTOMER_CHECKOUT_DEBIT_PURPOSE = 'customer_checkout_debit';\nexport const CUSTOMER_CHECKOUT_DEBIT_REVERSAL_PURPOSE = 'customer_checkout_debit_reversal';\nexport const BACKFILL_SENTINEL_KEY = 'customer_checkout_debit_backfill';\nexport const WALLET_OPS_TABLE = 'wallet_ops';\n\nexport function orderCustomerDebitIdempotencyKey(orderId: number | string): string {\n return `wallet:order:${orderId}:customer_debit`;\n}\n\nexport function paymentReversalIdempotencyKey(paymentNumber: string): string {\n return `wallet:payment-reversal:${paymentNumber}`;\n}\n\nexport function paymentNumberFromWalletPaymentKey(idempotencyKey: string): string | null {\n const prefix = 'wallet:payment:';\n if (!idempotencyKey.startsWith(prefix)) {\n return null;\n }\n return idempotencyKey.slice(prefix.length);\n}\n\n/**\n * True when this debit is a customer checkout (product-amount) debit for an order.\n * Settlement/vendor/affiliate debits must not use these keys or purpose.\n */\nexport function isCustomerCheckoutDebit(params: {\n idempotencyKey: string;\n referenceType?: string;\n purpose?: unknown;\n}): boolean {\n if (params.purpose === CUSTOMER_CHECKOUT_DEBIT_PURPOSE) {\n return true;\n }\n const key = params.idempotencyKey;\n if (key.startsWith('wallet:order:') && key.endsWith(':customer_debit')) {\n return true;\n }\n if (params.referenceType !== 'order') {\n return false;\n }\n return isLegacyCustomerCheckoutDebitKey(key);\n}\n\n/**\n * Legacy keys accepted during rollout only.\n * Sunset: `npm run wallet:legacy-order-debit-sunset` — drop this helper when LEGACY_ORDER_DEBIT_SUNSET_SQL returns 0 on prod.\n */\nexport function isLegacyCustomerCheckoutDebitKey(key: string): boolean {\n return (\n key.startsWith('wallet:full:') ||\n key.startsWith('wallet:split:') ||\n key.startsWith('wallet:payment:')\n );\n}\n\n/** Unreimbursed full/split + wallet:payment twins (duplicate customer checkout debits). */\nexport const UNREIMBURSED_TWIN_COUNT_SQL = `\nSELECT COUNT(*) AS c\nFROM wallet_ledger_entry f\nJOIN wallet_ledger_entry p\n ON p.account_id = f.account_id\n AND p.reference_type = 'order'\n AND p.reference_id = f.reference_id\n AND p.entry_type = 'debit'\n AND p.idempotency_key LIKE 'wallet:payment:%'\nWHERE f.entry_type = 'debit'\n AND f.reference_type = 'order'\n AND (\n f.idempotency_key LIKE 'wallet:full:%'\n OR f.idempotency_key LIKE 'wallet:split:%'\n )\n AND NOT EXISTS (\n SELECT 1 FROM wallet_ledger_entry r\n WHERE r.entry_type = 'credit'\n AND r.account_id = p.account_id\n AND r.reference_type = 'order'\n AND r.reference_id = p.reference_id\n AND (\n r.idempotency_key = CONCAT('wallet:payment-reversal:', SUBSTRING(p.idempotency_key, 17))\n OR JSON_UNQUOTE(JSON_EXTRACT(r.metadata, '$.purpose')) = 'customer_checkout_debit_reversal'\n )\n )\n`;\n\nexport const UNREIMBURSED_TWIN_DETAIL_SQL = `\nSELECT\n f.reference_id AS order_id,\n f.account_id,\n f.amount AS first_paise,\n p.amount AS second_paise,\n f.idempotency_key AS kept_key,\n p.idempotency_key AS duplicate_key,\n f.created_date AS first_at,\n p.created_date AS duplicate_at,\n f.id AS kept_id,\n p.id AS duplicate_id\nFROM wallet_ledger_entry f\nJOIN wallet_ledger_entry p\n ON p.account_id = f.account_id\n AND p.reference_type = 'order'\n AND p.reference_id = f.reference_id\n AND p.entry_type = 'debit'\n AND p.idempotency_key LIKE 'wallet:payment:%'\nWHERE f.entry_type = 'debit'\n AND f.reference_type = 'order'\n AND (\n f.idempotency_key LIKE 'wallet:full:%'\n OR f.idempotency_key LIKE 'wallet:split:%'\n )\n AND NOT EXISTS (\n SELECT 1 FROM wallet_ledger_entry r\n WHERE r.entry_type = 'credit'\n AND r.account_id = p.account_id\n AND r.reference_type = 'order'\n AND r.reference_id = p.reference_id\n AND (\n r.idempotency_key = CONCAT('wallet:payment-reversal:', SUBSTRING(p.idempotency_key, 17))\n OR JSON_UNQUOTE(JSON_EXTRACT(r.metadata, '$.purpose')) = 'customer_checkout_debit_reversal'\n )\n )\n`;\n\n/**\n * Exit condition for removing isLegacyCustomerCheckoutDebitKey from debit lookup.\n * File the sunset PR the same week this count hits 0 on prod.\n */\nexport const LEGACY_ORDER_DEBIT_SUNSET_SQL = `\nSELECT COUNT(*) AS c\nFROM wallet_ledger_entry\nWHERE entry_type = 'debit'\n AND reference_type = 'order'\n AND (\n idempotency_key LIKE 'wallet:full:%'\n OR idempotency_key LIKE 'wallet:split:%'\n OR idempotency_key LIKE 'wallet:payment:%'\n )\n`;\n","export enum WalletAccountType {\n CUSTOMER = 'customer',\n VENDOR = 'vendor',\n PLATFORM = 'platform',\n /** Investor cash wallet (interest/bonus/withdrawable). */\n INVESTOR = 'investor',\n /** Platform liability balancing investor principal/returns. */\n PLATFORM_INVESTOR_LIABILITY = 'platform_investor_liability',\n /** TDS withheld on investor returns pending remittance. */\n PLATFORM_TDS_PAYABLE = 'platform_tds_payable',\n /** GST TCS collected from sellers pending remittance. */\n PLATFORM_GST_TCS_PAYABLE = 'platform_gst_tcs_payable',\n /** IT TDS withheld from seller settlements pending remittance. */\n PLATFORM_IT_TDS_PAYABLE = 'platform_it_tds_payable',\n /** TDS withheld from affiliate commissions pending remittance. */\n PLATFORM_AFFILIATE_TDS_PAYABLE = 'platform_affiliate_tds_payable',\n}\n\nexport enum LedgerEntryType {\n CREDIT = 'credit',\n DEBIT = 'debit',\n HOLD = 'hold',\n RELEASE = 'release',\n CANCEL_HOLD = 'cancel_hold',\n TRANSFER = 'transfer',\n}\n\nexport enum HoldStatus {\n ACTIVE = 'active',\n RELEASED = 'released',\n CANCELLED = 'cancelled',\n}\n\nexport interface WalletReference {\n referenceType: string;\n referenceId: string;\n}\n\nexport interface WalletAccount {\n id: number;\n ownerType: WalletAccountType;\n ownerId: number;\n currency: string;\n availableBalance: number;\n heldBalance: number;\n}\n\nexport interface WalletBalance {\n available: number;\n held: number;\n total: number;\n}\n\nexport interface WalletLedgerEntry {\n id: number;\n accountId: number;\n entryType: LedgerEntryType;\n amount: number;\n balanceAfterAvailable: number;\n balanceAfterHeld: number;\n idempotencyKey: string;\n referenceType?: string;\n referenceId?: string;\n metadata?: Record<string, unknown>;\n createdAt: Date;\n}\n\nexport interface WalletHold {\n id: number;\n sourceAccountId: number;\n targetAccountId: number;\n amount: number;\n status: HoldStatus;\n referenceType: string;\n referenceId: string;\n idempotencyKey: string;\n createdAt: Date;\n}\n\nexport interface BalanceDeltas {\n availableDelta?: number;\n heldDelta?: number;\n}\n\nexport interface CreditParams {\n accountId: number;\n amount: number;\n idempotencyKey: string;\n reference?: WalletReference;\n metadata?: Record<string, unknown>;\n}\n\nexport interface DebitParams {\n accountId: number;\n amount: number;\n idempotencyKey: string;\n reference?: WalletReference;\n metadata?: Record<string, unknown>;\n}\n\nexport interface HoldParams {\n sourceAccountId: number;\n targetAccountId: number;\n amount: number;\n idempotencyKey: string;\n reference: WalletReference;\n metadata?: Record<string, unknown>;\n}\n\nexport interface ReleaseParams {\n reference: WalletReference;\n idempotencyKey: string;\n metadata?: Record<string, unknown>;\n}\n\nexport interface CancelHoldParams {\n reference: WalletReference;\n idempotencyKey: string;\n metadata?: Record<string, unknown>;\n /** When set and less than the active hold, reduce the hold instead of cancelling it. */\n amount?: number;\n}\n\nexport interface TransferParams {\n fromAccountId: number;\n toAccountId: number;\n amount: number;\n idempotencyKey: string;\n reference?: WalletReference;\n metadata?: Record<string, unknown>;\n}\n\nexport interface GetTransactionsParams {\n accountId: number;\n limit?: number;\n offset?: number;\n}\n\nexport interface LedgerMutationResult {\n entry: WalletLedgerEntry;\n account: WalletAccount;\n hold?: WalletHold;\n}\n","import { toMinorUnit } from '../currency';\nimport {\n CUSTOMER_CHECKOUT_DEBIT_PURPOSE,\n orderCustomerDebitIdempotencyKey,\n} from '../orderCustomerDebit';\nimport { WalletAccountType } from '../types';\nimport type { DeliveredContext, HookRuntime, OrderPaidContext, WalletLifecycleHooks } from './types';\n\nfunction isWalletOnlyPayment(paymentMethod?: string): boolean {\n return String(paymentMethod ?? '').trim().toLowerCase() === 'wallet';\n}\n\nexport function createOrderHooks(runtime: HookRuntime): Pick<\n WalletLifecycleHooks,\n 'onOrderPaid' | 'onLineDelivered'\n> {\n const { client, platformOwnerId, flags } = runtime;\n\n return {\n async onOrderPaid(ctx: OrderPaidContext): Promise<void> {\n if (!flags.enabled || !flags.ledgerWrites) {\n return;\n }\n\n const txCtx = { nativeTx: ctx.nativeTx };\n const platform = await client.getOrCreateAccount(\n WalletAccountType.PLATFORM,\n platformOwnerId,\n ctx.currency,\n txCtx\n );\n\n const orderTotalPaise = toMinorUnit(ctx.orderTotalRupees);\n const shouldDebitCustomer =\n isWalletOnlyPayment(ctx.paymentMethod) &&\n ctx.customerId &&\n !ctx.skipCustomerDebit;\n\n if (shouldDebitCustomer) {\n const debitRupees = Math.min(\n ctx.walletAmountRupees ?? ctx.orderTotalRupees,\n ctx.orderTotalRupees\n );\n const customer = await client.getOrCreateAccount(\n WalletAccountType.CUSTOMER,\n ctx.customerId,\n ctx.currency,\n txCtx\n );\n await client.debit(\n {\n accountId: customer.id,\n amount: toMinorUnit(debitRupees),\n idempotencyKey: orderCustomerDebitIdempotencyKey(ctx.orderId),\n reference: { referenceType: 'order', referenceId: String(ctx.orderId) },\n metadata: { purpose: CUSTOMER_CHECKOUT_DEBIT_PURPOSE, paymentNumber: ctx.paymentNumber },\n },\n txCtx\n );\n }\n\n await client.credit(\n {\n accountId: platform.id,\n amount: orderTotalPaise,\n idempotencyKey: `payment:${ctx.paymentNumber}`,\n reference: { referenceType: 'order', referenceId: String(ctx.orderId) },\n },\n txCtx\n );\n\n for (const line of ctx.lines) {\n const vendorNetRupees =\n line.sellerNetRupees != null && Number.isFinite(Number(line.sellerNetRupees))\n ? Number(line.sellerNetRupees)\n : line.commissionBaseRupees - line.commissionRupees;\n if (vendorNetRupees <= 0) {\n continue;\n }\n\n const vendor = await client.getOrCreateAccount(\n WalletAccountType.VENDOR,\n line.vendorId,\n ctx.currency,\n txCtx\n );\n\n await client.hold(\n {\n sourceAccountId: platform.id,\n targetAccountId: vendor.id,\n amount: toMinorUnit(vendorNetRupees),\n idempotencyKey: `order:${ctx.orderId}:hold:vendorOrder:${line.vendorOrderId}`,\n reference: {\n referenceType: 'vendor_order',\n referenceId: String(line.vendorOrderId),\n },\n },\n txCtx\n );\n }\n },\n\n async onLineDelivered(ctx: DeliveredContext): Promise<void> {\n if (!flags.enabled || !flags.ledgerWrites) {\n return;\n }\n\n await client.release(\n {\n reference: { referenceType: 'vendor_order', referenceId: String(ctx.vendorOrderId) },\n idempotencyKey: `vendorOrder:${ctx.vendorOrderId}:release`,\n },\n ctx.nativeTx != null ? { nativeTx: ctx.nativeTx } : undefined\n );\n },\n };\n}\n","import { toMinorUnit } from '../currency';\nimport { WalletAccountType } from '../types';\nimport type { HookRuntime, RefundContext, WalletLifecycleHooks } from './types';\n\nfunction isWalletPayment(paymentMethod?: string): boolean {\n return String(paymentMethod ?? '').trim().toLowerCase() === 'wallet';\n}\n\nfunction walletCreditRupees(ctx: RefundContext): number {\n if (ctx.walletRefundRupees != null && ctx.walletRefundRupees > 0) {\n return ctx.walletRefundRupees;\n }\n return isWalletPayment(ctx.paymentMethod) ? ctx.lineTotalRupees : 0;\n}\n\nfunction idempotencyPrefix(ctx: RefundContext): string {\n const scope = ctx.idempotencyScope ?? String(ctx.orderProductId);\n return ctx.refundReason === 'return' ? `return:${scope}` : `refund:${scope}`;\n}\n\nexport function createRefundHooks(runtime: HookRuntime): Pick<WalletLifecycleHooks, 'onRefund'> {\n const { client, platformOwnerId, flags } = runtime;\n\n return {\n async onRefund(ctx: RefundContext): Promise<void> {\n if (!flags.enabled || !flags.ledgerWrites) {\n await ctx.processExternalRefund();\n return;\n }\n\n const keyPrefix = idempotencyPrefix(ctx);\n\n if (!ctx.skipEscrow) {\n if (ctx.sellerNetClawbackRupees != null) {\n const clawbackPaise = toMinorUnit(ctx.sellerNetClawbackRupees);\n if (clawbackPaise > 0) {\n await client.cancelHold({\n reference: {\n referenceType: 'vendor_order',\n referenceId: String(ctx.vendorOrderId),\n },\n idempotencyKey: `${keyPrefix}:cancel_hold`,\n amount: clawbackPaise,\n });\n }\n } else {\n await client.cancelHold({\n reference: {\n referenceType: 'vendor_order',\n referenceId: String(ctx.vendorOrderId),\n },\n idempotencyKey: `${keyPrefix}:cancel_hold`,\n });\n }\n }\n\n const refundPaise = toMinorUnit(ctx.lineTotalRupees);\n if (refundPaise > 0) {\n const platform = await client.getOrCreateAccount(\n WalletAccountType.PLATFORM,\n platformOwnerId,\n ctx.currency\n );\n const balance = await client.getBalance(platform.id);\n if (balance.available >= refundPaise) {\n await client.debit({\n accountId: platform.id,\n amount: refundPaise,\n idempotencyKey: `${keyPrefix}:platform_debit`,\n reference: {\n referenceType: 'order_product',\n referenceId: String(ctx.orderProductId),\n },\n });\n }\n\n const creditRupees = walletCreditRupees(ctx);\n if (creditRupees > 0 && ctx.customerId) {\n const customer = await client.getOrCreateAccount(\n WalletAccountType.CUSTOMER,\n ctx.customerId,\n ctx.currency\n );\n await client.credit({\n accountId: customer.id,\n amount: toMinorUnit(creditRupees),\n idempotencyKey: `${keyPrefix}:customer_credit`,\n reference: {\n referenceType: 'order_product',\n referenceId: String(ctx.orderProductId),\n },\n });\n }\n }\n\n await ctx.processExternalRefund();\n },\n };\n}\n","import type { HookRuntime, SettlementDebitContext, WalletLifecycleHooks } from './types';\n\nexport function createSettlementHooks(\n _runtime: HookRuntime\n): Pick<WalletLifecycleHooks, 'onSettlementDebit'> {\n return {\n /** @deprecated Settlement wallet debits removed — vendor payouts use wallet payout requests. */\n async onSettlementDebit(_ctx: SettlementDebitContext): Promise<void> {\n return;\n },\n };\n}\n","import type { WalletPlugin } from './plugin-types';\n\nexport interface WalletHostConnection {\n manager: {\n transaction<T>(fn: (em: unknown) => Promise<T>): Promise<T>;\n };\n query(query: string, parameters?: unknown[]): Promise<unknown>;\n getRepository(entity: unknown): {\n find(options?: unknown): Promise<unknown[]>;\n findOne(options?: unknown): Promise<unknown | null>;\n };\n}\n\nlet activePlugin: WalletPlugin | undefined;\nlet activeConnection: WalletHostConnection | undefined;\n\nexport function bindWalletPlugin(plugin: WalletPlugin): void {\n activePlugin = plugin;\n}\n\nexport function bindWalletConnection(connection: unknown): void {\n activeConnection = connection as WalletHostConnection;\n}\n\nexport function resolveWalletPlugin(): WalletPlugin {\n if (!activePlugin) {\n throw new Error('Wallet plugin is not registered. Call registerWalletPlugin() during app bootstrap.');\n }\n return activePlugin;\n}\n\nexport function resolveWalletConnection(): WalletHostConnection {\n if (!activeConnection?.manager) {\n throw new Error('Wallet database connection is not registered. Pass host TypeORM Connection as dataSource.');\n }\n return activeConnection;\n}\n\nexport function clearWalletPluginForTests(): void {\n activePlugin = undefined;\n activeConnection = undefined;\n}\n","function pad2(value: number): string {\n return String(value).padStart(2, '0');\n}\n\n/** Server-local wall clock `YYYY-MM-DD HH:mm:ss` (matches host order paidDate, not UTC ISO). */\nexport function nowTimestamp(date: Date = new Date()): string {\n return `${date.getFullYear()}-${pad2(date.getMonth() + 1)}-${pad2(date.getDate())} ${pad2(date.getHours())}:${pad2(date.getMinutes())}:${pad2(date.getSeconds())}`;\n}\n\n/** Parse naive datetime as local wall clock (do not treat as UTC). */\nexport function parseLedgerDate(value: string | Date): Date {\n if (value instanceof Date) {\n return value;\n }\n const match = /^(\\d{4})-(\\d{2})-(\\d{2})[ T](\\d{2}):(\\d{2}):(\\d{2})/.exec(String(value));\n if (!match) {\n return new Date(value);\n }\n return new Date(\n Number(match[1]),\n Number(match[2]) - 1,\n Number(match[3]),\n Number(match[4]),\n Number(match[5]),\n Number(match[6])\n );\n}\n","import { BeforeInsert, BeforeUpdate, Column, Entity, PrimaryGeneratedColumn } from 'typeorm';\nimport { nowTimestamp } from '../timestamps';\n\n@Entity('wallet_account')\nexport class WalletAccountEntity {\n @PrimaryGeneratedColumn({ name: 'id' })\n public id!: number;\n\n @Column({ name: 'owner_type', length: 32 })\n public ownerType!: string;\n\n @Column({ name: 'owner_id' })\n public ownerId!: number;\n\n @Column({ name: 'currency', length: 8, default: 'INR' })\n public currency!: string;\n\n @Column({ name: 'available_balance', type: 'bigint', default: 0 })\n public availableBalance!: string;\n\n @Column({ name: 'held_balance', type: 'bigint', default: 0 })\n public heldBalance!: string;\n\n @Column({ name: 'created_by', nullable: true })\n public createdBy?: number;\n\n @Column({ name: 'created_date', nullable: true })\n public createdDate!: string;\n\n @Column({ name: 'modified_by', nullable: true })\n public modifiedBy?: number;\n\n @Column({ name: 'modified_date', nullable: true })\n public modifiedDate!: string;\n\n @BeforeInsert()\n public setCreated(): void {\n this.createdDate = nowTimestamp();\n this.modifiedDate = nowTimestamp();\n }\n\n @BeforeUpdate()\n public setModified(): void {\n this.modifiedDate = nowTimestamp();\n }\n}\n\n@Entity('wallet_ledger_entry')\nexport class WalletLedgerEntryEntity {\n @PrimaryGeneratedColumn({ name: 'id' })\n public id!: number;\n\n @Column({ name: 'account_id' })\n public accountId!: number;\n\n @Column({ name: 'entry_type', length: 32 })\n public entryType!: string;\n\n @Column({ name: 'amount', type: 'bigint' })\n public amount!: string;\n\n @Column({ name: 'balance_after_available', type: 'bigint' })\n public balanceAfterAvailable!: string;\n\n @Column({ name: 'balance_after_held', type: 'bigint' })\n public balanceAfterHeld!: string;\n\n @Column({ name: 'idempotency_key', length: 255, unique: true })\n public idempotencyKey!: string;\n\n @Column({ name: 'reference_type', length: 64, nullable: true })\n public referenceType?: string;\n\n @Column({ name: 'reference_id', length: 128, nullable: true })\n public referenceId?: string;\n\n @Column({ name: 'metadata', type: 'json', nullable: true })\n public metadata?: Record<string, unknown>;\n\n @Column({ name: 'created_by', nullable: true })\n public createdBy?: number;\n\n @Column({ name: 'created_date', nullable: true })\n public createdDate!: string;\n\n @Column({ name: 'modified_by', nullable: true })\n public modifiedBy?: number;\n\n @Column({ name: 'modified_date', nullable: true })\n public modifiedDate!: string;\n\n @BeforeInsert()\n public setCreated(): void {\n this.createdDate = nowTimestamp();\n this.modifiedDate = nowTimestamp();\n }\n}\n\n@Entity('wallet_hold')\nexport class WalletHoldEntity {\n @PrimaryGeneratedColumn({ name: 'id' })\n public id!: number;\n\n @Column({ name: 'source_account_id' })\n public sourceAccountId!: number;\n\n @Column({ name: 'target_account_id' })\n public targetAccountId!: number;\n\n @Column({ name: 'amount', type: 'bigint' })\n public amount!: string;\n\n @Column({ name: 'status', length: 16, default: 'active' })\n public status!: string;\n\n @Column({ name: 'reference_type', length: 64 })\n public referenceType!: string;\n\n @Column({ name: 'reference_id', length: 128 })\n public referenceId!: string;\n\n @Column({ name: 'idempotency_key', length: 255 })\n public idempotencyKey!: string;\n\n @Column({ name: 'created_by', nullable: true })\n public createdBy?: number;\n\n @Column({ name: 'created_date', nullable: true })\n public createdDate!: string;\n\n @Column({ name: 'modified_by', nullable: true })\n public modifiedBy?: number;\n\n @Column({ name: 'modified_date', nullable: true })\n public modifiedDate!: string;\n\n @BeforeInsert()\n public setCreated(): void {\n this.createdDate = nowTimestamp();\n this.modifiedDate = nowTimestamp();\n }\n\n @BeforeUpdate()\n public setModified(): void {\n this.modifiedDate = nowTimestamp();\n }\n}\n","import { fromMinorUnit } from '../currency';\nimport { resolveWalletConnection } from '../plugin-instance';\nimport { WalletAccountEntity, WalletLedgerEntryEntity } from '../typeorm/entities';\n\nexport interface WalletParityDelta {\n orderId: number;\n vendorOrderId: number;\n vendorPaymentRupees: number;\n holdPaise: number;\n deltaRupees: number;\n}\n\nexport interface WalletCacheMismatch {\n accountId: number;\n cachedAvailable: number;\n cachedHeld: number;\n ledgerAvailable: number;\n ledgerHeld: number;\n}\n\nexport interface WalletReconciliationReport {\n parityDeltas: WalletParityDelta[];\n cacheMismatches: WalletCacheMismatch[];\n}\n\nexport async function runWalletReconciliation(): Promise<WalletReconciliationReport> {\n const parityDeltas = await findVendorPaymentHoldParityDeltas();\n const cacheMismatches = await findCacheLedgerMismatches();\n return { parityDeltas, cacheMismatches };\n}\n\nasync function findVendorPaymentHoldParityDeltas(): Promise<WalletParityDelta[]> {\n const rows = (await resolveWalletConnection().query(`\n SELECT\n vo.order_id AS orderId,\n vp.vendor_order_id AS vendorOrderId,\n vp.amount AS vendorPaymentAmount,\n wh.amount AS holdAmount,\n wh.status AS holdStatus\n FROM vendor_payment vp\n INNER JOIN vendor_orders vo ON vo.vendor_order_id = vp.vendor_order_id\n LEFT JOIN wallet_hold wh\n ON wh.reference_type = 'vendor_order'\n AND wh.reference_id = CAST(vp.vendor_order_id AS CHAR)\n WHERE vp.vendor_order_id IS NOT NULL\n `)) as Array<Record<string, unknown>>;\n\n const deltas: WalletParityDelta[] = [];\n for (const row of rows) {\n const vendorPaymentRupees = Number(row.vendorPaymentAmount) || 0;\n const holdPaise = row.holdAmount != null ? Number(row.holdAmount) : 0;\n const holdRupees = fromMinorUnit(holdPaise);\n const delta = Math.abs(vendorPaymentRupees - holdRupees);\n if (delta > 0.01 && row.holdStatus === 'active') {\n deltas.push({\n orderId: Number(row.orderId),\n vendorOrderId: Number(row.vendorOrderId),\n vendorPaymentRupees,\n holdPaise,\n deltaRupees: delta,\n });\n }\n }\n return deltas;\n}\n\nasync function findCacheLedgerMismatches(): Promise<WalletCacheMismatch[]> {\n const connection = resolveWalletConnection();\n const accounts = (await connection\n .getRepository(WalletAccountEntity)\n .find()) as WalletAccountEntity[];\n const mismatches: WalletCacheMismatch[] = [];\n\n for (const account of accounts) {\n const lastEntry = (await connection\n .getRepository(WalletLedgerEntryEntity)\n .findOne({\n where: { accountId: account.id },\n order: { id: 'DESC' },\n })) as WalletLedgerEntryEntity | null;\n if (!lastEntry) {\n continue;\n }\n const cachedAvailable = Number(account.availableBalance);\n const cachedHeld = Number(account.heldBalance);\n const ledgerAvailable = Number(lastEntry.balanceAfterAvailable);\n const ledgerHeld = Number(lastEntry.balanceAfterHeld);\n if (cachedAvailable !== ledgerAvailable || cachedHeld !== ledgerHeld) {\n mismatches.push({\n accountId: account.id,\n cachedAvailable,\n cachedHeld,\n ledgerAvailable,\n ledgerHeld,\n });\n }\n }\n return mismatches;\n}\n","import type { WalletLifecycleHooks } from './types';\nimport { createOrderHooks } from './order';\nimport { createRefundHooks } from './refund';\nimport { createSettlementHooks } from './settlement';\nimport type { HookRuntime } from './types';\n\nexport function createLifecycleHooks(runtime: HookRuntime): WalletLifecycleHooks {\n return {\n ...createOrderHooks(runtime),\n ...createSettlementHooks(runtime),\n ...createRefundHooks(runtime),\n };\n}\n\nexport type {\n DeliveredContext,\n OrderPaidContext,\n RefundContext,\n SettlementDebitContext,\n WalletLifecycleHooks,\n WalletOrderPaidLine,\n} from './types';\n\nexport { runWalletReconciliation } from './reconciliation';\nexport type {\n WalletCacheMismatch,\n WalletParityDelta,\n WalletReconciliationReport,\n} from './reconciliation';\n"]}
1
+ {"version":3,"sources":["../../src/currency.ts","../../src/orderCustomerDebit.ts","../../src/types.ts","../../src/hooks/order.ts","../../src/errors.ts","../../src/hooks/refund.ts","../../src/hooks/settlement.ts","../../src/plugin-instance.ts","../../src/timestamps.ts","../../src/typeorm/entities.ts","../../src/hooks/reconciliation.ts","../../src/hooks/index.ts"],"names":["toMinorUnit","amount","Math","round","fromMinorUnit","CUSTOMER_CHECKOUT_DEBIT_PURPOSE","orderCustomerDebitIdempotencyKey","orderId","WalletAccountType","isWalletOnlyPayment","paymentMethod","String","trim","toLowerCase","createOrderHooks","runtime","client","platformOwnerId","flags","onOrderPaid","ctx","enabled","ledgerWrites","txCtx","nativeTx","platform","getOrCreateAccount","PLATFORM","currency","orderTotalPaise","orderTotalRupees","shouldDebitCustomer","customerId","skipCustomerDebit","debitRupees","min","walletAmountRupees","customer","CUSTOMER","debit","accountId","id","idempotencyKey","reference","referenceType","referenceId","metadata","purpose","paymentNumber","credit","line","lines","vendorNetRupees","sellerNetRupees","Number","isFinite","commissionBaseRupees","commissionRupees","vendor","VENDOR","vendorId","hold","sourceAccountId","targetAccountId","vendorOrderId","onLineDelivered","release","undefined","WalletError","Error","message","name","HoldNotFoundError","isWalletPayment","walletCreditRupees","walletRefundRupees","lineTotalRupees","idempotencyPrefix","scope","idempotencyScope","orderProductId","refundReason","isHoldNotFound","error","cancelEscrowHoldOrSkip","params","cancelHold","createRefundHooks","onRefund","processExternalRefund","keyPrefix","skipEscrow","sellerNetClawbackRupees","clawbackPaise","refundPaise","balance","getBalance","available","creditRupees","createSettlementHooks","_runtime","onSettlementDebit","_ctx","resolveWalletConnection","pad2","value","padStart","nowTimestamp","date","Date","getFullYear","getMonth","getDate","getHours","getMinutes","getSeconds","WalletAccountEntity","ownerType","ownerId","availableBalance","heldBalance","createdBy","createdDate","modifiedBy","modifiedDate","setCreated","setModified","length","default","type","nullable","WalletLedgerEntryEntity","entryType","balanceAfterAvailable","balanceAfterHeld","unique","WalletHoldEntity","status","runWalletReconciliation","parityDeltas","findVendorPaymentHoldParityDeltas","cacheMismatches","findCacheLedgerMismatches","rows","query","deltas","row","vendorPaymentRupees","vendorPaymentAmount","holdPaise","holdAmount","holdRupees","delta","abs","holdStatus","push","deltaRupees","connection","accounts","getRepository","find","mismatches","account","lastEntry","findOne","where","order","cachedAvailable","cachedHeld","ledgerAvailable","ledgerHeld","createLifecycleHooks"],"mappings":";;;;;;;;;;AAGO,SAASA,YAAYC,MAAAA,EAAc;AACxC,EAAA,OAAOC,IAAAA,CAAKC,KAAAA,CAAMF,MAAAA,GAAS,GAAA,CAAA;AAC7B;AAFgBD,MAAAA,CAAAA,WAAAA,EAAAA,aAAAA,CAAAA;AAOT,SAASI,cAAcH,MAAAA,EAAc;AAC1C,EAAA,OAAOA,MAAAA,GAAS,GAAA;AAClB;AAFgBG,MAAAA,CAAAA,aAAAA,EAAAA,eAAAA,CAAAA;;;ACVT,IAAMC,+BAAAA,GAAkC,yBAAA;AAKxC,SAASC,iCAAiCC,OAAAA,EAAwB;AACvE,EAAA,OAAO,gBAAgBA,OAAAA,CAAAA,eAAAA,CAAAA;AACzB;AAFgBD,MAAAA,CAAAA,gCAAAA,EAAAA,kCAAAA,CAAAA;;;ACLT,IAAKE,iBAAAA,6BAAAA,kBAAAA,EAAAA;;;;AAI8C,EAAAA,kBAAAA,CAAA,UAAA,CAAA,GAAA,UAAA;AAEK,EAAAA,kBAAAA,CAAA,6BAAA,CAAA,GAAA,6BAAA;AAEJ,EAAAA,kBAAAA,CAAA,sBAAA,CAAA,GAAA,sBAAA;AAEF,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AAO4G,OAAAA,CAAQ1C,WAAW,CAAA;AAC7C,IAAA,MAAMiD,eAAAA,GAAkBnH,MAAAA,CAAO6G,SAAAA,CAAU9B,qBAAqB,CAAA;AAC9D,IAAA,MAAMqC,UAAAA,GAAapH,MAAAA,CAAO6G,SAAAA,CAAU7B,gBAAgB,CAAA;AACpD,IAAA,IAAIiC,eAAAA,KAAoBE,eAAAA,IAAmBD,UAAAA,KAAeE,UAAAA,EAAY;AACpET,MAAAA,UAAAA,CAAWN,IAAAA,CAAK;AACdnH,QAAAA,SAAAA,EAAW0H,OAAAA,CAAQzH,EAAAA;AACnB8H,QAAAA,eAAAA;AACAC,QAAAA,UAAAA;AACAC,QAAAA,eAAAA;AACAC,QAAAA;OACF,CAAA;AACF,IAAA;AACF,EAAA;AACA,EAAA,OAAOT,UAAAA;AACT;AAhCenB,MAAAA,CAAAA,yBAAAA,EAAAA,2BAAAA,CAAAA;;;AC5DR,SAAS6B,qBAAqB5J,OAAAA,EAAoB;AACvD,EAAA,OAAO;AACL,IAAA,GAAGD,iBAAiBC,OAAAA,CAAAA;AACpB,IAAA,GAAGoF,sBAAsBpF,CAAAA;AACzB,IAAA,GAAGwE,kBAAkBxE,OAAAA;AACvB,GAAA;AACF;AANgB4J,MAAAA,CAAAA,oBAAAA,EAAAA,sBAAAA,CAAAA","file":"index.cjs","sourcesContent":["/**\n * Convert decimal currency amount (e.g. rupees) to smallest unit (paise).\n */\nexport function toMinorUnit(amount: number): number {\n return Math.round(amount * 100);\n}\n\n/**\n * Convert smallest currency unit (paise) back to decimal amount (rupees).\n */\nexport function fromMinorUnit(amount: number): number {\n return amount / 100;\n}\n","export const CUSTOMER_CHECKOUT_DEBIT_PURPOSE = 'customer_checkout_debit';\nexport const CUSTOMER_CHECKOUT_DEBIT_REVERSAL_PURPOSE = 'customer_checkout_debit_reversal';\nexport const BACKFILL_SENTINEL_KEY = 'customer_checkout_debit_backfill';\nexport const WALLET_OPS_TABLE = 'wallet_ops';\n\nexport function orderCustomerDebitIdempotencyKey(orderId: number | string): string {\n return `wallet:order:${orderId}:customer_debit`;\n}\n\nexport function paymentReversalIdempotencyKey(paymentNumber: string): string {\n return `wallet:payment-reversal:${paymentNumber}`;\n}\n\nexport function paymentNumberFromWalletPaymentKey(idempotencyKey: string): string | null {\n const prefix = 'wallet:payment:';\n if (!idempotencyKey.startsWith(prefix)) {\n return null;\n }\n return idempotencyKey.slice(prefix.length);\n}\n\n/**\n * True when this debit is a customer checkout (product-amount) debit for an order.\n * Settlement/vendor/affiliate debits must not use these keys or purpose.\n */\nexport function isCustomerCheckoutDebit(params: {\n idempotencyKey: string;\n referenceType?: string;\n purpose?: unknown;\n}): boolean {\n if (params.purpose === CUSTOMER_CHECKOUT_DEBIT_PURPOSE) {\n return true;\n }\n const key = params.idempotencyKey;\n if (key.startsWith('wallet:order:') && key.endsWith(':customer_debit')) {\n return true;\n }\n if (params.referenceType !== 'order') {\n return false;\n }\n return isLegacyCustomerCheckoutDebitKey(key);\n}\n\n/**\n * Legacy keys accepted during rollout only.\n * Sunset: `npm run wallet:legacy-order-debit-sunset` — drop this helper when LEGACY_ORDER_DEBIT_SUNSET_SQL returns 0 on prod.\n */\nexport function isLegacyCustomerCheckoutDebitKey(key: string): boolean {\n return (\n key.startsWith('wallet:full:') ||\n key.startsWith('wallet:split:') ||\n key.startsWith('wallet:payment:')\n );\n}\n\n/** Unreimbursed full/split + wallet:payment twins (duplicate customer checkout debits). */\nexport const UNREIMBURSED_TWIN_COUNT_SQL = `\nSELECT COUNT(*) AS c\nFROM wallet_ledger_entry f\nJOIN wallet_ledger_entry p\n ON p.account_id = f.account_id\n AND p.reference_type = 'order'\n AND p.reference_id = f.reference_id\n AND p.entry_type = 'debit'\n AND p.idempotency_key LIKE 'wallet:payment:%'\nWHERE f.entry_type = 'debit'\n AND f.reference_type = 'order'\n AND (\n f.idempotency_key LIKE 'wallet:full:%'\n OR f.idempotency_key LIKE 'wallet:split:%'\n )\n AND NOT EXISTS (\n SELECT 1 FROM wallet_ledger_entry r\n WHERE r.entry_type = 'credit'\n AND r.account_id = p.account_id\n AND r.reference_type = 'order'\n AND r.reference_id = p.reference_id\n AND (\n r.idempotency_key = CONCAT('wallet:payment-reversal:', SUBSTRING(p.idempotency_key, 17))\n OR JSON_UNQUOTE(JSON_EXTRACT(r.metadata, '$.purpose')) = 'customer_checkout_debit_reversal'\n )\n )\n`;\n\nexport const UNREIMBURSED_TWIN_DETAIL_SQL = `\nSELECT\n f.reference_id AS order_id,\n f.account_id,\n f.amount AS first_paise,\n p.amount AS second_paise,\n f.idempotency_key AS kept_key,\n p.idempotency_key AS duplicate_key,\n f.created_date AS first_at,\n p.created_date AS duplicate_at,\n f.id AS kept_id,\n p.id AS duplicate_id\nFROM wallet_ledger_entry f\nJOIN wallet_ledger_entry p\n ON p.account_id = f.account_id\n AND p.reference_type = 'order'\n AND p.reference_id = f.reference_id\n AND p.entry_type = 'debit'\n AND p.idempotency_key LIKE 'wallet:payment:%'\nWHERE f.entry_type = 'debit'\n AND f.reference_type = 'order'\n AND (\n f.idempotency_key LIKE 'wallet:full:%'\n OR f.idempotency_key LIKE 'wallet:split:%'\n )\n AND NOT EXISTS (\n SELECT 1 FROM wallet_ledger_entry r\n WHERE r.entry_type = 'credit'\n AND r.account_id = p.account_id\n AND r.reference_type = 'order'\n AND r.reference_id = p.reference_id\n AND (\n r.idempotency_key = CONCAT('wallet:payment-reversal:', SUBSTRING(p.idempotency_key, 17))\n OR JSON_UNQUOTE(JSON_EXTRACT(r.metadata, '$.purpose')) = 'customer_checkout_debit_reversal'\n )\n )\n`;\n\n/**\n * Exit condition for removing isLegacyCustomerCheckoutDebitKey from debit lookup.\n * File the sunset PR the same week this count hits 0 on prod.\n */\nexport const LEGACY_ORDER_DEBIT_SUNSET_SQL = `\nSELECT COUNT(*) AS c\nFROM wallet_ledger_entry\nWHERE entry_type = 'debit'\n AND reference_type = 'order'\n AND (\n idempotency_key LIKE 'wallet:full:%'\n OR idempotency_key LIKE 'wallet:split:%'\n OR idempotency_key LIKE 'wallet:payment:%'\n )\n`;\n","export enum WalletAccountType {\n CUSTOMER = 'customer',\n VENDOR = 'vendor',\n PLATFORM = 'platform',\n /** Investor cash wallet (interest/bonus/withdrawable). */\n INVESTOR = 'investor',\n /** Platform liability balancing investor principal/returns. */\n PLATFORM_INVESTOR_LIABILITY = 'platform_investor_liability',\n /** TDS withheld on investor returns pending remittance. */\n PLATFORM_TDS_PAYABLE = 'platform_tds_payable',\n /** GST TCS collected from sellers pending remittance. */\n PLATFORM_GST_TCS_PAYABLE = 'platform_gst_tcs_payable',\n /** IT TDS withheld from seller settlements pending remittance. */\n PLATFORM_IT_TDS_PAYABLE = 'platform_it_tds_payable',\n /** TDS withheld from affiliate commissions pending remittance. */\n PLATFORM_AFFILIATE_TDS_PAYABLE = 'platform_affiliate_tds_payable',\n}\n\nexport enum LedgerEntryType {\n CREDIT = 'credit',\n DEBIT = 'debit',\n HOLD = 'hold',\n RELEASE = 'release',\n CANCEL_HOLD = 'cancel_hold',\n TRANSFER = 'transfer',\n}\n\nexport enum HoldStatus {\n ACTIVE = 'active',\n RELEASED = 'released',\n CANCELLED = 'cancelled',\n}\n\nexport interface WalletReference {\n referenceType: string;\n referenceId: string;\n}\n\nexport interface WalletAccount {\n id: number;\n ownerType: WalletAccountType;\n ownerId: number;\n currency: string;\n availableBalance: number;\n heldBalance: number;\n}\n\nexport interface WalletBalance {\n available: number;\n held: number;\n total: number;\n}\n\nexport interface WalletLedgerEntry {\n id: number;\n accountId: number;\n entryType: LedgerEntryType;\n amount: number;\n balanceAfterAvailable: number;\n balanceAfterHeld: number;\n idempotencyKey: string;\n referenceType?: string;\n referenceId?: string;\n metadata?: Record<string, unknown>;\n createdAt: Date;\n}\n\nexport interface WalletHold {\n id: number;\n sourceAccountId: number;\n targetAccountId: number;\n amount: number;\n status: HoldStatus;\n referenceType: string;\n referenceId: string;\n idempotencyKey: string;\n createdAt: Date;\n}\n\nexport interface BalanceDeltas {\n availableDelta?: number;\n heldDelta?: number;\n}\n\nexport interface CreditParams {\n accountId: number;\n amount: number;\n idempotencyKey: string;\n reference?: WalletReference;\n metadata?: Record<string, unknown>;\n}\n\nexport interface DebitParams {\n accountId: number;\n amount: number;\n idempotencyKey: string;\n reference?: WalletReference;\n metadata?: Record<string, unknown>;\n}\n\nexport interface HoldParams {\n sourceAccountId: number;\n targetAccountId: number;\n amount: number;\n idempotencyKey: string;\n reference: WalletReference;\n metadata?: Record<string, unknown>;\n}\n\nexport interface ReleaseParams {\n reference: WalletReference;\n idempotencyKey: string;\n metadata?: Record<string, unknown>;\n}\n\nexport interface CancelHoldParams {\n reference: WalletReference;\n idempotencyKey: string;\n metadata?: Record<string, unknown>;\n /** When set and less than the active hold, reduce the hold instead of cancelling it. */\n amount?: number;\n}\n\nexport interface TransferParams {\n fromAccountId: number;\n toAccountId: number;\n amount: number;\n idempotencyKey: string;\n reference?: WalletReference;\n metadata?: Record<string, unknown>;\n}\n\nexport interface GetTransactionsParams {\n accountId: number;\n limit?: number;\n offset?: number;\n}\n\nexport interface LedgerMutationResult {\n entry: WalletLedgerEntry;\n account: WalletAccount;\n hold?: WalletHold;\n}\n","import { toMinorUnit } from '../currency';\nimport {\n CUSTOMER_CHECKOUT_DEBIT_PURPOSE,\n orderCustomerDebitIdempotencyKey,\n} from '../orderCustomerDebit';\nimport { WalletAccountType } from '../types';\nimport type { DeliveredContext, HookRuntime, OrderPaidContext, WalletLifecycleHooks } from './types';\n\nfunction isWalletOnlyPayment(paymentMethod?: string): boolean {\n return String(paymentMethod ?? '').trim().toLowerCase() === 'wallet';\n}\n\nexport function createOrderHooks(runtime: HookRuntime): Pick<\n WalletLifecycleHooks,\n 'onOrderPaid' | 'onLineDelivered'\n> {\n const { client, platformOwnerId, flags } = runtime;\n\n return {\n async onOrderPaid(ctx: OrderPaidContext): Promise<void> {\n if (!flags.enabled || !flags.ledgerWrites) {\n return;\n }\n\n const txCtx = { nativeTx: ctx.nativeTx };\n const platform = await client.getOrCreateAccount(\n WalletAccountType.PLATFORM,\n platformOwnerId,\n ctx.currency,\n txCtx\n );\n\n const orderTotalPaise = toMinorUnit(ctx.orderTotalRupees);\n const shouldDebitCustomer =\n isWalletOnlyPayment(ctx.paymentMethod) &&\n ctx.customerId &&\n !ctx.skipCustomerDebit;\n\n if (shouldDebitCustomer) {\n const debitRupees = Math.min(\n ctx.walletAmountRupees ?? ctx.orderTotalRupees,\n ctx.orderTotalRupees\n );\n const customer = await client.getOrCreateAccount(\n WalletAccountType.CUSTOMER,\n ctx.customerId,\n ctx.currency,\n txCtx\n );\n await client.debit(\n {\n accountId: customer.id,\n amount: toMinorUnit(debitRupees),\n idempotencyKey: orderCustomerDebitIdempotencyKey(ctx.orderId),\n reference: { referenceType: 'order', referenceId: String(ctx.orderId) },\n metadata: { purpose: CUSTOMER_CHECKOUT_DEBIT_PURPOSE, paymentNumber: ctx.paymentNumber },\n },\n txCtx\n );\n }\n\n await client.credit(\n {\n accountId: platform.id,\n amount: orderTotalPaise,\n idempotencyKey: `payment:${ctx.paymentNumber}`,\n reference: { referenceType: 'order', referenceId: String(ctx.orderId) },\n },\n txCtx\n );\n\n for (const line of ctx.lines) {\n const vendorNetRupees =\n line.sellerNetRupees != null && Number.isFinite(Number(line.sellerNetRupees))\n ? Number(line.sellerNetRupees)\n : line.commissionBaseRupees - line.commissionRupees;\n if (vendorNetRupees <= 0) {\n continue;\n }\n\n const vendor = await client.getOrCreateAccount(\n WalletAccountType.VENDOR,\n line.vendorId,\n ctx.currency,\n txCtx\n );\n\n await client.hold(\n {\n sourceAccountId: platform.id,\n targetAccountId: vendor.id,\n amount: toMinorUnit(vendorNetRupees),\n idempotencyKey: `order:${ctx.orderId}:hold:vendorOrder:${line.vendorOrderId}`,\n reference: {\n referenceType: 'vendor_order',\n referenceId: String(line.vendorOrderId),\n },\n },\n txCtx\n );\n }\n },\n\n async onLineDelivered(ctx: DeliveredContext): Promise<void> {\n if (!flags.enabled || !flags.ledgerWrites) {\n return;\n }\n\n await client.release(\n {\n reference: { referenceType: 'vendor_order', referenceId: String(ctx.vendorOrderId) },\n idempotencyKey: `vendorOrder:${ctx.vendorOrderId}:release`,\n },\n ctx.nativeTx != null ? { nativeTx: ctx.nativeTx } : undefined\n );\n },\n };\n}\n","export class WalletError extends Error {\n constructor(message: string) {\n super(message);\n this.name = 'WalletError';\n }\n}\n\nexport class WalletConfigError extends WalletError {\n constructor(message: string) {\n super(message);\n this.name = 'WalletConfigError';\n }\n}\n\nexport class InsufficientBalanceError extends WalletError {\n constructor(message: string = 'Insufficient available balance') {\n super(message);\n this.name = 'InsufficientBalanceError';\n }\n}\n\nexport class HoldNotFoundError extends WalletError {\n constructor(message: string = 'Active hold not found for reference') {\n super(message);\n this.name = 'HoldNotFoundError';\n }\n}\n\nexport class DuplicateOperationError extends WalletError {\n public readonly existingEntry: unknown;\n\n constructor(message: string, existingEntry: unknown) {\n super(message);\n this.name = 'DuplicateOperationError';\n this.existingEntry = existingEntry;\n }\n}\n\nexport class InvalidAmountError extends WalletError {\n constructor(message: string = 'Amount must be a positive integer in minor units') {\n super(message);\n this.name = 'InvalidAmountError';\n }\n}\n","import { toMinorUnit } from '../currency';\nimport { HoldNotFoundError } from '../errors';\nimport { WalletAccountType } from '../types';\nimport type { HookRuntime, RefundContext, WalletLifecycleHooks } from './types';\n\nfunction isWalletPayment(paymentMethod?: string): boolean {\n return String(paymentMethod ?? '').trim().toLowerCase() === 'wallet';\n}\n\nfunction walletCreditRupees(ctx: RefundContext): number {\n if (ctx.walletRefundRupees != null && ctx.walletRefundRupees > 0) {\n return ctx.walletRefundRupees;\n }\n return isWalletPayment(ctx.paymentMethod) ? ctx.lineTotalRupees : 0;\n}\n\nfunction idempotencyPrefix(ctx: RefundContext): string {\n const scope = ctx.idempotencyScope ?? String(ctx.orderProductId);\n return ctx.refundReason === 'return' ? `return:${scope}` : `refund:${scope}`;\n}\n\nfunction isHoldNotFound(error: unknown): boolean {\n if (error instanceof HoldNotFoundError) {\n return true;\n }\n return error instanceof Error && error.name === 'HoldNotFoundError';\n}\n\n/**\n * Cancel vendor escrow when present. Missing ACTIVE hold is a no-op so customer\n * refunds still complete (hold never created, already cancelled, or sellerNet was 0).\n */\nasync function cancelEscrowHoldOrSkip(\n client: HookRuntime['client'],\n params: {\n referenceId: string;\n idempotencyKey: string;\n amount?: number;\n }\n): Promise<void> {\n try {\n await client.cancelHold({\n reference: {\n referenceType: 'vendor_order',\n referenceId: params.referenceId,\n },\n idempotencyKey: params.idempotencyKey,\n ...(params.amount != null ? { amount: params.amount } : {}),\n });\n } catch (error) {\n if (!isHoldNotFound(error)) {\n throw error;\n }\n }\n}\n\nexport function createRefundHooks(runtime: HookRuntime): Pick<WalletLifecycleHooks, 'onRefund'> {\n const { client, platformOwnerId, flags } = runtime;\n\n return {\n async onRefund(ctx: RefundContext): Promise<void> {\n if (!flags.enabled || !flags.ledgerWrites) {\n await ctx.processExternalRefund();\n return;\n }\n\n const keyPrefix = idempotencyPrefix(ctx);\n\n if (!ctx.skipEscrow) {\n if (ctx.sellerNetClawbackRupees != null) {\n const clawbackPaise = toMinorUnit(ctx.sellerNetClawbackRupees);\n if (clawbackPaise > 0) {\n await cancelEscrowHoldOrSkip(client, {\n referenceId: String(ctx.vendorOrderId),\n idempotencyKey: `${keyPrefix}:cancel_hold`,\n amount: clawbackPaise,\n });\n }\n } else {\n await cancelEscrowHoldOrSkip(client, {\n referenceId: String(ctx.vendorOrderId),\n idempotencyKey: `${keyPrefix}:cancel_hold`,\n });\n }\n }\n\n const refundPaise = toMinorUnit(ctx.lineTotalRupees);\n if (refundPaise > 0) {\n const platform = await client.getOrCreateAccount(\n WalletAccountType.PLATFORM,\n platformOwnerId,\n ctx.currency\n );\n const balance = await client.getBalance(platform.id);\n if (balance.available >= refundPaise) {\n await client.debit({\n accountId: platform.id,\n amount: refundPaise,\n idempotencyKey: `${keyPrefix}:platform_debit`,\n reference: {\n referenceType: 'order_product',\n referenceId: String(ctx.orderProductId),\n },\n });\n }\n\n const creditRupees = walletCreditRupees(ctx);\n if (creditRupees > 0 && ctx.customerId) {\n const customer = await client.getOrCreateAccount(\n WalletAccountType.CUSTOMER,\n ctx.customerId,\n ctx.currency\n );\n await client.credit({\n accountId: customer.id,\n amount: toMinorUnit(creditRupees),\n idempotencyKey: `${keyPrefix}:customer_credit`,\n reference: {\n referenceType: 'order_product',\n referenceId: String(ctx.orderProductId),\n },\n });\n }\n }\n\n await ctx.processExternalRefund();\n },\n };\n}\n","import type { HookRuntime, SettlementDebitContext, WalletLifecycleHooks } from './types';\n\nexport function createSettlementHooks(\n _runtime: HookRuntime\n): Pick<WalletLifecycleHooks, 'onSettlementDebit'> {\n return {\n /** @deprecated Settlement wallet debits removed — vendor payouts use wallet payout requests. */\n async onSettlementDebit(_ctx: SettlementDebitContext): Promise<void> {\n return;\n },\n };\n}\n","import type { WalletPlugin } from './plugin-types';\n\nexport interface WalletHostConnection {\n manager: {\n transaction<T>(fn: (em: unknown) => Promise<T>): Promise<T>;\n };\n query(query: string, parameters?: unknown[]): Promise<unknown>;\n getRepository(entity: unknown): {\n find(options?: unknown): Promise<unknown[]>;\n findOne(options?: unknown): Promise<unknown | null>;\n };\n}\n\nlet activePlugin: WalletPlugin | undefined;\nlet activeConnection: WalletHostConnection | undefined;\n\nexport function bindWalletPlugin(plugin: WalletPlugin): void {\n activePlugin = plugin;\n}\n\nexport function bindWalletConnection(connection: unknown): void {\n activeConnection = connection as WalletHostConnection;\n}\n\nexport function resolveWalletPlugin(): WalletPlugin {\n if (!activePlugin) {\n throw new Error('Wallet plugin is not registered. Call registerWalletPlugin() during app bootstrap.');\n }\n return activePlugin;\n}\n\nexport function resolveWalletConnection(): WalletHostConnection {\n if (!activeConnection?.manager) {\n throw new Error('Wallet database connection is not registered. Pass host TypeORM Connection as dataSource.');\n }\n return activeConnection;\n}\n\nexport function clearWalletPluginForTests(): void {\n activePlugin = undefined;\n activeConnection = undefined;\n}\n","function pad2(value: number): string {\n return String(value).padStart(2, '0');\n}\n\n/** Server-local wall clock `YYYY-MM-DD HH:mm:ss` (matches host order paidDate, not UTC ISO). */\nexport function nowTimestamp(date: Date = new Date()): string {\n return `${date.getFullYear()}-${pad2(date.getMonth() + 1)}-${pad2(date.getDate())} ${pad2(date.getHours())}:${pad2(date.getMinutes())}:${pad2(date.getSeconds())}`;\n}\n\n/** Parse naive datetime as local wall clock (do not treat as UTC). */\nexport function parseLedgerDate(value: string | Date): Date {\n if (value instanceof Date) {\n return value;\n }\n const match = /^(\\d{4})-(\\d{2})-(\\d{2})[ T](\\d{2}):(\\d{2}):(\\d{2})/.exec(String(value));\n if (!match) {\n return new Date(value);\n }\n return new Date(\n Number(match[1]),\n Number(match[2]) - 1,\n Number(match[3]),\n Number(match[4]),\n Number(match[5]),\n Number(match[6])\n );\n}\n","import { BeforeInsert, BeforeUpdate, Column, Entity, PrimaryGeneratedColumn } from 'typeorm';\nimport { nowTimestamp } from '../timestamps';\n\n@Entity('wallet_account')\nexport class WalletAccountEntity {\n @PrimaryGeneratedColumn({ name: 'id' })\n public id!: number;\n\n @Column({ name: 'owner_type', length: 32 })\n public ownerType!: string;\n\n @Column({ name: 'owner_id' })\n public ownerId!: number;\n\n @Column({ name: 'currency', length: 8, default: 'INR' })\n public currency!: string;\n\n @Column({ name: 'available_balance', type: 'bigint', default: 0 })\n public availableBalance!: string;\n\n @Column({ name: 'held_balance', type: 'bigint', default: 0 })\n public heldBalance!: string;\n\n @Column({ name: 'created_by', nullable: true })\n public createdBy?: number;\n\n @Column({ name: 'created_date', nullable: true })\n public createdDate!: string;\n\n @Column({ name: 'modified_by', nullable: true })\n public modifiedBy?: number;\n\n @Column({ name: 'modified_date', nullable: true })\n public modifiedDate!: string;\n\n @BeforeInsert()\n public setCreated(): void {\n this.createdDate = nowTimestamp();\n this.modifiedDate = nowTimestamp();\n }\n\n @BeforeUpdate()\n public setModified(): void {\n this.modifiedDate = nowTimestamp();\n }\n}\n\n@Entity('wallet_ledger_entry')\nexport class WalletLedgerEntryEntity {\n @PrimaryGeneratedColumn({ name: 'id' })\n public id!: number;\n\n @Column({ name: 'account_id' })\n public accountId!: number;\n\n @Column({ name: 'entry_type', length: 32 })\n public entryType!: string;\n\n @Column({ name: 'amount', type: 'bigint' })\n public amount!: string;\n\n @Column({ name: 'balance_after_available', type: 'bigint' })\n public balanceAfterAvailable!: string;\n\n @Column({ name: 'balance_after_held', type: 'bigint' })\n public balanceAfterHeld!: string;\n\n @Column({ name: 'idempotency_key', length: 255, unique: true })\n public idempotencyKey!: string;\n\n @Column({ name: 'reference_type', length: 64, nullable: true })\n public referenceType?: string;\n\n @Column({ name: 'reference_id', length: 128, nullable: true })\n public referenceId?: string;\n\n @Column({ name: 'metadata', type: 'json', nullable: true })\n public metadata?: Record<string, unknown>;\n\n @Column({ name: 'created_by', nullable: true })\n public createdBy?: number;\n\n @Column({ name: 'created_date', nullable: true })\n public createdDate!: string;\n\n @Column({ name: 'modified_by', nullable: true })\n public modifiedBy?: number;\n\n @Column({ name: 'modified_date', nullable: true })\n public modifiedDate!: string;\n\n @BeforeInsert()\n public setCreated(): void {\n this.createdDate = nowTimestamp();\n this.modifiedDate = nowTimestamp();\n }\n}\n\n@Entity('wallet_hold')\nexport class WalletHoldEntity {\n @PrimaryGeneratedColumn({ name: 'id' })\n public id!: number;\n\n @Column({ name: 'source_account_id' })\n public sourceAccountId!: number;\n\n @Column({ name: 'target_account_id' })\n public targetAccountId!: number;\n\n @Column({ name: 'amount', type: 'bigint' })\n public amount!: string;\n\n @Column({ name: 'status', length: 16, default: 'active' })\n public status!: string;\n\n @Column({ name: 'reference_type', length: 64 })\n public referenceType!: string;\n\n @Column({ name: 'reference_id', length: 128 })\n public referenceId!: string;\n\n @Column({ name: 'idempotency_key', length: 255 })\n public idempotencyKey!: string;\n\n @Column({ name: 'created_by', nullable: true })\n public createdBy?: number;\n\n @Column({ name: 'created_date', nullable: true })\n public createdDate!: string;\n\n @Column({ name: 'modified_by', nullable: true })\n public modifiedBy?: number;\n\n @Column({ name: 'modified_date', nullable: true })\n public modifiedDate!: string;\n\n @BeforeInsert()\n public setCreated(): void {\n this.createdDate = nowTimestamp();\n this.modifiedDate = nowTimestamp();\n }\n\n @BeforeUpdate()\n public setModified(): void {\n this.modifiedDate = nowTimestamp();\n }\n}\n","import { fromMinorUnit } from '../currency';\nimport { resolveWalletConnection } from '../plugin-instance';\nimport { WalletAccountEntity, WalletLedgerEntryEntity } from '../typeorm/entities';\n\nexport interface WalletParityDelta {\n orderId: number;\n vendorOrderId: number;\n vendorPaymentRupees: number;\n holdPaise: number;\n deltaRupees: number;\n}\n\nexport interface WalletCacheMismatch {\n accountId: number;\n cachedAvailable: number;\n cachedHeld: number;\n ledgerAvailable: number;\n ledgerHeld: number;\n}\n\nexport interface WalletReconciliationReport {\n parityDeltas: WalletParityDelta[];\n cacheMismatches: WalletCacheMismatch[];\n}\n\nexport async function runWalletReconciliation(): Promise<WalletReconciliationReport> {\n const parityDeltas = await findVendorPaymentHoldParityDeltas();\n const cacheMismatches = await findCacheLedgerMismatches();\n return { parityDeltas, cacheMismatches };\n}\n\nasync function findVendorPaymentHoldParityDeltas(): Promise<WalletParityDelta[]> {\n const rows = (await resolveWalletConnection().query(`\n SELECT\n vo.order_id AS orderId,\n vp.vendor_order_id AS vendorOrderId,\n vp.amount AS vendorPaymentAmount,\n wh.amount AS holdAmount,\n wh.status AS holdStatus\n FROM vendor_payment vp\n INNER JOIN vendor_orders vo ON vo.vendor_order_id = vp.vendor_order_id\n LEFT JOIN wallet_hold wh\n ON wh.reference_type = 'vendor_order'\n AND wh.reference_id = CAST(vp.vendor_order_id AS CHAR)\n WHERE vp.vendor_order_id IS NOT NULL\n `)) as Array<Record<string, unknown>>;\n\n const deltas: WalletParityDelta[] = [];\n for (const row of rows) {\n const vendorPaymentRupees = Number(row.vendorPaymentAmount) || 0;\n const holdPaise = row.holdAmount != null ? Number(row.holdAmount) : 0;\n const holdRupees = fromMinorUnit(holdPaise);\n const delta = Math.abs(vendorPaymentRupees - holdRupees);\n if (delta > 0.01 && row.holdStatus === 'active') {\n deltas.push({\n orderId: Number(row.orderId),\n vendorOrderId: Number(row.vendorOrderId),\n vendorPaymentRupees,\n holdPaise,\n deltaRupees: delta,\n });\n }\n }\n return deltas;\n}\n\nasync function findCacheLedgerMismatches(): Promise<WalletCacheMismatch[]> {\n const connection = resolveWalletConnection();\n const accounts = (await connection\n .getRepository(WalletAccountEntity)\n .find()) as WalletAccountEntity[];\n const mismatches: WalletCacheMismatch[] = [];\n\n for (const account of accounts) {\n const lastEntry = (await connection\n .getRepository(WalletLedgerEntryEntity)\n .findOne({\n where: { accountId: account.id },\n order: { id: 'DESC' },\n })) as WalletLedgerEntryEntity | null;\n if (!lastEntry) {\n continue;\n }\n const cachedAvailable = Number(account.availableBalance);\n const cachedHeld = Number(account.heldBalance);\n const ledgerAvailable = Number(lastEntry.balanceAfterAvailable);\n const ledgerHeld = Number(lastEntry.balanceAfterHeld);\n if (cachedAvailable !== ledgerAvailable || cachedHeld !== ledgerHeld) {\n mismatches.push({\n accountId: account.id,\n cachedAvailable,\n cachedHeld,\n ledgerAvailable,\n ledgerHeld,\n });\n }\n }\n return mismatches;\n}\n","import type { WalletLifecycleHooks } from './types';\nimport { createOrderHooks } from './order';\nimport { createRefundHooks } from './refund';\nimport { createSettlementHooks } from './settlement';\nimport type { HookRuntime } from './types';\n\nexport function createLifecycleHooks(runtime: HookRuntime): WalletLifecycleHooks {\n return {\n ...createOrderHooks(runtime),\n ...createSettlementHooks(runtime),\n ...createRefundHooks(runtime),\n };\n}\n\nexport type {\n DeliveredContext,\n OrderPaidContext,\n RefundContext,\n SettlementDebitContext,\n WalletLifecycleHooks,\n WalletOrderPaidLine,\n} from './types';\n\nexport { runWalletReconciliation } from './reconciliation';\nexport type {\n WalletCacheMismatch,\n WalletParityDelta,\n WalletReconciliationReport,\n} from './reconciliation';\n"]}
@@ -116,6 +116,24 @@ function createOrderHooks(runtime) {
116
116
  }
117
117
  __name(createOrderHooks, "createOrderHooks");
118
118
 
119
+ // src/errors.ts
120
+ var _WalletError = class _WalletError extends Error {
121
+ constructor(message) {
122
+ super(message);
123
+ this.name = "WalletError";
124
+ }
125
+ };
126
+ __name(_WalletError, "WalletError");
127
+ var WalletError = _WalletError;
128
+ var _HoldNotFoundError = class _HoldNotFoundError extends WalletError {
129
+ constructor(message = "Active hold not found for reference") {
130
+ super(message);
131
+ this.name = "HoldNotFoundError";
132
+ }
133
+ };
134
+ __name(_HoldNotFoundError, "HoldNotFoundError");
135
+ var HoldNotFoundError = _HoldNotFoundError;
136
+
119
137
  // src/hooks/refund.ts
120
138
  function isWalletPayment(paymentMethod) {
121
139
  return String(paymentMethod ?? "").trim().toLowerCase() === "wallet";
@@ -133,6 +151,32 @@ function idempotencyPrefix(ctx) {
133
151
  return ctx.refundReason === "return" ? `return:${scope}` : `refund:${scope}`;
134
152
  }
135
153
  __name(idempotencyPrefix, "idempotencyPrefix");
154
+ function isHoldNotFound(error) {
155
+ if (error instanceof HoldNotFoundError) {
156
+ return true;
157
+ }
158
+ return error instanceof Error && error.name === "HoldNotFoundError";
159
+ }
160
+ __name(isHoldNotFound, "isHoldNotFound");
161
+ async function cancelEscrowHoldOrSkip(client, params) {
162
+ try {
163
+ await client.cancelHold({
164
+ reference: {
165
+ referenceType: "vendor_order",
166
+ referenceId: params.referenceId
167
+ },
168
+ idempotencyKey: params.idempotencyKey,
169
+ ...params.amount != null ? {
170
+ amount: params.amount
171
+ } : {}
172
+ });
173
+ } catch (error) {
174
+ if (!isHoldNotFound(error)) {
175
+ throw error;
176
+ }
177
+ }
178
+ }
179
+ __name(cancelEscrowHoldOrSkip, "cancelEscrowHoldOrSkip");
136
180
  function createRefundHooks(runtime) {
137
181
  const { client, platformOwnerId, flags } = runtime;
138
182
  return {
@@ -146,21 +190,15 @@ function createRefundHooks(runtime) {
146
190
  if (ctx.sellerNetClawbackRupees != null) {
147
191
  const clawbackPaise = toMinorUnit(ctx.sellerNetClawbackRupees);
148
192
  if (clawbackPaise > 0) {
149
- await client.cancelHold({
150
- reference: {
151
- referenceType: "vendor_order",
152
- referenceId: String(ctx.vendorOrderId)
153
- },
193
+ await cancelEscrowHoldOrSkip(client, {
194
+ referenceId: String(ctx.vendorOrderId),
154
195
  idempotencyKey: `${keyPrefix}:cancel_hold`,
155
196
  amount: clawbackPaise
156
197
  });
157
198
  }
158
199
  } else {
159
- await client.cancelHold({
160
- reference: {
161
- referenceType: "vendor_order",
162
- referenceId: String(ctx.vendorOrderId)
163
- },
200
+ await cancelEscrowHoldOrSkip(client, {
201
+ referenceId: String(ctx.vendorOrderId),
164
202
  idempotencyKey: `${keyPrefix}:cancel_hold`
165
203
  });
166
204
  }