Secure context
This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.
The PaymentRequest()
constructor creates a new PaymentRequest
object.
Syntax
var paymentRequest = new PaymentRequest(methodData, details[, options]);
Returns
An instance of the PaymentRequest
interface.
Parameters
- methodData
- Contains the payment method identifiers for the payment methods the merchant web site accepts and any associated payment method specific data. This parameter contains the following fields:
- supportedMethods
- For early implementations of the spec, this was a sequence of identifiers for payment methods that the merchant website accepts. Starting with more recent browsers, this parameter is more generic than credit cards, it is a single
DOMString
, and the meaning of thedata
parameter changes with thesupportedMethods
. - data
- A JSON-serializable object that provides optional information that might be needed by the supported payment methods.
- supportedNetworks Optional
- A sequence of identifiers for card networks that the merchant accepts. This parameter is supported from Chrome 57 onwards.
- details
- Provides information about the requested transaction. This parameter contains the following fields:
- total
- The total amount of the payment request.
- displayItems
- An array of optional line items for the payment request that the user agent may display, such as product details, tax, and shipping.
- shippingOptions
- The shipping options the user may choose from. If this sequence is blank, it indicates the merchant cannot ship to the current shipping address. The default shipping option may be indicated in this sequence.
- modifiers
- Modifiers for specific payment methods; for example, adjusting the total amount based on the payment method.
- options
- Lets you set options that control the behavior of the user agent. This parameter contains the following fields:
-
- requestPayerName
- A boolean indicating whether the user agent should collect the payer's name and submit it with the payment request. The default is
false
. - requestPayerEmail
- A boolean indicating whether the user agent should collect the payer's email address and submit it with the payment request. The default is
false
. - requestPayerPhone
- A boolean indicating whether the user agent should collect the payer's phone number and submit it with the payment request. The default is
false
. - requestShipping
- A boolean indicating whether the user agent should collect the payer's shipping address and submit it with the payment request. If you set this type to true, you should select an appropriate
shippingType
. The default isfalse
. - shippingType
- Lets you specify how the user interface refers to shipping when the word 'shipping' isn't appropriate for your use case. For example, in English speaking countries you would say "pizza delivery" not "pizza shipping". Valid values are
"shipping"
,"delivery"
, and"pickup"
. Quotation marks must be included. The default value is"shipping"
.
Examples
The following example shows minimal functionality and focuses instead on showing the complete context of instantiating a PaymentRequest
object.
var supportedInstruments = [{
supportedMethods: 'basic-card',
data: {
supportedNetworks: ['visa', 'mastercard', 'amex', 'jcb',
'diners', 'discover', 'mir', 'unionpay']
}
}];
var details = {
total: {label: 'Donation', amount: {currency: 'USD', value: '65.00'}},
displayItems: [
{
label: 'Original donation amount',
amount: {currency: 'USD', value: '65.00'}
}
],
shippingOptions: [
{
id: 'standard',
label: 'Standard shipping',
amount: {currency: 'USD', value: '0.00'},
selected: true
}
]
};
var options = {requestShipping: true};
try {
var request = new PaymentRequest(supportedInstruments, details, options);
// Add event listeners here.
// Call show() to trigger the browser's payment flow.
request.show().then(function(instrumentResponse) {
// Do something with the response from the UI.
})
.catch(function(err) {
// Do something with the error from request.show().
});
} catch (e) {
// Catch any other errors.
}
Specifications
Specification | Status | Comment |
---|---|---|
Payment Request API The definition of 'PaymentRequest() constructor' in that specification. |
Editor's Draft | Initial definition. |
Browser Compatibility
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support |
60 |
(Yes) | 56 (56) | ? |
No support |
? |
supportedNetworks parameter |
60 |
? | 56 (56) | ? |
No support |
? |
options.requestPayerName |
60 |
? | No support[1] | ? |
No support |
? |
Feature | Android Webview | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support |
No support |
53 | (Yes) | 56 (56) | ? |
No support |
? |
supportedNetworks parameter |
No support |
57 |
? | 56 (56) |
No support |
No support |
? |
options.requestPayerName |
No support |
58 | ? | No support[1] | ? |
No support |
? |
[1] Supported but disabled on all versions. Hidden behind the dom.payments.request.enabled
pref.