-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
104 lines (92 loc) · 2.95 KB
/
util.js
File metadata and controls
104 lines (92 loc) · 2.95 KB
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
var timeoutID1;
var timeoutID2;
/**
* Prints the given error message.
* @param {string} msg - The error message to print.
*/
function error(msg) { // eslint-disable-line no-unused-vars
if (timeoutID1) {
window.clearTimeout(timeoutID1);
}
if (timeoutID2) {
window.clearTimeout(timeoutID2);
}
let element = document.createElement('div');
element.innerHTML = msg;
element.className = 'error';
document.getElementById('msg').appendChild(element);
timeoutID1 = window.setTimeout(function() {
if (element.className !== 'error') {
return;
}
element.className = 'error-hide';
timeoutID2 = window.setTimeout(function() {
element.innerHTML = '';
element.className = '';
}, 500);
}, 10000);
}
/**
* Prints the given informational message.
* @param {string} msg - The information message to print.
*/
function info(msg) {
let element = document.createElement('div');
element.innerHTML = msg;
element.className = 'info';
document.getElementById('msg').appendChild(element);
}
/**
* Converts an address object into a dictionary.
* @param {PaymentAddress} addr - The address to convert.
* @return {object} The resulting dictionary.
*/
function toDictionary(addr) { // eslint-disable-line no-unused-vars
let dict = {};
if (addr) {
if (addr.toJSON) {
return addr;
}
dict.country = addr.country;
dict.region = addr.region;
dict.city = addr.city;
dict.dependentLocality = addr.dependentLocality;
dict.addressLine = addr.addressLine;
dict.postalCode = addr.postalCode;
dict.sortingCode = addr.sortingCode;
dict.languageCode = addr.languageCode;
dict.organization = addr.organization;
dict.recipient = addr.recipient;
dict.phone = addr.phone;
}
return dict;
}
/**
* Called when the payment request is complete.
* @param {string} message - The human readable message to display.
* @param {PaymentResponse} resp - The payment response.
*/
function done(message, resp) { // eslint-disable-line no-unused-vars
let element = document.getElementById('contents');
element.innerHTML = message;
if (resp.toJSON) {
info(JSON.stringify(resp, undefined, 2));
return;
}
let shippingOption = resp.shippingOption ?
'shipping, delivery, pickup option: ' + resp.shippingOption + '<br/>' :
'';
let shippingAddress = resp.shippingAddress ?
'shipping, delivery, pickup address: ' +
JSON.stringify(toDictionary(resp.shippingAddress), undefined, 2) +
'<br/>' :
'';
let instrument =
'instrument:' + JSON.stringify(resp.details, undefined, 2) + '<br/>';
let method = 'method: ' + resp.methodName + '<br/>';
let email = resp.payerEmail ? 'email: ' + resp.payerEmail + '<br/>' : '';
let phone = resp.payerPhone ? 'phone: ' + resp.payerPhone + '<br/>' : '';
let name = resp.payerName ? 'name: ' + resp.payerName + '<br/>' : '';
info(email + phone + name + shippingOption + shippingAddress + method +
instrument);
}