/** * 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 += "
"+reqdata['name']+"
"; }else{ var errors = ''; $.jGrowl(errors, {sticky:true, open:jgrowl_open, close:jgrowl_close}); } } }); } /** * Verify that the user has allocated enough money from all of their payment sources to cover the order */ function checkSubmit(mode){ // Count the payment sources var curPs = 0; try { curIn = $('#ps'+curPs); if(!curIn) curPs++; }catch(e){ curPs++; } // Count the total payments var totalPayments = 0; try { var curIn; while(curIn = $('#ps'+curPs)){ var amount = parseFloat(curIn.value.replace('$','')); if(amount > 0){ totalPayments += amount; } curPs++; } }catch(e){} // Check and output the remaining balance var remaining; try { remaining = parseFloat($('#item_total_box').innerHTML.replace('$','')); try { var curbox = null; var curboxnum = 0; var boxval; while(curbox = $('#invstip-'+curboxnum)){ boxval = parseFloat(curbox.value.replace('$','')); if(boxval > 0){ remaining += boxval; } curboxnum++; } }catch(e2){} try { var curbox = null; var curboxnum = 0; var boxval; while(curbox = $('#locked-invstip-'+curboxnum)){ boxval = parseFloat(curbox.innerHTML.replace('$','')); if(boxval > 0){ remaining += boxval; } curboxnum++; } }catch(e2){} }catch(e){ remaining = parseFloat('initial_balance; ?>'); } // Make the remaining value pretty remaining = (remaining-totalPayments) var remaining_pretty = Math.round(remaining*100); remaining_pretty = '$'+remaining_pretty; if(remaining_pretty == '$0') remaining_pretty = '$000' // 100*0 = 0 remaining_pretty = remaining_pretty.substr(0,remaining_pretty.length-2)+"."+remaining_pretty.substr(remaining_pretty.length-2,2); try { $('#remaining_balance').innerHTML = remaining_pretty; }catch(e){} // If errors are enabled, throw one if the total is not enough if(mode != 1){ if(remaining == 0){ $('#order_status_form').submit(); }else{ alert('The payment amounts you entered for this order do not add up to the total balance due.'); } } } var curPSnum; // Close the CCV prompt function cancelCVV(){ $("#cvv-model-dialog").dialog('close'); } /** * If the user chooses a credit card based payment method they must enter a CCV value. * This method prompts for one and performs basic validation on it. */ function checkCVV(){ var cvv = parseInt($('#cvvconfirm').value); if(cvv > 0){ $('#cvv'+curPSnum).value = cvv; $('#cvvconfirm').value = ''; cancelCVV(); checkSubmit(); }else{ alert('The CVV number you entered does not appear to be valid.'); } }