-
Notifications
You must be signed in to change notification settings - Fork 135
/
index.html
4633 lines (4620 loc) · 189 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>
Payment Request API
</title>
<script src='https://www.w3.org/Tools/respec/respec-w3c' class=
'remove'></script>
<script class='remove'>
var respecConfig = {
shortName: "payment-request",
github: "w3c/payment-request",
specStatus: "ED",
group: "payments",
formerEditors: [
{
name: "Domenic Denicola",
company: "Google",
w3cid: 52873,
},
{
name: "Adrian Bateman",
company: "Microsoft Corporation",
w3cid: 42763,
},
{
name: "Zach Koch",
company: "Google",
w3cid: 76588,
},
{
name: "Roy McElmurry",
company: "Facebook",
w3cid: 88345,
},
{
name: "Danyao Wang",
company: "Google",
w3cid: 110796,
},
],
editors: [
{
name: "Marcos Cáceres",
url: "https://github.com/marcoscaceres",
company: "Apple Inc.",
companyURL: "https://apple.com",
w3cid: 39125,
},
{
name: "Rouslan Solomakhin",
url: "https://github.com/rsolomakhin",
company: "Google",
companyURL: "https://www.google.com/",
w3cid: 83784,
},
{
name: "Ian Jacobs",
url: "http://www.w3.org/People/Jacobs/",
company: "W3C",
companyURL: "https://www.w3.org/",
w3cid: 2175,
},
],
previousMaturity: "CR",
crEnd: "2024-10-01",
testSuiteURI: "https://wpt.live/payment-request/",
implementationReportURI:
"https://w3c.github.io/test-results/payment-request/all.html",
prevRecURI: "https://www.w3.org/TR/2022/REC-payment-request-20220908/",
caniuse: "payment-request",
lint: {
"check-punctuation": true,
"wpt-tests-exist": true,
},
doJsonLd: true,
xref: "web-platform",
mdn: true,
};
</script>
</head>
<body data-cite="payment-method-id contact-picker">
<h1 id="title">
Payment Request API
</h1>
<section id='abstract'>
<p>
This specification standardizes an API to allow merchants (i.e. web
sites selling physical or digital goods) to utilize one or more payment
methods with minimal integration. User agents (e.g., browsers)
facilitate the payment flow between merchant and user.
</p>
</section>
<section id='sotd' class="updateable-rec">
<p>
In September 2022 the Web Payments Working Group published a <a href=
"https://www.w3.org/TR/2022/REC-payment-request-20220908/">Payment
Request Recommendation</a>. Following privacy and internationalization
reviews, the Recommendation excluded <a href=
"https://lists.w3.org/Archives/Public/public-payments-wg/2021Jun/0000.html">
capabilities</a> related to billing and shipping addresses. However,
implementations have continued to support those features interoperably,
and so the Working Group has decided to try to re-align the
specification with implementations, and re-engage the community on
associated issues.
</p>
<p>
This document is a Candidate Recommendation Snapshot based on the text
of the original Recommendation. A subsequent Candidate Recommendation
Draft will add back address capabilities and a small number of other
changes made since publication of the Recommendation.
</p>
<p>
As part of adding back support for addresses, this specification now
refers to the address components defined in the <a href=
"https://w3c.github.io/contact-picker/">Contact Picker API</a> rather
than define those components itself. Indeed, the Contact Picker API is
derived from the original definitions found in Payment Request API, and
pulled out of the specification because addresses are useful on the Web
beyond payments.
</p>
<p>
The Working Group plans to engage in discussion and follow the usual
review process before advancing the specification to Proposed
Recommendation status.
</p>
<p>
The working group will demonstrate implementation experience by
producing an <a href=
"https://w3c.github.io/test-results/payment-request/all.html">implementation
report</a>. The report will show two or more independent
implementations passing each mandatory test in the <a href=
"https://wpt.live/payment-request/">test suite</a> (i.e., each test
corresponds to a MUST requirement of the specification).
</p>
</section>
<section class='informative'>
<h2>
Introduction
</h2>
<p>
This specification describes an API that allows <a>user agents</a>
(e.g., browsers) to act as an intermediary between three parties in a
transaction:
</p>
<ul>
<li>The payee: the merchant that runs an online store, or other party
that requests to be paid.
</li>
<li>The payer: the party that makes a purchase at that online store,
and who authenticates and authorizes payment as required.
</li>
<li>The <dfn>payment method</dfn>: the means that the payer uses to pay
the payee (e.g., a card payment or credit transfer). The <dfn>payment
method provider</dfn> establishes the ecosystem to support that payment
method.
</li>
</ul>
<p>
A payment method defines:
</p>
<dl>
<dt>
An optional <dfn data-dfn-for="Payment Method">additional data
type</dfn>
</dt>
<dd>
Optionally, an IDL type that the <a>payment method</a> expects to
receive as the {{PaymentMethodData}}'s {{PaymentMethodData/data}}
member. If not specified for a given payment method, no conversion to
IDL is done and the payment method will receive
{{PaymentMethodData/data}} as JSON.
</dd>
<dt>
<dfn>Steps to validate payment method data</dfn>
</dt>
<dd>
Algorithmic steps that specify how a <a>payment method</a> validates
the {{PaymentMethodData/data}} member of the {{PaymentMethodData}},
after it is converted to the payment method's [=Payment
Method/additional data type=]. If not specified for a given payment
method, no validation is done.
</dd>
</dl>
<p>
The details of how to fulfill a payment request for a given <a>payment
method</a> is an implementation detail of a <dfn data-export="">payment
handler</dfn>, which is an application or service that handles requests
for payment. Concretely, a payment handler defines:
</p>
<dl>
<dt>
<dfn>Steps to check if a payment can be made</dfn>:
</dt>
<dd>
How a payment handler determines whether it, or the user, can
potentially "make a payment" is also an implementation detail of a
payment handler.
</dd>
<dt>
<dfn>Steps to respond to a payment request</dfn>:
</dt>
<dd>
Steps that return an object or <a>dictionary</a> that a merchant uses
to process or validate the transaction. The structure of this object
is specific to each <a>payment method</a>.
</dd>
<dt>
<dfn>Steps for when a user changes payment method</dfn> (optional)
</dt>
<dd>
<p>
Steps that describe how to handle the user changing payment method
or monetary instrument (e.g., from a debit card to a credit card)
that results in a <a>dictionary</a> or {{object}} or null.
</p>
</dd>
</dl>
<p>
This API 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.
</p>
<section id="goals">
<h2>
Goals and scope
</h2>
<ul>
<li>Allow the user agent to act as intermediary between a merchant,
user, and <a>payment method provider</a>.
</li>
<li>Enable user agents to streamline the user's payment experience by
taking into account user preferences, merchant information, security
considerations, and other factors.
</li>
<li>Standardize (to the extent that it makes sense) the communication
flow between a merchant, user agent, and <a>payment method
provider</a>.
</li>
<li>Enable a <a>payment method provider</a> to bring more secure
payment transactions to the web.
</li>
</ul>
<p>
The following are out of scope for this specification:
</p>
<ul>
<li>Create a new <a>payment method</a>.
</li>
<li>Integrate directly with payment processors.
</li>
</ul>
</section>
</section>
<section class="informative">
<h2>
Examples of usage
</h2>
<p>
In order to use the API, the developer needs to provide and keep track
of a number of key pieces of information. These bits of information are
passed to the {{PaymentRequest}} constructor as arguments, and
subsequently used to update the payment request being displayed to the
user. Namely, these bits of information are:
</p>
<ul>
<li>The |methodData|: A sequence of <a>PaymentMethodData</a>s that
represents the <a>payment methods</a> that the site supports (e.g., "we
support card-based payments, but only Visa and MasterCard credit
cards.").
</li>
<li>The |details|: The details of the transaction, as a
<a>PaymentDetailsInit</a> dictionary. This includes total cost, and
optionally a list of goods or services being purchased, for physical
goods, and shipping options. Additionally, it can optionally include
"modifiers" to how payments are made. For example, "if you pay with a
card belonging to network X, it incurs a US$3.00 processing fee".
</li>
<li>The |options|: Optionally, a list of things as {{PaymentOptions}}
that the site needs to deliver the good or service (e.g., for physical
goods, the merchant will typically need a physical address to ship to.
For digital goods, an email will usually suffice).
</li>
</ul>
<p>
Once a {{PaymentRequest}} is constructed, it's presented to the end
user via the {{PaymentRequest/show()}} method. The
{{PaymentRequest/show()}} returns a promise that, once the user
confirms request for payment, results in a {{PaymentResponse}}.
</p>
<section>
<h3>
Declaring multiple ways of paying
</h3>
<p>
When constructing a new {{PaymentRequest}}, a merchant uses the first
argument (|methodData|) to list the different ways a user can pay for
things (e.g., credit cards, Apple Pay, Google Pay, etc.). More
specifically, the |methodData| sequence contains
<a>PaymentMethodData</a> dictionaries containing the <a>payment
method identifiers</a> for the <a>payment methods</a> that the
merchant accepts and any associated <a>payment method</a> specific
data (e.g., which credit card networks are supported).
</p>
<pre class="example js" title="The `methodData` argument">
const methodData = [
{
supportedMethods: "https://example.com/payitforward",
data: {
payItForwardField: "ABC",
},
},
{
supportedMethods: "https://example.com/bobpay",
data: {
merchantIdentifier: "XXXX",
bobPaySpecificField: true,
},
},
];
</pre>
</section>
<section>
<h3>
Describing what is being paid for
</h3>
<p>
When constructing a new {{PaymentRequest}}, a merchant uses the
second argument of the constructor (|details|) to provide the details
of the transaction that the user is being asked to complete. This
includes the total of the order and, optionally, some line items that
can provide a detailed breakdown of what is being paid for.
</p>
<pre class="example js" title="The `details` argument">
const details = {
id: "super-store-order-123-12312",
displayItems: [
{
label: "Sub-total",
amount: { currency: "GBP", value: "55.00" },
},
{
label: "Value-Added Tax (VAT)",
amount: { currency: "GBP", value: "5.00" },
},
],
total: {
label: "Total due",
// The total is GBP£65.00 here because we need to
// add shipping (below). The selected shipping
// costs GBP£5.00.
amount: { currency: "GBP", value: "65.00" },
},
};
</pre>
</section>
<section>
<h3>
Adding shipping options
</h3>
<p>
Here we see an example of how to add two shipping options to the
|details|.
</p>
<pre class="example js" title="Adding shipping options">
const shippingOptions = [
{
id: "standard",
// Shipping by truck, 2 days
label: "🚛 Envío por camión (2 dias)",
amount: { currency: "EUR", value: "5.00" },
selected: true,
},
{
id: "drone",
// Drone shipping, 2 hours
label: "🚀 Drone Express (2 horas)",
amount: { currency: "EUR", value: "25.00" }
},
];
Object.assign(details, { shippingOptions });
</pre>
</section>
<section>
<h3>
Conditional modifications to payment request
</h3>
<p>
Here we see how to add a processing fee for using a card on a
particular network. Notice that it requires recalculating the total.
</p>
<pre class="example js" title=
"Modifying payment request based on card type">
// Certain cards incur a $3.00 processing fee.
const cardFee = {
label: "Card processing fee",
amount: { currency: "AUD", value: "3.00" },
};
// Modifiers apply when the user chooses to pay with
// a card.
const modifiers = [
{
additionalDisplayItems: [cardFee],
supportedMethods: "https://example.com/cardpay",
total: {
label: "Total due",
amount: { currency: "AUD", value: "68.00" },
},
data: {
supportedNetworks: networks,
},
},
];
Object.assign(details, { modifiers });
</pre>
</section>
<section>
<h3>
Requesting specific information from the end user
</h3>
<p>
Some financial transactions require a user to provide specific
information in order for a merchant to fulfill a purchase (e.g., the
user's shipping address, in case a physical good needs to be
shipped). To request this information, a merchant can pass a third
optional argument (|options:PaymentOptions |) to the
{{PaymentRequest}} constructor indicating what information they
require. When the payment request is shown, the user agent will
request this information from the end user and return it to the
merchant when the user accepts the payment request.
</p>
<pre class="example js" title="The `options` argument">
const options = {
requestPayerEmail: false,
requestPayerName: true,
requestPayerPhone: false,
requestShipping: true,
}
</pre>
</section>
<section>
<h3>
Constructing a <code>PaymentRequest</code>
</h3>
<p>
Having gathered all the prerequisite bits of information, we can now
construct a {{PaymentRequest}} and request that the browser present
it to the user:
</p>
<pre class="example js" title="Constructing a `PaymentRequest`">
async function doPaymentRequest() {
try {
const request = new PaymentRequest(methodData, details, options);
// See below for a detailed example of handling these events
request.onshippingaddresschange = ev => ev.updateWith(details);
request.onshippingoptionchange = ev => ev.updateWith(details);
const response = await request.show();
await validateResponse(response);
} catch (err) {
// AbortError, SecurityError
console.error(err);
}
}
async function validateResponse(response) {
try {
const errors = await checkAllValuesAreGood(response);
if (errors.length) {
await response.retry(errors);
return validateResponse(response);
}
await response.complete("success");
} catch (err) {
// Something went wrong...
await response.complete("fail");
}
}
// Must be called as a result of a click
// or some explicit user action.
doPaymentRequest();
</pre>
</section>
<section>
<h3>
Handling events and updating the payment request
</h3>
<p>
Prior to the user accepting to make payment, the site is given an
opportunity to update the payment request in response to user input.
This can include, for example, providing additional shipping options
(or modifying their cost), removing items that cannot ship to a
particular address, etc.
</p>
<pre class="example js" title="Registering event handlers">
const request = new PaymentRequest(methodData, details, options);
// Async update to details
request.onshippingaddresschange = ev => {
ev.updateWith(checkShipping(request));
};
// Sync update to the total
request.onshippingoptionchange = ev => {
// selected shipping option
const { shippingOption } = request;
const newTotal = {
currency: "USD",
label: "Total due",
value: calculateNewTotal(shippingOption),
};
ev.updateWith({ total: newTotal });
};
async function checkShipping(request) {
try {
const { shippingAddress } = request;
await ensureCanShipTo(shippingAddress);
const { shippingOptions, total } = await calculateShipping(shippingAddress);
return { shippingOptions, total };
} catch (err) {
// Shows error to user in the payment sheet.
return { error: `Sorry! we can't ship to your address.` };
}
}
</pre>
</section>
<section>
<h3>
Fine-grained error reporting
</h3>
<p>
A developer can use the
{{PaymentDetailsUpdate/shippingAddressErrors}} member of the
{{PaymentDetailsUpdate}} dictionary to indicate that there are
validation errors with specific attributes of a {{ContactAddress}}.
The {{PaymentDetailsUpdate/shippingAddressErrors}} member is a
{{AddressErrors}} dictionary, whose members specifically demarcate
the fields of a [=physical address=] that are erroneous while also
providing helpful error messages to be displayed to the end user.
</p>
<pre class="example js">
request.onshippingaddresschange = ev => {
ev.updateWith(validateAddress(request.shippingAddress));
};
function validateAddress(shippingAddress) {
const error = "Can't ship to this address.";
const shippingAddressErrors = {
city: "FarmVille is not a real place.",
postalCode: "Unknown postal code for your country.",
};
// Empty shippingOptions implies that we can't ship
// to this address.
const shippingOptions = [];
return { error, shippingAddressErrors, shippingOptions };
}
</pre>
</section>
<section>
<h3>
POSTing payment response back to a server
</h3>
<p>
It's expected that data in a {{PaymentResponse}} will be POSTed back
to a server for processing. To make this as easy as possible,
{{PaymentResponse}} can use the [=default toJSON steps=] (i.e.,
`.toJSON()`) to serializes the object directly into JSON. This makes
it trivial to POST the resulting JSON back to a server using the
[[[fetch]]]:
</p>
<pre class="example js" title="POSTing with `fetch()`">
async function doPaymentRequest() {
const payRequest = new PaymentRequest(methodData, details);
const payResponse = await payRequest.show();
let result = "";
try {
const httpResponse = await fetch("/process-payment", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: payResponse.toJSON(),
});
result = httpResponse.ok ? "success" : "fail";
} catch (err) {
console.error(err);
result = "fail";
}
await payResponse.complete(result);
}
doPaymentRequest();
</pre>
</section>
<section>
<h2>
Using with cross-origin iframes
</h2>
<p>
To indicate that a cross-origin [^iframe^] is allowed to invoke the
payment request API, the [^iframe/allow^] attribute along with the
"payment" keyword can be specified on the [^iframe^] element.
</p>
<pre class="example html" title=
"Using Payment Request API with cross-origin iframes">
<iframe
src="https://cross-origin.example"
allow="payment">
</iframe>
</pre>
<p>
If the [^iframe^] will be navigated across multiple origins that
support the Payment Request API, then one can set [^iframe/allow^] to
`"payment *"`. The [[[permissions-policy]]] specification provides
further details and examples.
</p>
</section>
</section>
<section data-dfn-for="PaymentRequest">
<h2>
<dfn>PaymentRequest</dfn> interface
</h2>
<pre class="idl">
[SecureContext, Exposed=Window]
interface PaymentRequest : EventTarget {
constructor(
sequence<PaymentMethodData> methodData,
PaymentDetailsInit details,
optional PaymentOptions options = {}
);
[NewObject]
Promise<PaymentResponse> show(optional Promise<PaymentDetailsUpdate> detailsPromise);
[NewObject]
Promise<undefined> abort();
[NewObject]
Promise<boolean> canMakePayment();
readonly attribute DOMString id;
readonly attribute ContactAddress? shippingAddress;
readonly attribute DOMString? shippingOption;
readonly attribute PaymentShippingType? shippingType;
attribute EventHandler onshippingaddresschange;
attribute EventHandler onshippingoptionchange;
attribute EventHandler onpaymentmethodchange;
};
</pre>
<div class="note">
<p>
A developer creates a {{PaymentRequest}} to make a payment request.
This is typically associated with the user initiating a payment
process (e.g., by activating a "Buy," "Purchase," or "Checkout"
button on a web site, selecting a "Power Up" in an interactive game,
or paying at a kiosk in a parking structure). The {{PaymentRequest}}
allows developers to exchange information with the <a>user agent</a>
while the user is providing input (up to the point of user approval
or denial of the payment request).
</p>
<p>
The {{PaymentRequest/shippingAddress}},
{{PaymentRequest/shippingOption}}, and
{{PaymentRequest/shippingType}} attributes are populated during
processing if the {{PaymentOptions/requestShipping}} member is set.
</p>
</div>
<p>
A |request|'s <dfn>payment-relevant browsing context</dfn> is that
{{PaymentRequest}}'s [=relevant global object=]'s browsing context's
<a>top-level browsing context</a>. Every <a>payment-relevant browsing
context</a> has a <dfn>payment request is showing</dfn> boolean, which
prevents showing more than one payment UI at a time.
</p>
<p class="Note">
The <a>payment request is showing</a> boolean simply prevents more than
one payment UI being shown in a single browser tab. However, a
<a>payment handler</a> can restrict the <a>user agent</a> to showing
only one payment UI across all browser windows and tabs. Other payment
handlers might allow showing a payment UI across disparate browser
tabs.
</p>
<section>
<h2>
Constructor
</h2>
<p>
The {{PaymentRequest}} is constructed using the supplied sequence of
<a>PaymentMethodData</a> |methodData| including any <a>payment
method</a> specific {{PaymentMethodData/data}}, the
<a>PaymentDetailsInit</a> |details|, and the {{PaymentOptions}}
|options|.
</p>
<p data-tests=
"payment-request-constructor.https.sub.html, payment-request-insecure.http.html">
The <code><dfn data-lt=
"PaymentRequest.PaymentRequest()">PaymentRequest(|methodData|,
|details|, |options|)</dfn></code> constructor MUST act as follows:
</p>
<ol class="algorithm">
<li>If [=this=]'s [=relevant global object=]'s [=associated
`Document`=] is not <a>allowed to use</a> the <a>"payment"</a>
permission, then [=exception/throw=] a {{"SecurityError"}}
{{DOMException}}.
</li>
<li>Establish the request's id:
<ol>
<li data-tests="payment-request-id-attribute.https.html">If
|details|.{{PaymentDetailsInit/id}} is missing, add an
{{PaymentDetailsInit/id}} member to |details| and set its value
to a <abbr title="Universally Unique Identifier">UUID</abbr>
[[RFC4122]].
</li>
</ol>
</li>
<li>Let |serializedMethodData| be an empty list.
</li>
<li>Process payment methods:
<ol>
<li>If the length of the |methodData| sequence is zero, then
[=exception/throw=] a {{TypeError}}, optionally informing the
developer that at least one <a>payment method</a> is required.
</li>
<li>Let |seenPMIs:Set| be the empty [=set=].
</li>
<li>For each |paymentMethod| of |methodData|:
<ol>
<li data-tests=
"payment-request-ctor-pmi-handling.https.sub.html">Run the
steps to <a>validate a payment method identifier</a> with
|paymentMethod|.{{PaymentMethodData/supportedMethods}}. If it
returns false, then throw a {{RangeError}} exception.
Optionally, inform the developer that the payment method
identifier is invalid.
</li>
<li>Let |pmi| be the result of parsing
|paymentMethod|.{{PaymentMethodData/supportedMethods}} with
[=basic URL parser=]:
<ol>
<li>If failure, set |pmi| to
|paymentMethod|.{{PaymentMethodData/supportedMethods}}.
</li>
</ol>
</li>
<li>If |seenPMIs| [=set/contain|contains=] |pmi| throw a
{{RangeError}} {{DOMException}} optionally informing the
developer that this [=payment method identifier=] is a
duplicate.
</li>
<li>[=set/Append=] |pmi| to |seenPMIs|.
</li>
<li>If the {{PaymentMethodData/data}} member of
|paymentMethod| is missing, let |serializedData| be null.
Otherwise, let |serializedData| be the result of [=serialize
a JavaScript value to a JSON string|serialize=]
|paymentMethod|.{{PaymentMethodData/data}} into a JSON
string. Rethrow any exceptions.
</li>
<li>If |serializedData| is not null, and if the specification
that defines the
|paymentMethod|.{{PaymentMethodData/supportedMethods}}
specifies an [=Payment Method/additional data type=]:
<ol>
<li>Let |object| be the result of <a data-cite=
"ECMASCRIPT#sec-json.parse">JSON-parsing</a>
|serializedData|.
</li>
<li>
<p>
Let |idl| be the result of [=converted to an IDL
value|converting=] |object| to an IDL value of the
[=Payment Method/additional data type=]. Rethrow any
exceptions.
</p>
</li>
<li>
<p>
Run the <a>steps to validate payment method data</a>,
if any, from the specification that defines the
|paymentMethod|.{{PaymentMethodData/supportedMethods}}
on |object|. Rethrow any exceptions.
</p>
<p class="note">
These step assures that any IDL type conversion and
validation errors are caught as early as possible.
</p>
</li>
</ol>
</li>
<li>Add the tuple
(|paymentMethod|.{{PaymentMethodData/supportedMethods}},
|serializedData|) to |serializedMethodData|.
</li>
</ol>
</li>
</ol>
</li>
<li>Process the total:
<ol>
<li data-tests=
"payment-request-ctor-currency-code-checks.https.sub.html">
<a>Check and canonicalize total amount</a>
|details|.{{PaymentDetailsInit/total}}.{{PaymentItem/amount}}.
Rethrow any exceptions.
</li>
</ol>
</li>
<li>If the {{PaymentDetailsBase/displayItems}} member of |details| is
present, then for each |item| in
|details|.{{PaymentDetailsBase/displayItems}}:
<ol>
<li data-tests=
"payment-request-ctor-currency-code-checks.https.sub.html">
<a>Check and canonicalize amount</a>
|item|.{{PaymentItem/amount}}. Rethrow any exceptions.
</li>
</ol>
</li>
<li>Let |selectedShippingOption| be null.
</li>
<li>If the {{PaymentOptions/requestShipping}} member of |options| is
present and set to true, process shipping options:
<ol>
<li>Let |options:PaymentShippingOption| be an empty
<code><a>sequence</a></code><{{PaymentShippingOption}}>.
</li>
<li>If the {{PaymentDetailsBase/shippingOptions}} member of
|details| is present, then:
<ol>
<li>Let |seenIDs| be an empty set.
</li>
<li>For each |option:PaymentShippingOption| in
|details|.{{PaymentDetailsBase/shippingOptions}}:
<ol>
<li data-tests=
"payment-request-ctor-currency-code-checks.https.sub.html">
<a>Check and canonicalize amount</a>
|item|.{{PaymentItem/amount}}. Rethrow any exceptions.
</li>
<li>If |seenIDs| contains
|option|.{{PaymentShippingOption/id}}, then throw a
{{TypeError}}. Optionally, inform the developer that
shipping option IDs must be unique.
</li>
<li>Otherwise, append
|option|.{{PaymentShippingOption/id}} to |seenIDs|.
</li>
<li>If |option|.{{PaymentShippingOption/selected}} is
true, then set |selectedShippingOption| to
|option|.{{PaymentShippingOption/id}}.
</li>
</ol>
</li>
</ol>
</li>
<li>Set |details|.{{PaymentDetailsBase/shippingOptions}} to
|options|.
</li>
</ol>
</li>
<li>Let |serializedModifierData| be an empty list.
</li>
<li>Process payment details modifiers:
<ol>
<li>Let |modifiers| be an empty
<code><a>sequence</a></code><{{PaymentDetailsModifier}}>.
</li>
<li>If the {{PaymentDetailsBase/modifiers}} member of |details|
is present, then:
<ol>
<li>Set |modifiers| to
|details|.{{PaymentDetailsBase/modifiers}}.
</li>
<li>For each |modifier| of |modifiers|:
<ol>
<li>If the {{PaymentDetailsModifier/total}} member of
|modifier| is present, then:
<ol>
<li data-tests=
"payment-request-ctor-currency-code-checks.https.sub.html">
<a>Check and canonicalize total amount</a>
|modifier|.{{PaymentDetailsModifier/total}}.{{PaymentItem/amount}}.
Rethrow any exceptions.
</li>
</ol>
</li>
<li>If the
{{PaymentDetailsModifier/additionalDisplayItems}} member
of |modifier| is present, then for each |item| of
|modifier|.{{PaymentDetailsModifier/additionalDisplayItems}}:
<ol>
<li data-tests=
"payment-request-ctor-currency-code-checks.https.sub.html">
<a>Check and canonicalize amount</a>
|item|.{{PaymentItem/amount}}. Rethrow any
exceptions.
</li>
</ol>
</li>
<li>If the {{PaymentDetailsModifier/data}} member of
|modifier| is missing, let |serializedData| be null.
Otherwise, let |serializedData| be the result of
[=serialize a JavaScript value to a JSON
string|serialize=]
|modifier|.{{PaymentDetailsModifier/data}} into a JSON
string. Rethrow any exceptions.
</li>
<li>Add the tuple
(|modifier|.{{PaymentDetailsModifier/supportedMethods}},
|serializedData|) to |serializedModifierData|.
</li>
<li>Remove the {{PaymentDetailsModifier/data}} member of
|modifier|, if it is present.
</li>
</ol>
</li>
</ol>
</li>
<li>Set |details|.{{PaymentDetailsBase/modifiers}} to
|modifiers|.
</li>
</ol>
</li>
<li>Let |request:PaymentRequest| be a new {{PaymentRequest}}.
</li>
<li>Set |request|.{{PaymentRequest/[[handler]]}} to `null`.
</li>
<li>Set |request|.{{PaymentRequest/[[options]]}} to |options|.
</li>
<li>Set |request|.{{PaymentRequest/[[state]]}} to
"[=PaymentRequest/created=]".
</li>
<li>Set |request|.{{PaymentRequest/[[updating]]}} to false.
</li>
<li>Set |request|.{{PaymentRequest/[[details]]}} to |details|.
</li>
<li>Set |request|.{{PaymentRequest/[[serializedModifierData]]}} to
|serializedModifierData|.
</li>
<li>Set |request|.{{PaymentRequest/[[serializedMethodData]]}} to
|serializedMethodData|.
</li>
<li>Set |request|.{{PaymentRequest/[[response]]}} to null.
</li>
<li>Set the value of |request|'s {{PaymentRequest/shippingOption}}
attribute to |selectedShippingOption|.
</li>
<li>Set the value of the {{PaymentRequest/shippingAddress}} attribute
on |request| to null.
</li>
<li>If |options|.{{PaymentOptions/requestShipping}} is set to true,
then set the value of the {{PaymentRequest/shippingType}} attribute
on |request| to |options|.{{PaymentOptions/shippingType}}. Otherwise,
set it to null.
</li>
<li>Return |request|.
</li>
</ol>
</section>
<section data-dfn-for="PaymentRequest">
<h2>
<dfn>id</dfn> attribute
</h2>
<p data-tests="payment-request-id-attribute.https.html">
When getting, the {{PaymentRequest/id}} attribute returns this
{{PaymentRequest}}'s
{{PaymentRequest/[[details]]}}.{{PaymentDetailsInit/id}}.
</p>
<div class="note">
<p>
For auditing and reconciliation purposes, a merchant can associate
a unique identifier for each transaction with the
{{PaymentDetailsInit/id}} attribute.
</p>
</div>
</section>
<section data-dfn-for="PaymentRequest">
<h2>
<dfn>show()</dfn> method
</h2>
<div class="note">
<p>
The {{PaymentRequest/show()}} method is called when a developer
wants to begin user interaction for the payment request. The
{{PaymentRequest/show()}} method returns a {{Promise}} that will be
resolved when the <a>user accepts the payment request</a>. Some
kind of user interface will be presented to the user to facilitate
the payment request after the {{PaymentRequest/show()}} method
returns.
</p>
<p>
Each payment handler controls what happens when multiple browsing
context simultaneously call the {{PaymentRequest/show()}} method.
For instance, some payment handlers will allow multiple payment UIs
to be shown in different browser tabs/windows. Other payment
handlers might only allow a single payment UI to be shown for the
entire user agent.
</p>
</div>
<p data-tests="payment-request-show-method.https.html">
The <code>show(optional |detailsPromise|)</code> method MUST act as
follows:
</p>
<ol class="algorithm">