(function () { var initScript = function (url, callback) { var script = document.createElement("script"); script.type = "text/javascript"; if (script.readyState) { script.onreadystatechange = function () { if (script.readyState == "loaded" || script.readyState == "complete") { script.onreadystatechange = null; callback(); } }; } else { script.onload = function () { callback(); }; } script.src = url; document.getElementsByTagName("head")[0].appendChild(script); }; var recurpaySubscription = function ($) { window.recurpay = {}; recurpay.domainURL = "https://friendsdiaper.recurpay.com"; recurpay.checkoutAPI = recurpay.domainURL + '/checkout/initiate.rp'; recurpay.customer = 'guest'; recurpay.checkout = "recurpay"; recurpay.store_url = "https://friendsdiaper.in"; recurpay.product_id = "5512641806502"; recurpay.schema = "{"title":{"subscribe_modal":"Subscribe and Save","subscribe_button":"Subscribe Now"},"css":{"brand_color":"#000000","button_color":"#ffffff"}}"; recurpay.schema = JSON.parse(recurpay.schema.replace(/"/g, '"')); var subscriptionSelector = $('.recurpay-subscription-btn[data-product-id]'); var subscriptionSelectorLen = $(subscriptionSelector).length; $('head').append(``); if (subscriptionSelectorLen > 0) { for (i = 0; i < subscriptionSelectorLen; i++) { var recurHTML = ''; recurHTML += ''; recurHTML += '
'; recurHTML += '
×
' + recurpay.schema.title.subscribe_modal + '
'; recurHTML += '
'; recurHTML += '
'; $(subscriptionSelector).html(recurHTML); } } function randomStrings() { var recurChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz"; var recurLength = 24; var recurString = ''; for (var i = 0; i < recurLength; i++) { var recurNum = Math.floor(Math.random() * recurChars.length); recurString += recurChars.substring(recurNum, recurNum + 1); } return (recurString); } if (typeof (Storage) !== "undefined") { if (localStorage.getItem("recurpayTracking") == null) { recurpay.token = randomStrings(); localStorage.setItem("recurpayTracking", recurpay.token); } else { recurpay.token = localStorage.getItem("recurpayTracking"); } } function recurPlan(data) { var plansData = ""; var PlanHtml = ''; $.each(data.plans, function (key, plan) { if (plan.properties.shipment_count == null || plan.properties.shipment_count == undefined) { var shipment_count = 1; } else { var shipment_count = plan.properties.shipment_count; } PlanHtml += '
'; }); plansData = 'Select Plan' + PlanHtml; return plansData; } function updateProductPreview(product) { var product_id = product; var parentElem = $('.recurpay-subscription-btn[data-product-id="' + product_id + '"]'); if ($(parentElem).find('[name="recurpay_variant"]').length <= 0) { $(parentElem).find('.recur-element-list').html('There are no subscription plans for this product.'); $(parentElem).find('.recur-final-btn').attr("disabled", true); } else { if ($(parentElem).find('[name="recurpay_variant"]').val() == undefined || $('[name="recurpay_variant"]').val() == null) { $(parentElem).find('.recur-element-list').html('There are no subscription plans for this product.'); $(parentElem).find('.recur-final-btn').attr("disabled", true); } else { if ($(parentElem).find('.recur-element-wrapper').length <= 0) { $(parentElem).find('.recur-element-list').html('There are no subscription plans for this product.'); $(parentElem).find('.recur-final-btn').attr("disabled", true); } else { $(parentElem).find('.recur-final-btn').removeAttr("disabled"); } } } } function updatePrice(product) { var product_id = product; var parentElem = $('.recurpay-subscription-btn[data-product-id="' + product_id + '"]'); var getVariant = $(parentElem).find('select[name="recurpay_variant"]').find("option:selected").val(); var getQty = $(parentElem).find('input[name="recurpay_quantity"]').val(); var getPrice = parseInt($(parentElem).find('select[name="recurpay_variant"]').find("option:selected").attr("data-price")) * getQty; var getLen = $(parentElem).find('.recur-element-wrapper input[name="plan_id"]').length; for (var i = 0; i < getLen; i++) { var getElem = $(parentElem).find('.recur-element-wrapper input[name="plan_id"]')[i]; var getDiscount = parseInt($(getElem).attr('data-plan-discount')); var shipmentCount = parseInt($(getElem).attr('data-shipment-count')); var discounted_price = Math.round((getPrice - (getPrice * getDiscount / 100)) * shipmentCount); $(getElem).parent().find('.recur-frequency-amount').html("₹" + discounted_price); } } function getPlans(product) { var product_id = product; var apiURL = recurpay.domainURL + "/api/storefront/product/" + product_id + "/plans.json"; $.ajax({ url: apiURL, type: 'GET', async: false, success: function (response) { var planHTML = ''; if (response != "ERROR" && response.success == true) { if (response.data.plans.length > 0) { planHTML = recurPlan(response.data); if ($('.recurpay-subscription-btn[data-product-id="' + product_id + '"] .recur-element-list').length) { $('.recurpay-subscription-btn[data-product-id="' + product_id + '"] .recur-element-list').html(planHTML); updateProductPreview(product_id); updatePrice(product_id); } else { updateProductPreview(product_id); } } else { if ($('.recurpay-subscription-btn[data-product-id="' + product_id + '"] .recur-element-list').length) { $('.recurpay-subscription-btn[data-product-id="' + product_id + '"] .recur-element-list').html(''); } updateProductPreview(product_id); } } } }); } function recurVariant(data) { var productData = ""; var variantHtml = ''; var hideSelect = false; $.each(data.variants, function (key, variant) { if (data.variants.length == 1 && variant.title == "Default Title") { hideSelect = true; } variantHtml += ''; }); if (hideSelect == true) { productData = 'Select Option' } else { productData = 'Select Option' } return productData; } function recurProduct(data) { var finalHTML = ""; if (data.image == null || data.image == undefined) { var recur_img_src = "https://cdn.shopify.com/s/files/1/1087/0872/files/images_798e43ad-99c3-41bc-ba72-fccdbf88665b.png?32370" } else { var recur_img_src = data.image.src; } finalHTML += '
' finalHTML += '
' + data.title + '
' finalHTML += '
' finalHTML += '
-
' finalHTML += '' finalHTML += '
+
' return finalHTML; } function getProduct(product) { var product_id = product; var apiURL = recurpay.domainURL + "/api/products/search/" + product_id; $('.recurpay-subscription-btn[data-product-id="' + product_id + '"] .recurpay-product-block').html('
'); $.ajax({ url: apiURL, type: 'GET', async: false, success: function (response) { var variantHTML = ''; var productHTML = ''; if (response != "ERROR" && response.success == true) { if (response.data.products.length > 0) { variantHTML = recurVariant(response.data.products[0]); productHTML = recurProduct(response.data.products[0]); $('.recurpay-subscription-btn[data-product-id="' + product_id + '"]').find('.recur-final-btn').removeAttr("disabled"); if ($('.recurpay-subscription-btn[data-product-id="' + product_id + '"] .recurpay-variant-list').length) { $('.recurpay-subscription-btn[data-product-id="' + product_id + '"] .recurpay-variant-list').html(variantHTML); } if ($('.recurpay-subscription-btn[data-product-id="' + product_id + '"] .recurpay-product-block').length) { $('.recurpay-subscription-btn[data-product-id="' + product_id + '"] .recurpay-product-block').html(productHTML); } getPlans(product_id); } else { if ($('.recurpay-subscription-btn[data-product-id="' + product_id + '"] .recurpay-variant-list').length) { $('.recurpay-subscription-btn[data-product-id="' + product_id + '"] .recurpay-variant-list').html(''); } if ($('.recurpay-subscription-btn[data-product-id="' + product_id + '"] .recurpay-product-block').length) { $('.recurpay-subscription-btn[data-product-id="' + product_id + '"] .recurpay-product-block').html(''); } } } else { $('.recurpay-subscription-btn[data-product-id="' + product_id + '"] .recurpay-product-block').html(''); $('.recurpay-subscription-btn[data-product-id="' + product_id + '"]').find('.recur-element-list').html('No such product found.'); $('.recurpay-subscription-btn[data-product-id="' + product_id + '"]').find('.recur-final-btn').attr("disabled", true); } } }); } function recurCart(cartData) { var checkoutData = [], cartItems = cartData.items, cartAttributes = [], cartNote = cartData.note, recurpayCheckout = true; recurHide = {}; for (var i = 0; i < cartItems.length; i++) { var Planid = '', item = cartItems[i], property = item.properties, lineProperty = []; if (property) { if (property._PlanId) { Planid = property._PlanId recurpayCheckout = true; } } $.each(cartData.attributes, function (key, value) { if (key != "Subscription" && key != '_PlanId') { cartAttributes.push({ name: key, value: value }); } }); if (Planid == '') { checkoutData.push({ quantity: item.quantity, variant_id: item.id, properties: lineProperty, type: 'ONETIME', currency: 'INR' }); } else { checkoutData.push({ quantity: item.quantity, variant_id: item.id, properties: lineProperty, plan_id: Planid, type: 'SUBSCRIPTION', currency: 'INR' }); } } recurHide = { "navigation": { "top": { "cart": true, } } }; if (recurpayCheckout) { var getCheckoutData = JSON.stringify(checkoutData), getCartAttributes = JSON.stringify(cartAttributes), getRecurHide = JSON.stringify(recurHide), cartNote = $('[name="note"]').val(); checkoutForm = $("
" + "" + "" + "" + "" + "" + "" + "" + "
"); $('body').append(checkoutForm); checkoutForm.submit(); } else { document.location.href = "/checkout"; } } function addSubscription(product) { var product_id = product; var recurpay_action = "checkout"; var parentElem = $('.recurpay-subscription-btn[data-product-id="' + product_id + '"]'); var variantSelector = $(parentElem).find('[name="recurpay_variant"] option:selected'), quantitySelector = $(parentElem).find('[name="quantity"]'), subscriptionPlanId = $(parentElem).find('[name="plan_id"]:checked').attr('data-plan-id'), subscriptionSellingPlanId = $(parentElem).find('[name="plan_id"]:checked').attr('data-selling-plan-id'), subscriptionPlanName = $(parentElem).find('[name="plan_id"]:checked').attr('data-plan-name'), subscriptionPlanType = $(parentElem).find('[name="plan_id"]:checked').attr('data-plan-type'), planVariant = parseInt(variantSelector.val()), planQuantity = 1, planParams = {}; cartData = {}; if (quantitySelector.length) { planQuantity = parseInt(quantitySelector.val()); } planParams = { id: planVariant, quantity: planQuantity, properties: { 'Subscription': subscriptionPlanName, '_PlanId': subscriptionPlanId } } cartData = { attributes: {}, items: [], note: null, total_price: 0 } cartData.items.push(planParams); if(recurpay.checkout == "shopify"){ var getURL = recurpay.store_url+"/cart/clear?return_to=/cart/add?items[][id]="+planVariant+"%26items[][quantity]="+planQuantity+"%26items[][selling_plan]="+subscriptionSellingPlanId+"%26return_to=/checkout"; window.location.href = getURL; } else{ recurCart(cartData); } } $(document).on('click', '.recur-final-btn', function () { $(this).addClass("recur-btn-loading"); var product_id = $(this).parents(".recurpay-subscription-btn").attr("data-product-id"); addSubscription(product_id); }); $(document).on('click', '.subscribe-btn[data-recurpay-subscription-btn]', function (e) { e.preventDefault(); $('.recurpay-modal').hide(); $('body').css("overflow", "hidden"); $(this).parent().find('.recurpay-modal').show(); var product_id = $(this).parent().attr("data-product-id"); getProduct(product_id); }); $(document).on('click', '.recur-close', function () { $(this).parents('.recurpay-modal').hide(); $('body').css("overflow", "initial"); }); $(document).on('click', '.recur-plus-btn', function (e) { e.preventDefault(); var recurVal = $(this).parent().find('[name="recurpay_quantity"]').val(); recurVal++; if (recurVal >= 1) { $(this).parent().find('[name="recurpay_quantity"]').val(recurVal); } var product_id = $(this).parents(".recurpay-subscription-btn").attr("data-product-id"); updatePrice(product_id); }); $(document).on('click', '.recur-minus-btn', function (e) { e.preventDefault(); var recurVal = $(this).parent().find('[name="recurpay_quantity"]').val(); recurVal--; if (recurVal >= 1) { $(this).parent().find('[name="recurpay_quantity"]').val(recurVal); } var product_id = $(this).parents(".recurpay-subscription-btn").attr("data-product-id"); updatePrice(product_id); }); $(document).on('change', '[name="recurpay_variant"]', function () { var product_id = $(this).find('option:selected').attr('data-product-id'); updatePrice(product_id); }); } if ((typeof (jQuery) == 'undefined')) { initScript('https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js', function () { var recurpayScript = jQuery.noConflict(true); recurpaySubscription(recurpayScript); }) } else { recurpaySubscription(jQuery); } })();