(function($, window, document) { $(function() { $('#applePay').on('click', function() { var request = { countryCode: 'NL', currencyCode: "EUR", supportedNetworks: ['amex', 'maestro', 'masterCard', 'visa', 'vPay'], merchantCapabilities: ['supports3DS'], requiredBillingContactFields: ['postalAddress','email', 'name', 'phone'], lineItems: [{label: 'Test product 1', amount: '0.50' }, {label: 'Test product 2', amount: '0.50' }], total: { label: 'LauLaman Apps', amount: '1.00' } }; var session = new ApplePaySession(3, request); session.onvalidatemerchant = function (event) { var promise = performValidation(event.validationURL); promise.then(function (merchantSession) { session.completeMerchantValidation(JSON.parse(merchantSession)); }); }; function performValidation(valURL) { return new Promise(function(resolve, reject) { var xhr = new XMLHttpRequest(); xhr.onload = function() { resolve(JSON.parse(this.responseText)); }; xhr.onerror = reject; xhr.open('POST', '\/wallet\/apple\u002Dpay\/start\u002Dsession\/'); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8') xhr.send("validationURL=" + valURL + "&token=eUfhVPRUzt2_nKkcdBgtJVOTskdGohUI\u002DumTpqdqz5Y"); }); } session.onpaymentauthorized = function (event) { var promise = sendPaymentToken(event.payment.token, event.payment.shippingContact); promise.then(function (success) { var status; if (success) { status = ApplePaySession.STATUS_SUCCESS; document.getElementById("applePay").style.display = "none"; } else { status = ApplePaySession.STATUS_FAILURE; } session.completePayment(status); }); }; function sendPaymentToken(paymentToken, shippingContact) { return new Promise(function(resolve) { console.log(resolve); var xhr = new XMLHttpRequest(); xhr.onload = function() { resolve(true); }; xhr.onerror = resolve(false); xhr.open('POST', '\/wallet\/apple\u002Dpay\/authorize\/'); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8') xhr.send("shippingContact=" + encodeURIComponent(JSON.stringify(shippingContact)) + "&paymentToken=" + encodeURIComponent(JSON.stringify(paymentToken)) + "&token=eUfhVPRUzt2_nKkcdBgtJVOTskdGohUI\u002DumTpqdqz5Y"); }); } session.oncancel = function(event) { }; session.begin(); }); }); } (window.jQuery, window, document));