Newer
Older
ubFramework / Portal / docroot / user / billing / makepayment.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: makepayment.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 - One Time Payment" );

/* 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/makepayment.html" 
) );

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

// Setup Form Data Global
$form_data = $data ['data'];

$message = "";

if ( isset ( $form_data ['payment_opt'] ) ) {
  switch ( $form_data ['payment_opt'] ) {
    case 0 :
      $amount = $form_data ['past_due'];
      break;
    case 1 :
      $amount = $form_data ['current_balance'];
      break;
    case 2 :
      $amount = $form_data ['payment_amount'];
      if ( $amount > $form_data ['current_balance'] ) {
        $form_data ['payment_amount'] = $form_data ['current_balance'];
        $amount = $form_data ['current_balance'];
        $message = "NOTE: Your Payment Was Adjuested To Your Current Balance. You Cannot Pay More Than Your Current Balance.<br />";
      }
      break;
    default :
      $amount = 0;
      break;
  }
  if ( $amount <= 0 )
    $message .= "Payment Amount Must Be Greater Than $0.00<br />";
}

if ( isset ( $form_data ['cvv'] ) && !is_numeric ( $form_data ['cvv'] ) ) {
  $message = "Invalid CVV Code.<br />";
  $form_data ['uap'] = "Next";
}

if ( isset ( $form_data ['uap'] ) && $form_data ['uap'] == "Next" && $amount > 0 ) {
  $ubF->tpl->define ( array (
      "body" => "billing/makepayment_payinfo.html" 
  ) );
  
  $query = "SELECT * FROM payment_method WHERE account_number = '" . $ubF->session->account_number . "' AND pm_id = " . $form_data ['payment_method'];
  $result = $ubF->DB['main']->query ( $query );
  
  $qData = $result->fetch_assoc ();
  
  if ( $qData ['card_type'] == 0 )
    $hidden = "****-******-*";
  else
    $hidden = "****-****-****-";
  
  $ubF->tpl->assign ( "NICKNAME", $qData ['nickname'] );
  $ubF->tpl->assign ( "CARD_NUMBER", $hidden . $qData ['last_four'] );
  $ubF->tpl->assign ( "METHOD_TYPE", $data->libs['ubf']->paymentMethodTypes [$qData ['method_type']] );
  $ubF->tpl->assign ( "CARD_TYPE", $data->libs['ubf']->paymentCardTypes [$qData ['card_type']] );
  $ubF->tpl->assign ( "EXP_MONTH", $qData ['exp_month'] );
  $ubF->tpl->assign ( "EXP_YEAR", $qData ['exp_year'] );
  $ubF->tpl->assign ( "CVV", $form_data ['cvv'] );
  $ubF->tpl->assign ( "TYPE", $data->libs['ubf']->paymentTypes [$qData ['type']] );
  $ubF->tpl->assign ( "CARDHOLDER_NAME", $qData ['cardholder_name'] );
  $ubF->tpl->assign ( "ADDRESS1", $qData ['address1'] );
  $ubF->tpl->assign ( "ADDRESS2", $qData ['address2'] );
  $ubF->tpl->assign ( "CITY", $qData ['city'] );
  $ubF->tpl->assign ( "STATE", $qData ['state'] );
  $ubF->tpl->assign ( "ZIP", $qData ['zip'] );
  
  $ubF->tpl->assign ( "PAYMENT_METHOD", $form_data ['payment_method'] );
  $ubF->tpl->assign ( "PAYMENT_OPT", $form_data ['payment_opt'] );
  $ubF->tpl->assign ( "PAST_DUE", $form_data ['past_due'] );
  $ubF->tpl->assign ( "CURRENT_BALANCE", $form_data ['current_balance'] );
  $ubF->tpl->assign ( "PAYMENT_AMOUNT", $form_data ['payment_amount'] );
  $ubF->tpl->assign ( "PAYMENT_DATE", $form_data ['payment_date'] );
}
else if ( isset ( $form_data ['uap'] ) && $form_data ['uap'] == "Review" && $amount > 0 ) {
  $ubF->tpl->define ( array (
      "body" => "billing/makepayment_review.html" 
  ) );
  
  if ( $amount < $form_data ['current_balance'] )
    $message = "NOTE: Your Payment Is Less Than Your Current Balance.<br />";
  if ( $amount > $form_data ['current_balance'] ) {
    $message = "NOTE: Your Payment Was Adjuested To Your Current Balance. You Cannot Pay More Than Your Current Balance.<br /><br />";
    $amount = $form_data ['current_balance'];
  }
  
  $ubF->tpl->assign ( "NICKNAME", $form_data ['nickname'] );
  $ubF->tpl->assign ( "PAYMENT_METHOD", $form_data ['payment_method'] );
  $ubF->tpl->assign ( "PAYMENT_OPT", $form_data ['payment_opt'] );
  $ubF->tpl->assign ( "CURRENT_BALANCE", $form_data ['current_balance'] );
  $ubF->tpl->assign ( "PAYMENT_AMOUNT", number_format ( $amount, 2 ) );
  $ubF->tpl->assign ( "PAYMENT_DATE", $form_data ['payment_date'] );
  $ubF->tpl->assign ( "CVV", $form_data ['cvv'] );
}
else if ( isset ( $form_data ['uap'] ) && $form_data ['uap'] == "Confirm" && $amount > 0 ) {
  $ubF->tpl->define ( array (
      "body" => "billing/makepayment_confirm.html" 
  ) );
  
  $payment_id = time ();
  $query = "INSERT INTO payments (account_number,payment_id,payment_date,payment_type,payment_status,payment_amount,pm_id) VALUES('" . $ubF->session->account_number . "'," . $payment_id . "," . strtotime ( $form_data ['payment_date'] ) . ",1,0," . $amount . "," . $form_data ['payment_method'] . ")";
  $ubF->DB['main']->query ( $query );
  Header ( "Location: " . $form_data ['url'] . "?data[uap]=Confirmed&data[payment_id]=" . $payment_id );
  exit ( 0 );
}
else if ( isset ( $form_data ['uap'] ) && $form_data ['uap'] == "Confirmed" ) {
  $ubF->tpl->define ( array (
      "body" => "billing/makepayment_confirm.html" 
  ) );
  $ubF->tpl->assign ( "PAYMENT_ID", $form_data ['payment_id'] );
}
else {
  $query = "SELECT pm_id,nickname,last_four FROM payment_method WHERE active = 1 AND account_number = '" . $ubF->session->account_number . "' ORDER BY nickname";
  $result = $ubF->DB['main']->query ( $query );
  
  $payment_method = "";
  while ( $qData = $result->fetch_assoc () ) {
    if ( $qData ['pm_id'] == $form_data ['payment_method'] )
      $payment_method .= "<option value=\"" . $qData ['pm_id'] . "\" selected>" . $qData ['nickname'] . " " . $qData ['last_four'] . "</option>\n";
    else
      $payment_method .= "<option value=\"" . $qData ['pm_id'] . "\">" . $qData ['nickname'] . " " . $qData ['last_four'] . "</option>\n";
  }
  $result->free ();
  
  if ( !isset ( $form_data ['payment_amount'] ) )
    $form_data ['payment_amount'] = "0.00";
  
  $query = "SELECT balance,due_date as past_due FROM invoices WHERE balance > 0 AND account_number = '" . $form_data ['session']->account_number . "'";
  $result = $ubF->DB['main']->query ( $query );
  
  $past_due = 0.00;
  $current_balance = 0.00;
  
  $curTime = time ();
  while ( $qData = $result->fetch_assoc () ) {
    if ( $qData ['due_date'] < $curTime )
      $past_due += $qData ['balance'];
    
    $current_balance += $qData ['balance'];
  }
  $result->free ();
  
  if ( !isset ( $form_data ['payment_opt'] ) )
    $form_data ['payment_opt'] = 1;
  
  switch ( $form_data ['payment_opt'] ) {
    case 0 :
      $po_0 = "checked";
      $po_1 = "";
      $po_2 = "";
      break;
    case 1 :
      $po_0 = "";
      $po_1 = "checked";
      $po_2 = "";
      break;
    case 2 :
      $po_0 = "";
      $po_1 = "";
      $po_2 = "checked";
      break;
  }
  
  if ( !isset ( $form_data ['payment_date'] ) )
    $form_data ['payment_date'] = date ( "m/d/y" );
  
  $ubF->tpl->assign ( "PAYMENT_METHOD", $payment_method );
  $ubF->tpl->assign ( "PAST_DUE", number_format ( $past_due, 2 ) );
  $ubF->tpl->assign ( "CURRENT_BALANCE", number_format ( $current_balance, 2 ) );
  $ubF->tpl->assign ( "PAYMENT_AMOUNT", $form_data ['payment_amount'] );
  $ubF->tpl->assign ( "PO_0", $po_0 );
  $ubF->tpl->assign ( "PO_1", $po_1 );
  $ubF->tpl->assign ( "PO_2", $po_2 );
  $ubF->tpl->assign ( "PAYMENT_DATE", $form_data ['payment_date'] );
}

if ( strlen ( $message ) == 0 )
  $ubF->tpl->assign ( "MESSAGE", "" );
else
  $ubF->tpl->assign ( "MESSAGE", "<font style=\"color:red;line-height:16px;\">" . $message . "</font>" );
  
  /* 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" );
?>