-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapple_pay.php
More file actions
141 lines (101 loc) · 4.34 KB
/
Copy pathapple_pay.php
File metadata and controls
141 lines (101 loc) · 4.34 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
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
<html>
<head><title>Apple Pay For The Web Example</title></head>
<body>
<?php if($this->hasApplePayEnabled()): ?>
<div id="adyen-apple-pay-button" class="apple-pay-button" style="display: none;"></div>
<script type="text/javascript">
var applePayButton = document.getElementById("adyen-apple-pay-button");
applePayButton.observe('click', applePayButtonClicked);
// Only show button for browsers that suppoprt ApplePaySession
document.addEventListener("DOMContentLoaded", function () {
if(window.ApplePaySession) {
var promise = ApplePaySession.canMakePaymentsWithActiveCard("merchant.com.canine-clothing");
promise.then(function (canMakePayments) {
if (canMakePayments) {
$('adyen-apple-pay-button').show();
}
});
}
});
// do request
function applePayButtonClicked(event)
{
var paymentRequest = {
currencyCode: "USD",
countryCode: "US",
total: {
label: "Canine Clothing",
amount: "19.99"
},
supportedNetworks: ["amex","discover","masterCard","visa"],
merchantCapabilities: ["supports3DS"],
//requiredShippingAddressFields:["postalAddress"]
};
var session = new ApplePaySession(1, paymentRequest);
session.onvalidatemerchant = function(event) {
var promise = performValidation(event.validationURL);
promise.then(function (merchantSession) {
session.completeMerchantValidation(merchantSession);
});
}
session.onpaymentauthorized = function(event) {
var promise = sendPayment(event.payment.token);
promise.then(function (success) {
var status;
if(success)
status = ApplePaySession.STATUS_SUCCESS;
else
status = ApplePaySession.STATUS_FAILURE;
session.completePayment(status);
});
}
session.begin();
}
function performValidation(validationURL)
{
// Return a new promise.
return new Promise(function(resolve, reject) {
var url = '<?php echo $this->getUrl('adyen/applePay/requestMerchantSession', array('_secure'=>true));?>';
ajaxReq = new Ajax.Request(url, {
parameters: {validationURL: validationURL, domainName: location.host, isAjax: 1, method: 'POST'},
onSuccess: function (response) {
var data = JSON.parse(response.responseText);
if(data) {
resolve(data);
} else {
reject(Error(response.responseText));
}
},
onFailure: function() {
reject(Error("Network Error"));
}
});
});
}
function sendPayment(payment) {
// Return a new promise.
return new Promise(function(resolve, reject) {
var url = '<?php echo $this->getUrl('adyen/applePay/sendPayment', array('_secure'=>true));?>';
ajaxReq = new Ajax.Request(url, {
parameters: {payment: JSON.stringify(payment), isAjax: 1, method: 'POST'},
onSuccess: function (response) {
if (response.responseText && response.responseText.length >= 0) {
var data = JSON.parse(response.responseText);
}
var success = true; // TODO
if(success) {
resolve(success);
} else {
reject(Error(response.responseText));
}
},
onFailure: function() {
reject(Error("Network Error"));
}
});
});
}
</script>
<?php endif; ?>
</body>
</html>