Skip to content

Instantly share code, notes, and snippets.

@mjarpitanand
Last active December 3, 2019 11:20
Show Gist options
  • Save mjarpitanand/d3db435ed84e34a5beeaaea533c758c7 to your computer and use it in GitHub Desktop.
Save mjarpitanand/d3db435ed84e34a5beeaaea533c758c7 to your computer and use it in GitHub Desktop.
var Rely = {};
/* New variables */
var image_width = '87px';
if(product_type === "rely"){
image_width = '50px';
}
// paylater , installement, rely
/* End of new variables */
Rely.loadScript = function (t, e) {
var a = document.createElement("script");
a.type = "text/javascript", a.readyState ? a.onreadystatechange = function () {
"loaded" != a.readyState && "complete" != a.readyState || (a.onreadystatechange = null, e())
} : a.onload = function () {
e()
}, a.src = t, document.getElementsByTagName("head")[0].appendChild(a)
},
Rely.renderParagraph = function (t, e) {
var a = t(document.createElement("p")).addClass("rely-paragraph");
// Created a paragraph to render in shopify client.
null !== rely_product && a.attr("data-product-id", rely_product.id);
// checking condition for null
var r = t(document.createElement("span")).addClass("afterpay-text1").appendTo(a);
// A span element created to append to the paragraph created.
var text_message1 = ""; var text_message2 = "";
if(message_type === "m1"){
text_message1 = "as low as ";
text_message2 = "";
}else if(message_type === "m2"){
text_message1 = rely_month_installment + " interest-free payments of";
text_message2 = " ";
}else if(message_type === "m3"){
text_message1 = "pay in ";
text_message2 = rely_month_installment+ " payments of ";
}else{}
r.html("Or " + text_message1 + text_message2 );
//A text is apeened to the span and whole will go that base paragraph
t(document.createElement("span")).addClass("rely-instalments").html(Rely.generateInstalmentHTML(e.total_price)).appendTo(a);
t(document.createElement("span")).html(' with').appendTo(a);
// A span os created to get the price and append to base span -> main paragraph
var temp_image_url = "https://res.cloudinary.com/postman/image/upload/w_152,h_56,c_fit,f_auto,t_team_logo/v1/team/05202b0a812dd096cb0717ac874349557659ab77fd6d27b974de6e7671fde5c6";
// One by default value wont alter the condiotions
if(product_type === "paylater"){
temp_image_url = "https://www.rely.sg/wp-content/uploads/2019/11/rely_paylater-rounded@2x.png";
}else if(product_type === "installment"){
temp_image_url = "https://www.rely.sg/wp-content/uploads/2019/11/rely_ins_lightblue@2x.png";
}else if(product_type === "rely"){
temp_image_url = "https://d1ydipeh1r4ifv.cloudfront.net/st/rely-primary-payment-logo.png";
}else{}
// End of the condtions for image
t(document.createElement("img")).addClass("rely-logo").css({
"vertical-align": "middle",
marginLeft: "8px",
width: image_width
}).attr("src", temp_image_url).attr("height", "20").attr("alt", "Rely").appendTo(a);
// Whole image class is here
if(message_type === "m3"){
t(document.createElement("span")).addClass("rely-text3").html(" ").appendTo(a);
}
t(document.createElement("a")).addClass("rely-learnmore").attr('target', '_blank').attr('title', ' Learn More about Rely').attr('href', 'https://www.rely.sg/how-it-works/').html("Learn More").css({
'font-size': 'small',
'text-decoration': 'underline',
'margin-left' : '2px'
}).appendTo(a);
return a
},
Rely.generateInstalmentHTML = function (t) {
var e = Math.round(t / rely_month_installment);
return Rely.formatMoney(e, rely_shop_money_format)
},
Rely.generateInsideLimitHTML = function (t) {
return t.find(".rely-instalments").html("Pay in " + rely_month_installment + " interest-free installments of "), t
},
Rely.init = function (t) {
Rely.track();
t("body").on("change", t('form[action^="/cart/add"]'), function (a) {
var e = Rely.renderParagraph(t, {
total_price: rely_current_variant.price
});
var r = t(a.target).closest("form"),
p = parseInt(t("input[name=id]:checked, select[name=id], input[name=id], hidden[name=id]", r).val(), 10),
o = !1;
!isNaN(p) && p > 0 && t.each(rely_product.variants, function (i, a) {
a.id == p && a.available && (e.find(".rely-instalments").html(Rely.generateInstalmentHTML(a.price)), t(".rely_widget").html(""), t(".rely_widget").append(e))
})
});
t('form[action^="/cart/add"]').trigger("change")
},
Rely.track = function () {
!(function (e, b) {
if (!b.__SV) {
var a, f, i, g;
window.alooma = b;
b._i = [];
b.init = function (a, e, d) {
function f(b, h) {
var a = h.split(".");
2 == a.length && (b = b[a[0]], h = a[1]);
b[h] = function () {
b.push([h].concat(Array.prototype.slice.call(arguments, 0)))
}
}
var c = b;
"undefined" !== typeof d ? c = b[d] = [] : d = "alooma";
c.people = c.people || [];
c.toString = function (b) {
var a = "alooma";
"alooma" !== d && (a += "." + d);
b || (a += " (stub)");
return a
};
c.people.toString = function () {
return c.toString(1) + ".people (stub)"
};
i = "disable time_event track track_custom_event track_pageview track_links track_forms register register_once alias unregister identify name_tag set_config people.set people.set_once people.increment people.append people.union people.track_charge people.clear_charges people.delete_user".split(" ");
for (g = 0; g < i.length; g++) f(c, i[g]);
b._i.push([a, e, d])
};
b.__SV = 1.2;
a = e.createElement("script");
a.type = "text/javascript";
a.async = !0;
a.src = "undefined" !== typeof ALOOMA_CUSTOM_LIB_URL ? ALOOMA_CUSTOM_LIB_URL : "file:" === e.location.protocol && "//cdn.alooma.com/libs/alooma-latest.min.js".match(/^\/\//) ? "https://cdn.alooma.com/libs/alooma-latest.min.js" : "//cdn.alooma.com/libs/alooma-latest.min.js";
f = e.getElementsByTagName("script")[0];
f.parentNode.insertBefore(a, f)
}
})(document, window.alooma || []);
alooma.init("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjbGllbnROYW1lIjoicmVseS1zZyIsImlucHV0TGFiZWwiOiJyZWx5LWFuYWx5dGljcy1tNDEiLCJpbnB1dFR5cGUiOiJKU1NESyJ9.LRBp3Xi2xQ0tyu785mP63-9p4aq31XCcu7DFl2cbGws", {
"api_host": "https://inputs.alooma.com"
});
},
Rely.formatMoney = function (cents, format) {
if (typeof cents == "string") {
cents = cents.replace(".", "")
}
var value = "";
var placeholderRegex = /\{\{\s*(\w+)\s*\}\}/;
var formatString = format || this.money_format;
function defaultOption(opt, def) {
return typeof opt == "undefined" ? def : opt
}
function formatWithDelimiters(number, precision, thousands, decimal) {
precision = defaultOption(precision, 2);
thousands = defaultOption(thousands, ",");
decimal = defaultOption(decimal, ".");
if (isNaN(number) || number == null) {
return 0
}
number = (number / 100).toFixed(precision);
var parts = number.split("."),
dollars = parts[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1" + thousands),
cents = parts[1] ? decimal + parts[1] : "";
return dollars + cents
}
switch (formatString.match(placeholderRegex)[1]) {
case "amount":
value = formatWithDelimiters(cents, 2);
break;
case "amount_no_decimals":
value = formatWithDelimiters(cents, 0);
break;
case "amount_with_comma_separator":
value = formatWithDelimiters(cents, 2, ".", ",");
break;
case "amount_no_decimals_with_comma_separator":
value = formatWithDelimiters(cents, 0, ".", ",");
break
}
return formatString.replace(placeholderRegex, value)
};
"function" != typeof jQuery || !Object.prototype.hasOwnProperty.call(jQuery, "fn") ? Rely.loadScript("https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js", function () {
Rely.jQuery_1_12_4 = jQuery.noConflict(!0), Rely.init(Rely.jQuery_1_12_4)
}) : Rely.init(jQuery);
// Javascript for page sdk.
$(function(){
var rely_tag_paylater = document.querySelector('#rely_paylater_page');
var rely_tag_installment = document.querySelector('#rely_installment_page');
var cssStylesObject = {
backgroundColor: '#ccc',
position: 'fixed',
width: '100%',
height: '100%',
top: '0px',
left: '0px',
zIndex: '9000'
};
if(rely_tag_paylater === null && rely_tag_installment === null){
console.log('Note: Page element cannot be find!');
}else if(rely_tag_paylater === null & rely_tag_installment !== null){
Object.assign(rely_tag_installment.style, cssStylesObject);
rely_tag_installment.innerHTML = `<div>installment</div>`;
}else if(rely_tag_paylater !== null & rely_tag_installment === null){
Object.assign(rely_tag_paylater.style, cssStylesObject);
rely_tag_paylater.innerHTML = `<div>PayLater</div>`;
}
// End of Javascript for page sdk.
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment