1
+ function onBuyClicked ( event ) {
2
+ if ( ! window . PaymentRequest ) {
3
+ return ;
4
+ }
5
+ // Payment Request API is available.
6
+ // Stop the default anchor redirect.
7
+ event . preventDefault ( ) ;
8
+
9
+ var supportedInstruments = [ {
10
+ supportedMethods : [
11
+ 'visa' , 'mastercard' , 'amex' , 'discover' , 'maestro' ,
12
+ 'diners' , 'jcb' , 'unionpay' , 'bitcoin'
13
+ ]
14
+ } ] ;
15
+
16
+ var details = {
17
+ displayItems : [ {
18
+ label : 'Original donation amount' ,
19
+ amount : { currency : 'USD' , value : '65.00' }
20
+ } , {
21
+ label : 'Friends and family discount' ,
22
+ amount : { currency : 'USD' , value : '-10.00' }
23
+ } ] ,
24
+ total : {
25
+ label : 'Total due' ,
26
+ amount : { currency : 'USD' , value : '55.00' }
27
+ }
28
+ } ;
29
+
30
+ var options = {
31
+ requestShipping : true ,
32
+ requestPayerEmail : true ,
33
+ requestPayerPhone : true ,
34
+ requestPayerName : true
35
+ } ;
36
+
37
+ // Initialization
38
+ var request = new PaymentRequest ( supportedInstruments , details , options ) ;
39
+
40
+ // When user selects a shipping address
41
+ request . addEventListener ( 'shippingaddresschange' , e => {
42
+ e . updateWith ( ( ( details , addr ) => {
43
+ var shippingOption = {
44
+ id : '' ,
45
+ label : '' ,
46
+ amount : { currency : 'USD' , value : '0.00' } ,
47
+ selected : true
48
+ } ;
49
+ // Shipping to US is supported
50
+ if ( addr . country === 'US' ) {
51
+ shippingOption . id = 'us' ;
52
+ shippingOption . label = 'Standard shipping in US' ;
53
+ shippingOption . amount . value = '0.00' ;
54
+ details . total . amount . value = '55.00' ;
55
+ // Shipping to JP is supported
56
+ } else if ( addr . country === 'JP' ) {
57
+ shippingOption . id = 'jp' ;
58
+ shippingOption . label = 'International shipping' ;
59
+ shippingOption . amount . value = '10.00' ;
60
+ details . total . amount . value = '65.00' ;
61
+ // Shipping to elsewhere is unsupported
62
+ } else {
63
+ // Empty array indicates rejection of the address
64
+ details . shippingOptions = [ ] ;
65
+ return Promise . resolve ( details ) ;
66
+ }
67
+ // Hardcode for simplicity
68
+ if ( details . displayItems . length === 2 ) {
69
+ details . displayItems [ 2 ] = shippingOption ;
70
+ } else {
71
+ details . displayItems . push ( shippingOption ) ;
72
+ }
73
+ details . shippingOptions = [ shippingOption ] ;
74
+
75
+ return Promise . resolve ( details ) ;
76
+ } ) ( details , request . shippingAddress ) ) ;
77
+ } ) ;
78
+
79
+ // When user selects a shipping option
80
+ request . addEventListener ( 'shippingoptionchange' , e => {
81
+ e . updateWith ( ( ( details ) => {
82
+ // There should be only one option. Do nothing.
83
+ return Promise . resolve ( details ) ;
84
+ } ) ( details ) ) ;
85
+ } ) ;
86
+
87
+ // Show UI then continue with user payment info
88
+ request . show ( ) . then ( result => {
89
+ // POST the result to the server
90
+ return fetch ( '/payment-api/pay' , {
91
+ method : 'POST' ,
92
+ credentials : 'include' ,
93
+ headers : {
94
+ 'Content-Type' : 'application/json'
95
+ } ,
96
+ body : JSON . stringify ( result . toJSON ( ) )
97
+ } ) . then ( res => {
98
+ // Only if successful
99
+ if ( res . status === 200 ) {
100
+ return res . json ( ) ;
101
+ } else {
102
+ throw 'Failure' ;
103
+ }
104
+ } ) . then ( response => {
105
+ // You should have received a JSON object
106
+ if ( response . success == true ) {
107
+ return result . complete ( 'success' ) ;
108
+ } else {
109
+ return result . complete ( 'fail' ) ;
110
+ }
111
+ } ) . then ( ( ) => {
112
+ console . log ( 'Thank you!' ,
113
+ result . shippingAddress . toJSON ( ) ,
114
+ result . methodName ,
115
+ result . details . toJSON ( ) ) ;
116
+ } ) . catch ( ( ) => {
117
+ return result . complete ( 'fail' ) ;
118
+ } ) ;
119
+ } ) . catch ( function ( err ) {
120
+ console . error ( 'Uh oh, something bad happened: ' + err . message ) ;
121
+ } ) ;
122
+ }
123
+
124
+ // Assuming an anchor is the target for the event listener.
125
+ document . querySelector ( 'button' ) . addEventListener ( 'click' , onBuyClicked ) ;
0 commit comments