/** * Author: Up and Running Software, Inc. * * Short Description: Perform verification on a credit card number to determine its type and validity. * * Long Description: This contains Javascript code designed to handle the front of the page where the user * enters new payment sources. In particular, it contains methods for validating credit cards using * a mod-10 check and regular expressions. It also contains methods that interface with the * modular payment processing system to dynamically load and submit payment source forms via AJAX. * * Library: jQuery */ function checkCardNumber(cardNum){ // Patterns for matching different credit cards var amx = /^3[47][0-9]{13}$/; var dc = /^6[0-9]{15}$/; var mc = /^5[1-5][0-9]{14}$/; var visa = /^4[0-9]{12}([0-9]{3})?$/; // Flag for mod-10 check var luhncheck = false; // Perform a mod-10 check on the number var digits = []; var digitsCount = 0; var double = false; for(var ind = cardNum.length-1; ind >=0; ind--){ if(double){ var num = parseInt(cardNum.charAt(ind))*2; if(num >= 10){ digits[digitsCount++] = 1; digits[digitsCount++] = num-10; }else{ digits[digitsCount++] = num; } }else{ digits[digitsCount++] = parseInt(cardNum.charAt(ind)); } double = !double; } sum = 0; for(var ind = 0; ind < digitsCount; ind++){ sum += parseInt(digits[ind]); } luhncheck = !(sum % 10); // Throw an error if the number is invalid or does not match any of the accepted card types $("#psource_form #card_type_label").text(""); if((!amx.test(cardNum) && !dc.test(cardNum) && !mc.test(cardNum) && !visa.test(cardNum)) || !luhncheck){ alert('The Credit Card number you entered is not valid or not a supported card type. American Express, Discover Card, Mastercard and Visa are accepted.'); }else{ // Update the display to show the credit card type if(amx.test(cardNum)){ $("#psource_form #card_type_label").text("American Express"); }else if (dc.test(cardNum)){ $("#psource_form #card_type_label").text("Discover Card"); }else if (mc.test(cardNum)){ $("#psource_form #card_type_label").text("Master Card"); }else if (visa.test(cardNum)){ $("#psource_form #card_type_label").text("Visa Card"); } } } // Track the current payment source type curType = 0; /** * Load the new payment form if the user wants to add a new payment source */ function loadNewPaymentForm(typeid){ curType = typeid; notify_processing(); $("#paymentFormContainer").load(base_url+"paymentsource/addform/type/"+typeid, '',function(){ close_notify_processing(false); }); } /** * Submit the new payment form to save the data to the database */ function savePaymentMethod(){ form = $("#psource_form"); form.ajaxError(close_notify_failed_processing); form.ajaxSubmit({ url: base_url + 'paymentsource/savepsource/type/'+curType, success: function(data) { // JSON data is returned var reqdata = eval(data); // Check whether any errors occurred, if they did display them, otherwise allow the user to use the new payment source to pay for their order if(reqdata['status']){ $('#new_p_source').selectedIndex = 0; $("#paymentFormContainer").innerHTML = ''; $("#selected_payment_sources").innerHTML += "