Merchant Web SDKs
Instance.paymentRequest
doc

Instance.paymentRequest

Accept payments and get shipping information from shoppers who use Apple Pay and Google Pay.

Options

Use the Options object to handle 1-click checkout options such as shipping, and customize the display of the payment request button:

interface Instance {
// ...
paymentRequest: (options: Options) => {
destroy: () => void
}
}

interface Options {
target: HTMLElement
locale: 'en'
requestShipping?: boolean
shippingOptions?: ShippingOption[]
onShippingOptionChange?: (shippingOption: ShippingOption) => Promise<{
status: 'fail' | 'success'
total: {
amount: number
label?: string
}
}>
onShippingAddressChange?: (shippingAddress: Address) => Promise<{
status: 'fail' | 'success'
shippingOptions?: ShippingOption[]
total: {
amount: number
label?: string
}
}>
buttonStyle?: {
radius?: 'none' | 'small' | 'large' | 'round'
size?: 'none' | 'small' | 'large'
variant?: 'light' | 'dark' | 'light-outlined'
action?: 'subscribe' | 'donate' | 'pay' | 'buy'
}
}

interface ShippingOption {
id: string
label: string
amount: number
description?: string
}

type Address =
| {
city: string
phone: string
region: string
country: string
recipient: string
postalCode: string
sortingCode: string
addressLine: string[]
dependentLocality: string
}
| {
city: string
region: string
country: string
postalCode: string
}
FieldDescriptionFormat
targetDOM element where secure iframe with the button is renderedHTMLElement
onSuccessCallback when the payment is successfully completedFunction
onErrorCallback if a transaction fails, and the reason is available in the message parameter.Function
onCancelCallback if a user did not complete the transaction and canceled the authorization, or closed the payment pop-up window.Function
localeControls the language of the text in the widget. Possible values are: auto (default) - detect using browser locale, en (English), en-US (English - US), es (Spanish), de (German), fr (French), it (Italian), nl (Dutch), pl (Polish), pt (Portuguese), cs (Czech), hu (Hungarian), sk (Slovak), ja (Japanese), sv (Swedish), bg (Bulgarian), el (Greek), ro (Romanian), lt (Lithuanian), hr (Croatian)String
buttonStyleDictionary that controls the style of the buttonDictionary
validateFunction that needs to be fulfilled before payment is processedFunction
ShippingOptionsAn array of ShippingOption objects that define the shipping options that will be available to the userArray
onShippingOptionChangeFunction to determine the behaviour of the widget when Shipping options change.Function
onShippingAddressChangeFunction to determine the behaviour of the widget when Shipping address changes.Function

The ShippingOption interface is defined as follows:

FieldDescription
idA unique ID to represent the shipping option
labelThe label to be displayed as the option sheet
amountHow much the shipping option cost
descriptionA descriptive text that will be displayed below option label (optional, and shown only on Google Pay)

The Address interface is defined as follows:

FieldDescription
regionA subdivision of a country e.g. state or province
countryThe two-letter ISO-3166 country code.
postalCodeThe postal code (also known in some places as ZIP code). Some regions do not have postal codes. In such cases, this field will be set to an empty string.
cityThe name of a city, town, village, etc.
phoneThe phone number
recipientThe name of the recipient
sortingCodeThe sorting code
addressLineAn array of address line items
dependentLocalityThe locality (e.g. city or town)

Note that the onShippingAddressChange callback will receive a partial object containing the following:

{
city: string
region: string
country: string
postalCode: string
}

On completion of the payment, the full address will be received. e.g., in the paymentRequest.validate method.

The buttonStyle dictionary accepts the following parameters:

FieldDescriptionEnum
actionReplace the default text of for an action. For example, Buy with Apple Pay. This can be useful depending on the context of the button. Default: nullpay, donate, buy, subscribe, null
sizeControl whether the button should occupy the full width of the division it is in (large) or not (small). Default: largesmall, large
radiusControl the size of the border radius. Default: smallnone, small, large
variantControl the theme of the button. Default: darklight, dark, light-outlined

Returns

Methods object:

FieldDescriptionFormat
renderRender the payment request buttonFunction
canMakePaymentCheck if the user has support for a payment method e.g. Apple pay or Google PayFunction
destroyManually destroy the button if neededFunction

Example

<div id="revolut-payment-request"></div>
RevolutCheckout("<token>").then((instance) => {
const shippingOptions = [
{
id: 'flex',
label: 'The big flex express shipping',
amount: 1000,
},
{
id: 'countrystrong',
label: 'Country strong shipping',
amount: 3000,
},
]

const paymentRequest = instance.paymentRequest({
target: document.getElementById('revolut-payment-request'),
requestShipping: true,
shippingOptions,
onShippingOptionChange: (selectedShippingOption) => {
// ideally compute new total price. calls can be made to a server here
return Promise.resolve({
status: 'success',
total: {
amount: 777 + selectedShippingOption.amount,
},
})
},
onShippingAddressChange: (selectedShippingAddress) => {
// ideally compute new total price. calls can be made to a server here
const newShippingOption = {
id: 'new-shipping',
label: 'The new ultra-fast method',
amount: 500,
description: 'The shipping method faster than lightning',
}

return Promise.resolve({
status: 'success',
shippingOptions: [newShippingOption, ...shippingOptions],
total: {
amount: 777 + newShippingOption.amount,
},
})
},
onSuccess() {
setResult('Paid')
},
onError(error) {
setResult(`Error: ${error.message}`)
},
// buttonStyle: { size: 'small', variant: 'light-outlined' },
})

paymentRequest.canMakePayment().then((method) => {
if (method) {
paymentRequest.render()
} else {
setResult('Not supported')
paymentRequest.destroy()
}
})
})
Was this page helpful?