1'use strict'; 2 3// 4// Digital River 5// 6/* global readCookie, createCookie */ 7function CartProvider() { 8 // init 9 var countryCode = jQuery('body').data('site'), 10 path = window.location.pathname.split('/'), 11 base_url = void 0, 12 store_url = void 0, 13 apiKey = void 0, 14 themeID = '', 15 tokenKeyName = 'dr-htc-token-' + countryCode, 16 regionLocale = $('header').data('region-locale'), 17 regionCurrency = $('header').data('region-currency'), 18 apiText = void 0; 19 // US and CA countries 20 if (countryCode === 'us' || countryCode === 'ca' || countryCode === 'ca-fr') { 21 base_url = 'https://api.digitalriver.com/v1/shoppers/'; 22 store_url = 'https://store.us.htc.com/store/htcus/SessionToken'; 23 apiKey = 'UnZqOVp4laTfF1l71GqnzXR5e34MUG3M'; 24 } else if (countryCode === 'au') { 25 // APAC countries: AU 26 base_url = 'https://api.digitalriver.com/v1/shoppers/'; 27 store_url = 'https://shop.au.htc.com/store/htcapac/SessionToken'; 28 apiKey = 'cede30a1de9c44f6b9183ff9be9ae309'; 29 themeID = 40354300; 30 } else if (countryCode === 'hk-en' || countryCode === 'hk-tc' || countryCode === 'sea') { 31 base_url = 'https://api.digitalriver.com/v1/shoppers/'; 32 store_url = 'https://shop.apac.htc.com/store/htcapac/SessionToken'; 33 apiKey = 'e1a0f189b4f44b998fc8e47d515ab418'; 34 themeID = 40354300; 35 } else { 36 // active EMEA countries : UK, FR, DE, IE 37 base_url = 'https://api.digitalriver.com/v1/shoppers/'; 38 store_url = 'https://shop.eu.htc.com/store/htcemea/SessionToken'; 39 apiKey = '864d71cdaffb4e03a43a535c23d3292a'; 40 } // END: checking regions 41 if (typeof regionLocale !== 'undefined' && regionLocale !== undefined && regionLocale !== '' && typeof regionCurrency !== 'undefined' && regionCurrency !== undefined && regionCurrency !== '') { 42 apiText = 'locale=' + regionLocale + '¤cy=' + regionCurrency + '&'; 43 } else { 44 apiText = ''; 45 } 46 // Get Anonymous Shopping Token and Store in cookie 47 this.createAnonToken = function (callback) { 48 // console.log('tokenKeyName', tokenKeyName); 49 var token = readCookie(tokenKeyName); 50 if (token === null || token === '') { 51 $.ajax({ 52 url: store_url + '?' + apiText + 'format=json&method=get&ThemeID=' + themeID + '&apiKey=' + apiKey, 53 dataType: 'jsonp', 54 success: function success(results) { 55 createCookie(tokenKeyName, results.access_token, 1); 56 if (typeof callback === 'function') { 57 callback(); 58 } 59 }, 60 error: function error(xhr, textStatus, errorThrown) { 61 // console.log('createAnonToken error'); 62 } 63 }); 64 } else if (typeof callback === 'function') { 65 callback(); 66 } 67 }; 68 // Add a product to cart 69 this.addToCart = function (productId, quantity, callback) { 70 this.createAnonToken(function () { 71 var accessToken = readCookie(tokenKeyName); 72 $.ajax({ 73 url: base_url + 'me/carts/active/line-items?format=json&method=post&productId=' + productId + '&quantity=' + quantity + '&token=' + accessToken, 74 dataType: 'jsonp', 75 tryCount: 0, 76 retryLimit: 3, 77 success: function success(results) { 78 // set cart active cookie 79 createCookie('isCartActive', 'true', 1); 80 if (typeof callback === 'function') { 81 callback(); 82 } 83 }, 84 error: function error(xhr, textStatus, errorThrown) {} 85 }); 86 }); 87 }; 88 // Remove Item from Cart 89 this.removeItemFromCart = function (lineItemId, callback) { 90 this.createAnonToken(function () { 91 var accessToken = readCookie(tokenKeyName); 92 $.ajax({ 93 url: base_url + 'me/carts/active/line-items/' + lineItemId + '?format=json&method=delete&token='
93+ accessToken, 94 dataType: 'jsonp', 95 tryCount: 0, 96 retryLimit: 3, 97 success: function success(results) { 98 if (typeof callback === 'function') { 99 callback(); 100 } 101 }, 102 error: function error(xhr, textStatus, errorThrown) {} 103 }); 104 }); 105 }; 106 // Get cart 107 this.getCart = function (callback) { 108 // check if cart is active 109 var isCartActive = readCookie('isCartActive'); 110 // var isCartActive = 'true'; 111 if (isCartActive !== null && isCartActive === 'true') { 112 this.createAnonToken(function () { 113 var accessToken = readCookie(tokenKeyName); 114 // console.log('1.getCart=' + base_url + 'me/carts/active?format=json&expand=lineitems.lineItem.product.sku&method=get&token=' + accessToken); 115 $.ajax({ 116 url: base_url + 'me/carts/active?format=json&expand=lineitems.lineItem.product.sku&method=get&token=' + accessToken, 117 dataType: 'jsonp', 118 tryCount: 0, 119 retryLimit: 3, 120 success: function success(data) { 121 if (data && data.errors) { 122 createCookie('tokenKeyName', '', -1); 123 } 124 var dataItems = []; 125 if (data.cart.lineItems.lineItem) { 126 dataItems = data.cart.lineItems.lineItem; 127 } 128 var cartItems = []; 129 $.each(dataItems, function (index, dataItem) { 130 var item = { 131 id: dataItem.id, 132 name: dataItem.product.displayName, 133 image: dataItem.product.thumbnailImage, 134 quantity: dataItem.quantity, 135 price: dataItem.pricing.formattedListPriceWithQuantity, 136 saleprice: dataItem.pricing.formattedSalePriceWithQuantity 137 }; 138 cartItems[index] = item; 139 }); 140 var cart = { 141 items: cartItems, 142 totalItems: data.cart.totalItemsInCart, 143 subTotal: data.cart.pricing.formattedSubtotal, 144 checkoutURL: base_url + 'me/carts/active/web-checkout?token=' + accessToken + '&ThemeID=' + themeID 145 }; 146 if (typeof callback === 'function') { 147 callback(cart); 148 } 149 }, 150 error: function error(xhr, textStatus, errorThrown) {} 151 }); 152 }); 153 } 154 }; 155 // Checkout 156 this.checkout = function () { 157 this.getCart(function (cart) { 158 window.location = cart.checkoutURL; 159 }); 160 }; 161 // Get product info 162 this.getProduct = function (productSKU, callback) { 163 this.createAnonToken(function () { 164 // accessToken = readCookie(tokenKeyName); //not used 165 $.ajax({ 166 url: base_url + 'me/products/' + productSKU + '?' + apiText + 'format=json&method=get&apiKey=' + apiKey + '&expand=pricing,inventoryStatus', 167 dataType: 'jsonp', 168 tryCount: 0, 169 retryLimit: 3, 170 success: function success(data) { 171 var itemStatus = {}; 172 if (data.product) { 173 itemStatus = { 174 price: data.product.pricing.formattedListPrice, 175 saleprice: data.product.pricing.formattedSalePriceWithQuantity, 176 overrideOutOfStock: false, 177 availableStock: '', 178 isAvailable: data.product.inventoryStatus.productIsInStock, 179 isBackOrder: data.product.inventoryStatus.productIsAllowsBackorders, 180 isPreOrder: data.product.inventoryStatus.productIsAllowsBackorders 181 }; 182 } 183 if (typeof callback === 'function') { 184 callback(itemStatus); 185 } 186 }, 187 error: function error(xhr, textStatus, errorThrown) { 188 // console.log('getProduct error'); 189 } 190 }); 191 }); 192 }; 193} 194// init 195window.cartProvider = new CartProvider();
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.