createCardField()

Creates an embedded card input field for collecting card details directly on your page. The card field is rendered in a PCI-compliant iframe, allowing you to accept card payments without handling sensitive card data.

Key features:

  • PCI-compliant iframe for secure card data collection
  • Customisable styles and classes for seamless UI integration
  • Real-time validation with error callbacks
  • Support for saved payment methods
  • Built-in loading indicators
Info

For a complete implementation guide with examples, see: Accept card payments via card field - Web

Type signature

RevolutCheckoutInstance.createCardField(
options: CardFieldOptions
): RevolutCheckoutCardField

interface CardFieldOptions {
target: HTMLElement
onSuccess?: () => void
onError?: (error: RevolutCheckoutError) => void
onValidation?: (errors: ValidationError[]) => void
onCancel?: () => void
onStatusChange?: (status: FieldStatus) => void
locale?: Locale | 'auto'
styles?: FieldStyles
classes?: FieldClasses
theme?: 'light' | 'dark'
hidePostcodeField?: boolean
showLoadingIndicator?: boolean
savePaymentMethodFor?: 'merchant' | 'customer'
name?: string
email?: string
phone?: string
billingAddress?: Address
shippingAddress?: Address
}

interface RevolutCheckoutCardField {
submit: (meta?: SubmitMeta) => void
validate: () => void
destroy: () => void
}

Parameters

ParameterDescriptionTypeRequired
optionsConfiguration object for the card field widget[CardFieldOptions](#cardfield Options-interface)Yes

CardFieldOptions interface

ParameterDescriptionTypeRequired
targetDOM element where the card field iframe should be mountedHTMLElementYes
onSuccessCallback triggered when payment completes successfully() => voidNo
onErrorCallback triggered when payment fails. Receives RevolutCheckoutError(error: RevolutCheckoutError) => voidNo
onValidationCallback triggered when card field validation state changes(errors: ValidationError[]) => voidNo
onCancelCallback triggered when user cancels payment() => voidNo
onStatusChangeCallback triggered when field status changes (focused, invalid, empty, etc.)(status: FieldStatus) => voidNo
localeWidget languageLocale | 'auto' (default: 'auto')No
stylesStyles applied to different states inside the secure iframeFieldStylesNo
classesCSS classes applied to the target element for different statesFieldClassesNo
themeColor scheme of the widget'light' | 'dark' (default: 'light')No
hidePostcodeFieldDon't ask for postcode inside the card field. When enabled, provide full billingAddress in submit()boolean (default: false)No
showLoadingIndicatorShow loading popup if payment remains pendingboolean (default: false)No
savePaymentMethodForSave payment method for future use'merchant' | 'customer'No
namePre-fill customer's full namestringNo
emailPre-fill customer's email addressstringNo
phonePre-fill customer's phone numberstringNo
billingAddressPre-fill customer's billing addressAddressNo
shippingAddressPre-fill customer's shipping addressAddressNo

FieldStyles

type FieldStyles = {
default?: Partial<CSSStyleDeclaration>
focused?: Partial<CSSStyleDeclaration>
invalid?: Partial<CSSStyleDeclaration>
empty?: Partial<CSSStyleDeclaration>
autofilled?: Partial<CSSStyleDeclaration>
completed?: Partial<CSSStyleDeclaration>
}

Styles object for customising the card field appearance inside the secure iframe.

PropertyDescriptionType
defaultBase styles always applied regardless of statePartial<CSSStyleDeclaration>
focusedApplied when user focuses inside the inputPartial<CSSStyleDeclaration>
invalidApplied on validation errorPartial<CSSStyleDeclaration>
emptyApplied when user hasn't entered any dataPartial<CSSStyleDeclaration>
autofilledApplied when user used browser autofillPartial<CSSStyleDeclaration>
completedApplied when card field is completed without errorsPartial<CSSStyleDeclaration>

FieldClasses

type FieldClasses = {
default?: string
focused?: string
invalid?: string
empty?: string
autofilled?: string
completed?: string
}

CSS classes applied to the target element outside the secure iframe. Default values:

PropertyDefault valueDescription
default'rc-card-field'Base class always applied
focused'rc-card-field--focused'Applied when input is focused
invalid'rc-card-field--invalid'Applied on validation error
empty'rc-card-field--empty'Applied when field is empty
autofilled'rc-card-field--autofilled'Applied when browser autofills
completed'rc-card-field--completed'Applied when field is complete

FieldStatus

type FieldStatus = {
focused: boolean
invalid: boolean
empty: boolean
autofilled: boolean
completed: boolean
}

Status object passed to onStatusChange callback, indicating current field state.

Return value

RevolutCheckoutCardField

interface RevolutCheckoutCardField {
submit: (meta?: SubmitMeta) => void
validate: () => void
destroy: () => void
}

The method returns a RevolutCheckoutCardField object containing:

MethodDescriptionType
submitSubmit card details with optional customer metadata(meta?: SubmitMeta) => void
validateManually trigger validation of entered card details() => void
destroyRemove the card field and clean up resources() => void

submit(meta)

The submit() method accepts an optional SubmitMeta object containing customer information:

type SubmitMeta = {
name?: string
email?: string
phone?: string
savePaymentMethodFor?: 'merchant' | 'customer'
billingAddress?: Address
shippingAddress?: Address
}
ParameterDescriptionTypeRequired
nameCustomer's full name in format "FirstName LastName"stringNo*
emailCustomer's email addressstringNo*
phoneCustomer's phone numberstringNo
savePaymentMethodForSave payment method for 'customer' (customer-initiated) or 'merchant' (merchant-initiated recurring payments)'merchant' | 'customer'No
billingAddressCustomer's billing addressAddressNo*
shippingAddressCustomer's shipping address (displayed in Merchant Dashboard only)AddressNo

* Required if not provided during order creation via API

Callback events

The card field provides callback functions for handling payment lifecycle events in your frontend.

Caution

Widget callbacks are not guaranteed to fire due to network issues, browser closures, or ad-blockers. Always use webhooks for critical backend operations like order fulfilment.

onSuccess

() => void

Triggered when the payment completes successfully.

Use cases:

  • Display success message to the customer
  • Redirect to order confirmation page
  • Update UI to reflect successful payment

Example:

onSuccess: () => {
console.log('Payment successful!')
window.location.href = '/confirmation'
}

onError

(error: RevolutCheckoutError) => void

Triggered when the payment fails. The error parameter is a RevolutCheckoutError object containing error details.

Use cases:

  • Display error message to the customer
  • Log error for debugging
  • Re-enable checkout form

Example:

onError: (error) => {
console.error('Payment failed:', error.message)
alert(`Payment failed: ${error.message}`)
}

onValidation

(errors: ValidationError[]) => void

Triggered when card field validation state changes. Receives an array of ValidationError objects. Empty array means all fields are valid.

Use cases:

  • Display inline validation errors
  • Enable/disable submit button based on validation state
  • Track validation issues

Example:

onValidation: (errors) => {
if (errors.length === 0) {
console.log('Card details are valid')
submitButton.disabled = false
} else {
console.log('Validation errors:', errors)
submitButton.disabled = true
}
}

onCancel

() => void

Triggered when the user cancels the payment flow.

Use cases:

  • Display cancellation message
  • Re-enable checkout form
  • Track abandonment analytics

Example:

onCancel: () => {
console.log('Payment cancelled')
alert('Payment was cancelled. You can try again.')
}

onStatusChange

(status: FieldStatus) => void

Triggered when the card field status changes (focused, invalid, empty, autofilled, completed).

Use cases:

  • Update UI based on field state
  • Show/hide helper text
  • Apply custom styling

Example:

onStatusChange: (status) => {
console.log('Field status:', status)
if (status.completed && !status.invalid) {
console.log('Card details are complete and valid')
}
}

Usage examples

import RevolutCheckout from '@revolut/checkout'

// Initialise with order token
const instance = await RevolutCheckout(orderToken, 'prod')

// Create card field
const cardField = instance.createCardField({
target: document.getElementById('card-field'),

onSuccess: () => {
console.log('Payment successful!')
window.location.href = '/confirmation'
},

onError: (error) => {
console.error('Payment failed:', error.message)
alert(`Payment failed: ${error.message}`)
},

onValidation: (errors) => {
const submitButton = document.getElementById('submit-button')
submitButton.disabled = errors.length > 0
}
})

// Handle form submission
document.getElementById('submit-button').addEventListener('click', () => {
cardField.submit({
name: 'John Doe',
email: '[email protected]',
billingAddress: {
countryCode: 'GB',
postcode: 'EC1A 1BB',
city: 'London',
streetLine1: '1 Example Street'
}
})
})

Pre-fill customer information

You can improve the checkout experience by pre-filling customer information:

const cardField = instance.createCardField({
target: document.getElementById('card-field'),
email: '[email protected]',
billingAddress: {
countryCode: 'GB',
region: 'Greater London',
city: 'London',
postcode: 'EC1A 1BB',
streetLine1: '1 Example Street',
streetLine2: 'Flat 2B'
},
onSuccess: () => {
window.location.href = '/confirmation'
},
onError: (error) => {
alert(`Payment failed: ${error.message}`)
}
})

// Submit without additional metadata since it's pre-filled
document.getElementById('submit-button').addEventListener('click', () => {
cardField.submit()
})

Save payment method for future use

You can save the customer's payment method for future payments using savePaymentMethodFor:

  • 'customer' - For customer-initiated future payments (express checkout, saved cards)
  • 'merchant' - For merchant-initiated recurring payments (subscriptions, auto-renewals)
// Option 1: Set during card field creation
const cardField = instance.createCardField({
target: document.getElementById('card-field'),
savePaymentMethodFor: 'customer', // or 'merchant'

onSuccess: () => {
console.log('Payment successful and card saved!')
window.location.href = '/confirmation'
},

onError: (error) => {
alert(`Payment failed: ${error.message}`)
}
})

document.getElementById('submit-button').addEventListener('click', () => {
cardField.submit({
name: 'John Doe',
email: '[email protected]',
billingAddress: {
countryCode: 'GB',
postcode: 'EC1A 1BB',
city: 'London',
streetLine1: '1 Example Street'
}
})
})
// Option 2: Set at submit time
const cardField = instance.createCardField({
target: document.getElementById('card-field'),

onSuccess: () => {
window.location.href = '/confirmation'
},

onError: (error) => {
alert(`Payment failed: ${error.message}`)
}
})

document.getElementById('submit-button').addEventListener('click', () => {
cardField.submit({
name: 'John Doe',
email: '[email protected]',
savePaymentMethodFor: 'customer', // or 'merchant'
billingAddress: {
countryCode: 'GB',
postcode: 'EC1A 1BB',
city: 'London',
streetLine1: '1 Example Street'
}
})
})

See also

Was this page helpful?