「逾時」不等於「失敗」
confirm 逾時的當下,錢可能已經扣了。每個錯誤都帶著 outcome,告訴你請求有沒有送出;client.reconcile() 再去查付款明細,給你一個有憑據的答案。會動到錢的請求,結果未知時它不會替你重送。
npm install line-pay-merchantimport { createLinePayClient } from 'line-pay-merchant'
export const linePay = createLinePayClient({
channelId: process.env.LINE_PAY_CHANNEL_ID ?? '',
channelSecretKey: process.env.LINE_PAY_CHANNEL_SECRET ?? '',
env: 'sandbox' // 'production' for real payments
})export async function confirmOrder(transactionId: string, amount: number) {
try {
const { body } = await linePay.confirm({
transactionId,
amount,
currency: 'TWD'
})
return { paid: true, orderId: body.info.orderId }
} catch (error) {
// Anything but a timeout or a cut connection is an ordinary failure.
if (!needsReconcile(error)) throw error
// The payment may have gone through. Ask the payment details.
const result = await linePay.reconcile(error)
if (result.status === 'applied' && result.operation === 'confirm') {
// Not received from LINE Pay but inferred from the payment details. Check
// result.body.info.orderId against the order you sent to `request`.
return { paid: true, orderId: result.body.info.orderId }
}
// `not-applied` and `indeterminate` alike: one look does not settle it. A
// payment that LINE Pay is still processing is not in the details yet, and
// "not there now" is not "never". Keep the order as it is, tell the user it
// is being checked, and look again a few minutes later (see the background
// job): only a second look that still finds nothing lets the user try again.
return { paid: false, checking: true, status: result.status }
}
}1.0 已知限制
一般付款流程已在正式環境測試;付款請求、付款/退款明細、狀態查詢、退款、重複 confirm 與對帳已在 V4 Sandbox 驗證。預先授權付款、先授權後請款、已授權但尚未 confirm、混合支付、其他幣別等進階情境尚未完整驗證。部分退款後的折扣及促銷金額對帳也尚未完整驗證,不能視為已保證正確。原始碼仍明確標記待驗證事項。