Forked from kanwei/Syracuse - Installs DTD tools on donation form
Last active
February 26, 2020 14:43
-
-
Save ecolui/bf21e56ed93a887b4b3dba0660faa7f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
//Syracuse University | |
//Installing DTD tools for main donation form | |
//https://cusecommunity.syr.edu/s/1632/17/form/landing.aspx?sid=1632&gid=2&pgid=383&sitebuilder=1&contentbuilder=1 | |
//Syracuse public API key | |
var DDCONF = {API_KEY: "YTQ4NDNhNTktYjIy"}; | |
//loading the dtd script wipes out localstorage, so capture that data early | |
var dtd_streamlined_search_data = {}; | |
if (localStorage){ | |
if (localStorage.doublethedonation){ | |
try{ | |
var ss_data = JSON.parse(localStorage.doublethedonation); | |
dtd_streamlined_search_data.company_id = ss_data.doublethedonation_company_id; | |
dtd_streamlined_search_data.entered_text = ss_data.doublethedonation_entered_text; | |
} | |
catch(e){} | |
} | |
} | |
var process_dtd_code = function(){ | |
//Remove legacy matching gift tool | |
setTimeout(function(){ jQuery("table.checkboxCategory td:contains('My employer will match my gift')").remove() }, 1000); | |
//Find out if we are on the donation page or finish page | |
var url_search = window.location.search; | |
var current_page = ""; | |
if (url_search.indexOf("billing=") !== -1) | |
current_page = "billing"; | |
else if (url_search.indexOf("gfid=") !== -1) | |
current_page = "donation"; | |
else if (url_search.indexOf("showconf=") !== -1) | |
current_page = "review"; | |
else if (url_search.indexOf("fb_ciuid=") !== -1 || url_search.indexOf("tid=") !== -1 || url_search.indexOf("fshid=") !== -1) | |
current_page = "finish"; | |
//function that will execute on the donation page | |
var donation_page_function = function(){ | |
var dtdbootstrapfn = function(){ | |
if (window.doublethedonation){ | |
doublethedonation.plugin.load_streamlined_input(); | |
} | |
}; | |
//try to put the streamlined search as a field element | |
var field_target = jQuery(".imod_categoryWrapper").first(); | |
if (field_target.length){ | |
var label = "<div class='imod_fieldLabel'><label for='dd-input'>Matching Gift Company</label></div>"; | |
var ss_input = "<div class='imod_fieldInput idbmsInstanceColumnInput'><div id='dd-company-name-input'></div></div>"; | |
jQuery("<div class='imod_fieldWrapper'>" + label + ss_input + "</div>").insertAfter(field_target); | |
} | |
//else if (backup_target.length){ | |
// jQuery( "<div>See if your company will match your donation!</div><div id='dd-company-name-input'></div><br><br>").insertBefore(backup_target); | |
//} | |
var script = document.createElement("script"); | |
script.src = "https://doublethedonation.com/api/js/ddplugin.js"; | |
document.head.appendChild(script); | |
jQuery('head').append('<link href="https://doublethedonation.com/api/css/ddplugin.css" rel="stylesheet" />'); | |
script.onload = dtdbootstrapfn; | |
}; | |
//function that will execute on the finish page | |
var finish_page_function = function(){ | |
var dtdbootstrapfn = function(){ | |
try{ | |
if (window.doublethedonation) { | |
var formDataLabels = jQuery("div.imod_fieldLabel span"); | |
var filter_hof = function(target){ | |
return function(index, elem){ | |
return (elem.innerHTML.toUpperCase().indexOf(target.toUpperCase()) !== -1); | |
}; | |
}; | |
var donation_amount = [ jQuery("td.CommerceSubTotal").first().text(), | |
formDataLabels.filter(filter_hof("I would like to give")).first().siblings().first().text(), | |
formDataLabels.filter(filter_hof("Donation Amount")).first().siblings().first().text(), | |
formDataLabels.filter(filter_hof("Amount")).first().siblings().first().text()] | |
.filter(function(s){return s != ""}).shift(); | |
var campaign = jQuery('title').text().replace(/^\s+|\s+$/gm,'') || ""; | |
var email = formDataLabels.filter(filter_hof("Email")).first().parent().siblings().first().text(); | |
var domain = doublethedonation.integrations.core.strip_domain(email); | |
var donation_identifier = jQuery(".PrimaryConfirmationNumber").children().first().text().split(/\W+/).pop() || (email + '' + Date.now()); | |
var donor_first_name = formDataLabels.filter(filter_hof("Name")).first().parent().siblings().first().text(); | |
var phone = formDataLabels.filter(filter_hof("Phone")).first().parent().siblings().first().text(); | |
//load the plugin | |
var target = jQuery("div h3:contains('Transaction Summary')").parent()[0] || jQuery("div.CommerceSummary")[0]; | |
jQuery( "<div id='dd-container'></div><br>" ).insertAfter(target); | |
doublethedonation.plugin.load_plugin(); | |
doublethedonation.plugin.set_donation_identifier(donation_identifier); | |
doublethedonation.plugin.set_donation_campaign(campaign); | |
doublethedonation.plugin.email_domain( domain ); | |
//register the donation | |
doublethedonation.integrations.core.register_donation({ | |
"360matchpro_public_key": DDCONF.API_KEY, | |
"campaign": campaign, | |
"donation_identifier": donation_identifier, | |
"donation_amount": donation_amount, | |
"donor_phone": phone, | |
"donor_first_name": donor_first_name, | |
"donor_email": email, | |
"doublethedonation_company_id" : dtd_streamlined_search_data.company_id, | |
"doublethedonation_entered_text" : dtd_streamlined_search_data.entered_text | |
}); | |
if (dtd_streamlined_search_data.company_id) | |
doublethedonation.plugin.set_company(dtd_streamlined_search_data.company_id); | |
} | |
} | |
catch(e){console.log('DTD error registering the donation' + e.toString())}; | |
} | |
var script = document.createElement("script"); | |
script.onload = dtdbootstrapfn; | |
script.src = "https://doublethedonation.com/api/js/ddplugin.js"; | |
document.head.appendChild(script); | |
jQuery('head').append('<link href="https://doublethedonation.com/api/css/ddplugin.css" rel="stylesheet" />'); | |
}; | |
switch (current_page){ | |
case "donation" : donation_page_function(); break; | |
case "finish" : finish_page_function(); break; | |
} | |
}; | |
if (typeof jQuery !== 'undefined'){ | |
jQuery( document ).ready(function() { | |
process_dtd_code(); | |
}); | |
} | |
else{ | |
document.addEventListener('DOMContentLoaded', function(event){ | |
process_dtd_code(); | |
}); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment