Skip to content

Instantly share code, notes, and snippets.

@gabelula
Created July 26, 2012 22:04
Show Gist options
  • Save gabelula/3184855 to your computer and use it in GitHub Desktop.
Save gabelula/3184855 to your computer and use it in GitHub Desktop.
var Analog = Analog || {};
function post_to(path, params, method) {
method = method || "post"; // Set method to post by default, if not specified.
var form = document.createElement("form");
form.setAttribute("method", method);
form.setAttribute("action", path);
for(var key in params) {
if(params.hasOwnProperty(key)) {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key);
hiddenField.setAttribute("value", params[key]);
form.appendChild(hiddenField);
}
}
document.body.appendChild(form);
form.submit();
}
function submitViaAjax(form) {
jQuery.ajax({
type: "POST",
url: form.action,
crossDomain: true,
xhrFields: {
withCredentials: true
},
statusCode: {},
success: function(data, textStatus, jqXHR) {
//post_to("https://cybersource.analoganalytics.com/cybersource/receipt", data);
console.log("*************SUCCESS*************");
console.log(data);
},
error: function(jqXHR, textStatus, errorThrown) {
//post_to("https://cybersource.analoganalytics.com/cybersource/decline", data);
console.log("*************************FAIL**********************");
}
});
return false;
}
if (!Analog.CyberSourceOrderForm) {
var submitted = false;
Analog.CyberSourceOrderForm = function($) {
var _config = {
submitButtonSelector: "input[type=submit]",
submitButtonSubmittingValue: "Processing...",
creditCardCvvValidationMessage: "Please enter a valid 3- or 4-digit security code.",
zipCodeValidationMessage: "Please enter a valid postal code."
};
var _submitHandler = function(form) {
var $submitButton = $(form).find(_config.submitButtonSelector);
$submitButton.val(_config.submitButtonSubmittingValue);
$submitButton.attr("disabled", "disabled");
//
// Call submit on the raw DOM element to avoid infinite recursion.
if (!submitted) {
submitted = true;
form.submit();
}
//submitViaAjax(form);
};
$.validator.addMethod("creditcard-cvv", function(value, element) {
return /^(?!000)\d{3,4}$/.test(value);
}, _config.creditCardCvvValidationMessage);
$.validator.addMethod("zipCode", function(value, element) {
return Analog.Address.validate_postal_code(value);
}, _config.zipCodeValidationMessage);
return {
validate: function(orderFormSelector, config) {
if (config) {
$.extend(_config, config);
}
$(orderFormSelector).validate({
debug: false,
errorClass: "validationError",
errorElement: "label",
submitHandler: _submitHandler
});
}
};
}(jQuery);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment