CargoLabs Docs

Events

What the SDK tells your page, and when.

The SDK surfaces two callbacks. Under the hood they are driven by postMessage events from the hosted flow; if you use the direct iframe path you will handle those messages yourself.

onComplete(sale)

Fires exactly once, after the buyer's payment succeeds and the policy is issued. The modal stays open on the confirmation screen; the buyer closes it when they are done, and onClose does not fire afterwards.

coverport.mount('#insure-btn', {
  key: 'pk_live_…',
  onComplete: async (sale) => {
    await fetch('/api/insurance/record', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        purchaseId: sale.purchaseId,
        amountTotal: sale.amountTotal,
        currency: sale.currency,
        testMode: sale.testMode,
        shipmentId: currentShipment.id, // your own correlation id
      }),
    });
  },
});

Client events are a signal, not the source of truth

onComplete runs in the buyer's browser and can be lost if the tab closes. Treat it as a hint to update your UI and to record a correlation id. Use the REST API from your server to confirm the purchase before you rely on it for billing or fulfilment.

onClose()

Fires when the modal is dismissed without a completed sale. Any of these trigger it:

  • The close button in the corner of the modal
  • A click on the backdrop
  • The Escape key
  • The hosted flow sending a close message (for example, a "No thanks" action inside the flow)
  • handle.destroy() while a modal is open

Use it to resume whatever the buyer was doing, or to log abandonment.

Ordering guarantees

ScenarioCallbacks
Buyer completes purchase, then closes modalonComplete only
Buyer closes modal before payingonClose only
open() called while a modal is showingOld modal is removed silently, no onClose; new modal opens
destroy() on a handle with an open modalonClose (unless a sale had already completed)

On this page