Accept payments and get shipping information from shoppers who use Apple Pay and Google Pay.
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
}
Field | Description | Format |
---|---|---|
target | DOM element where secure iframe with the button is rendered | HTMLElement |
onSuccess | Callback when the payment is successfully completed | Function |
onError | Callback if a transaction fails, and the reason is available in the message parameter. | Function |
onCancel | Callback if a user did not complete the transaction and canceled the authorization, or closed the payment pop-up window. | Function |
locale | Controls 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 |
buttonStyle | Dictionary that controls the style of the button | Dictionary |
validate | Function that needs to be fulfilled before payment is processed | Function |
ShippingOptions | An array of ShippingOption objects that define the shipping options that will be available to the user | Array |
onShippingOptionChange | Function to determine the behaviour of the widget when Shipping options change. | Function |
onShippingAddressChange | Function to determine the behaviour of the widget when Shipping address changes. | Function |
The ShippingOption
interface is defined as follows:
Field | Description |
---|---|
id | A unique ID to represent the shipping option |
label | The label to be displayed as the option sheet |
amount | How much the shipping option cost |
description | A descriptive text that will be displayed below option label (optional, and shown only on Google Pay) |
The Address
interface is defined as follows:
Field | Description |
---|---|
region | A subdivision of a country e.g. state or province |
country | The two-letter ISO-3166 country code. |
postalCode | The 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. |
city | The name of a city, town, village, etc. |
phone | The phone number |
recipient | The name of the recipient |
sortingCode | The sorting code |
addressLine | An array of address line items |
dependentLocality | The 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:
Field | Description | Enum |
---|---|---|
action | Replace 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: null | pay , donate , buy , subscribe , null |
size | Control whether the button should occupy the full width of the division it is in (large ) or not (small ). Default: large | small , large |
radius | Control the size of the border radius. Default: small | none , small , large |
variant | Control the theme of the button. Default: dark | light , dark , light-outlined |
Methods
object:
Field | Description | Format |
---|---|---|
render | Render the payment request button | Function |
canMakePayment | Check if the user has support for a payment method e.g. Apple pay or Google Pay | Function |
destroy | Manually destroy the button if needed | Function |
<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()
}
})
})