Skip to main content

Events & Callbacks Reference

Complete technical reference for all Primer SDK events, callbacks, and instance methods.

Practical Guide Available

For practical examples and implementation patterns, see the Events Guide.

DOM Events

All Primer SDK events are CustomEvents with bubbles: true and composed: true, allowing them to propagate through the DOM and cross shadow DOM boundaries.

Events Overview

Event NameDetail TypeDescription
primer:state-changeSdkStateContextTypeDispatched when SDK state changes (loading, processing, success, error)
primer:methods-updateInitializedPaymentsDispatched when available payment methods are updated
primer:readyPrimerJSDispatched when SDK is fully initialized and ready
primer:card-network-changeCardNetworksContextTypeDispatched when card network is detected or changed
primer:card-submitCardSubmitPayloadTriggerable event to submit the card form programmatically
primer:card-successCardSubmitSuccessPayloadDispatched when card form submission succeeds
primer:card-errorCardSubmitErrorsPayloadDispatched when card form validation errors occur

primer:state-change

Dispatched when SDK state changes during the payment lifecycle.

Payload Properties:

PropertyTypeDescription
isSuccessfulbooleanWhether the payment operation completed successfully
isProcessingbooleanWhether a payment operation is currently in progress
errorError | nullError object if an error occurred
isLoadingbooleanWhether the SDK is loading resources or initializing
failure{ code: string; message: string; details?: Record<string, unknown> } | nullStructured failure information with code and message

Usage Note: Listen to this event to show loading spinners, success messages, or error states.

primer:methods-update

Dispatched when available payment methods are loaded or updated.

Payload Properties:

The event detail is an InitializedPayments instance with the following methods:

MethodReturnsDescription
toArray()InitializedPaymentMethod[]Returns all payment methods as an array
size()numberReturns the count of available payment methods
get<T>(type: T)PaymentMethodByType<T> | undefinedRetrieves a specific payment method by type

Usage Note: Use this event to dynamically render payment methods or check availability.

primer:ready

Dispatched when the Primer SDK is fully initialized and ready for use.

Payload Properties:

The event detail contains the PrimerJS instance with the following properties:

PropertyTypeDescription
onPaymentStart() => voidCallback for payment creation start
onPaymentPrepare(data: PaymentData, handler: PrepareHandler) => voidCallback for payment preparation
onPaymentComplete(data: PaymentCompleteData) => voidCallback for payment completion
refreshSession()() => Promise<void>Method to refresh the checkout session
getPaymentMethods()() => PaymentMethodInfo[]Method to retrieve cached payment methods

Usage Note: Use this event to configure callbacks and access PrimerJS instance methods.

primer:card-network-change

Dispatched when card network is detected or changes based on card number input.

Payload Properties:

PropertyTypeDescription
detectedCardNetworkCardNetwork | nullThe detected card network (Visa, Mastercard, etc.)
selectableCardNetworksCardNetwork[]Array of selectable card networks for co-branded cards
isLoadingbooleanWhether card network detection is in progress

CardNetwork Type:

PropertyTypeDescription
displayNamestringHuman-readable card network name
networkstringCard network identifier

Usage Note: Use this event to display card brand logos or network-specific messaging.

primer:card-submit

Triggerable event to submit the card form programmatically from anywhere in your application.

Payload Properties:

PropertyTypeDescription
sourcestring (optional)Identifier for the trigger source (e.g., "custom-button")

Usage Note: Dispatch this event to trigger card form submission when using external submit buttons.

primer:card-success

Dispatched when card form submission completes successfully.

Payload Properties:

PropertyTypeDescription
resultCardSubmitResultSubmission result data

CardSubmitResult Type:

PropertyTypeDescription
successboolean (optional)Whether submission was successful
tokenstring (optional)Payment token generated
analyticsIdstring (optional)Analytics tracking identifier
paymentIdstring (optional)Payment identifier

Usage Note: Use this event to confirm successful card form validation and submission.

primer:card-error

Dispatched when card form validation errors occur.

Payload Properties:

PropertyTypeDescription
errorsInputValidationError[]Array of validation errors

InputValidationError Type:

PropertyTypeDescription
fieldstringField name that failed validation
namestringError name/type
errorstringHuman-readable error message

Usage Note: Use this event to display validation error messages to users.

PrimerJS Callbacks

Callbacks are set on the PrimerJS instance (accessible via the primer:ready event) to handle payment lifecycle events.

Callbacks Overview

CallbackParametersDescription
onPaymentStartNoneCalled when payment creation begins
onPaymentPreparedata: PaymentData, handler: PrepareHandlerCalled before payment is created, allows validation or abort
onPaymentCompletedata: PaymentCompleteDataCalled when payment completes (success or error)

onPaymentStart

Called when payment creation begins.

onPaymentStart?: () => void;

Usage Note: Use this callback to show loading indicators or disable UI elements during payment processing.

onPaymentPrepare

Called before payment is created, allowing validation or aborting the payment flow.

onPaymentPrepare?: (data: PaymentData, handler: PrepareHandler) => void;

Parameters:

NameTypeDescription
dataPaymentDataContains paymentMethodType being used
handlerPrepareHandlerObject with continuePaymentCreation() and abortPaymentCreation() methods

Usage Note: Use this callback to validate order details or perform pre-payment checks before proceeding.

onPaymentComplete

Called when payment completes with success, pending, or error status.

onPaymentComplete?: (data: PaymentCompleteData) => void;

Parameters:

NameTypeDescription
dataPaymentCompleteDataContains payment result, status, and error (if applicable)

Usage Note: Use this callback to handle payment outcomes and redirect users appropriately.

PrimerJS Instance Methods

Public methods available on the PrimerJS instance (accessible via the primer:ready event).

Methods Overview

MethodReturnsDescription
refreshSession()Promise<void>Synchronizes client-side SDK with server-side session updates
getPaymentMethods()PaymentMethodInfo[]Returns cached list of available payment methods

refreshSession()

Synchronizes client-side SDK with server-side session updates.

async refreshSession(): Promise<void>

Use Case: Call this method after updating the checkout session on your server to refresh payment methods or order details.

getPaymentMethods()

Returns the cached list of available payment methods.

getPaymentMethods(): PaymentMethodInfo[]

Use Case: Use this method to check which payment methods are available without waiting for events.

Type Definitions

Payment Types

PaymentCompleteData

interface PaymentCompleteData {
payment: Payment | null;
status: 'success' | 'pending' | 'error';
error?: PrimerClientError;
}

PaymentData

interface PaymentData {
paymentMethodType: string;
}

Event Payload Types

SdkStateContextType / SdkState

type SdkStateContextType = SdkState | null;

type SdkState = {
isSuccessful: boolean;
isProcessing: boolean;
error: Error | null;
isLoading: boolean;
failure: {
code: string;
message: string;
details?: Record<string, unknown>;
} | null;
};

InitializedPayments

class InitializedPayments {
get<T extends RedirectPaymentMethodTypes>(
type: T,
): RedirectPaymentMethod | undefined;

get<T extends (typeof PaymentMethodType)[keyof typeof PaymentMethodType]>(
type: T,
): PaymentMethodByType<T> | undefined;

toArray(): InitializedPaymentMethod[];

size(): number;
}

CardNetworksContextType / CardNetwork

type CardNetworksContextType = {
detectedCardNetwork: CardNetwork | null;
selectableCardNetworks: CardNetwork[];
isLoading: boolean;
} | null;

type CardNetwork = {
displayName: string;
network: string;
};

CardSubmitPayload

interface CardSubmitPayload {
source?: string;
}

CardSubmitSuccessPayload / CardSubmitResult

interface CardSubmitSuccessPayload {
result: CardSubmitResult;
}

interface CardSubmitResult {
success?: boolean;
token?: string;
analyticsId?: string;
paymentId?: string;
[key: string]: unknown;
}

CardSubmitErrorsPayload

interface CardSubmitErrorsPayload {
errors: InputValidationError[];
}

Handler Types

PrepareHandler

interface PrepareHandler {
continuePaymentCreation: () => void;
abortPaymentCreation: () => void;
}

See Also