Background reconcile
An unknown outcome does not always happen while the user is still waiting. The response may be lost after the process was killed, and the user may have left the page long ago. So you need a background job that brings the orders that are "in progress" to a definite state.
How
async function settleOne(order: Order, options: SettleOptions) {
// Nothing was written down, so there is nothing to look up.
if (order.pendingConfirm === undefined) {
await orders.transition(order.id, held(order), 'needs-attention')
return
}
const result = await linePay.reconcile(order.pendingConfirm)
if (result.status === 'applied') {
if (result.operation === 'confirm') {
await markPaid(order, result.body.info.orderId)
}
} else if (result.status === 'not-applied') {
// The second look, minutes after the first, still shows nothing. If the
// user comes back to the confirmUrl and LINE Pay did process the first
// confirm after all, the second one is refused as a duplicate, and
// reconcile finds the payment. A claim that has changed hands since this
// order was read is not touched.
await orders.transition(order.id, held(order), 'awaiting-approval')
} else if (order.updatedAt.getTime() < Date.now() - options.giveUpAfterMs) {
await orders.transition(order.id, held(order), 'needs-attention')
}
}
/**
* Settle the orders whose confirm never got an answer. Run it every minute or
* so: from cron, a queue, or `setInterval`, one run at a time.
*/
async function settlePending(options: SettleOptions): Promise<void> {
const stale = await orders.findStale(
'confirming',
new Date(Date.now() - options.olderThanMs)
)
// One order that cannot be settled must not hold up the others.
const failures: unknown[] = []
for (const order of stale) {
try {
await settleOne(order, options)
} catch (error) {
failures.push(error)
}
}
if (failures.length > 0) {
throw new AggregateError(
failures,
`${failures.length} of ${stale.length} orders could not be settled`
)
}
}settlePending finds the orders that have been in confirming for longer than olderThanMs, and reconciles them with the pendingConfirm (a ReconcileContext) saved earlier:
| Reconcile result | Next step of the order |
|---|---|
applied | → paid (whatever state the order is in: the payment is the fact). |
not-applied | → awaiting-approval: this is the second look (the first was taken while the request was handled, and that one does not let go), and minutes later there is still nothing. If the user returns to the confirmUrl they can start again. |
indeterminate | Stays in confirming and is looked at again in the next round. Still unclear after giveUpAfterMs → needs-attention. |
One order that cannot be settled (say the database is briefly unreachable) does not hold up the others. settlePending goes through every order, and only then throws all the failures together as one AggregateError, so that the scheduler sees the error and can alert; the orders that were not settled come up again in the next round.
How to run it
- On a long-running server,
setIntervalis enough (see Express). Better is a separate worker or scheduler (cron, a queue) that is not tied to the process that serves HTTP. - In a serverless environment, use the scheduler of the platform (see Web-standard handler).
- Run one at a time in a process (the example waits for the previous run), and several processes may run at the same time: every transition is a conditional update, so only one wins for an order, and the loser does nothing.
Choosing the thresholds
olderThanMs: longer than a request can take overconfirmand the lookups after it. With the default time limits that is at most 40 seconds forconfirm, then up to 3 × 60 seconds for the payment details, about 3.7 minutes in all, so 5 minutes is recommended. Too short and the sweep competes with a request that is still going. With theclaimIdandmarkPaidnothing goes wrong when it does, but work is wasted, and the user may be told to "try again" for nothing.giveUpAfterMs: payment details sometimes appear late. Give them enough time, for example an hour, before handing over to a person.
A success that comes late
not-applied says "the payment details do not show it now". The recipe guards against letting go on a missing payment in two ways: the request does not release the order when it sees not-applied, and the sweep takes a second look minutes later; and markPaid makes the order paid wherever it has been given back to, as soon as the payment details show the payment.
What is left is a payment that LINE Pay completes long after both looks found nothing, with the user never coming back. Only the transaction report of the Merchant Center shows that, so comparing the orders with the report every day is the last line of defence, and no library can do it for you.
Someone has to look at needs-attention
This state means "the system cannot tell", not "failed". Please:
- alert on this state (logs, notification or dashboard);
- look the transaction up in the LINE Pay Merchant Center;
- set the order to
paidorawaiting-approvalby hand, according to what you find.
Refunds too
A refund must not be re-sent, which makes the saved record even more important:
export async function refundWithRecord(
transactionId: string,
refundAmount: number,
save: (context: ReconcileContext) => Promise<void>
) {
// A refund is not idempotent, so the record matters even more here: it is the
// only way to find out later whether the refund happened.
await save({
operation: 'refund',
startedAt: new Date(),
channelId,
transactionId,
refundAmount
})
return linePay.refund({ transactionId, refundAmount })
}The background job calls linePay.reconcile(context) on such a record:
applied: exactly one matching refund was found; record it.indeterminate: never re-send because of it. Check the Merchant Center. For a refund,reconcilenever answersnot-appliedfrom the payment details alone (see the limits).
Reading the context back from the database
A ReconcileContext can be stored as JSON. When you read it back, startedAt is a string, and reconcile takes it as it is (an ISO 8601 string with a time zone, or milliseconds since the epoch); there is no need to turn it into a Date:
await linePay.reconcile(saved)When startedAt is missing or cannot be trusted (for example a string without a time zone), reconcile answers indeterminate and says why, instead of guessing.