This specification describes a web API to allow merchants (i.e. web sites selling physical or digital goods) to easily accept payments from different payment methods with minimal integration. User agents (e.g. browsers) will facilitate the payment flow between merchant and user.
The Web Platform Incubator Community Group is incubating this specification to gather feedback to refine the API. The specification is intended to align with the chartered scope of the Web Payments Working Group and it is possible that work on the API may be transferred to that group in future.
If you wish to make comments regarding this document, please contribute to the issues list in the GitHub repo. Pull requests with proposed text changes are welcome, but please note that you will be asked to join the Web Platform Incubator Community Group and agree to the CLA for any substantive proposals.
Buying things on the web, particularly on mobile, is a frustrating experience for users. Every web site has its own flow and its own validation rules, and most require users to manually type in the same set of information over and over again. Likewise, it is difficult and time consuming for developers to create good checkout flows that support various payment schemes.
This specification describes an API that allows user agents (e.g. browsers) to act as an intermediary between the three key parties in every transaction: the merchant (e.g. an online web store), the buyer (e.g. the user buying from the online web store), and the Payment Method (e.g. credit card). Information necessary to process and confirm a transaction is passed between the Payment Method and the merchant via the user agent with the buyer confirming and authorizing as necessary across the flow.
In addition to better, more consistent user experiences, this also enables web sites to take advantage of more secure payment schemes (e.g. tokenization and system-level authentication) that are not possible with standard JavaScript libraries. This has the potential to reduce liability for the merchant and helps protect sensitive user information.
The API described in this document forms part of the Payment Request system described in the Payment Request Architecture [[PAYMENTARCH]] document.
As well as sections marked as non-normative, all authoring guidelines, diagrams, examples, and notes in this specification are non-normative. Everything else in this specification is normative.
This specification defines one class of products:
A user agent MUST behave as described in this specification in order to be considered conformant. In this specification, user agent means a Web browser or other interactive user agent as defined in [[!HTML5]].
User agents MAY implement algorithms given in this specification in any way desired, so long as the end result is indistinguishable from the result that would be obtained by the specification's algorithms.
A conforming Payment Request API user agent MUST also be a conforming implementation of the IDL fragments of this specification, as described in the “Web IDL” specification. [[!WEBIDL]]
This specification relies on several other underlying specifications.
TypeError
, JSON.stringify, and JSON.parse are
defined by [[!ECMA-262-2015]].
This document uses the format object@[[\slotname]] to mean the internal slot [[\slotname]] of the object object.
The term JSON-serializable object used in this specification means an object that can be serialized to a string using JSON.stringify and later deserialized back to an object using JSON.parse with no loss of data.
Event
type and the terms fire an event, dispatch flag,
stop propagation flag, and stop immediate propagation flag are defined by [[!DOM4]].
DOMException and the following DOMException types from [[!DOM4]] are used:
Type | Message (optional) |
---|---|
InvalidStateError | The object is in an invalid state |
NotSupportedError | The payment method was not supported |
SecurityError | The operation is only supported in a secure context |
[Constructor(sequence<DOMString> supportedMethods, PaymentDetails details, optional PaymentOptions options, optional object data)] interface PaymentRequest : EventTarget { Promise<PaymentResponse> show(); void abort(); readonly attribute ShippingAddress? shippingAddress; readonly attribute DOMString? shippingOption; /* Supports "shippingaddresschange" event */ attribute EventHandler onshippingaddresschange; /* Supports "shippingoptionchange" event */ attribute EventHandler onshippingoptionchange; };
A web page creates a PaymentRequest
to make a payment request. This is
typically associated with the user activating a "Buy", "Purchase", or "Checkout" button.
The PaymentRequest
allows the web page to exchange information with the
user agent while the user is providing input before approving or denying a payment request.
The following example shows how to construct a PaymentRequest
and begin the
user interaction:
var payment = new PaymentRequest(supportedMethods, details, options, data); payment.addEventListener("shippingaddresschange", function (changeEvent) { // Process shipping address change }); payment.show().then(function(paymentResponse) { // Process paymentResponse // paymentResponse.methodName contains the selected payment method // paymentResponse.details contains a payment method specific response paymentResponse.complete(true); }).catch(function(err) { console.error("Uh oh, something bad happened", err.message); });
The PaymentRequest
is constructed using the supplied supportedMethods
list, the payment details
, the payment options
, and any payment
method specific data
.
The supportedMethods
sequence contains the payment method identifiers
for the payment methods that the merchant web site accepts.
["visa", "bitcoin", "bobpay.com"]
The details
object contains information about the transaction that the
user is being asked to complete such as the line items in an order.
{ "items": [ { "id": "basket", "label": "Sub-total", "amount": { "currencyCode": "USD", "value" : "55.00" }, // US$55.00 }, { "id": "tax", "label": "Sales Tax", "amount": { "currencyCode": "USD", "value" : "5.00" }, // US$5.00 }, { "id": "total", "label": "Total due", "amount": { "currencyCode": "USD", "value" : "60.00" }, // US$60.00 } ] }
The options
object contains information about what options the web page
wishes to use from the payment request system.
{ "requestShipping": true }
data
is a JSON-serializable object that provides optional information that might
be needed by the supported payment methods.
{ "bobpay.com": { "merchantIdentifier": "XXXX", "bobPaySpecificField": true }, "bitcoin": { "address": "XXXX" } }
The PaymentRequest
constructor MUST act as follows:
supportedMethods
sequence is zero, then throw
a TypeError
.
SecurityError
.
SecurityError
.
There is an open issue about requiring
a top-level browsing context for using PaymentRequest
. Requiring one
is a mitigation for a user being tricked into thinking a trusted site is asking for
payment when in fact an untrusted iframe is asking for payment. The problem is some iframes may
have a legitimate reason to request payment.
details
does not contain a sequence of items
with length greater
than zero, then throw a TypeError
.
data
is not a JSON-serializable object, then throw a TypeError
.
data
does not match one of the payment method identifiers
in supportedMethods
, then throw a TypeError
.
TypeError
.
PaymentRequest
.supportedMethods
into request@[[\supportedMethods]].details
into request@[[\details]].options
into request@[[\options]].data
into request@[[\data]].shippingAddress
attribute on request to null.
shippingOption
attribute on request to null.
details
contains a shippingOptions
sequence with a
length of 1, then set shippingOption
to the id
of
the only ShippingOption
in the sequence.
The show
method is called when the page wants to begin user interaction for the
payment request. The show
method will return a Promise that will be resolved when the
user accepts the payment request. Some kind of user interface will be shown to the user to facilitate the
payment request after the show
method returns.
The show
method MUST act as follows:
PaymentRequest
object on which the method is called..
InvalidStateError
.NotSupportedError
.
The abort
method may be called if the web page wishes to abort the payment
request after the show
method has been called and before the [[\acceptPromise]]
has been resolved.
The abort
method MUST act as follows:
InvalidStateError
.The internal slot [[\state]] follows the following state transitions:
shippingAddress
is populated when the user provides a shipping
address. It is null by default.
When a user provides a shipping address, the shipping address changed algorithm runs.
onshippingaddresschange
is an EventHandler
for an
Event
named shippingaddresschange
.
shippingOption
is populated when the user chooses a shipping
option. It is null by default.
When a user chooses a shipping option, the shipping option changed algorithm runs.
onshippingoptionchange
is an EventHandler
for an
Event
named shippingoptionchange
.
Instances of PaymentRequest
are created with the internal slots in
the following table:
Internal Slot | Description (non-normative) |
---|---|
[[\supportedMethods]] | The supportMethods supplied to the constructor. |
[[\details]] |
The current PaymentDetails for the payment request initially
supplied to the constructor and then updated with calls to updateWith . |
[[\options]] | The PaymentOptions supplied to the constructor. |
[[\data]] |
The payment method specific data supplied to the constructor used
by a Payment App to influence the app's behavior.
|
[[\state]] | The current state of the payment request. |
[[\updating]] |
true is there is a pending updateWith call to update
the payment request and false otherwise.
|
[[\acceptPromise]] |
The pending Promise created during show that will be
resolved if the user accepts the payment request.
|
dictionary CurrencyAmount { required DOMString currencyCode; required DOMString value; };
A CurrencyAmount
dictionary is used to supply monetary amounts.
The following fields MUST be supplied for a CurrencyAmount
to be valid:
currencyCode
currencyCode
is a string containing a three-letter alphabetic code for the
currency as defined by [[!ISO4217]]. For example, "USD"
for US Dollars.
value
^-?[0-9]+(\.[0-9]+)?$
.
The following example shows how to represent US$55.00.
{ "currencyCode": "USD", "value" : "55.00" }
dictionary PaymentDetails { sequence<PaymentItem> items; sequence<ShippingOption> shippingOptions; };
The PaymentDetails
dictionary is passed to the PaymentRequest
constructor and provides information about the requested transaction. The PaymentDetails
dictionary is also used to update the payment request using updateWith
.
The following fields are part of the PaymentDetails
dictionary:
items
PaymentItem
dictionaries indicates what the payment
request is for. The sequence must contain at least one PaymentItem
. The last
PaymentItem
in the sequence represents the total amount of the payment
request. It is the responsibility of the calling code to ensure that the total amount is
the sum of the preceding items. The user agent MAY not validate that this is true.
shippingOptions
If the sequence is empty, then this indicates that the merchant
cannot ship to the current shippingAddress
.
If the sequence only contains one item, then this is the shipping option that
will be used and shippingOption
will be set to the id
of this option without running the shipping option changed algorithm.
dictionary PaymentOptions { boolean requestShipping = false; };
The PaymentOptions
dictionary is passed to the PaymentRequest
constructor and provides information about the options desired for the payment request.
The following fields MAY be passed to the PaymentRequest
constructor:
requestShipping
true
when physical goods need to be shipped by the merchant to the user.
This would be set to false
for an online-only electronic purchase transaction.
If this value is not supplied then the the PaymentRequest
behaves as
if a value of false
had been supplied.
dictionary PaymentItem { required DOMString id; required DOMString label; required CurrencyAmount amount; };
A sequence of one or more PaymentItem
dictionaries is included in the PaymentDetails
dictionary to indicate the what the payment request is for and the value asked for.
The following fields MUST be included in a PaymentItem
for it to be valid:
id
PaymentItem
. It MUST be
unique for a given PaymentRequest
.label
amount
CurrencyAmount
containing the monetary amount for the item.
interface ShippingAddress { /* [...] fields TBC */ };
If the requestShipping flag was set to true
in the PaymentOptions
passed to the PaymentRequest constructor, then the user agent will populate the
shippingAddress
field of the PaymentRequest
object with
the user's selected shipping address.
ShippingAddress
interface are
yet to be defined.
dictionary ShippingOption { required string id; required string label; required CurrencyAmount amount; };
The ShippingOption dictionary has fields describing a shipping option. A web page can provide the user with one or more shipping options by calling the updateWith method in response to a change event.
The following fields MUST be included in a PaymentItem
for it to be valid:
id
ShippingOption
. It MUST be
unique for a given PaymentRequest
.label
amount
CurrencyAmount
containing the monetary amount for the item.
interface PaymentResponse { readonly attribute DOMString methodName; readonly attribute object details; Promise<void> complete(boolean success); };
A PaymentResponse
is returned when a user has selected a payment method and
approved a payment request. It contains the following fields:
methodName
details
The complete
method must be called after the user has accepted the payment
request and the [[\acceptPromise]] has been resolved. The complete
method
takes a boolean argument that indicates the payment was successfully processed if true
and
that processing failed if false
. Calling the complete
method tells the user
agent that the user interaction is over (and should cause any remaining user interface to be closed).
The complete
method MUST act as follows:
InvalidStateError
.
success
to the Payment App that accepted the
payment request.
undefined
.Instances of PaymentResponse
are created with the internal slots in
the following table:
Internal Slot | Description (non-normative) |
---|---|
[[\completeCalled]] |
true if the complete method has been called and false
otherwise.
|
Event name | Interface | Dispatched when... |
---|---|---|
shippingaddresschange |
PaymentRequestUpdateEvent | The user provides a new shipping address. |
shippingoptionchange |
PaymentRequestUpdateEvent | The user chooses a new shipping option. |
[Constructor(DOMString type, optional PaymentRequestUpdateEventInit eventInitDict)] interface PaymentRequestUpdateEvent : Event { void updateWith(Promise<PaymentDetails> d); }; dictionary PaymentRequestUpdateEventInit : EventInit { };
The PaymentRequestUpdateEvent
enables the web page to update
the details of the payment request in response to a user interaction.
If the web page wishes to update the payment request then it should call updateWith
and provide a promise that will resolve with a PaymentDetails
dictionary containing changed values that the user agent SHOULD present to the user.
The PaymentRequestUpdateEvent constructor MUST set the internal slot [[\waitForUpdate]] to false.
The updateWith
method MUST act as follows:
PaymentRequest
object that is the target of
the event.
InvalidStateError
.InvalidStateError
.
InvalidStateError
.
InvalidStateError
.
d
to indicate that the payment request is valid again.
The user agent SHOULD disable any part of the user interface that could cause another update event to be fired. Only one update may be processed at a time.
d
settles.d
is resolved with details
and details
is a
PaymentDetails
dictionary, then:
details
contains an items
value, then copy
this value to the items
field of target@[[\details]].
details
contains a shippingOptions
sequence, then
copy this value to the shippingOptions
field of target@[[\details]].
details
contains a shippingOptions
sequence with a
length of 1, then set newOption to the id
of the only
ShippingOption
in the sequence.
shippingOption
on target to
newOption.
When the internal slot [[\state]] of a PaymentRequest
object is set to
interactive, the user agent will trigger the following algorithms based
on user interaction.
The shipping address changed algorithm runs when the user provides a new shipping address. It MUST run the following steps:
PaymentRequest
object that the user is
interacting with.shippingaddresschange
.shippingAddress
attribute on request to the
shipping address provided by the user.
The shipping option changed algorithm runs when the user chooses a new shipping option. It MUST run the following steps:
PaymentRequest
object that the user is
interacting with.shippingoptionchange
.shippingOption
attribute on request to the
id
string of the ShippingOption
provided by the user.
The PaymentRequest updated algorithm is run by other algorithms above to fire
an event to indicate that a user has made a change to a PaymentRequest
called request with an event name of name.
It MUST run the following steps:
PaymentRequestUpdateEvent
.
The PaymentRequest
interface allows a web page to call abort
to tell the user agent to abort the payment request and to tear down any user interface that
might be shown. For example, a web page may choose to do this the goods they are selling are
only available for a limited amount of time. If the user does not accept the payment request
within the allowed time period, then the request will be aborted.
A user agent may not always be able to abort a request. For example, if the user agent has delegated responsibility for the request to another app. To support this situation, the user agent must run the User agent delegates payment request algorithm. The algorithm MUST run the following steps:
PaymentRequest
object that the user is
interacting with.
We believe there are user agent configurations that can cause the UI to get into a state where cancellation by the web page during user interaction is difficult. Users should still be able to cancel the payment but script will not be able to. We need to investigate in more detail the consequences of this and whether it is really needed.
If we specify delegated
then it isn't necessary for all user agents to be
able to move to this state but it would be necessary for all payment flows that wish to call
abort
to account for the situation where this may fail in the delegated
state.
The user accepts the payment request algorithm runs when the user accepts the payment request and confirms that they want to pay. It MUST run the following steps:
PaymentRequest
object that the user is
interacting with.
delegated
, then terminate this algorithm and take no further action.
The user agent user interface should ensure that this never occurs.
requestShipping
value of request@[[\options]]
is true
, then if the shippingAddress
attribute of request
is null
or if the shippingOption
attribute of request
is null
, then terminate this algorithm and take no further action. This should
never occur.
PaymentResponse
.
methodName
attribute value of response to the payment method identifier
for the payment method that the user selected to accept the payment.
details
attribute value of response to a JSON-serializable object
containing the payment method specific message used by the merchant to process
the transaction. The format of this response will be defined by a Payment Transaction
Message Specification.