-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (83 loc) · 2.24 KB
/
index.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
const status = document.querySelector('#status');
function buildPaymentRequest() {
if (!window.PaymentRequest) {
return null;
}
// This needs to provide a manifest link HTTP header property, it does not
// currently.
const supportedInstruments = [{
supportedMethods: ['https://sauski.github.com/payment-request-driveby/manifest.json']
}
];
const details = {
total: {
label: 'Donation',
amount: {
currency: 'USD',
value: '55.00',
},
},
displayItems: [{
label: 'Original donation amount',
amount: {
currency: 'USD',
value: '65.00',
},
}, {
label: 'Friends and family discount',
amount: {
currency: 'USD',
value: '-10.00',
},
}],
};
let request = null;
try {
request = new PaymentRequest(supportedInstruments, details);
if (request.canMakePayment) {
request.canMakePayment().then(function(result) {
info(result ? 'Can make payment' : 'Cannot make payment');
}).catch(function(err) {
status.textContent = err;
});
}
if (request.hasEnrolledInstrument) {
request.hasEnrolledInstrument().then(function(result) {
info(result ? 'Has enrolled instrument' : 'No enrolled instrument');
}).catch(function(err) {
status.textContent = err;
});
}
} catch (e) {
status.textContent = 'Developer mistake: \'' + e.message + '\'';
}
return request;
}
let request = buildPaymentRequest();
function handlePaymentResponse(response) {
response.complete('success')
.then(function() {
done('This is a demo website. No payment will be processed.', response);
})
.catch(function(err) {
status.textContent = err;
request = buildPaymentRequest();
});
}
function onBuyClicked() {
if (!window.PaymentRequest || !request) {
status.textContent ='PaymentRequest API is not supported.';
return;
}
try {
request.show()
.then(handlePaymentResponse)
.catch(function(err) {
status.textContent = err;
request = buildPaymentRequest();
});
} catch (e) {
status.textContent = 'Developer mistake: \'' + e.message + '\'';
request = buildPaymentRequest();
}
}