-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpayment-request.js
543 lines (483 loc) · 13.8 KB
/
payment-request.js
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
/*
Prepare for 2.0
<link rel="import" href="../polymer/lib/utils/flattened-nodes-observer.html">
*/
/**
`payment-request`
Payment request API implementation
@demo demo/index.html
*/
/*
FIXME(polymer-modulizer): the above comments were extracted
from HTML and may be out of place here. Review them and
then delete this comment!
*/
import { PolymerElement } from '@polymer/polymer/polymer-element.js';
import '@polymer/iron-selector/iron-selector.js';
import { afterNextRender } from '@polymer/polymer/lib/utils/render-status.js';
import { dom } from '@polymer/polymer/lib/legacy/polymer.dom.js';
import { FlattenedNodesObserver } from '@polymer/polymer/lib/utils/flattened-nodes-observer.js'
const $_documentContainer = document.createElement('template');
$_documentContainer.innerHTML = `<dom-module id="payment-request">
<template>
<style>
:host {
display: block;
}
</style>
<slot id="methods" name="method"></slot>
<slot id="total" name="total"></slot>
<iron-selector id="shippingOptionsSelector" attr-for-selected="id" selected-attribute="selected" selected="{{shippingOptionSelected}}">
<slot id="shippingOptions" name="shipping-option"></slot>
</iron-selector>
<slot id="buyButton" name="button"></slot>
<slot id="items"></slot>
</template>
</dom-module>`;
document.head.appendChild($_documentContainer.content);
/**
* Fired when user interaction begins for the payment request.
*
* @event response
* @param {PaymentResponse} paymentResponse The payment information to process.
*/
/**
* Fired when a PaymentRequest is created.
*
* @event request
* @param {PaymentRequest} paymentRequest The payment request.
*/
/**
* Fired when the payment request is aborted
*
* @event aborted
*/
/**
* Fired when payment request generate an error.
*
* @event error
* @param {Error} error The request error.
*/
/**
* Fired when PaymentRequest object can be used to make a payment.
*
* @event can-make-payment
*/
/**
* Fired when PaymentRequest object cannot be used to make a payment.
*
* @event cannot-make-payment
*/
export const PaymentRequestElement = class extends PolymerElement {
static get is() {
return 'payment-request';
}
static get importMeta() {
return import.meta;
}
static get properties() {
return {
/**
* This is a human-readable description of the total.
* The user agent may display this to the user.
*/
label: String,
/**
* A valid decimal monetary value containing a monetary amount of the total
*/
value: Number,
/**
* A string containing a currency identifier of the total.
* The value of currency can be any string that is valid within
* the currency system indicated by currencySystem.
*/
currency: {
type: String,
value: 'EUR'
},
/**
* Contains line items for the payment request that the user agent may display.
*/
items: {
type: Array,
readOnly: true,
notify: true,
value: () => {
return [];
},
},
/**
* Contains the total amount of the payment request.
*/
total: {
type: Object,
readOnly: true,
notify: true,
value: () => {
return {};
}
},
/**
* Is used to store supported payment methods and
* any associated payment method specific data for those methods.
*/
methods: {
type: Array,
readOnly: true,
value: () => {
return [];
}
},
/**
* Provides information about the requested transaction.
*/
details: {
type: Object,
computed: '_computeDetails(total, items, shippingItem)',
},
payerName: {
type: Boolean,
value: false
},
payerEmail: {
type: Boolean,
value: false
},
payerPhone: {
type: Boolean,
value: false
},
shipping: {
type: Boolean,
value: false
},
shippingType: {
type: String,
value: 'shipping'
},
options: {
type: Object,
computed: '_computeOptions(payerName, payerEmail, payerPhone, shipping, shippingType)'
},
shippingOptions: {
type: Array,
value: () => {
return [];
}
},
shippingOptionSelected: {
type: String
},
shippingItem: {
type: Object,
value: () => {
return null;
}
},
lastRequest: {
type: Object,
notify: true,
observer: '_dispatchRequest'
},
lastResponse: {
type: Object,
readOnly: true,
notify: true,
observer: '_dispatchResponse'
},
lastError: {
type: Object,
readOnly: true,
notify: true,
observer: '_dispatchError'
},
lastCanMakePayment: {
type: Boolean,
value: false,
notify: true
}
}
}
static get observers() {
return [
'_updateTotal(items.length, shippingItem)',
'_updateShippingOptions(shippingOptionSelected)',
'_updateLastRequest(methods.*, details.*, options.*)',
'checkCanMakePayment(methods, lastRequest)'
];
}
connectedCallback() {
super.connectedCallback();
afterNextRender(this, this._updatePropertiesFromNodes);
this.buyButtonTap = this.buyButtonTap.bind(this);
this.onShippingAddressChange = this.onShippingAddressChange.bind(this);
this.onShippingOptionChange = this.onShippingOptionChange.bind(this);
this.__updatePropertyFromNodes = this.__updatePropertyFromNodes.bind(this);
this.$.buyButton.addEventListener('tap', this.buyButtonTap);
}
disconnectedCallback() {
super.connectedCallback();
this.__unobserveNodes('methods');
this.__unobserveNodes('items');
this.__unobserveNodes('shippingOptions');
this.$.buyButton.removeEventListener('tap', this.buyButtonTap);
}
_updatePropertiesFromNodes() {
this.__observeNodes('methods');
this.__observeNodes('items');
this.__observeNodes('shippingOptions');
}
__updatePropertyFromNodes(observerInfo) {
var property = observerInfo.target.id;
if (!observerInfo) {
observerInfo = {
target: this.$[property],
addedNodes: dom(this.$[property]).getDistributedNodes(),
removedNodes: []
};
this.splice(property, 0, this[property].length - 1);
}
// Add items from added nodes
observerInfo.addedNodes.filter(this._isElementNode).forEach(function(node) {
this.splice(property, this[property].length, 0, node.dictionary);
}.bind(this));
// Remove items from removed nodes
observerInfo.removedNodes.filter(this._isElementNode).forEach(function(node) {
this.splice(property, this[property].indexOf(node.dictionary), 1);
}.bind(this));
}
_isElementNode(node) {
return (node.nodeType === Node.ELEMENT_NODE && node.dictionary);
}
__observerNode(node) {
return '_' + node + 'Observer';
}
__observeNodes(property) {
// Watch for future updates.
if (!this[this.__observerNode(property)]) {
this[this.__observerNode(property)] = new FlattenedNodesObserver(this.$[property], this.__updatePropertyFromNodes);
}
}
__unobserveNodes(property) {
if (this[this.__observerNode(property)]) {
dom(this).unobserveNodes(this[this.__observerNode(property)]);
}
}
_updateTotal() {
var totalDom = dom(this.$.total);
var total = totalDom.getDistributedNodes()[0];
if (!total) {
total = document.createElement('payment-item');
total.setAttribute('slot', 'total');
totalDom.appendChild(total);
import(PaymentRequestElement.importMeta.url + '/../payment-item.js')
.then(() => this._updateTotal())
.catch((err) => console.error(err));
return;
}
var currency = this.items.length && this.items[0]?
this.items[0].amount.currency :
this.currency;
var value = 0;
for (var i = 0; i < this.items.length; i++) {
value += this.items[i] ? this.items[i].amount.value : 0;
}
if (this.shippingItem) {
value += this.shippingItem.amount.value;
}
total.label = this.label || 'Total';
total.value = value || 0;
total.currency = currency || 'EUR';
this._setTotal(total.dictionary);
}
_computeDetails(total, items, shippingItem) {
return {
total: total,
displayItems: shippingItem ? items.concat(shippingItem): items
};
}
_computeOptions(payerName, payerEmail, payerPhone, shipping, shippingType) {
return {
requestPayerName: payerName,
requestPayerEmail: payerEmail,
requestPayerPhone: payerPhone,
requestShipping: shipping,
shippingType: shippingType
};
}
/**
* Construct a PaymentRequest using the supplied methodData list including any
* payment method specific data, the payment details, and the payment options
*
* @return {PaymentRequest}
*/
_updateLastRequest(methods, details, options) {
methods = methods.base;
details = details.base;
options = options.base;
this.updateLastRequest(methods, details, options);
}
updateLastRequest(methods, details, options) {
methods = methods || this.methods;
details = details || this.details;
options = options || this.options;
if (methods.length &&
details.displayItems.length &&
details.total.amount.currency &&
window.PaymentRequest) {
this.lastRequest = new PaymentRequest(methods, details, options);
this.addRequestListeners();
} else {
this.lastRequest = null;
}
}
addRequestListeners() {
if (this.shipping) {
this.lastRequest.addEventListener('shippingaddresschange', this.onShippingAddressChange);
this.lastRequest.addEventListener('shippingoptionchange', this.onShippingOptionChange);
}
}
removeRequestListeners() {
if (this.shipping) {
this.lastRequest.removeEventListener('shippingaddresschange', this.onShippingAddressChange);
this.lastRequest.removeEventListener('shippingoptionchange', this.onShippingOptionChange);
}
}
/**
* Method executed when payButton is tapped.
* You can override it to do something more complex.
*/
buyButtonTap() {
if ('PaymentRequest' in window) {
this.show();
} else {
this._setLastError({
detail: 'Payment Request API not supported'
});
}
}
/**
* Determine if the PaymentRequest object can be used to make a payment.
*
* @return {Promise}
*/
checkCanMakePayment(methods, lastRequest) {
var promise;
if (methods.length && lastRequest && lastRequest.canMakePayment) {
promise = lastRequest.canMakePayment()
.then(this.set.bind(this, 'lastCanMakePayment'))
.then(this._setLastError.bind(this, null))
.catch(this._setLastError.bind(this));
} else {
promise = new Promise(function(resolve, reject) {
resolve();
});
}
return promise;
}
/**
* Begin user interaction for the payment request.
*
* @return {Promise}
*/
show() {
if (this.lastRequest) {
return this.lastRequest.show()
.then(this._setLastResponse.bind(this))
.catch(this._setLastError.bind(this))
.then(this.updateLastRequest.bind(this));
} else {
var detail;
if (!this.methods.length) {
detail = 'There aren\'t payment methods';
} else if (!this.details.displayItems.length) {
detail = 'There aren\'t items to pay';
} else {
detail = 'Payment Request wasn\'t be created';
}
this._setLastError({ detail: detail });
}
}
/**
* Abort the payment request
* @return {Promise}
*/
abort() {
var promise;
if (this.lastRequest) {
promise = this.lastRequest.abort()
.then(function() {
this._setLastResponse(null);
this.dispatchEvent(
new CustomEvent('aborted')
);
}.bind(this));
} else {
promise = new Promise(function(resolve, reject) {
resolve('There aren\'t any active request');
});
}
return promise
.catch(this._setLastError.bind(this));
}
onShippingAddressChange(evt) {
evt.updateWith(this.updateWithShippingAddress(evt));
}
onShippingOptionChange(evt) {
evt.updateWith(this.updateWithShippingOptions(evt));
}
updateWithShippingAddress(evt) {
this.changeShippingOption(evt.target.shippingOption);
this._updateTotal();
this._updateShippingOptions();
return Promise.resolve(this.details);
}
updateWithShippingOptions(evt) {
this.changeShippingOption(evt.target.shippingOption);
this._updateTotal();
this._updateShippingOptions();
return Promise.resolve(this.details);
}
changeShippingOption(shippingOption) {
shippingOption = shippingOption || this.$.shippingOptionsSelector.selected;
// Add shipping option to displayed items
if (shippingOption) {
this.$.shippingOptionsSelector.select(shippingOption);
this.shippingItem = this.$.shippingOptionsSelector.selectedItem.dictionary;
}
}
_updateShippingOptions() {
this.set('details.shippingOptions', this.$.shippingOptionsSelector.items.map(function(item) {
return item.dictionary;
}));
}
_dispatchError(error) {
if (error) {
this.dispatchEvent(
new CustomEvent('error', {
detail: error
})
);
}
return error;
}
_dispatchResponse(response) {
if (response !== null) {
this.dispatchEvent(
new CustomEvent('response', {
detail: response
})
);
}
return response;
}
_dispatchRequest(request) {
this.dispatchEvent(
new CustomEvent('request', {
detail: request
})
);
return request;
}
};
window.customElements.define(PaymentRequestElement.is, PaymentRequestElement);