1var CheckoutContextWrapper = function (checkout) { 2 /** 3 * Private property `checkoutCtrl` 4 * @private 5 * The reason of private access is polymorphism, we should support another types of context objects 6 * with the same interface 7 */ 8 var _checkout = checkout; 9 10 /** 11 * Public config for some default values 12 * @type {{country: string, currency: string}} 13 */ 14 this.defaults = { 15 currency: 'usd', 16 country: 'US', 17 paymentRequestButtonWrapperId: 'payment-request-button-wrapper', 18 paymentRequestButtonId: 'payment-request-button', 19 }; 20 21 /** 22 * @returns {boolean} 23 */ 24 this.isEnabled = function () { 25 return _checkout.isCardProcessorRain(); 26 }; 27 28 /** 29 * @returns {{paymentRequest: function(Object), elements: function(), confirmCardPayment: function(string, Object, Object?)}} 30 */ 31 this.getStripe = function () { 32 return _checkout.Rain.stripe; 33 }; 34 35 /** 36 * @returns {string} 37 */ 38 this.getCurrency = function () { 39 if (_checkout.InitInfo && _checkout.InitInfo.Currency && typeof _checkout.InitInfo.Currency === 'string') { 40 return _checkout.InitInfo.Currency.toLowerCase(); 41 } 42 43 return this.defaults.currency; 44 }; 45 46 /** 47 * @returns {string} 48 */ 49 this.getCountry = function () { 50 var countryIso = _checkout.CountryIso2KeyGet(_checkout.InitInfo.DomesticCountry); 51 52 if (countryIso && typeof countryIso === 'string') { 53 return countryIso.toUpperCase(); 54 } 55 56 return this.defaults.country; 57 }; 58 59 /** 60 * @returns {number} 61 */ 62 this.getTotalAmount = function () { 63 var amountDue = _checkout.getAmountDue(); 64 65 if (typeof amountDue !== 'number' || isNaN(amountDue)) { 66 return 0; 67 } 68 69 return parseInt((amountDue * 100).toFixed(0)); 70 }; 71 72 /** 73 * @returns {boolean} 74 */ 75 this.areShippingOptionsRequired = function () { 76 return _checkout.areShippingOptionsRequired(); 77 }; 78 79 /** 80 * @returns {{id: string, label: string, detail: string, amount: number}[]} 81 */ 82 this.getRainRequestPaymentShippingOptions = function () { 83 var inStorePickupOptions = []; 84 var curbsidePickupOptions = []; 85 var shippingOptions = []; 86 87 if (_checkout.isStorePickupEnabled() && _checkout.enhancedShippingInfo.storePickupOptions) { 88 inStorePickupOptions = Object.entries(_checkout.enhancedShippingInfo.storePickupOptions).map(function (entry) { 89 var option = entry[1]; 90 91 return { 92 id: option.id, 93 label: option.store_location_name + '(' + option.delivery_time + ')', 94 detail: 'In-Store Pickup', 95 amount: 0, 96 }; 97 }); 98 } 99 100 if (_checkout.isCurbsidePickupEnabled()) { 101 curbsidePickupOptions = Object.entries(_checkout.enhancedShippingInfo.curbsidePickupOptions).map(function (entry) { 102 var option = entry[1]; 103 104 return { 105 id: option.id, 106 label: option.store_location_name + '(' + option.delivery_time + ')', 107 detail: 'Curbside Pickup', 108 amount: 0, 109 }; 110 }); 111 } 112 113 if (_checkout.isShippingEnabled()) { 114 shippingOptions = Object.entries(_checkout.enhancedShippingInfo.shippingOptions).map(function (entry) { 115 var option = entry[1]; 116 117 var price = _checkout.isTaxInclusive() ? _checkout.addShippingTaxes(option.price) : option.price; 118 var amount = parseInt((price * 100).toFixed(0)); 119 120 return { 121 id: option.id, 122 label: option.name, 123 detail: option.delivery_time ?? '', 124 amount: amount, 125 }; 126 }); 127 128 shippingOptions.sort(function (shippingOption1, shippingOption2) { 129 if (shippingOption1.amount >
129 shippingOption2.amount) { 130 return 1; 131 } 132 if (shippingOption1.amount < shippingOption2.amount) { 133 return -1; 134 } 135 return 0; 136 }) 137 } 138 139 return [].concat(shippingOptions, inStorePickupOptions, curbsidePickupOptions); 140 }; 141 142 /** 143 * @param id {string|null} 144 */ 145 this.setShippingOptionId = function (id) { 146 _checkout.ShippingInfo.OptionId = id; 147 }; 148 149 /** 150 * @returns {?string} 151 */ 152 this.getShippingOptionId = function () { 153 return _checkout.ShippingInfo.OptionId; 154 }; 155 156 /** 157 * @param validate {boolean?} 158 * @returns {Promise} 159 */ 160 this.onShippingOptionChange = function (validate) { 161 if (typeof validate === 'undefined') { 162 validate = false; 163 } 164 165 return _checkout.ShippingVerify().then(function () { 166 if (validate && !_checkout.ShippingStatus.Valid) { 167 if (_checkout.ShippingStatus.NotValidMessages) { 168 console.warn(_checkout.ShippingStatus.NotValidMessages); 169 } 170 171 return Promise.reject(new Error('Invalid Shipping data')); 172 } 173 }); 174 }; 175 176 /** 177 * @param address {{country: string, city: string, region: string, postalCode: string, recipient: string, addressLine: string[]}} 178 */ 179 this.setShippingAddress = function (address) { 180 if (!address) { 181 // Maybe we aren't shipping anything 182 return; 183 } 184 var country = _checkout.InitInfo.CountriesIso2[address.country]; 185 186 _checkout.ShippingCountrySet(country); 187 _checkout.ShippingInfo.City = address.city; 188 _checkout.ShippingInfo.State = address.region; 189 _checkout.ShippingInfo.Zip = address.postalCode; 190 _checkout.ShippingInfo.Address1 = address.addressLine[0] || ''; 191 _checkout.ShippingInfo.Address2 = address.addressLine[1] || ''; 192 193 var nameParts = address.recipient.split(' ', 2); 194 195 _checkout.ShippingInfo.FirstName = nameParts[0] || ''; 196 _checkout.ShippingInfo.LastName = nameParts[1] || ''; 197 }; 198 199 /** 200 * @returns {{country: string, city: string, postalCode: string, recipient: string, region: string, addressLine: string[]}} 201 */ 202 this.getShippingAddress = function () { 203 var country = _checkout.ShippingInfo.CountryIso2Key || ''; 204 var city = _checkout.ShippingInfo.City || ''; 205 var region = _checkout.ShippingInfo.State || ''; 206 var postalCode = _checkout.ShippingInfo.Zip || ''; 207 var addressLine = [_checkout.ShippingInfo.Address1, _checkout.ShippingInfo.Address2].filter(s => !!s); 208 var recipient = [_checkout.ShippingInfo.FirstName, _checkout.ShippingInfo.LastName].filter(s => !!s).join(' '); 209 210 return { 211 country: country, 212 city: city, 213 region: region, 214 postalCode: postalCode, 215 addressLine: addressLine, 216 recipient: recipient, 217 }; 218 }; 219 220 /** 221 * @param validate {boolean?} 222 * @returns {Promise} 223 */ 224 this.loadShippingOptions = function (validate) { 225 return _checkout.ShippingOptionsGet() 226 .then(function () { 227 this.onShippingOptionChange(validate); 228 }.bind(this)); 229 }; 230 231 this.resetShippingAddress = function () { 232 this.setShippingOptionId(null); 233 234 this.setShippingAddress({ 235 country: _checkout.InitInfo.DomesticCountry, 236 city: '', 237 region: '', 238 postalCode: '', 239 recipient: '', 240 addressLine: [], 241 }); 242 }; 243 244 this.resetBillingAddress = function () { 245 _checkout.BillingInfo.AddressSameAsShipping = true; 246 247 this.setBillingInfo('', '', '', { 248 country: _checkout.InitInfo.DomesticCountry, 249 city: '', 250 state: '', 251 postal_code: '', 252 line1: '', 253 line2: '', 254 }); 255 }; 256 257 /** 258 * @returns {Promise} 259 */ 260 this.createTransaction = function () { 261 _checkout.setProcessor(_checkout.PROCESSOR_RAIN); 262 _checkout.BillingInfo.AddressSameAsShipping = _checkout.isPickup(); 263 264 return _checkout.PreProcess().then(function(id) { 265 if (!id) { 266 throw new Error('Unable to create transaction'); 267 } 268 }); 269 }; 270 271 /** 272 * @returns {Promise<String>} 273 */ 274 this.createPaymentIntent = function (paymentMethod) { 275 return _checkout.SubmitInfoGet(_checkout.getSubmitInfoGetData(paymentMethod)).then(function () { 276 if (!_checkout.Rain.processorToken && !_checkout.TransactionId) { 277 throw new Error('Unable to create Payment Intent'); 278 } 279 280 return _checkout.Rain.processorToken; 281 }); 282 }; 283 284 /** 285 * @param result 286 * @returns {Promise} 287 */ 288 this.handleIntentResult = function (result) { 289 return _checkout.handleIntentResult(result); 290 }; 291 292 /** 293 * 294 * @returns {Promise} 295 */ 296 this.finishTransaction = function () { 297 _checkout.setProcessingCheckout(false); 298 299 return _checkout.PostProcess(); 300 }; 301 302 /** 303 * @returns {string} 304 */ 305 this.getPaymentRequestButtonId = function () { 306 return this.defaults.paymentRequestButtonId; 307 }; 308 309 this.showPaymentRequestButton = function () { 310 var wrapper = document.getElementById(this.defaults.paymentRequestButtonWrapperId); 311 312 wrapper.style.display = 'block'; 313 314 _checkout.isRequestPaymentSupported = true; 315 _checkout.safeApply(); 316 }; 317 318 /** 319 * @param name {string} 320 * @param email {string} 321 * @param phone {string} 322 * @param address {{line1: string, line2: string, city: string, state: string, country: string, postal_code: string}} 323 */ 324 this.setBillingInfo = function (name, email, phone, address) { 325 if (name) {
326 var nameParts = name.split(' ', 2); 327 _checkout.BillingInfo.FirstName = nameParts[0] || ''; 328 _checkout.BillingInfo.LastName = nameParts[1] || ''; 329 } else { 330 _checkout.BillingInfo.FirstName = ''; 331 _checkout.BillingInfo.LastName = ''; 332 } 333 334 _checkout.BillingInfo.Email = email; 335 _checkout.BillingInfo.Phone = phone; 336 337 var country = _checkout.InitInfo.CountriesIso2[address.country]; 338 339 _checkout.setBillingCountry(country); 340 341 _checkout.BillingInfo.Address1 = address.line1; 342 _checkout.BillingInfo.Address2 = address.line2; 343 _checkout.BillingInfo.City = address.city; 344 _checkout.BillingInfo.State = address.state; 345 _checkout.BillingInfo.Zip = address.postal_code; 346 _checkout.BillingInfo.AddressSameAsShipping = _checkout.isPickup(); 347 }; 348 349 this.onBillingChange = function () { 350 _checkout.BillingVerify(); 351 352 _checkout.BillingOptionsGet(); 353 }; 354};
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.