跳到內容

line-pay-merchantLINE Pay Online API V4 的 Node.js 函式庫

零 runtime 依賴、完整型別。付款結果不確定的時候,它如實標成「未知」,並提供查證的方法,而不是當成失敗。

安裝 ​

sh
npm install line-pay-merchant

十行看懂 ​

ts
import { 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
})
ts
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、混合支付、其他幣別等進階情境尚未完整驗證。部分退款後的折扣及促銷金額對帳也尚未完整驗證,不能視為已保證正確。原始碼仍明確標記待驗證事項。

Released under the MIT License. An independent library, not made or endorsed by LINE Pay.