-
Notifications
You must be signed in to change notification settings - Fork 89
/
applePayCaller.js
138 lines (111 loc) · 3.72 KB
/
applePayCaller.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
if (window.ApplePaySession) {
var merchantIdentifier = 'ApplePayDemoTestDev15';
var promise = ApplePaySession.canMakePaymentsWithActiveCard(merchantIdentifier);
promise.then( function (canMakePayments) {
if (canMakePayments){
console.log("Apple Pay Payment Available");
$("#applePayButton").prop('disabled', false);
}else{
console.log("Apple Pay is available but not activated yet");
}
});
}
else{
console.log("Apple Pay not available in this browser");
}
function createTransaction(dataObj) {
console.log('starting createTransaction');
console.log(dataObj);
let objJsonStr = JSON.stringify(dataObj);
console.log(objJsonStr);
let objJsonB64 = window.btoa(objJsonStr);
console.log(objJsonB64);
$.ajax({
url: "transactionCaller.php",
data: {amount: '15.00', dataDesc: 'COMMON.APPLE.INAPP.PAYMENT', dataValue: objJsonB64},
method: 'POST',
timeout: 5000
}).done(function(data){
console.log(data);
console.log('Success');
}).fail(function(){
console.log('Error');
})
return true;
}
function applePayButtonClicked(){
console.log('Apple Pay Initiated');
var request = {
countryCode: 'US',
currencyCode: 'USD',
supportedNetworks: ['visa', 'masterCard', 'amex', 'discover'],
merchantCapabilities: ['supports3DS','supportsCredit', 'supportsDebit'], // Make sure NOT to include supportsEMV here
total: { label: 'Test Spices', amount: '15.00' },
}
var session = new ApplePaySession(1, request);
// Merchant Validation
session.onvalidatemerchant = function (event) {
console.log(event);
var promise = performValidation(event.validationURL);
promise.then(function (merchantSession) {
session.completeMerchantValidation(merchantSession);
});
}
function performValidation(valURL) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
console.log(this.responseText);
var data = JSON.parse(this.responseText);
console.log(data);
resolve(data);
};
xhr.onerror = reject;
xhr.open('POST', 'validateApplePayMerchant.php', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send('validationUrl='+valURL);
});
}
session.onpaymentmethodselected = function(event) {
console.log('starting onpaymentmethodselected');
console.log(event);
var newTotal = { type: 'final', label: 'Test Spices', amount: '15.00' };
var newLineItems =[{type: 'final',label: 'Spice #202', amount: '15.00' }]
session.completePaymentMethodSelection( newTotal, newLineItems);
}
session.onpaymentauthorized = function (event) {
console.log('starting session.onpaymentauthorized');
console.log(event);
var promise = sendPaymentToken(event.payment.token);
promise.then(function (success) {
var status;
if (success){
status = ApplePaySession.STATUS_SUCCESS;
console.log('Apple Pay Payment SUCCESS ');
} else {
status = ApplePaySession.STATUS_FAILURE;
}
console.log( "result of sendPaymentToken() function = " + success );
session.completePayment(status);
});
}
function sendPaymentToken(paymentToken) {
return new Promise(function(resolve, reject) {
console.log('starting function sendPaymentToken()');
console.log(paymentToken);
/* Send Payment token to Payment Gateway, here its defaulting to True just to mock that part */
returnFromGateway = createTransaction(paymentToken.paymentData);
console.log(returnFromGateway);
console.log("defaulting to successful payment by the Token");
if ( returnFromGateway == true )
resolve(true);
else
reject;
});
}
session.oncancel = function(event) {
console.log('starting session.cancel');
console.log(event);
}
session.begin();
}