<?php

/***************************************************************************
 *   copyright            : (C) 2017 PCRepairTracker.com
 *   email                : info@pcrepairtracker.com
 *   This program is a copyrighted work.
 *   Please see the license.txt file for details
 *
 ***************************************************************************/

if (array_key_exists('func',$_REQUEST)) {
$func = $_REQUEST['func'];
} else {
$func = "";
}

                                                                                                    
function nothing() {
require_once("header.php");


require_once("footer.php");
                                                                                                    
}


function add() {

if (array_key_exists('amounttopay',$_REQUEST)) {
$amounttopay =  $_REQUEST['amounttopay'];
} else {
die("error");
}

if (array_key_exists('invtotal',$_REQUEST)) {
$invtotal =  $_REQUEST['invtotal'];
} else {
die("error");
}


$invoiceid = $_REQUEST['invoiceid'];
$invtax = $_REQUEST['invtax'];

                                                                                                                                               
require("header.php");

echo "<h3>".pcrtlang("Add Credit Card Payment")."</h3>";

echo "<table class=table style=\"max-width:400px;\"><tr><td><form action=$securedomain/Stripe.php?func=add3 method=post>";
echo pcrtlang("Amount to Pay").": </td><td>$money $amounttopay<input type=hidden name=amounttopay value=\"".mf("$amounttopay")."\"><input type=hidden name=invtotal value=\"".mf("$invtotal")."\"><input type=hidden name=invtax value=\"$invtax\"></td></tr>";
echo "<tr><td>".pcrtlang("Credit Card Number").": </td><td><input type=text name=ccnumber size=20 class=\"form-control\"> ".pcrtlang("16 digits, no dashes or spaces")."</td></tr>";
echo "<tr><td>".pcrtlang("Expiration Month").": </td><td><input type=text name=ccmonth size=4 class=\"form-control\"> ".pcrtlang("2 digits")."</td></tr>";
echo "<tr><td>".pcrtlang("Expiration Year").": </td><td><input type=text name=ccyear size=4 class=\"form-control\"> ".pcrtlang("4 digits")."</td></tr>";
echo "<tr><td>".pcrtlang("Security Code on Back").": </td><td><input type=text name=cccid size=5 class=\"form-control\"></td></tr>";

echo "<tr><td colspan=2>&nbsp;</td></tr>";

echo "<tr><td>".pcrtlang("First Name on Card").": </td>";
echo "<td><input type=text name=cfirstname size=25 class=\"form-control\"></td></tr>";

echo "<tr><td>".pcrtlang("Last Name on Card").": </td>";
echo "<td><input type=text name=clastname size=25 class=\"form-control\"></td></tr>";

echo "<input type=hidden name=invoiceid value=\"$invoiceid\">";


echo "<tr><td></td><td><input type=submit class=\"btn btn-primary\" value=\"".pcrtlang("Pay Invoice")."\" onclick=\"this.disabled=false;this.value='".pcrtlang("Please Wait")."...'; this.form.submit();\"></form></td></tr></table>";


require("footer.php");
}



function add3() {
require_once("validate.php");

require("deps.php");
require_once("common.php");

if (function_exists('date_default_timezone_set')) {
date_default_timezone_set("$pcrt_timezone");
}
$currentdatetime = date('Y-m-d H:i:s');


$amounttopay = $_REQUEST['amounttopay'];
$invtotal = $_REQUEST['invtotal'];
$invtax = $_REQUEST['invtax'];
$ccnumber = pv($_REQUEST['ccnumber']);
$ccmonth = pv($_REQUEST['ccmonth']);
$ccyear = pv($_REQUEST['ccyear']);
$cccid = pv($_REQUEST['cccid']);

$cfirstname = pv($_REQUEST['cfirstname']);
$clastname = pv($_REQUEST['clastname']);
$custname = "$cfirstname $clastname";

$invoiceid = pv($_REQUEST['invoiceid']);


if ($demo == "yes") {
die("Sorry this feature is disabled in demo mode");
}

$amounttopaycents = number_format($amounttopay, 2, '', ''); 

require_once('stripe/init.php');

$isapproved = 1;

try {
\Stripe\Stripe::setApiKey("$stripe_api_key");
$ch = \Stripe\Charge::create(array(
  "amount" => "$amounttopaycents",
  "currency" => "$stripe_currency",
  "card" => array(
    "name" => "$custname",
    "number" => "$ccnumber",
    "exp_month" => $ccmonth,
    "exp_year" => $ccyear,
    "cvc" => $cccid
  ),
  "description" => pcrtlang("Charge from")." $businessname")
);

}
  catch (Exception $e) {
    $chargeerror = $e->getMessage();
$isapproved = 0;
}


if ($isapproved == 0) {

require_once("common.php");
require("header.php");
echo "<br><br><br><div class=\"bg-danger text-center\">";
echo "<br><h3 class=\"text-danger\"><i class=\"fa fa-warning fa-lg\"></i> ".pcrtlang("Payment Failed")."...</h3><br>";
$thereason = $chargeerror;
echo pcrtlang("Reason").": $thereason<br><br>";
echo "<a href=index.php><i class=\"fa fa-home fa-lg\"></i> ".pcrtlang("Go Home")."</a><br><br>";
echo "</div>";
require("footer.php");

} else {

$cc_transid = $ch->id;
$cardinfo = $ch->card;
$cccardtype = $cardinfo['type'];   

$ccnumber2 = substr("$ccnumber", -4);

$ccmonth = pv($_REQUEST['ccmonth']);
$ccyear = pv($_REQUEST['ccyear']);
$custname = urlencode("$custname");


header("Location: $domain/checkoutinvoice.php?func=checkout&invoiceid=$invoiceid&ccnumber=$ccnumber2&transid=$cc_transid&cardtype=$cccardtype&cardnamefirst=$cfirstname&cardnamelast=$clastname&ccmonth=$ccmonth&ccyear=$ccyear&amounttopay=$amounttopay&invtotal=$invtotal&invtax=$invtax");


}
                                                                                                                                               
}



switch($func) {
                                                                                                                             
    default:
    nothing();
    break;
                                
    case "add":
    add();
    break;

    case "add3":
    add3();
    break;



}

?>
