1/** 2 * Object that holds Apple Pay JS specific configuration 3 * @param options 4 * @returns 5 */ 6function ApplePay(options){ 7 8 var obj = this; 9 10 this.settings = jQuery.extend({ 11 merchantId : null, 12 request: null, 13 grandTotalCalculator: function(){return 0;}, 14 paymentAvailableCallback: function(){ 15 $('.apple-pay-button, .apple-pay-label').show(); 16 }, 17 callback: null, 18 errorCallback:null, 19 shippingContactSelectedCallback:null, 20 paymentAuthorizedErrorCallback: null 21 22 } , options || {}); 23 24 this.start = function(){ 25 if (window.ApplePaySession) { 26 27 var promise = ApplePaySession.canMakePaymentsWithActiveCard(obj.settings.merchantId); 28 promise.then( function (canMakePayments) { 29 if (canMakePayments){ 30 obj.settings.paymentAvailableCallback(); 31 console.log("Apple Pay Payment Available"); 32 } 33 }); 34 35 } else { 36 if(obj.settings.callback != null) { 37 obj.settings.callback(); 38 } 39 console.log('no apple pay session exists'); 40 } 41 } 42} 43 44function getCsrfToken(){ 45 return getCookieWithHelper(false, 'csrfToken'); 46} 47 48function getLelaboCountry(){ 49 return getCookieWithHelper(false, 'acdc_country_code'); 50} 51 52function applePayCartClicked(applePay){ 53 54 var settings = applePay.settings; 55 56 if (!window.ApplePaySession) { 57 console.log("Apple Pay is not available in this browser"); 58 } 59 60 var request = jQuery.extend(true, {}, settings.request); 61 request.total.amount = settings.grandTotalCalculator(); 62 63 var session; 64 if(ApplePaySession.supportsVersion(3)) { 65 session = new ApplePaySession(3, request); 66 } else { 67 session = new ApplePaySession(1, request); 68 } 69 70 // Merchant Validation 71 session.onvalidatemerchant = function (event) { 72 var promise = performValidation(event.validationURL); 73 promise.then(function (merchantSession) { 74 var merchSession = JSON.parse(merchantSession); 75 session.completeMerchantValidation(merchSession); 76 }).catch(function(){ 77 console.log('Error') 78 }); 79 } 80 81 function performValidation(valURL) { 82 return new Promise(function(resolve, reject) { 83 84 console.log('Getting session'); 85 var xhr = new XMLHttpRequest(); 86 xhr.onload = function() { 87 resolve(this.response); 88 }; 89 xhr.onerror = reject; 90 xhr.open('POST', js_site_var['context_path'] + "/app/applepay/merchantValidation.json", true); 91 xhr.setRequestHeader('csrfToken', getCsrfToken()); 92 xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 93 xhr.send('valUrl=' + valURL); 94 95 }); 96 } 97 98 session.onpaymentmethodselected = function(event) { 99 jQuery.ajax({ 100 type: 'get', 101 url: js_site_var['context_path'] + "/app/applepay/lineTotals.json", 102 timeout: 5000, 103 headers: {'csrfToken': getCsrfToken() }, 104 dataType: 'json', 105 106 success: function (data) { 107 if(ApplePaySession.supportsVersion(3)) { 108 var update = {}; 109 110 update.newTotal = data.grandtotal; 111 update.newLineItems = data.line; 112 113 session.completePaymentMethodSelection(update); 114 } else { 115 var newTotal = data.grandtotal; 116 var newLineItems = data.line; 117 118 session.completePaymentMethodSelection(newTotal, newLineItems); 119 } 120 121 console.log('completePaymentMethodSelection') 122 }, 123 error: function(xhr){ 124 var json = xhr.responseJSON; 125 console.log(json); 126 } 127 }); 128 } 129 130 session.onshippingcontactselected = function(event) { 131 console.log('onshippingcontactselected called'); 132 jQuery.ajax({ 133 type: 'post', 134 url: js_site_var['context_path'] + "/app/applepay/lelaboShippingMethods.json", 135 headers: {'csrfToken': getCsrfToken() }, 136 data: JSON.stringify({ 137 lelaboCountryCode: getLelaboCountry(), 138 city: event.shippingContact.locality, 139 province: event.shippingContact.administrativeArea, 140 postalCode: event.shippingContact.postalCode, 141 country: event.shippingContact.countryCode 142 }), 143 timeout: 5000, 144 contentType: 'application/json', 145 dataType: 'json', 146 147 success: function (data) { 148 for(i = 0; i < data.shipping.length; i++) { 149 let shippingInfo = data.shipping[i]; 150 let label = shippingInfo.label; 151 shippingInfo.label = htmlDecode(label); 152 } 153 154 if(ApplePaySession.supportsVersion(3)) { 155 var update = {}; 156 157 update.newLineItems = data.line; 158 update.newShippingMethods = data.shipping; 159 update.newTotal = data.grandTotal; 160 161 if(typeof settings.onShippingContactValidation === 'function'){ 162 settings.onShippingContactValidation(event, update); 163 }else if(event.shippingContact.countryCode.toLowerCase() != 'us') { 164 var error = new ApplePayError("addressUnserviceable", "country", "Apple Pay can only be used for domestic orders in the United States. Please select a US address.") 165 update.errors = [error]; 166 } 167 168 session.completeShippingContactSelection(update); 169 } else { 170 var status = ApplePaySession.STATUS_SUCCESS; 171 172 /** 173 * if onShippingContactStatus is overriden and the result is true... 174 * meaning the contact country code didn't match the event.shippingContact.countryCode 175 * 176 */ 177 if((typeof settings.onShippingContactStatus === 'function' && settings.onShippingContactStatus(event)) || event.shippingContact.countryCode.toLowerCase() != 'us') { 178 status = ApplePaySession.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS; 179 } 180 181 var shipping = data.shipping; 182 var grandTotal = data.grandTotal; 183 var lines = data.line; 184 185 session.completeShippingContactSelection(status, shipping, grandTotal, lines); 186 } 187 188 if(settings.shippingContactSelectedCallback == 'function'){ 189 settings.shippingContactSelectedCallback(event,data); 190 } 191 192 console.log('after completeShippingContactSelection') 193 }, 194 error: function(xhr){ 195 if(typeof settings.errorCallback === 'function'){ 196 settings.errorCallback(event,xhr,session); 197 } else{ 198 var json = xhr.responseJSON; 199 console.log(json); 200 } 201 } 202 }); 203 } 204 205 session.onshippingmethodselected = function(event) { 206 jQuery.ajax({ 207 type: 'post', 208 url: js_site_var['context_path'] + "/app/applepay/selectShippingMethod.json", 209 data: JSON.stringify({ shippingMethod: event.shippingMethod.identifier }), 210 headers: {'csrfToken': getCsrfToken() }, 211 timeout: 5000, 212 dataType: 'json', 213 contentType: 'application/json', 214 success: function (data) { 215 if(ApplePaySession.supportsVersion(3)) { 216 var update = {}; 217 218 update.newTotal = data.grandtotal; 219 update.newLineItems = data.line; 220 221 session.completeShippingMethodSelection(update); 222 } else { 223 var status = ApplePaySession.STATUS_SUCCESS; 224 var newTotal = data.grandtotal; 225 var newLineItems = data.line; 226 227 session.completeShippingMethodSelection(status, newTotal, newLineItems); 228 } 229 230 console.log('completeShippingMethodSelection') 231 }, 232 error: function(xhr){ 233 var json = xhr.responseJSON; 234 console.log(json); 235 } 236 }); 237 238 } 239 240 session.onpaymentauthorized = function (event) { 241 let data = {}; 242 var billingContact = event.payment.billingContact; 243 var billingAddress1 = billingContact.addressLines[0]; 244 245 var billingAddress2; 246 if (typeof billingContact.addressLines[1] === 'undefined'){ 247 billingAddress2 = ''; 248 } 249 else{ 250 billingAddress2 = billingContact.addressLines[1] 251 } 252 253 data.billingFirstName = billingContact.givenName; 254 data.billingLastName = billingContact.familyName; 255 data.billingAddress1 = billingAddress1; 256 data.billingAddress2 = billingAddress2; 257 data.billingPhoneNumber = billingContact.phoneNumber; 258 data.billingCity = billingContact.locality; 259 data.billingProvince = billingContact.administrativeArea; 260 data.billingPostalCode = billingContact.postalCode; 261 data.billingCountry = billingContact.countryCode 262 263 var shippingContact = event.payment.shippingContact; 264 265 if(shippingContact.givenName.length > 0) { 266 var shippingAddress1 = shippingContact.addressLines[0]; 267 var shippingAddress2; 268 if (typeof shippingContact.addressLines[1] === 'undefined') { 269 shippingAddress2 = ''; 270 } else { 271 shippingAddress2 = shippingContact.addressLines[1]; 272 } 273 274 data.shippingFirstName = shippingContact.givenName; 275 data.shippingLastName = shippingContact.familyName; 276 data.shippingAddress1 = shippingAddress1; 277 data.shippingAddress2 = shippingAddress2; 278 data.shippingPhoneNumber = shippingContact.phoneNumber; 279 data.shippingCity = shippingContact.locality; 280 data.shippingProvince = shippingContact.administrativeArea; 281 data.shippingPostalCode = shippingContact.postalCode; 282 data.shippingCountry = shippingContact.countryCode; 283 data.shippingPhoneNumber = shippingContact.phoneNumber; 284 } 285 286 data.shippingEmail = shippingContact.emailAddress.trim(); 287 288 jQuery.ajax({ 289 type: 'post', 290 url: js_site_var['context_path'] + "/app/applepay/lelaboSetBilling.json", 291 headers: {'csrfToken': getCsrfToken() }, 292 data: JSON.stringify(data), 293 contentType: 'application/json', 294 timeout: 5000, 295 dataType: 'json', 296 297 success: function (data) { 298 var promise = sendPaymentToken(event.payment.token); 299 promise.then(function (success) { 300 if(ApplePaySession.supportsVersion(3)) { 301 result = {}; 302 303 result.status = "STATUS_SUCCESS"; 304 305 session.completePayment(result); 306 } else { 307 status = ApplePaySession.STATUS_SUCCESS; 308 309 session.completePayment(status); 310 } 311 }, 312 313 function(errors) { 314 if(ApplePaySession.supportsVersion(3)) { 315 result = {}; 316 317 result.status = "STATUS_FAILURE"; 318 result.errors = []; 319 320 for(var i = 0; i < errors.length; i++) { 321 var error = new ApplePayError("billingContactInvalid"); 322 error.message = errors[i]; 323 error.contactField = "name"; 324 325 result.errors.push(error); 326 } 327 328 session.completePayment(result); 329 } else { 330 status = ApplePaySession.STATUS_INVALID_BILLING_POSTAL_ADDRESS; 331 332 session.completePayment(status); 333 } 334 }); 335 }, 336 error: function(xhr){ 337 if(settings.paymentAuthorizedErrorCallback !== null) { 338 settings.paymentAuthorizedErrorCallback(event,xhr,session); 339 } else { 340 var json = xhr.responseJSON; 341 var fieldErrors = (json && json.fieldErrors) ? json.fieldErrors : []; 342 var sawShipping = false;
343 var sawBilling = false; 344 345 if(ApplePaySession.supportsVersion(3)) { 346 var result = { 347 status: "STATUS_FAILURE", 348 errors: [] 349 }; 350 351 for(var i = 0; i < fieldErrors.length; i++) { 352 var mapped = mapFieldToApplePayError(fieldErrors[i].field, fieldErrors[i].message); 353 result.errors.push(mapped.error); 354 if(mapped.scope === "shipping") sawShipping = true; 355 if(mapped.scope === "billing") sawBilling = true; 356 } 357 358 session.completePayment(result); 359 } else { 360 for(var j = 0; j < fieldErrors.length; j++) { 361 var f = fieldErrors[j].field || ""; 362 if(f.indexOf("shipping") === 0) sawShipping = true; 363 else if(f.indexOf("billing") === 0) sawBilling = true; 364 } 365 var fallback = sawShipping && !sawBilling 366 ? ApplePaySession.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS 367 : ApplePaySession.STATUS_INVALID_BILLING_POSTAL_ADDRESS; 368 session.completePayment(fallback); 369 } 370 } 371 } 372 }) 373 374 } 375 376 function sendPaymentToken(paymentToken) { 377 return new Promise(function(resolve, reject) { 378 379 returnFromGateway = createTransaction(paymentToken); 380 381 if ( returnFromGateway == undefined ) 382 resolve(true); 383 else 384 reject(returnFromGateway); 385 }); 386 } 387 388 function createTransaction(dataObj) { 389 390 console.log('starting createTransaction'); 391 console.log(dataObj); 392 393 var objJsonStr = JSON.stringify(dataObj.paymentData); 394 console.log(objJsonStr); 395 var objJsonB64 = window.btoa(objJsonStr); 396 console.log(objJsonB64); 397 398 var result; 399 400 jQuery.ajax({ 401 async: false, 402 url: js_site_var['context_path'] + "/app/applepay/setAuthToken.json", 403 data: JSON.stringify({token: objJsonB64, data: dataObj}), 404 headers: {'csrfToken': getCsrfToken() }, 405 method: 'POST', 406 contentType: 'application/json', 407 timeout: 5000, 408 success: function(data){ 409 result = checkout(); 410 }, 411 error: function(xhr){ 412 var json = xhr.responseJSON; 413 console.log(json); 414 session.abort(); 415 } 416 417 }); 418 419 return result; 420 } 421 422 function checkout() { 423 console.log('starting checkout'); 424 var result; 425 var xhrObj = new XMLHttpRequest(); 426 427 jQuery.ajax({ 428 async: false, 429 type: 'get', 430 url: js_site_var['context_path'] + "/app/cart/checkout", 431 headers: {'csrfToken': getCsrfToken() }, 432 timeout: 5000, 433 xhr: function() { 434 return xhrObj; 435 }, 436 success: function (data, status, xhr) { 437 if(data.errors) { 438 result = data.errors; 439 } else { 440 result = this.xhr().responseURL; 441 } 442 } 443 }); 444 445 if(Array.isArray(result)) { 446 return result; 447 } else { 448 window.location = result; 449 } 450 } 451 452 session.oncancel = function(event) { 453 var url = js_site_var['context_path'] + "/app/applepay/clear" 454 jQuery.ajax({ 455 type: 'get', 456 url: url , 457 dataType: 'json', 458 success: function (data) { 459 console.log('apple pay session cleared') 460 } 461 }); 462 463 console.log('starting session.cancel'); 464 console.log(event); 465 } 466 467 session.begin(); 468 469} 470 471function htmlDecode(input) { 472 var doc = new DOMParser().parseFromString(input, "text/html"); 473 return doc.documentElement.textContent; 474} 475 476function mapFieldToApplePayError(field, message) { 477 var scope = "billing"; 478 var suffix = field || ""; 479 if(suffix.indexOf("shipping") === 0) { 480 scope = "shipping"; 481 suffix = suffix.substring("shipping".length); 482 } else if(suffix.indexOf("billing") === 0) { 483 scope = "billing"; 484 suffix = suffix.substring("billing".length); 485 } 486 487 var contactFieldMap = { 488 "FirstName": "name", 489 "LastName": "name", 490 "Address1": "addressLines", 491 "Address2": "addressLines", 492 "City": "locality", 493 "Province": "administrativeArea", 494 "PostalCode": "postalCode", 495 "Country": "countryCode", 496 "PhoneNumber": "phoneNumber", 497 "Email": "emailAddress" 498 }; 499 var contactField = contactFieldMap[suffix] || "postalAddress"; 500 501 var errorType = scope === "shipping" ? "shippingContactInvalid" : "billingContactInvalid"; 502 var error = new ApplePayError(errorType, contactField, message); 503 return { error: error, scope: scope }; 504}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.