Newer
Older
ubFramework / Portal / docroot / user / billing / add_pm.php
@Christopher W. Olsen Christopher W. Olsen on 10 Dec 2017 9 KB Cleaning Up Making It A Sub Module
<?php

/**
 * ****************************************************************************************
 * Copyright (c) 2013 Christopher W.
 * Olsen <cwolsen@ubixtechnologies.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this list of
 * conditions, the following disclaimer and the list of authors. Redistributions in binary
 * form must reproduce the above copyright notice, this list of conditions, the following
 * disclaimer and the list of authors in the documentation and/or other materials provided
 * with the distribution. Neither the name of the uBix Cube Project nor the names of its
 * contributors may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $Id: add_pm.php 940 2017-09-07 20:12:42Z reddawg $
 *
 * ***************************************************************************************
 */

/* Main include info */
ini_set ( 'include_path', $_SERVER ['DOCUMENT_ROOT'] . "/include/" );
include ("config.php");

/* Assign the page title */
$ubF->tpl->assign ( "PAGE_TITLE", "Billing - Add Payment Method" );

/* Main defines */
$ubF->tpl->define ( array (
    "index" => "user/wrapper/default.html" 
) );
$ubF->tpl->define ( array (
    "menu" => "user/billing/menu.html" 
) );
$ubF->tpl->define ( array (
    "body" => "user/billing/add_pm.html" 
) );

$ubF->session->Validate ( 'U_A_', 0 );

$form_data = $data ['data'];

if ( isset ( $form_data ['add_card'] ) && $form_data ['add_card'] == "Review" ) {
  $message = "";
  if ( strlen ( $form_data ['nickname'] ) < 2 )
    $message .= "Invalid Nickname, It Must Be Atleast 2 Characters.<br />";
  if ( !(strlen ( $form_data ['card_number'] ) == 15 || strlen ( $form_data ['card_number'] ) == 16) )
    $message .= "Invalid Credit Card Number.<br />";
  if ( $form_data ['exp_year'] == date ( "Y" ) && $form_data ['exp_month'] < date ( "m" ) )
    $message .= "Invalid Expiration Date.<br />";
  if ( !(strlen ( $form_data ['cvv'] ) == 3 || strlen ( $form_data ['cvv'] ) == 4) || !is_numeric ( $form_data ['cvv'] ) )
    $message .= "Invalid CVV Code.<br />";
  if ( strlen ( $form_data ['cardholder_name'] ) < 3 )
    $message .= "Invalid Name On Card.<br />";
  if ( strlen ( $form_data ['address1'] ) < 3 )
    $message .= "Invalid Address.<br />";
  if ( strlen ( $form_data ['city'] ) < 3 )
    $message .= "Invalid City.<br />";
  if ( strlen ( $form_data ['zip'] ) != 5 || !is_numeric ( $form_data ['zip'] ) )
    $message .= "Invalid Zip.<br />";
  
  if ( strlen ( $message ) == 0 ) {
    $ubF->tpl->define ( array (
        "body" => "billing/add_pm_review.html" 
    ) );
    
    $ubF->tpl->assign ( "NICKNAME", $form_data ['nickname'] );
    $ubF->tpl->assign ( "CARD_NUMBER", $form_data ['card_number'] );
    $ubF->tpl->assign ( "METHOD_TYPE_STRING", $data->libs['ubf']->paymentMethodTypes [$form_data ['method_type']] );
    $ubF->tpl->assign ( "METHOD_TYPE", $form_data ['method_type'] );
    $ubF->tpl->assign ( "CARD_TYPE_STRING", $data->libs['ubf']->paymentCardTypes [$form_data ['card_type']] );
    $ubF->tpl->assign ( "CARD_TYPE", $form_data ['card_type'] );
    $ubF->tpl->assign ( "EXP_MONTH", $form_data ['exp_month'] );
    $ubF->tpl->assign ( "EXP_YEAR", $form_data ['exp_year'] );
    $ubF->tpl->assign ( "CVV", $form_data ['cvv'] );
    $ubF->tpl->assign ( "TYPE_STRING", $data->libs['ubf']->paymentTypes [$form_data ['type']] );
    $ubF->tpl->assign ( "TYPE", $form_data ['type'] );
    $ubF->tpl->assign ( "CARDHOLDER_NAME", $form_data ['cardholder_name'] );
    $ubF->tpl->assign ( "ADDRESS1", $form_data ['address1'] );
    $ubF->tpl->assign ( "ADDRESS2", $form_data ['address2'] );
    $ubF->tpl->assign ( "CITY", $form_data ['city'] );
    $ubF->tpl->assign ( "STATE", $form_data ['state'] );
    $ubF->tpl->assign ( "ZIP", $form_data ['zip'] );
    
    $ubF->tpl->parse ( "MENU", array (
        "menu" 
    ) );
    $ubF->tpl->parse ( "BODY", array (
        "body" 
    ) );
    $ubF->tpl->parse ( "INDEX", array (
        "index" 
    ) );
    $ubF->tpl->FastPrint ( "INDEX" );
    exit ();
  }
}
else if ( isset ( $form_data ['add_card'] ) && $form_data ['add_card'] == "Add" ) {
  $query = "SELECT card_number FROM payment_method WHERE active = 1 AND card_number = '" . $form_data ['card_number'] . "'";
  $result = $ubF->DB['main']->query ( $query );
  if ( $result->num_rows > 0 )
    $message = "Credit Card Already On File.<br />\n";
  else {
    $query = "INSERT INTO payment_method (account_number,nickname,card_number,method_type,card_type,exp_month,exp_year,cvv,type,cardholder_name,address1,address2,city,state,zip,last_four) VALUES('" . $ubF->session->account_number . "','" . $form_data ['nickname'] . "','" . $data->libs['ubf']->encryptData ( $form_data ['card_number'] ) . "'," . $form_data ['method_type'] . "," . $form_data ['card_type'] . "," . $form_data ['exp_month'] . "," . $form_data ['exp_year'] . ",'" . $form_data ['cvv'] . "'," . $form_data ['type'] . ",'" . $form_data ['cardholder_name'] . "','" . $form_data ['address1'] . "','" . $form_data ['address2'] . "','" . $form_data ['city'] . "','" . $form_data ['state'] . "'," . $form_data ['zip'] . "," . substr ( $form_data ['card_number'], -4, 4 ) . ")";
    $ubF->DB['main']->query ( $query );
    // $message = "Credit Card Successfully Added To Your Account.<br />\n";
    Header ( "Location: /billing/billing.php" );
    exit ();
  }
  $result->free ();
}

$method_type = "";
foreach ( $data->libs['ubf']->paymentMethodTypes as $key => $val ) {
  if ( $key == $form_data ['method_type'] )
    $method_type .= "<option value=\"$key\" selected>$val</option>\n";
  else
    $method_type .= "<option value=\"$key\">$val</option>\n";
}

$card_type = "";
foreach ( $data->libs['ubf']->paymentCardTypes as $key => $val ) {
  if ( $key == $form_data ['card_type'] )
    $card_type .= "<option value=\"$key\" selected>$val</option>\n";
  else
    $card_type .= "<option value=\"$key\">$val</option>\n";
}

$exp_month = "";
for ( $i = 1 ; $i <= 12 ; $i++ ) {
  if ( $i == $form_data ['exp_month'] )
    $exp_month .= "<option value=\"$i\" selected>$i</option>\n";
  else
    $exp_month .= "<option value=\"$i\">$i</option>\n";
}

$exp_year = "";
for ( $i = date ( "Y" ) ; $i <= (date ( "Y" ) + 10) ; $i++ ) {
  if ( $i == $form_data ['exp_year'] )
    $exp_year .= "<option value=\"$i\" selected>$i</option>\n";
  else
    $exp_year .= "<option value=\"$i\">$i</option>\n";
}

$type = "";
foreach ( $data->libs['ubf']->paymentTypes as $key => $val ) {
  if ( $key == $form_data ['type'] )
    $type .= "<option value=\"$key\" selected>$val</option>\n";
  else
    $type .= "<option value=\"$key\">$val</option>\n";
}

$state = "";
$query = "SELECT * FROM state_list ORDER BY state_code";
$result = $ubF->DB['main']->query ( $query );
while ( $sData = $result->fetch_assoc () ) {
  if ( $sData ['state_code'] == $form_data ['state'] )
    $state .= "<option value=\"" . $sData ['state_code'] . "\" selected>" . $sData ['state_name'] . "</option>\n";
  else
    $state .= "<option value=\"" . $sData ['state_code'] . "\">" . $sData ['state_name'] . "</option>\n";
}
$result->free ();

if ( strlen ( $message ) == 0 )
  $ubF->tpl->assign ( "MESSAGE", $message );
else
  $ubF->tpl->assign ( "MESSAGE", "<font style=\"color:red;\">" . $message . "</font>" );

$ubF->tpl->assign ( "NICKNAME", $form_data ['nickname'] );
$ubF->tpl->assign ( "CARD_NUMBER", $form_data ['card_number'] );
$ubF->tpl->assign ( "METHOD_TYPE", $method_type );
$ubF->tpl->assign ( "CARD_TYPE", $card_type );
$ubF->tpl->assign ( "EXP_MONTH", $exp_month );
$ubF->tpl->assign ( "EXP_YEAR", $exp_year );
$ubF->tpl->assign ( "CVV", $form_data ['cvv'] );
$ubF->tpl->assign ( "TYPE", $type );
$ubF->tpl->assign ( "CARDHOLDER_NAME", $form_data ['cardholder_name'] );
$ubF->tpl->assign ( "ADDRESS1", $form_data ['address1'] );
$ubF->tpl->assign ( "ADDRESS2", $form_data ['address2'] );
$ubF->tpl->assign ( "CITY", $form_data ['city'] );
$ubF->tpl->assign ( "STATE", $state );
$ubF->tpl->assign ( "ZIP", $form_data ['zip'] );

/* Parse and print */
$ubF->tpl->parse ( "MENU", array (
    "menu" 
) );
$ubF->tpl->parse ( "BODY", array (
    "body" 
) );
$ubF->tpl->parse ( "INDEX", array (
    "index" 
) );
$ubF->tpl->FastPrint ( "INDEX" );
?>