-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
80 lines (80 loc) · 3.67 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
<html>
<head>
<script type="text/javascript">
function onVisaCheckoutReady(){
// Get the payment request from the service worker
navigator.serviceWorker.register("payment-service-worker.js")
.then(function() {
return navigator.serviceWorker.ready;
})
.then(function() {
// The controller may or may not be ready at this point.
// See: https://github.com/slightlyoff/ServiceWorker/issues/799
return new Promise(function(resolve, reject) {
if (navigator.serviceWorker.controller) {
resolve();
} else {
navigator.serviceWorker.addEventListener("controllerchange", function() {
console.log("controllerchange");
resolve();
});
}
});
})
.then(function() {
// Request the PaymentRequest from the service worker
return new Promise(function(resolve, reject) {
console.log("creating messagechannel");
var messageChannel = new MessageChannel();
messageChannel.port1.onmessage = function(event) {
if (event.data.error) {
reject(event.data.error);
} else {
resolve(event.data);
}
}
navigator.serviceWorker.controller.postMessage("get_request", [messageChannel.port2]);
});
})
.then(function(paymentRequest) {
// Now that we have the "payment request", we can start the Visa Checkout flow
console.log(JSON.stringify(paymentRequest));
V.init({
apikey: "7O07VN664O10JW6A9ESS113p8sf9JeGzr6_2haC9F9m_ANtLM",
paymentRequest: paymentRequest
});
V.on("payment.success",
function(payment) {
response = {
"methodName" : "visa checkout",
"details" : payment
}
document.getElementById("output").textContent = JSON.stringify(response, null, 4);
navigator.payments.submitPaymentResponse(JSON.stringify(response));
});
V.on("payment.cancel",
function(payment) {
document.getElementById("output").textContent = JSON.stringify(payment, null, 4);
navigator.payments.submitPaymentResponse(null);
});
V.on("payment.error",
function(payment, error) {
document.getElementById("output").textContent = JSON.stringify(error, null, 4);
navigator.payments.submitPaymentResponse(null);
});
document.getElementById("button").click();
})
.catch(function(error) {
console.log(error);
});
}
</script>
</head>
<body>
<img alt="Visa Checkout" class="v-button" id="button" role="button" src="https://sandbox.secure.checkout.visa.com/wallet-services-web/xo/button.png" style="display: none"/>
<script type="text/javascript"
src="https://sandbox-assets.secure.checkout.visa.com/checkout-widget/resources/js/integration/v1/sdk.js">
</script>
<pre id="output"></pre>
</body>
</html>