Last active
February 5, 2018 19:04
-
-
Save MattArnold/dd7705131d0e85ea7710dccb5093ad43 to your computer and use it in GitHub Desktop.
Add White Pages Pro Identity Check To Order Screen
This file contains hidden or 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
// ==UserScript== | |
// @name Add White Pages Pro Identity Check To Order Screen | |
// @namespace https://gist.githubusercontent.com/MattArnold | |
// @version 0.2 | |
// @description Add White Pages Pro Identity Check To Order Screen | |
// @author Matt | |
// @include https://moosejaw.info/MachII/EditOrderERP.aspx?OrderID=* | |
// @match https://moosejaw.info/MachII/EditOrderERP.aspx?OrderID=* | |
// @updateURL https://gist.githubusercontent.com/MattArnold/dd7705131d0e85ea7710dccb5093ad43/raw/533e71b34753a5a47b3d170c4930a825bd4b595f | |
// @downloadURL https://gist.githubusercontent.com/MattArnold/dd7705131d0e85ea7710dccb5093ad43/raw/533e71b34753a5a47b3d170c4930a825bd4b595f | |
// @grant none | |
// @require http://code.jquery.com/jquery-latest.js | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
var output = document.createElement('div'); | |
output.className += " DGItem"; | |
output.style.padding = "0px 0px 1rem 0px"; | |
var order_number = $('#ctl00_cphMainContent_lblEcomOrderNumberOut').text(); | |
// Collect the info White Pages Pro needs to perform their Identity Check API. Put it into a string to go on the end of a URL. | |
var buildWppRequest = function() { | |
var bill_first_name = $("#ctl00_cphMainContent_lblBillCustomerFirstOut").text(); | |
var bill_last_name = $("#ctl00_cphMainContent_lblBillCustomerLastOut").text(); | |
var bill_name = (bill_first_name || bill_last_name) ? "primary.name=" + bill_first_name + "+" + bill_last_name : ""; | |
bill_name.replace(/\s'/g + '+'); | |
var email = $('#ctl00_cphMainContent_lblCustomerEmailOut').text(); | |
var bill_phone = $('#ctl00_cphMainContent_lblBillPhoneOut').text(); | |
var bill_address_one = $("#ctl00_cphMainContent_lblBillAddress1Out").text().replace(/\./g, ''); | |
var bill_address_two = $("#ctl00_cphMainContent_lblBillAddress2Out").text().replace(/\./g, ''); | |
var bill_city = $("#ctl00_cphMainContent_lblBillCityOut").text(); | |
var bill_state = $("#ctl00_cphMainContent_lblBillStateOut").text(); | |
var bill_zip = $("#ctl00_cphMainContent_lblBillZipOut").text(); | |
var bill_country = $("#ctl00_cphMainContent_lblBillCountryOut").text(); | |
var ship_first_name = $("#ctl00_cphMainContent_txtShipCustomerFirst").val(); | |
var ship_last_name = $("#ctl00_cphMainContent_txtShipCustomerLast").val(); | |
var ship_phone = $('#ctl00_cphMainContent_lblPhoneNumberOut').text(); | |
var ship_address_one = $("#ctl00_cphMainContent_txtShipAddress1").val().replace(/\./g, ''); | |
var ship_address_two = $("#ctl00_cphMainContent_txtShipAddress2").val().replace(/\./g, ''); | |
var ship_city = $("#ctl00_cphMainContent_txtShipCity").val(); | |
var ship_state = $("#ctl00_cphMainContent_txtShipState").val(); | |
var ship_zip = $("#ctl00_cphMainContent_txtShipZip").val(); | |
var ship_country = $("#ctl00_cphMainContent_ddlShipCountry").val(); | |
var ip = $("#ctl00_cphMainContent_tcOrders_tbReviewStatus_lblFormatRemoteIP").text(); | |
// Each piece of info which doesn't exist on the screen is left as a blank "", so it is not submitted in the request. | |
var wpp_bill_name = (bill_first_name || bill_last_name) ? "primary.name=" + bill_first_name + "+" + bill_last_name : ""; | |
wpp_bill_name = wpp_bill_name.replace(/\s'/g + '+'); | |
var wpp_bill_phone = (bill_phone) ? "&primary.phone=" + bill_phone : ""; | |
var wpp_bill_address_one = (bill_address_one) ? "&primary.address.street_line_1=" + bill_address_one.replace(/\s'/g, '+').replace(/#/, '') : ""; | |
var wpp_bill_city = (bill_city) ? "&primary.address.city=" + bill_city.replace(/\s'/g + '+') : ""; | |
var wpp_bill_state = (bill_state) ? "&primary.address.state_code=" + bill_state : ""; | |
var wpp_bill_zip = (bill_zip) ? "&primary.address.postal_code=" + bill_zip : ""; | |
var wpp_bill_country = (bill_country) ? "&primary.address.country_code=" + bill_country : ""; | |
var wpp_ship_name = (ship_first_name || ship_last_name) ? "&secondary.name=" + ship_first_name + "+" + ship_last_name : ""; | |
wpp_ship_name = wpp_ship_name.replace(/\s'/g + '+'); | |
var wpp_ship_phone = (ship_phone) ? "&secondary.phone=" + ship_phone : ""; | |
var wpp_ship_address_one = (ship_address_one) ? "&secondary.address.street_line_1=" + ship_address_one.replace(/\s'/g, '+').replace(/#/, '') : ""; | |
var wpp_ship_city = (ship_city) ? "&secondary.address.city=" + ship_city.replace(/\s'/g + '+') : ""; | |
var wpp_ship_state = (ship_state) ? "&secondary.address.state_code=" + ship_state : ""; | |
var wpp_ship_zip = (ship_zip) ? "&secondary.address.postal_code=" + ship_zip : ""; | |
var wpp_ship_country = (ship_country) ? "&secondary.address.country_code=" + ship_country : ""; | |
var wpp_email = (email) ? "&email_address=" + email : ""; | |
var wpp_ip = (ip) ? "&ip_address=" + ip : ""; | |
return wpp_bill_name + wpp_bill_phone + wpp_bill_address_one + wpp_bill_city + wpp_bill_state + wpp_bill_zip + wpp_ship_name + wpp_ship_phone + wpp_ship_address_one + wpp_ship_city + wpp_ship_state + wpp_ship_zip + wpp_ship_country + wpp_email + wpp_ip; | |
}; | |
var request = buildWppRequest(); | |
console.log('request'); | |
console.log(request); | |
// Take the info we got back from White Pages Pro Identity Check and make a paragraph to appear on our order page. | |
var buildOutput = function(r) { | |
var pp_warnings = ""; | |
if (r.primary_phone_checks) { | |
var pp = r.primary_phone_checks; | |
var pp_subscriber_name = pp.subscriber_name; | |
var pp_is = "Billing phone is: "; | |
pp_warnings += (pp.phone_to_name === "No match") ? "Billing phone subscriber name '" + pp_subscriber_name + "' does not match name on billing address. " : ""; | |
pp_warnings += (pp.phone_to_address === "No match") ? "Address of the billing phone does not match billing address. " : ""; | |
pp_warnings += (pp.phone_to_address === "Zip+4 match") ? "Address of the billing phone only matches zip+4 of billing address. " : ""; | |
pp_warnings += (pp.phone_to_address === "City/State match") ? "Address of the billing phone only matches city & state of billing address. " : ""; | |
pp_is += (pp.is_valid === true) ? "" : "Not real. "; | |
pp_is += (pp.country_code === "US") ? "" : "Outside U.S. "; | |
pp_is += (pp.line_type === "Non-fixed VOIP" || pp.line_type === "Tollfree") ? pp.line_type + ". " : ""; | |
pp_is += (pp.is_prepaid === true) ? "Prepaid. " : ""; | |
pp_warnings += (pp_is !== "Billing phone is: ") ? pp_is : ""; | |
} | |
var sp_warnings = ""; | |
if (r.primary_phone_checks) { | |
var sp = r.secondary_phone_checks; | |
var sp_subscriber_name = sp.subscriber_name; | |
var sp_is = "Shipping phone is: "; | |
sp_warnings += (sp.phone_to_name === "No match") ? "Shipping phone subscriber name '" + sp_subscriber_name + "' does not match name on billing address. " : ""; | |
sp_warnings += (sp.phone_to_address === "No match") ? "Address of the shipping phone does not match shipping address. " : ""; | |
sp_warnings += (sp.phone_to_address === "Zip+4 match") ? "Address of the shipping phone only matches zip+4 of shipping address. " : ""; | |
sp_warnings += (sp.phone_to_address === "City/State match") ? "Address of the shipping phone only matches city & state of shipping address. " : ""; | |
sp_is += (sp.is_valid === true) ? "" : "Not real. "; | |
sp_is += (sp.country_code === "US") ? "" : "Outside U.S. "; | |
sp_is += (sp.line_type === "Non-fixed VOIP" || sp.line_type === "Tollfree") ? sp.line_type + ". " : ""; | |
sp_is += (sp.is_prepaid === true) ? "Prepaid. " : ""; | |
sp_warnings += (sp_is !== "Shipping phone is: ") ? sp_is : ""; | |
} | |
var pa_warnings = ""; | |
if (r.primary_address_checks) { | |
var pa = r.primary_address_checks; | |
var pa_resident_name = pa.resident_name; | |
var pa_is = "Billing address is: "; | |
pa_warnings += (pa.address_to_name === "No match") ? "Billing address resident name '" + pa_resident_name + "' does not match name on order. " : ""; | |
pa_warnings += (pa.diagnostics.includes("Validated only Postcode, City, Country")) ? "Only validated postcode, city, country of billing address. " : ""; | |
pa_warnings += (pa.diagnostics.includes("Validated only City, Country")) ? "Only validated city & country of billing address. " : ""; | |
pa_warnings += (pa.diagnostics.includes("Validated only Country")) ? "Only validated country of billing address. " : ""; | |
pa_is += (pa.is_valid === true) ? "" : "Not real. "; | |
pa_is += (pa.is_active === true) ? "" : "Not receiving mail. "; | |
pa_is += (pa.type === "Commercial mail drop" || pa.line_type === "PO box" || pa.line_type === "PO box throwback") ? pa.type + ". " : ""; | |
pa_is += (pa.is_forwarder === true) ? "Freight forwarding company. " : ""; | |
pa_warnings += (pa_is !== "Billing address is: ") ? pa_is : ""; | |
} | |
var sa_warnings = ""; | |
if (r.secondary_address_checks) { | |
var sa = r.secondary_address_checks; | |
var sa_resident_name = sa.resident_name; | |
var sa_is = "Shipping address is: "; | |
sa_warnings += (sa.address_to_name === "No match") ? "Shipping address resident name '" + sa_resident_name + "' does not match name on billing address. " : ""; | |
sa_warnings += (sa.diagnostics.includes("Validated only Postcode, City, Country")) ? "Only validated postcode, city, country of shipping address. " : ""; | |
sa_warnings += (sa.diagnostics.includes("Validated only City, Country")) ? "Only validated city & country of shipping address. " : ""; | |
sa_warnings += (sa.diagnostics.includes("Validated only Country")) ? "Only validated country of shipping address. " : ""; | |
sa_is += (sa.is_valid === true) ? "" : "Not real. "; | |
sa_is += (sa.is_active === true) ? "" : "Not receiving mail. "; | |
sa_is += (sa.type === "Commercial mail drop" || sa.line_type === "PO box" || sa.line_type === "PO box throwback") ? sa.type + ". " : ""; | |
sa_is += (sa.is_forwarder === true) ? "Freight forwarding company. " : ""; | |
sa_warnings += (sa_is !== "Shipping address is: ") ? sa_is : ""; | |
} | |
var e_warnings = ""; | |
if (r.secondary_address_checks) { | |
var e = r.email_address_checks; | |
var e_registered_name = e.registered_name; | |
var e_is = "Email is: "; | |
e_warnings += (e.email_to_name === "No match") ? "Name '" + e_registered_name + "' registered to email does not match name on order. " : ""; | |
e_is += (e.is_valid) ? "" : "Non existent and undeliverable. "; | |
e_is += (e.is_autogenerated) ? "Auto-generated. " : ""; | |
e_is += (e.is_disposable) ? "Disposable domain. " : ""; | |
e_warnings += (e_is !== "Email is: ") ? e_is : ""; | |
} | |
return "Whitepages Pro provides these warning signs: " + pa_warnings + pp_warnings + sa_warnings + sp_warnings + e_warnings; | |
}; | |
var callApi = function(url) { | |
// Find out the timestamps of cached items | |
var expiries = Object.keys(localStorage).reduce(function(collection, key) { | |
var this_timestamp = JSON.parse(localStorage.getItem(key)).timestamp; | |
collection[this_timestamp] = key; | |
return collection; | |
}, {}); | |
var expiryDates = Object.keys(expiries); | |
// Find the oldest (smallest timestamp) order record and delete it, until there are only 300 orders cached (about a week's worth) | |
while (expiryDates.length > 300) { | |
var oldestDate = Math.min.apply(null, expiryDates); | |
this.destroyItem(expiries[oldestDate]); | |
} | |
$.get(url, function(response) { | |
var request_and_response = JSON.stringify({ | |
"request": request, | |
"response": response, | |
"expirationTime": Date.now() | |
}); | |
// Cache the response in localstorage so it could be used when visiting the same order again, to save on API calls. | |
localStorage.setItem(order_number, request_and_response); | |
output.innerText = buildOutput(response); | |
}).fail(function(){ | |
output.innerText = "There was an error getting info about this order from Whitepages Pro."; | |
}); | |
}; | |
var displayWPP = function() { | |
const BASE_URL = "https://proapi.whitepages.com/3.2/identity_check.json?"; | |
const API_KEY = "&api_key=4c024b0f168a49e3aa763013209d6623"; | |
document.getElementById("ctl00_cphMainContent_tcOrders_tpNotes_upOrderNotes").prepend(output); | |
var url = BASE_URL + request + API_KEY; | |
// If the response is cached in localstorage, use that when visiting the same order again, to save on API calls. | |
// Otherwise make an API call to get the info for the first time. | |
var cached = JSON.parse(localStorage.getItem(order_number)); | |
if (cached) { | |
if (cached.request == request) { | |
output.innerText = buildOutput(cached.response); | |
} else { | |
callApi(url); | |
} | |
} else { | |
callApi(url); | |
} | |
}; // End displayWPP | |
$(document).ready(function() { | |
// If Signifyd status is "Approved", we do not need this script to run at all. | |
var signifyd_status = $('#ctl00_cphMainContent_tcOrders_tbReviewStatus_lblFormatStatus').text().replace('Signifyd: ', ''); | |
if (signifyd_status === "Rejected") { | |
displayWPP(); | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment