function formatPriceToLocale(amount, currency) { if (!(typeof amount === "number") || (!currency)) { throw Error('formatPriceToLocale(): params amount ' + amount + ' or currency ' + currency + ' are invalid!'); } var amountAsNumber = Number(amount); return amountAsNumber.toLocaleString(undefined, { style: "currency", currency: currency }); } function DiscountCoupon(correlationId, accountId, currency, singleProductCheckout) { this.correlationId = correlationId; this.accountId = accountId; this.objectName = null; this.appliedDiscountValue = 0; this.appliedDiscountType = null; this.customersCode = null; this.currency = currency; this.singleProductCheckout = singleProductCheckout; this.insertDiscountLinkToBody = function() { var discountCouponDiv = document.getElementById("discountCoupon"); if (discountCouponDiv) { discountCouponDiv.innerHTML = "I have a discount code"; var _this = this; document.getElementById("addCodeLink").addEventListener('click', function (event) { _this.putCouponCodeProcess(); }) return true; } return false; }; this.init = function() { var _this = this; if (_this.insertDiscountLinkToBody()) { return true; } return false; }; this.getAppliedDiscountValue = function() { return this.appliedDiscountValue; }; this.getAppliedDiscountType = function() { return this.appliedDiscountType; }; this.getCustomersCode = function() { return this.customersCode; }; this.defineDiscountVariablesOnDocument = function() { discountValue = this.appliedDiscountValue; discountType = this.appliedDiscountType; setupShippingMethodSelect(); }; this.changeButtonText = function() { classElements = document.getElementsByClassName('ableToDiscount'); for (classElement of classElements ) { var buttonText = classElement.innerHTML; if (!["Request Quotation","Solicitar presupuesto","Richiesta d'offerta","solicite uma cotação"].includes(buttonText)) {//to prevent display problems var fragmentButtonText = buttonText.split(" "); var number = fragmentButtonText.pop(); var text = fragmentButtonText.join(" ") var newPriceValue = formatPriceToLocale(Number(number.replace(/[^\d.]/g, '') * (100-this.getAppliedDiscountValue())/100 ), this.currency); var newButtonText = text + " " + newPriceValue + " (-" + this.getAppliedDiscountValue() + "%)" ; classElement.innerHTML = newButtonText; } } }; this.putCouponCodeProcess = function() { Swal.fire({ title: 'Put here you Discount Code!', input: 'text', showCancelButton: true, confirmButtonText: 'Confirm', cancelButtonText: 'Cancelar', customClass: "animated fadeIn", inputPlaceholder: 'Put Coupon Code here!', onClose: function(modal) { $(".swal2-popup").removeClass('fadeIn'); } }).then((result) => { if (result.value) { this.discountCouponRequest(result.value); } else if (result.dismiss != 'cancel') { this.errorResponceMessage('empty_field'); } }) }; this.discountCouponRequest = function(discountCode) { var interactionInstant = new Date(); var discountRequestObject = { accountId: this.accountId, discountCode: discountCode, timezone: interactionInstant.getTimezoneOffset() }; request .post('/beta/discountcoupon/request') .set('X-Request-ID', correlationId) .send(discountRequestObject) /*.end();*/ .end((err, res) => { if (err) { this.errorResponceMessage(res.body.reason) return false; } if (res.body.success) { if (( res.body.type == "percentage") && (res.body.data) && (res.body.data.value)) { this.customersCode = discountCode; this.SuccessResponceMessage(res.body.type,res.body.data.value) return true; } this.errorResponceMessage(null); return false; } }); }; this.SuccessResponceMessage = function(type, value) { switch(type) { case 'percentage': title = 'Concratulations!!'; text = 'You hace a discount of ' + value + '% in this purchase!'; break; } Swal.fire({ type: 'success', title: title, text: text, showConfirmButton: true, confirmButtonText: 'Confirm', customClass: "animated fadeIn", onClose: function(modal) { $(".swal2-popup").removeClass('fadeIn'); }, timer: 10000 }).then(() => { this.updatePrice(type, value); }); }; this.updatePrice = function(type, value) { this.appliedDiscountValue = value; this.appliedDiscountType = type; this.changeButtonText(); this.defineDiscountVariablesOnDocument(); var discountCouponDiv = document.getElementById("discountCoupon"); if (discountCouponDiv) { discountCouponDiv.innerHTML = "

" + "Applied discount:" + " -" +this.getAppliedDiscountValue() + "%" + "

"; } if (!singleProductCheckout) { window.parent.shoppingCart.setDiscountType(this.appliedDiscountType); window.parent.shoppingCart.setDiscountValue(this.appliedDiscountValue); window.parent.shoppingCart.updateShoppingCartTemplate(); window.parent.shoppingCart.updateTemplate(); } }; this.errorResponceMessage = function(reason) { switch(reason) { case 'outdated_coupon_code': title = 'Outdated Coupon Code'; text = 'The code is correct but it is outdated'; break; case 'not_valid_coupon': title = 'That is not a valid coupon!'; text = 'Try it with other one!'; break; case 'empty_field': title = 'Empty Code!'; text = 'Your Coupon code is empty!'; break; default: title = 'Could not serve your request!'; text = 'Please try again at a later time!'; break; } Swal.fire({ type: 'error', title: title, text: text, showConfirmButton: true, confirmButtonText: 'Confirm', timer: 10000 }).then(() => { //this.updatePrice(null, 0); }); }; this.init(); }