Payment Flow

Authorize a Payment Order, collect the card, cancel, and chain several sales.

Take a payment

Each sale is one Payment Order:

  1. Create the Payment Order. Keep its id, objectSecret, shop and environment associated with the purchase before authorizing or collecting.
  2. authorizePaymentOrder() prepares the payment on the phone.
  3. collectPayment() asks the customer to tap, then confirms the payment.
import { LemonwayTerminal, LemonwayTerminalError, type AuthorizedPayment } from '@lemonway/terminal';

async function collect(authorized: AuthorizedPayment) {
  try {
    const payment = await LemonwayTerminal.collectPayment(authorized);
    showSuccess(payment.amount, payment.currency);
  } catch (error) {
    if (!(error instanceof LemonwayTerminalError)) throw error;
    if (error.code === 'PAYMENT_RESULT_UNKNOWN') {
      showPendingPayment(authorized.paymentOrderId);
      return;
    }
    showPaymentError(error.code, error.message, error.hint);
  }
}

The phone must be connected before collectPayment().

collectPayment() resolves a PaymentResult:

FieldDescription
paymentOrderIdThe Payment Order id (po_…).
paymentIntentIdThe payment id (pi_…).
statusPayment status, succeeded for a captured payment.
amountAmount in cents.
currencyCurrency code, lowercase (eur).
❗️

The result is for the UI

Use collectPayment() and payment.success for UI feedback. Confirm the captured Payment Order before closing the sale.


Cancel a payment

Call cancelCollectPayment() while the app is waiting for the customer to tap. Successful cancellation of the read causes collectPayment() to reject with PAYMENT_CANCELED and emits payment.canceled. The Payment Order stays open.

During confirmation, cancelCollectPayment() rejects with PAYMENT_IN_PROGRESS. Wait for the result and check the same order if the outcome is uncertain.

After a failure

ResultNext step
PAYMENT_CANCELED during card reading, or CARD_READ_ERRORCall collectPayment() again with the same authorization.
PAYMENT_DECLINEDVerify that the existing order is refused before starting a new order for another attempt.
PAYMENT_RESULT_UNKNOWNKeep the purchase pending and block a new charge. Check the same Payment Order.

Keep the order associated with the purchase across screen changes and app restarts. Before creating a replacement order after an error, verify the existing order’s status. A timeout while checking its status leaves the purchase unresolved.

Several payments

  • One payment at a time. A second collectPayment() while one is running throws PAYMENT_IN_PROGRESS.
  • Keep the phone connected between sales. Do not reconnect for each payment.
  • Retry the same authorization after a read failure. Keep the result of authorizePaymentOrder() for the active order and reuse it when the card read can be retried.
  • Split payments. Create one Payment Order per card and group them with your own reference.
  • Several phones. Each phone is its own reader. All phones of a shop share the same location.

Test in sandbox

Use init({ ..., env: 'sandbox', simulated: true }) to select a simulated reader. Connect with a valid connection token and location. Call setSimulatedCard() with SimulatedCard.success or SimulatedCard.declined before collecting to exercise either result.

When payment simulation is enabled by the sandbox Payment API, collectPayment() uses the API mock. It uses the selected test card and defaults to a successful card. When the API reports that its mock is disabled, the SDK uses the reader selected by simulated.

The sandbox must return a valid simulation mode before the SDK can collect. If detection fails, collectPayment() rejects and emits payment.error before card collection. An unavailable endpoint, including HTTP 404, stops collection. Correct the error and retry with the same authorization. Successful detection is cached until the next init().


Did this page help you?