Handle events

The SDK provides several events you can listen to in order to control the payment flow:

EventDescriptionHandler Signature
onReadyCalled when checkout is initialized and ready(event: CheckoutReadyEvent) => void
onSubmitCalled when user submits payment. Call event.resolve() to continue(event: PaymentSubmitEvent) => void
onSuccessCalled when payment succeeds. Call event.resolve() to complete(event: PaymentSuccessEvent) => void
onErrorCalled when payment fails. Call event.resolve() to allow retry(event: PaymentErrorEvent) => void
onPartialPaidCalled when an order is partially paid. Call event.resolve() to continue(event: PartialPaymentEvent) => void
onInfoUpdatedCalled when info (e.g., cart contents) is updated(event: InfoUpdatedEvent) => void
onShippingUpdatedCalled when shipping selection changes(event: ShippingUpdatedEvent) => void
onStateChangeCalled when the SDK lifecycle state changes (NOT_READY, INITIALIZING, READY)(state: CheckoutState) => void

Using Events for Custom Logic

You can use the onSubmit event to run async logic before the payment is processed:

onSubmit: async (event) => {
  // Perform async merchant logic
  // e.g., create internal order, freeze stock, validate inventory
  try {
    await merchantApi.createOrder({
      items: cartItems,
      customer: customerInfo
    });
    // Async done - continue with payment
    event.resolve();
  } catch (error) {
    // Cancel payment if order creation fails
    event.reject(error);
  }
}

You can use the onSuccess event to handle post-payment logic:

onSuccess: async (event) => {
  // Log the transaction
  console.log('Order ID:', event.orderId);
  console.log('Payment status:', event.paymentStatus); // { code, action }

  // Redirect to return URL or show success page
  window.location.href = event.returnUrl;

  // Mark handler as complete
  event.resolve();
}

Attach the handlers to the checkout.events object after initialization:

const checkout = await window.PayPartsSDK.init({ sessionToken, apiUrl });

checkout.events = {
  onReady: (event) => console.log('Ready', event.config),
  onSubmit: async (event) => {
    await merchantApi.createOrder(cartItems);
    event.resolve();
  },
  onSuccess: (event) => {
    window.location.href = event.returnUrl;
    event.resolve();
  },
  onError: (event) => {
    console.error(event.error, event.errors);
    event.resolve();
  },
};

Did this page help you?