forked from w3c/payment-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
2266 lines (2248 loc) · 91.7 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 lang="en-US">
<head>
<meta charset="utf-8">
<title>
Payment Handler 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-handler/",
specStatus: "ED",
previousPublishDate: "2019-10-21",
prevVersion: "FPWD",
previousMaturity: "WD",
editors: [
{ name: "Adrian Hope-Bailie",
url: "https://github.com/adrianhopebailie",
company: "Coil",
companyURL: "https://coil.com",
w3cid: 42590
},
{ name: "Ian Jacobs",
url: "http://www.w3.org/People/Jacobs/",
company: "W3C",
companyURL: "https://www.w3.org/",
w3cid: 2175
},
{ name: "Rouslan Solomakhin",
url: "https://github.com/rsolomakhin",
company: "Google",
companyURL: "https://www.google.com/",
w3cid: 83784
},
{ name: "Jinho Bang",
company: "Samsung",
companyURL: "https://www.samsung.com/",
w3cid: 77147
},
],
formerEditors: [
{ name: "Andre Lyver",
url: "https://github.com/lyverovski",
company: "Shopify",
companyURL: "https://shopify.com",
w3cid: 87485
},
{ name: "Tommy Thorsen",
url: "https://github.com/tommythorsen",
company: "Opera",
companyURL: "https://opera.com",
w3cid: 90636,
},
{ name: "Adam Roach",
url: "https://github.com/adamroach",
company: "Mozilla",
companyURL: "https://mozilla.org",
w3cid: 67785
},
],
group: "payments",
testSuiteURI: "https://wpt.live/payment-handler/",
xref: "web-platform",
};
</script>
</head>
<body data-cite="service-workers payment-method-id">
<section id="abstract">
<p>
This specification defines capabilities that enable Web applications to
handle requests for payment.
</p>
</section>
<section id="sotd">
<p>
The Web Payments Working Group maintains <a href=
"https://github.com/w3c/payment-handler/issues">a list of all bug
reports that the group has not yet addressed</a>. This draft highlights
some of the pending issues that are still to be discussed in the
working group. No decision has been taken on the outcome of these
issues including whether they are valid. Pull requests with proposed
specification text for outstanding issues are strongly encouraged.
</p>
</section>
<section class="informative">
<h2>
Introduction
</h2>
<p>
This specification defines a number of new features to allow web
applications to handle requests for payments on behalf of users:
</p>
<ul>
<li>An origin-based permission to handle payment request events.
</li>
<li>A payment request event type {{PaymentRequestEvent}}). A <a>payment
handler</a> is an event handler for the {{PaymentRequestEvent}}.
</li>
<li>An extension to the service worker registration interface
({{PaymentManager}} to manage the definition, display, and user
selection of {{PaymentInstrument}}s.
</li>
<li>A mechanism to respond to the {{PaymentRequestEvent}}.
</li>
</ul>
<p class="note">
This specification does not address how software built with
operating-system specific mechanisms (i.e., "native apps") handle
payment requests.
</p>
</section>
<section id="model">
<h2>
Overview of Handling Payment Requests
</h2>
<p>
In this document we envision the following flow:
</p>
<ol>
<li>An origin requests permission from the user to handle payment
requests for a set of supported payment methods. For example, a user
visiting a retail or bank site may be prompted to register a payment
handler from that origin. The origin establishes the scope of the
permission but the origin's capabilities may evolve without requiring
additional user consent.
</li>
<li>
<a>Payment handlers</a> are defined in <a>service worker</a> code.
</li>
<li>The {{PaymentManager}} is used to set a list of <a data-lt=
"PaymentManager.instruments">payment instruments</a>. Each
<a data-lt="PaymentInstrument">payment instrument</a> provides data
to the user agent to improve the user experience of selecting payment
credentials:
<ul>
<li>The <a data-lt="PaymentInstrument.method">method</a> informs
the user agent decision whether to display this instrument as a
candidate for payment.
</li>
<li>When the instrument matches what the payee accepts, the user
agent may display the <a data-lt="PaymentInstrument.name">name</a>
and <a data-lt="PaymentInstrument.icons">icon</a>. These provide
hints about payment credentials that the user agent will return in
the <a>PaymentHandlerResponse</a> if the user selects this
instrument.
</li>
</ul>
</li>
<li>When the merchant (or other <dfn>payee</dfn>) calls the
[[payment-request]] method <a>canMakePayment()</a> or <a>show()</a>
(e.g., when the user pushes a button on a checkout page), the user
agent computes a list of candidate payment handlers, comparing the
payment methods accepted by the merchant with those supported by
registered payment handlers. For payment methods that support
additional filtering, <a>CanMakePaymentEvent</a> is used as part of
determining whether there is a match.
</li>
<li>The user agent displays a set of choices to the user: the
registered <a data-lt="PaymentManager.instruments">instruments</a> of
the candidate payment handlers. The user agent displays these choices
using information (labels and icons) provided at registration or
otherwise available from the Web app.
</li>
<li>When the user (the <dfn>payer</dfn>) selects an <a data-lt=
"PaymentManager.instruments">instrument</a>, the user agent fires a
{{PaymentRequestEvent}} (cf. the <a>user interaction task source</a>)
in the service worker whose <a data-lt=
"ServiceWorkerRegistration.paymentManager">PaymentManager</a> the
instrument was registered with. The {{PaymentRequestEvent}} includes
some information from the PaymentRequest (defined in
[[!payment-request]]) as well as additional information (e.g., origin
and selected instrument).
</li>
<li>Once activated, the payment handler performs whatever steps are
necessary to <a href="#handling-a-payment-request">handle the payment
request</a>, and return an appropriate payment response to the
<a>payee</a>. If interaction with the user is necessary, the <a>payment
handler</a> can open a window for that purpose.
</li>
<li>The user agent receives a response asynchronously once the payment
handler has finished handling the request. The response becomes the
PaymentResponse (of [[!payment-request]]).
</li>
</ol>
<p class="note">
An origin may implement a payment app with more than one service worker
and therefore multiple <a>payment handlers</a> may be registered per
origin. The handler that is invoked is determined by the selection made
by the user of a <a data-lt="PaymentManager.instruments">payment
instrument</a>. The <a>service worker</a> which stored the <a data-lt=
"PaymentManager.instruments">payment instrument</a> with its
<a data-lt="ServiceWorkerRegistration.paymentManager">PaymentManager</a>
is the one that will be invoked.
</p>
<section class="informative" id="handling-a-payment-request">
<h2>
Handling a Payment Request
</h2>
<p>
A <dfn>payment handler</dfn> is a Web application that can handle a
request for payment on behalf of the user.
</p>
<p>
The logic of a payment handler is driven by the payment methods that
it supports. Some payment methods expect little to no processing by
the payment handler which simply returns payment card details in the
response. It is then the job of the payee website to process the
payment using the returned data as input.
</p>
<p>
In contrast, some payment methods, such as a crypto-currency payments
or bank originated credit transfers, require that the payment handler
initiate processing of the payment. In such cases the payment handler
will return a payment reference, endpoint URL or some other data that
the payee website can use to determine the outcome of the payment (as
opposed to processing the payment itself).
</p>
<p>
Handling a payment request may include numerous interactions: with
the user through a new window or other APIs (such as
[[[WebCryptoAPI]]]) or with other services and origins through web
requests or other means.
</p>
<p>
This specification does not address these activities that occur
between the payment handler accepting the {{PaymentRequestEvent}} and
the payment handler returning a response. All of these activities
which may be required to configure the payment handler and handle the
payment request, are left to the implementation of the payment
handler, including:
</p>
<ul>
<li>how the user establishes an account with an origin that provides
payment services.
</li>
<li>how an origin authenticates a user.
</li>
<li>how communication takes place between the payee server and the
payee Web application, or between a payment app origin and other
parties.
</li>
</ul>
<p>
Thus, an origin will rely on many other Web technologies defined
elsewhere for lifecycle management, security, user authentication,
user interaction, and so on.
</p>
</section>
<section class="informative">
<h2>
Structure of a Web Payment App
</h2>
<figure>
<img alt=
"Architecture of a (Web) payment apps as defined in this specification."
src="app-arch.png">
<figcaption>
A Web payment app is associated with an origin. Payment handlers
respond to {{PaymentRequestEvent}}s. {{PaymentManager}} manage the
definition, display, and user selection of {{PaymentInstrument}}s.
A {{PaymentInstrument}} supports one or more payment methods.
</figcaption>
</figure>
</section>
<section class="informative">
<h2>
Relation to Other Types of Payment Apps
</h2>
<p>
This specification does not address how third-party mobile payment
apps interact (through proprietary mechanisms) with user agents, or
how user agents themselves provide simple payment app functionality.
</p>
<figure>
<img alt=
"Different types of payment apps. Payment Handler API is for Web apps."
src="app-types.png">
<figcaption>
Payment Handler API enables Web apps to handle payments. Other
types of payment apps may use other (proprietary) mechanisms.
</figcaption>
</figure>
</section>
</section>
<section id="registration">
<h2>
Registration
</h2>
<p>
One registers a payment handler with the user agent when assigning the
first {{PaymentInstrument}} to it through the
{{PaymentInstruments/set()}} method.
</p>
<section data-dfn-for="ServiceWorkerRegistration">
<h2>
Extension to the `ServiceWorkerRegistration` interface
</h2>
<pre class="idl">
partial interface ServiceWorkerRegistration {
[SameObject] readonly attribute PaymentManager paymentManager;
};
</pre>
<p>
The <dfn>paymentManager</dfn> attribute exposes payment handler
functionality in the service worker.
</p>
</section>
<section data-dfn-for="PaymentManager">
<h2>
<dfn>PaymentManager</dfn> interface
</h2>
<pre class="idl">
[SecureContext, Exposed=(Window,Worker)]
interface PaymentManager {
[SameObject] readonly attribute PaymentInstruments instruments;
attribute DOMString userHint;
};
</pre>
<p>
The {{PaymentManager}} is used by <a>payment handler</a>s to manage
their associated instruments as well as supported payment methods.
</p>
<section>
<h2>
<dfn>instruments</dfn> attribute
</h2>
<p>
This attribute allows manipulation of payment instruments
associated with a service worker (and therefore its payment
handler). To be a candidate payment handler, a handler must have at
least one registered payment instrument to present to the user.
That instrument needs to match the payment methods specified by the
payment request.
</p>
</section>
<section>
<h2>
<dfn>userHint</dfn> attribute
</h2>
<p>
When displaying payment handler name and icon, the user agent may
use this string to improve the user experience. For example, a user
hint of "**** 1234" can remind the user that a particular card is
available through this payment handler. When a agent displays all
payment instruments available through a payment handler, it may
cause confusion to display the additional hint.
</p>
</section>
</section>
<section data-dfn-for="PaymentInstruments">
<h2>
<dfn>PaymentInstruments</dfn> interface
</h2>
<pre class="idl">
[SecureContext, Exposed=(Window,Worker)]
interface PaymentInstruments {
Promise<boolean> delete(DOMString instrumentKey);
Promise<any> get(DOMString instrumentKey);
Promise<sequence<DOMString>> keys();
Promise<boolean> has(DOMString instrumentKey);
Promise<undefined> set(DOMString instrumentKey, PaymentInstrument details);
Promise<undefined> clear();
};
</pre>
<p>
The {{PaymentInstruments}} interface represents a collection of
payment instruments, each uniquely identified by an
<dfn>instrumentKey</dfn>. The <var>instrumentKey</var> identifier
will be passed to the payment handler to indicate the
{{PaymentInstrument}} selected by the user, if any.
</p>
<section>
<h2>
<dfn>delete()</dfn> method
</h2>
<p>
When called, this method executes the following steps:
</p>
<ol>
<li>Let |p:Promise| be [=a new promise=].
</li>
<li>Return |p| and perform the remaining steps in parallel:
</li>
<li>If the collection contains a {{PaymentInstrument}} with a
matching <var>instrumentKey</var>, remove it from the collection
and resolve |p| with <b>true</b>.
</li>
<li>Otherwise, resolve |p| with <b>false</b>.
</li>
</ol>
</section>
<section>
<h2>
<dfn>get()</dfn> method
</h2>
<p>
When called, this method executes the following steps:
</p>
<ol>
<li>Let |p:Promise| be [=a new promise=].
</li>
<li>Return |p| and perform the remaining steps in parallel:
</li>
<li>If the collection contains a {{PaymentInstrument}} with a
matching <var>instrumentKey</var>, resolve |p| with that
{{PaymentInstrument}}.
</li>
<li>Otherwise, resolve |p| with `undefined`.
</li>
</ol>
</section>
<section>
<h2>
<dfn>keys()</dfn> method
</h2>
<p>
When called, this method executes the following steps:
</p>
<ol>
<li>Let |p:Promise| be [=a new promise=].
</li>
<li>Return |p| and perform the remaining steps in parallel:
</li>
<li>Resolve |p| with a <dfn>Sequence</dfn> that contains all the
<var>instrumentKey</var>s for the {{PaymentInstrument}}s contained
in the collection, in original insertion order.
</li>
</ol>
</section>
<section>
<h2>
<dfn>has()</dfn> method
</h2>
<p>
When called, this method executes the following steps:
</p>
<ol>
<li>Let |p:Promise| be [=a new promise=].
</li>
<li>Return |p| and perform the remaining steps in parallel:
</li>
<li>If the collection contains a {{PaymentInstrument}} with a
matching <var>instrumentKey</var>, resolve |p| with <b>true</b>.
</li>
<li>Otherwise, resolve |p| with <b>false</b>.
</li>
</ol>
</section>
<section>
<h2>
<dfn>set()</dfn> method
</h2>
<p>
When called, this method executes the following steps:
</p>
<ol>
<li>Let <var>registration</var> be the {{PaymentInstrument}}'s
associated <a>service worker registration</a>.
</li>
<li>If <var>registration</var> has no <a>active worker</a>, then
reject a {{Promise}} with an {{"InvalidStateError"}}
{{DOMException}} and terminate these steps.
</li>
<li>Upon user agent discretion and depending on user consent,
optionally return a {{Promise}} rejected with a
{{NotAllowedError}}.
</li>
<li>If the <a data-lt="PaymentInstrument.icons">icons</a> member of
<var>details</var> is present, then:
<ol>
<li>Let <var>convertedIcons</var> be the result of running the
<a>convert image objects</a> algorithm passing
<var>details</var>.<a data-lt=
"PaymentInstrument.icons">icons</a> as the argument.
</li>
<li>If the <var>convertedIcons</var> is an empty
<a>Sequence</a>, then return a {{Promise}} rejected with a
{{TypeError}}.
</li>
<li>Set <var>details</var>.<a data-lt=
"PaymentInstrument.icons">icons</a> to
<var>convertedIcons</var>.
</li>
</ol>
</li>
<li>Let |p:Promise| be [=a new promise=].
</li>
<li>Return |p| and perform the remaining steps in parallel:
</li>
<li>If the <a data-lt="PaymentInstrument.icons">icons</a> member of
<var>details</var> is present, then for each <var>icon</var> in
<var>details</var>.<a data-lt="PaymentInstrument.icons">icons</a>:
<ol>
<li>If the user agent wants to display the <var>icon</var>,
then:
<ol>
<li>Let <var>fetchedImage</var> be the result of
<a data-cite="appmanifest#fetching-image-resources">steps
to fetch an image resource</a> passing <var>icon</var> as
the argument.
</li>
<li>Set <var>icon</var>.{{ PaymentRequestEvent/[[fetchedImage]] }} to
<var>fetchedImage</var>.
</li>
</ol>
</li>
</ol>
</li>
<li>If the collection contains a {{PaymentInstrument}} with a
matching <var>instrumentKey</var>, replace it with the
{{PaymentInstrument}} in <var>details</var>.
</li>
<li>Otherwise, insert the {{PaymentInstrument}} in
<var>details</var> as a new member of the collection and associate
it with the key <var>instrumentKey</var>.
</li>
<li>Resolve |p|.
</li>
</ol>
</section>
<section>
<h2>
<dfn>clear()</dfn> method
</h2>
<p>
When called, this method executes the following steps:
</p>
<ol>
<li>Let |p:Promise| be [=a new promise=].
</li>
<li>Return |p| and perform the remaining steps in parallel:
</li>
<li>Remove all {{PaymentInstrument}}s from the collection and
resolve |p|.
</li>
</ol>
</section>
<section data-dfn-for="PaymentInstrument" data-link-for=
"PaymentInstrument">
<h2>
<dfn>PaymentInstrument</dfn> dictionary
</h2>
<pre class="idl">
dictionary PaymentInstrument {
required DOMString name;
sequence<ImageObject> icons;
DOMString method;
};
</pre>
<dl>
<dt>
<dfn>name</dfn> member
</dt>
<dd>
The <a>name</a> member is a string that represents the label for
this {{PaymentInstrument}} as it is usually displayed to the
user.
</dd>
<dt>
<dfn>icons</dfn> member
</dt>
<dd>
The <a>icons</a> member is an array of image objects that can
serve as iconic representations of the payment instrument when
presented to the user for selection.
</dd>
<dt>
<dfn>method</dfn> member
</dt>
<dd>
The <a>method</a> member is the <a>payment method identifier</a>
of the <a>payment method</a> supported by this instrument.
</dd>
</dl>
</section>
<section data-dfn-for="ImageObject" data-link-for="ImageObject">
<h2>
<dfn>ImageObject</dfn> dictionary
</h2>
<pre class="idl">
dictionary ImageObject {
required USVString src;
DOMString sizes;
DOMString type;
};
</pre>
<dl>
<dt>
<dfn>src</dfn> member
</dt>
<dd>
The <a>src</a> member is used to specify the <a>ImageObject</a>'s
source. It is a URL from which the user agent can fetch the
image’s data.
</dd>
<dt>
<dfn>sizes</dfn> member
</dt>
<dd>
The <a>sizes</a> member is used to specify the
<a>ImageObject</a>'s sizes. It follows the spec of sizes member
in HTML [^link^] element, which is a string consisting of an
<a>unordered set of unique space-separated tokens</a> which are
[=ASCII case-insensitive=] that represents the dimensions of an
image. Each keyword is either an [=ASCII case-insensitive=] match
for the string "any", or a value that consists of two valid
non-negative integers that do not have a leading U+0030 DIGIT
ZERO (0) character and that are separated by a single U+0078
LATIN SMALL LETTER X or U+0058 LATIN CAPITAL LETTER X character.
The keywords represent icon sizes in raw pixels (as opposed to
CSS pixels). When multiple image objects are available, a user
agent MAY use the value to decide which icon is most suitable for
a display context (and ignore any that are inappropriate). The
parsing steps for the <a>sizes</a> member MUST follow
<a data-cite="HTML#attr-link-sizes">the parsing steps for HTML
link element sizes attribute</a>.
</dd>
<dt>
<dfn>type</dfn> member
</dt>
<dd>
The <a>type</a> member is used to specify the
<a>ImageObject</a>'s MIME type. It is a hint as to the media type
of the image. The purpose of this member is to allow a user agent
to ignore images of media types it does not support.
</dd>
</dl>
</section>
<section>
<h2>
<dfn>Convert image objects</dfn>
</h2>
<p>
When this algorithm with <var>inputImages</var> parameter is
invoked, the user agent must run the following steps:
</p>
<ol class="algorithm">
<li>Let <var>outputImages</var> be an empty <a>Sequence</a> of <a>
ImageObject</a>.
</li>
<li>For each <var>image</var> in <var>inputImages</var>:
<ol>
<li>If <var>image</var>.<a data-lt="ImageObject.type">type</a>
is not a <a data-cite="mimesniff">valid MIME type string</a> or
the value of type is not a supported media format, then return
an empty <a>Sequence</a> of <a>ImageObject</a>.
</li>
<li>If <var>image</var>.<a data-lt=
"ImageObject.sizes">sizes</a> is not a <a data-lt=
"ImageObject.sizes">valid value</a>, then return an empty
<a>Sequence</a> of <a>ImageObject</a>.
</li>
<li>Let <var>url</var> be the result of parsing
<var>image</var>.<a data-lt="ImageObject.src">src</a> with the
<a>this</a>'s <a>relevant settings object</a>'s [=environment
settings object/api base url=].
</li>
<li>If <var>url</var> is failure, then return an empty
<a>Sequence</a> of <a>ImageObject</a>.
</li>
<li>If <var>url</var>'s [=url/scheme=] is not "https", then
return an empty <a>Sequence</a> of <a>ImageObject</a>.
</li>
<li>Set <var>image</var>.<a data-lt="ImageObject.src">src</a>
to <var>url</var>.
</li>
<li>Append <var>image</var> to <var>outputImages</var>
</li>
</ol>
</li>
<li>Return <var>outputImages</var>.
</li>
</ol>
<p>
According to the step 2.3, it is also possible to use the relative
url for <var>image</var>.<a data-lt="ImageObject.src">src</a>. The
following examples illustrate how relative URL resolution works in
different execution contexts.
</p>
<pre class="example html" title=
"Resolving the relative URL of image.src in window context.">
<-- In this example, code is located in https://www.example.com/bobpay/index.html -->
<script>
const instrumentKey = "c8126178-3bba-4d09-8f00-0771bcfd3b11";
navigator.serviceWorker.register("/register/sw.js");
const registration = await navigator.serviceWorker.ready;
await registration.paymentManager.paymentInstruments.set({
instrumentKey,
{
name: "My Bob Pay Account: [email protected]",
method: "https://bobpay.com",
icons: [{
src: "icon/lowres.webp",
sizes: "48x48",
type: "image/webp"
}]
});
const storedInstrument =
await registration.paymentManager.paymentInstruments.get(instrumentKey);
// storedInstrument.icons[0].src == "https://www.example.com/bobpay/icon/lowres.webp";
</script>
</pre>
<pre class="example js" title=
"Resolving the relative URL of image.src in service worker context.">
// In this example, code is located in https://www.example.com/register/sw.js
const instrumentKey = "c8126178-3bba-4d09-8f00-0771bcfd3b11";
await self.registration.paymentManager.paymentInstruments.set({
instrumentKey,
{
name: "My Bob Pay Account: [email protected]",
method: "https://bobpay.com",
icons: [{
src: "../bobpay/icon/lowres.webp",
sizes: "48x48",
type: "image/webp"
}]
});
const storedInstrument =
await registration.paymentManager.paymentInstruments.get(instrumentKey);
// storedInstrument.icons[0].src == "https://www.example.com/bobpay/icon/lowres.webp";
</pre>
</section>
<section id="register-example" class="informative">
<h2>
Registration Example
</h2>
<p>
The following example shows how to register a payment handler:
</p>
<pre class="example js" title="Payment Handler Registration">
button.addEventListener("click", async() => {
if (!window.PaymentManager) {
return; // not supported, so bail out.
}
navigator.serviceWorker.register("/sw.js");
const registration = await navigator.serviceWorker.ready;
// Excellent, we got it! Let's now set up the user's payment
// instruments.
await addInstruments(registration);
}, { once: true });
function addInstruments(registration) {
return Promise.all([
registration.paymentManager.instruments.set(
"dc2de27a-ca5e-4fbd-883e-b6ded6c69d4f",
{
name: "My Example Pay Account: [email protected]",
method: "https://example.com/pay",
}),
registration.paymentManager.instruments.set(
"c8126178-3bba-4d09-8f00-0771bcfd3b11",
{
name: "My Bob Pay Account: [email protected]",
method: "https://bobpay.com"
}),
]);
};
</pre>
</section>
</section>
</section>
<section id="canmakepayment">
<h2>
Can make payment
</h2>
<p>
If the <a>payment handler</a> supports <a>CanMakePaymentEvent</a>, the
<a>user agent</a> may use it to help with filtering of the available
payment handlers.
</p>
<p>
Implementations may impose a timeout for developers to respond to the
<a>CanMakePaymentEvent</a>. If the timeout expires, then the
implementation will behave as if {{CanMakePaymentEvent/respondWith()}}
was called with `false`.
</p>
<section data-dfn-for="ServiceWorkerGlobalScope">
<h2>
Extension to `ServiceWorkerGlobalScope`
</h2>
<pre class="idl">
partial interface ServiceWorkerGlobalScope {
attribute EventHandler oncanmakepayment;
};
</pre>
<section>
<h2>
<dfn>oncanmakepayment</dfn> attribute
</h2>
<p>
The {{ServiceWorkerGlobalScope/oncanmakepayment}} attribute is an
<a>event handler</a> whose corresponding <a>event handler event
type</a> is "canmakepayment".
</p>
</section>
</section>
<section data-dfn-for="CanMakePaymentEvent">
<h2>
The <dfn>CanMakePaymentEvent</dfn>
</h2>
<p>
The <a>CanMakePaymentEvent</a> is used to check whether the payment
handler is able to respond to a payment request.
</p>
<pre class="idl">
[Exposed=ServiceWorker]
interface CanMakePaymentEvent : ExtendableEvent {
constructor(DOMString type, optional CanMakePaymentEventInit eventInitDict = {});
readonly attribute USVString topOrigin;
readonly attribute USVString paymentRequestOrigin;
readonly attribute FrozenArray<PaymentMethodData> methodData;
undefined respondWith(Promise<boolean> canMakePaymentResponse);
};
</pre>
<p>
The <dfn>topOrigin</dfn>, <dfn>paymentRequestOrigin</dfn>,
<dfn>methodData</dfn>, and <dfn>modifiers</dfn> members share their
definitions with those defined for {{PaymentRequestEvent}}.
</p>
<section>
<h2>
<dfn>respondWith()</dfn> method
</h2>
<p>
This method is used by the payment handler to indicate whether it
can respond to a payment request.
</p>
</section>
<section data-dfn-for="CanMakePaymentEventInit">
<h2>
<dfn>CanMakePaymentEventInit</dfn> dictionary
</h2>
<pre class="idl">
dictionary CanMakePaymentEventInit : ExtendableEventInit {
USVString topOrigin;
USVString paymentRequestOrigin;
sequence<PaymentMethodData> methodData;
};
</pre>
<p>
The <dfn>topOrigin</dfn>, <dfn>paymentRequestOrigin</dfn>, and
<dfn>methodData</dfn> members share their definitions with those
defined for {{PaymentRequestEvent}}.
</p>
</section>
</section>
<section>
<h2>
<dfn>Handling a CanMakePaymentEvent</dfn>
</h2>
<p>
Upon receiving a <a>PaymentRequest</a>, the <a>user agent</a> MUST
run the following steps:
</p>
<ol>
<li>If <a>user agent</a> settings prohibit usage of
<a>CanMakePaymentEvent</a> (e.g., in private browsing mode),
terminate these steps.
</li>
<li>Let <var>registration</var> be a {{ServiceWorkerRegistration}}.
</li>
<li>If <var>registration</var> is not found, terminate these steps.
</li>
<li>
<p>
<a>Fire Functional Event</a> "<code>canmakepayment</code>" using
<a>CanMakePaymentEvent</a> on <var>registration</var> with the
following properties:
</p>
<dl>
<dt>
<a data-lt="CanMakePaymentEvent.topOrigin">topOrigin</a>
</dt>
<dd>
the [=serialization of an origin=] of the top level payee web
page.
</dd>
<dt>
<a data-lt=
"CanMakePaymentEvent.paymentRequestOrigin">paymentRequestOrigin</a>
</dt>
<dd>
the [=serialization of an origin=] of the context where
PaymentRequest was initialized.
</dd>
<dt>
<a data-lt="CanMakePaymentEvent.methodData">methodData</a>
</dt>
<dd>
The result of executing the <a>MethodData Population
Algorithm</a>.
</dd>
<dt>
<a data-lt="CanMakePaymentEvent.modifiers">modifiers</a>
</dt>
<dd>
The result of executing the <a>Modifiers Population
Algorithm</a>.
</dd>
</dl>
</li>
</ol>
</section>
<section id="canmakepayment-example" class="informative">
<h2>
Example of handling the <a>CanMakePaymentEvent</a>
</h2>
<p>
This example shows how to write a service worker that listens to the
<a>CanMakePaymentEvent</a>. When a <a>CanMakePaymentEvent</a> is
received, the service worker always returns true.
</p>
<pre class="example js" title="Handling the CanMakePaymentEvent">
self.addEventListener("canmakepayment", function(e) {
e.respondWith(true);
});
</pre>
</section>
<section>
<h2>
Filtering of Payment Instruments
</h2>
<p>
Given a <a>PaymentMethodData</a> and a {{PaymentInstrument}} that
match on <a>payment method identifier</a>, this algorithm returns
<code>true</code> if this instrument can be used for payment:
</p>
<ol class="algorithm">
<li>Let <var>instrument</var> be the given {{PaymentInstrument}}.
</li>
<li>Let <var>methodName</var> be the <a>payment method identifier</a>
string specified in the <a>PaymentMethodData</a>.
</li>
<li>Let <var>methodData</var> be the payment method specific data of
<a>PaymentMethodData</a>.
</li>
<li>Let <var>paymentHandlerOrigin</var> be the <a>origin</a> of the
{{ServiceWorkerRegistration}} scope URL of the payment handler with
this <var>instrument</var>.
</li>
<li>Let <var>paymentMethodManifest</var> be the <a>ingested</a> and
<a>parsed</a> <a>payment method manifest</a> for the
<var>methodName</var>.
</li>
<li>If <var>methodName</var> is a <a>standardized payment method
identifier</a> or is a <a>URL-based payment method identifier</a>
with the <code>"*"</code> string <a>supported origins</a> in
<var>paymentMethodManifest</var>, return <code>true</code>.
</li>
<li>Otherwise, if the <a>URL-based payment method identifier</a>
<var>methodName</var> has the same <a>origin</a> as
<var>paymentHandlerOrigin</var>, fire the <a>CanMakePaymentEvent</a>
in the payment handler and return the result.
</li>
<li>Otherwise, if <a>supported origins</a> in
<var>paymentMethodManifest</var> is an ordered set of [=url/origin=]
that contains the <var>paymentHandlerOrigin</var>, fire the
<a>CanMakePaymentEvent</a> in the payment handler and return the
result.
</li>
<li>Otherwise, return `false`.
</li>
</ol>
</section>
</section>
<section id="invocation">
<h2>
Invocation
</h2>
<p>
Once the user has selected an Instrument, the user agent fires a
{{PaymentRequestEvent}} and uses the subsequent
<a>PaymentHandlerResponse</a> to create a PaymentReponse for
[[!payment-request]].
</p>
<p class="issue" title=
"Support for Abort() being delegated to Payment Handler" data-number=
"117">
Payment Request API supports delegation of responsibility to manage an
abort to a payment app. There is a proposal to add a
paymentRequestAborted event to the Payment Handler interface. The event
will have a respondWith method that takes a boolean parameter
indicating if the paymentRequest has been successfully aborted.
</p>
<section data-dfn-for="ServiceWorkerGlobalScope" data-link-for=
"ServiceWorkerGlobalScope">
<h2>
Extension to <a>ServiceWorkerGlobalScope</a>
</h2>