forked from w3c/payment-request
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
5369 lines (5357 loc) · 217 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 = {
github: "https://github.com/w3c/payment-request/",
specStatus: "ED",
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,
},
],
editors: [
{
name: "Marcos Cáceres",
url: "https://github.com/marcoscaceres",
companyURL: "https://www.mozilla.com/",
company: "Mozilla",
w3cid: 39125,
},
{
name: "Danyao Wang",
url: "https://github.com/danyao",
company: "Google",
companyURL: "https://www.google.com/",
w3cid: 110796,
},
{
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,
},
],
wg: "Web Payments Working Group",
wgURI: "https://www.w3.org/Payments/WG/",
wgPatentURI: "https://www.w3.org/2004/01/pp-impl/83744/status",
testSuiteURI: "https://wpt.live/payment-request/",
implementationReportURI:
"https://w3c.github.io/test-results/payment-request/all.html",
caniuse: "payment-request",
lint: {
"check-punctuation": true,
},
doJsonLd: true,
xref: "web-platform",
mdn: true,
};
</script>
<style>
dt { margin-top: 0.75em; }
table { margin-top: 0.75em; border-collapse:collapse; border-style:hidden hidden none hidden }
table thead { border-bottom:solid }
table tbody th:first-child { border-left:solid }
table td, table th { border-left:solid; border-right:solid; border-bottom:solid thin; vertical-align:top; padding:0.2em }
li { margin-top: 0.5em; margin-bottom: 0.5em;}
</style>
</head>
<body data-cite="payment-method-id payment-method-basic-card">
<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'>
<p>
The working group maintains <a href=
"https://github.com/w3c/payment-request/issues?utf8=%E2%9C%93&q=is%3Aopen%20is%3Aissue%20-label%3A%22Priority%3A%20Postponed%22%20">
a list of all bug reports that the group has not yet addressed</a>.
Pull requests with proposed specification text for outstanding issues
are strongly encouraged.
</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>
<p>
There has been no change in dependencies on other workings groups
during the development of this specification.
</p>
<section>
<h3>
Changes since last publication
</h3>
<p>
Substantive changes to the Payment Request API since the 9 July 2018
version are as follows. The complete list of changes, including all
editorial changes, is viewable in the <a href=
"https://github.com/w3c/payment-request/commits/gh-pages">commit
history</a>.
</p>
<ul>
<li>Added support for notification when the user selects a payment
handler, but before confirming payment. This allows merchant to
update totals, validate acceptance, etc.
</li>
<li>Added support to notify site of billing address selection. This
allows a merchant to update a total (e.g., for VAT in Europe). To
enhance privacy, only some billing address data is returned to the
merchant as long as the user has not confirmed payment.
</li>
<li data-link-for="PaymentResponse">Added support for {{retry()}} and
fine-grain error reporting to the user.
</li>
<li>Added support for merchant validation by the payment handler.
</li>
<li data-link-for="PaymentRequest">Clearer definition of
<a>canMakePayment()</a> and worked to align implementations.
<a>canMakePayment()</a> does not reveal whether payment handler is
primed to pay.
</li>
<li>Removed `languageCode` and `regionCode` from {{PaymentAddress}}.
</li>
<li>Removed `currencySystem`.
</li>
<li>"<a>Payment request is showing</a>" boolean now attached to
top-level browsing context. Previously, only a single payment UI was
allowed to be shown at a time across the whole browser. This now
allows multiple browser tabs to show a payment UI at the same time
(for those payment handlers able to support it).
</li>
<li>Integrated with [[[feature-policy]]].
</li>
<li>Defined handling of multiple applicable modifiers.
</li>
</ul>
</section>
</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 basic card payment). The <dfn>payment method
provider</dfn> establishes the ecosystem to support that payment
method.
</li>
</ul>
<p>
The details of how to fulfill a payment request for a given <a>payment
method</a> is an implementation detail of a <dfn>payment handler</dfn>.
Concretely, each 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. For an example, see the <a data-cite=
"?payment-method-basic-card#steps-to-check-if-a-payment-can-be-made">can
make payment</a> algorithm of [[[?payment-method-basic-card]]].
</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>. For an example of such an
object, see the {{BasicCardResponse}} dictionary of
[[[?payment-method-basic-card]]].
</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
<a>PaymentOptions</a> 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 data-link-for="PaymentRequest">
Once a {{PaymentRequest}} is constructed, it's presented to the end
user via the <a>show()</a> method. The <a>show()</a> returns a promise
that, once the user confirms request for payment, results in a
<a>PaymentResponse</a>.
</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: "basic-card",
data: {
supportedNetworks: ["visa", "mastercard"],
},
},
{
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: "USD", value: "55.00" },
},
{
label: "Sales Tax",
amount: { currency: "USD", value: "5.00" },
type: "tax"
},
],
total: {
label: "Total due",
// The total is USD$65.00 here because we need to
// add shipping (below). The selected shipping
// costs USD$5.00.
amount: { currency: "USD", 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",
label: "🚛 Ground Shipping (2 days)",
amount: { currency: "USD", value: "5.00" },
selected: true,
},
{
id: "drone",
label: "🚀 Drone Express (2 hours)",
amount: { currency: "USD", 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: "USD", value: "3.00" },
};
// Modifiers apply when the user chooses to pay with
// a card.
const modifiers = [
{
additionalDisplayItems: [cardFee],
supportedMethods: "basic-card",
total: {
label: "Total due",
amount: { currency: "USD", 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|) 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 json = request.shippingAddress.toJSON();
await ensureCanShipTo(json);
const { shippingOptions, total } = await calculateShipping(json);
return { shippingOptions, total };
} catch (err) {
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 <a data-link-for=
"PaymentDetailsUpdate">shippingAddressErrors</a> member of the
<a>PaymentDetailsUpdate</a> dictionary to indicate that there are
validation errors with specific attributes of a {{PaymentAddress}}.
The <a data-link-for="PaymentDetailsUpdate">shippingAddressErrors</a>
member is a <a>AddressErrors</a> dictionary, whose members
specifically demarcate the fields of a <a>physical address</a> 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 data-link-for="PaymentResponse">
<h3>
POSTing payment response back to a server
</h3>
<p>
It's expected that data in a <a>PaymentResponse</a> will be POSTed
back to a server for processing. To make this as easy as possible,
<a>PaymentResponse</a> provides a <a>toJSON()</a> method that
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, options);
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>
<section data-dfn-for="PaymentRequest" data-link-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<void> abort();
[NewObject]
Promise<boolean> canMakePayment();
[NewObject]
Promise<boolean> hasEnrolledInstrument();
readonly attribute DOMString id;
readonly attribute PaymentAddress? shippingAddress;
readonly attribute DOMString? shippingOption;
readonly attribute PaymentShippingType? shippingType;
attribute EventHandler onmerchantvalidation;
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 data-link-for="PaymentRequest">
The <a>shippingAddress</a>, <a>shippingOption</a>, and
<a>shippingType</a> 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 <a>relevant global object</a>'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 <a>PaymentOptions</a>
|options|.
</p>
<p data-tests=
"payment-request-constructor.https.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 data-link-for="PaymentDetailsBase" class="algorithm">
<li>If the <a>current settings object</a>'s [=environment settings
object / responsible document=] is not <a>allowed to use</a> the
"[=payment-feature|payment=]" feature, 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 data-link-for="PaymentMethodData">
<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>For each |paymentMethod| of |methodData|:
<ol>
<li data-tests=
"payment-request-ctor-pmi-handling.https.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>If the {{PaymentMethodData/data}} member of
|paymentMethod| is missing, let |serializedData| be null.
Otherwise, let |serializedData| be the result of
<a>JSON-serializing</a>
|paymentMethod|.{{PaymentMethodData/data}} into a string.
Rethrow any exceptions.
</li>
<li>If |serializedData| is not null, and if required by the
specification that defines the
|paymentMethod|.<a>supportedMethods</a>:
<ol>
<li>Let |object| be the result of <a data-cite=
"ECMASCRIPT#sec-json.parse">JSON-parsing</a>
|serializedData|.
</li>
<li>
<p>
[=converted to an IDL value|Convert=] |object| to an
IDL value of the type specified by the specification
that defines the
|paymentMethod|.<a>supportedMethods</a> (e.g.,
{{BasicCardRequest}} in the case of
[[[?payment-method-basic-card]]]). Rethrow any
exceptions.
</p>
<p class="note">
This step assures that any IDL type conversion 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.html">
<a>Check and canonicalize total amount</a>
|details|.{{PaymentDetailsInit/total}}.{{PaymentItem/amount}}.
Rethrow any exceptions.
</li>
</ol>
</li>
<li>If the <a>displayItems</a> member of |details| is present, then
for each |item| in |details|.<a>displayItems</a>:
<ol>
<li data-tests=
"payment-request-ctor-currency-code-checks.https.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><<a>PaymentShippingOption</a>>.
</li>
<li>If the <a>shippingOptions</a> member of |details| is present,
then:
<ol data-link-for="PaymentShippingOption">
<li>Let |seenIDs| be an empty set.
</li>
<li>For each |option| in |details|.<a data-link-for=
"PaymentDetailsBase">shippingOptions</a>:
<ol>
<li data-tests=
"payment-request-ctor-currency-code-checks.https.html">
<a>Check and canonicalize amount</a>
|item|.{{PaymentItem/amount}}. Rethrow any exceptions.
</li>
<li>If |seenIDs| contains |option|.<a>id</a>, then throw
a {{TypeError}}. Optionally, inform the developer that
shipping option IDs must be unique.
</li>
<li>Otherwise, append |option|.<a>id</a> to |seenIDs|.
</li>
<li>If |option|.<a>selected</a> is true, then set
|selectedShippingOption| to |option|.<a>id</a>.
</li>
</ol>
</li>
</ol>
</li>
<li>Set |details|.{{PaymentDetailsBase/shippingOptions}} to
|options|.
</li>
</ol>
</li>
<li>Let |serializedModifierData| be an empty list.
</li>
<li data-link-for="PaymentDetailsBase">Process payment details
modifiers:
<ol>
<li>Let |modifiers| be an empty
<code><a>sequence</a></code><<a>PaymentDetailsModifier</a>>.
</li>
<li>If the <a>modifiers</a> member of |details| is present, then:
<ol>
<li>Set |modifiers| to |details|.<a>modifiers</a>.
</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.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.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
<a>JSON-serializing</a>
|modifier|.{{PaymentDetailsModifier/data}} into a 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/[[options]]=] to |options|.
</li>
<li>Set |request|.<a>[[\state]]</a> to "<a>created</a>".
</li>
<li>Set |request|.<a>[[\updating]]</a> 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|.<a>[[\response]]</a> 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" data-link-for="PaymentRequest">
<h2>
<dfn>id</dfn> attribute
</h2>
<p data-tests="payment-request-id-attribute.https.html">
When getting, the <a>id</a> attribute returns this
{{PaymentRequest}}'s
[=PaymentRequest/[[details]]=].{{PaymentDetailsInit/id}}.
</p>
</section>
<section data-dfn-for="PaymentRequest" data-link-for="PaymentRequest">
<h2>
<dfn>show()</dfn> method
</h2>
<div class="note">
<p>
The <a>show()</a> method is called when a developer wants to begin
user interaction for the payment request. The <a>show()</a> 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 <a>show()</a> method returns.
</p>
<p>
Each payment handler controls what happens when multiple browsing
context simultaneously call the <a>show()</a> 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">
<li data-tests=
"payment-request-show-method.https.html, show-method-postmessage-manual.https.html">
If the [=relevant global object=] of [=this=] does not have
[=transient activation=], return [=a promise rejected with=] with a
{{"SecurityError"}} {{DOMException}}.
</li>
<li>Let |request:PaymentRequest| be the <a>context object</a>.
</li>
<li>Let |document| be |request|'s <a>relevant global object</a>'s <a>
associated <code>Document</code></a>.
</li>
<li data-tests="rejects_if_not_active.https.html">If |document| is
not [=Document/fully active=], then return <a>a promise rejected
with</a> an {{"AbortError"}} {{DOMException}}.
</li>
<li>
<p>
Optionally, if the <a>user agent</a> wishes to disallow the call
to <a>show()</a> to protect the user, then return a promise
rejected with a {{"SecurityError"}} {{DOMException}}. For
example, the <a>user agent</a> may limit the rate at which a page
can call <a>show()</a>, as described in section <a href=
"#privacy"></a>.
</p>
</li>
<li>If |request|.<a>[[\state]]</a> is not "<a>created</a>" then
return <a>a promise rejected with</a> an {{"InvalidStateError"}}
{{DOMException}}.
</li>
<li>If the <a>user agent</a>'s <a>payment request is showing</a>
boolean is true, then:
<ol>
<li>Set |request|.<a>[[\state]]</a> to "<a>closed</a>".
</li>
<li>Return <a>a promise rejected with</a> an {{"AbortError"}}
{{DOMException}}.
</li>
</ol>
</li>
<li>Set |request|.<a>[[\state]]</a> to "<a>interactive</a>".
</li>
<li>Let |acceptPromise:Promise| be <a>a new promise</a>.
</li>
<li>Set |request|.<a>[[\acceptPromise]]</a> to |acceptPromise|.
</li>
<li>
<p>
Optionally:
</p>
<ol>
<li>Reject |acceptPromise| with an {{"AbortError"}}
{{DOMException}}.
</li>