Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class credit{
- function credit(){
- }
- function validate($cc_number, $cvv2, $date){
- $this->validateCard($cc_number);
- $this->validateExDate($date);
- $this->validateCVV($cc_number, $cvv2);
- }
- function validateCard($cc_number){
- global $e;
- $first_number = substr($cc_number, 0, 1);
- switch ($first_number){
- case 3:
- if (!preg_match('/^3\d{3}[ \-]?\d{6}[ \-]?\d{5}$/', $cc_number)) $e->setError("American Express number is not valid");
- break;
- case 4:
- if (!preg_match('/^4\d{3}[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$/', $cc_number)) $e->setError("Visa number is not valid");
- break;
- case 5:
- if (!preg_match('/^5\d{3}[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$/', $cc_number)) $e->setError("MasterCard number is not valid");
- break;
- case 6:
- if (!preg_match('/^6011[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$/', $cc_number)) $e->setError("Discover Card number is not valid");
- break;
- default:
- $e->setError("Credit Card number is not valid: Error 1");
- break;
- }
- if($e->countErrors() == 0) $this->validateCard2($cc_number);
- }
- function validateCard2($cc_number){
- global $e;
- $checksum = 0;
- $j = 1;
- for ($i = strlen($cc_number) - 1; $i >= 0; $i--){
- $calc = substr($cc_number, $i, 1) * $j;
- if ($calc > 9){
- $checksum = $checksum + 1;
- $calc = $calc - 10;
- }
- $checksum += $calc;
- if ($j == 1){
- $j = 2;
- }else{
- $j = 1;
- }
- }
- if ($checksum % 10 != 0) $e->setError("Credit Card number is not valid: Error 2");
- }
- function validateExDate($date){
- global $e;
- $month = substr($date, 0, 2);
- $year = substr($date, 2, 2);
- $current_month = date("m");
- $current_year = date("y");
- if ($year < $current_year){
- $e->setError("Expiration Date is not valid : Error 3");
- }else{
- if ($year == $current_year)
- {
- if ($month < $current_month){
- $e->setError("Expiration Date is not valid : Error 4");
- }
- }
- }
- }
- function validateCVV($cc_number, $cvv2){
- global $e;
- $first_number = substr($cc_number, 0, 1);
- if ($first_number == 3){
- if (!preg_match("/^\d{4}$/", $cvv2)) $e->setError("CVV number is not valid : Error 5");
- }else{
- if (!preg_match("/^\d{3}$/", $cvv2)) $e->setError("CVV number is not valid : Error 6");
- }
- }
- function maskCard($cc_number){
- $count = strlen($cc_number);
- for($i = 1; $i <= ($count -4); $i++){
- echo "X";
- }
- echo substr($cc_number, -4);
- }
- }
- ?>
- <?php
- require_once "class.error.php";
- $e = new error;
- require_once "class.credit.php";
- $cc = new credit;
- if($_POST){
- $cc_number = $_POST['cc_number'];
- $expire_month = $_POST['expire_month'];
- $expire_year = $_POST['expire_year'];
- $cc_cvv = $_POST['cc_cvv'];
- $date = $expire_month . $expire_year;
- $cc->validate($cc_number, $cc_cvv, $date);
- if($e->countErrors() == 0){
- //process form
- header("location: somewhere.php");
- die();
- }
- }
- ?>
- <?php $e->showErrors(); ?>
- <form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
- <table border="0" cellpadding="4" width="450">
- <tr>
- <td width="200" class="tbl_left">Payment Type: </td>
- <td width="250" class="tbl_right">Credit Card</td>
- </tr>
- <tr>
- <td class="tbl_left">Card Holder Name: </td>
- <td class="tbl_right"><input type="text" name="cc_name" /></td>
- </tr>
- <tr>
- <td class="tbl_left">Card Type: </td>
- <td class="tbl_right"><select name="cc_type">
- <option value="American Express">American Express</option>
- <option value="Discover">Discover</option>
- <option value="Master Card">Master Card</option>
- <option value="Visa">Visa</option>
- </select>
- </td>
- </tr>
- <tr>
- <td class="tbl_left">Credit Card Number: </td>
- <td class="tbl_right"><input type="text" name="cc_number" /></td>
- </tr>
- <tr>
- <td class="tbl_left">Expiration Date: </td>
- <td class="tbl_right"><select name="expire_month">
- <option value="01">January</option>
- <option value="02">February</option>
- <option value="03">March</option>
- <option value="04">April</option>
- <option value="05">May</option>
- <option value="06">June</option>
- <option value="07">July</option>
- <option value="08">August</option>
- <option value="09">September</option>
- <option value="10">October</option>
- <option value="11">November</option>
- <option value="12">December</option>
- </select>
- <select name="expire_year">
- <?php
- for($x=date(Y); $x<=2050; $x++){
- echo '<option value="'.substr($x, -2).'">'.$x.'</option>'."\n";
- }
- ?>
- </select>
- </td>
- </tr>
- <tr>
- <td class="tbl_left">CVV Number:</td>
- <td class="tbl_right"><input type="text" name="cc_cvv" size="5" maxlength="4" /></td>
- </tr>
- <tr>
- <td class="tbl_left">Enter a Promo Code: </td>
- <td class="tbl_right"><input type="text" name="promo_code" /></td>
- </tr>
- </table>
- </form>
- echo $cc->maskCard($cc_number);
Add Comment
Please, Sign In to add comment