Payment Flow
Authorize a Payment Order, collect the card, cancel, and chain several sales.
Take a payment
Each sale is one Payment Order:
- Create the Payment Order. Keep its
id,objectSecret, shop and environment associated with the purchase before authorizing or collecting. authorizePaymentOrder()prepares the payment on the phone.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:
| Field | Description |
|---|---|
paymentOrderId | The Payment Order id (po_…). |
paymentIntentId | The payment id (pi_…). |
status | Payment status, succeeded for a captured payment. |
amount | Amount in cents. |
currency | Currency code, lowercase (eur). |
The result is for the UIUse
collectPayment()andpayment.successfor 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
| Result | Next step |
|---|---|
PAYMENT_CANCELED during card reading, or CARD_READ_ERROR | Call collectPayment() again with the same authorization. |
PAYMENT_DECLINED | Verify that the existing order is refused before starting a new order for another attempt. |
PAYMENT_RESULT_UNKNOWN | Keep 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 throwsPAYMENT_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().
Updated 8 days ago