<?php
require 'autoload.php'; 
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;

define("AUTHORIZENET_LOG_FILE","phplog");

	if(session_status()==PHP_SESSION_NONE){session_start();}
	
	if(isset($_SESSION['amount']) && isset($_SESSION['uid']) && isset($_SESSION['oid']) && isset($_SESSION['tx_id']))
	{
		if($_SESSION['tx_id']=="0x9817907279856729875628975".$_SESSION['amount'].$_SESSION['oid']."8174897942856725".$_SESSION['uid']."891748972457285")
		{
			$cardNumber="";
			$expiry="";
			$cvv="";
			$cardName="";

			$amountFinal=$_SESSION['amount'];

			if(isset($_POST['cc_number']))
			{
				$card=explode(" ",$_POST['cc_number']);

				for($i=0;$i<=count($card)-1;$i++)
				{
					$cardNumber.=$card[$i];
				}
			}
			else
			{
				echo "<script>alert('Please Enter Card Number');window.location.href='index.php';</script>";
			}

			if(isset($_POST['expiry']))
			{
				$expiry1=explode('/',$_POST['expiry'])[0];
				$expiry2=explode('/',$_POST['expiry'])[1];

				$expiry="20".$expiry2."-".$expiry1;
			}
			else
			{
				echo "<script>alert('Please Enter Card Expiry Month');window.location.href='index.php';</script>";
			}

			if(isset($_POST['cvv']))
			{
				$cvv=$_POST['cvv'];
			}
			else
			{
				echo "<script>alert('Please Enter Card CVV Code');window.location.href='index.php';</script>";
			}
			
			if(isset($_POST['name']))
			{
				$cardName=$_POST['name'];
			}
			else
			{
				echo "<script>alert('Please Enter Card Name');window.location.href='index.php';</script>";
			}

				/*echo $cardNumber."<br>";
				echo $expiry."<br>";
				echo $cvv."<br>";
				echo $amountFinal."<br>";
				echo $cardName."<br>";*/

			// Common setup for API credentials  
			  $merchantAuthentication = new AnetAPI\MerchantAuthenticationType();   
			  /*$merchantAuthentication->setName("2rw3RgNjC9X");   
			  $merchantAuthentication->setTransactionKey("4qLk46SV7XTm755D");  */
			 // $merchantAuthentication->setName("35jy5SfZ75er");   
			 // $merchantAuthentication->setTransactionKey("2wC55Bm38eZZ45rr");  
			   //PRODUCTION KEYS
  $merchantAuthentication->setName("5Sny6Zn8VZ");   
  $merchantAuthentication->setTransactionKey("7HuNsGj2y94E947D");
			  $refId = 'ref' . time();

			// Create the payment data for a credit card
			  $creditCard = new AnetAPI\CreditCardType();
			  $creditCard->setCardNumber($cardNumber);
			  $creditCard->setExpirationDate($expiry);
			  $creditCard->setCardCode($cvv);
			  $paymentOne = new AnetAPI\PaymentType();
			  $paymentOne->setCreditCard($creditCard);

			// Create a transaction
			  $transactionRequestType = new AnetAPI\TransactionRequestType();
			  $transactionRequestType->setTransactionType("authCaptureTransaction");   
			  $transactionRequestType->setAmount($amountFinal);
			  $transactionRequestType->setPayment($paymentOne);
			  $request = new AnetAPI\CreateTransactionRequest();
			  $request->setMerchantAuthentication($merchantAuthentication);
			  $request->setRefId( $refId);
			  $request->setTransactionRequest($transactionRequestType);
			  $controller = new AnetController\CreateTransactionController($request);
			  $response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::PRODUCTION);   

			if ($response != null) 
			{
			  $tresponse = $response->getTransactionResponse();
			  if (($tresponse != null) && ($tresponse->getResponseCode()=="1"))
			  {
				  ?>
				<!DOCTYPE html>
					<html>
						<head>
							<meta charset="utf-8">
							<title>Invoice | <?php echo $_COOKIE['uid']; ?></title>
							<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
					<link rel="stylesheet" type="text/css" href="invoice.css">
						</head>
						<body>
							<span><img alt="" src="http://meatneatz.com/mnez/images/mnelogo.png" style="width: 100px"></span>
							<header>
								
								<h1>Invoice </h1>
								<aside>
									<div>
									<h3>Thank You <?php echo $cardName; ?>,</h3>
									<p>We have received your order and our team is working on it. You will be contacted shortly after payment confirmation</p>
									<p>Print this email if you like or mention invoice# <b><?php echo $_SESSION['oid']; ?> </b> in coresspondance through email</p>
								</div>
								</aside>

							</header>
							<article>
						<!-- 	<p>Hello Taha<?php echo $cardName; ?><br><?php echo $_SESSION['tx_id']; ?></p> -->
								<table class="meta">
									<tr>
										<th><span >Invoice #</span></th>
										<td><span ><?php echo $_SESSION['oid']; ?></span></td>
									</tr>

									<tr>
										<th><span >Total</span></th>
										<td><span data-prefix>$</span><span><?php echo $_SESSION['amount']; ?></span></td>
									</tr>
									<tr>
										<th><span >Payment Status</span></th>
										<td><span data-prefix></span><span style="color:green; font-weight: bold;">Paid</span></td>
									</tr>
									
								</table>
								<table class="balance">
								
									<tr>
										<th><span >Date</span></th>
										<td><span ><?php echo  date("F j, Y, g:i a"); ?></span></td>
									</tr>
									<tr>
										<th><span >Amount Due:</span></th>
										<td><span id="prefix" >$</span><span><?php echo $_SESSION['amount']; ?></span></td>
									</tr>
									<tr>
										<th><span >Amount Paid</span></th>
										<td><span data-prefix>$</span><span ><?php echo $_SESSION['amount']; ?></span></td>
									</tr>
									
								</table>
							</article>
							<aside>
								<h1><span >Additional Notes</span></h1>
								<div>
									<p>If you have any issues then you can 
										<br>Call us at <b>(607) 431 3100</b>
										<br>Drop Email at <b>contact@shop.meatneatz.com</b>
										<br>Visit www.meatneatz.com </p>
								</div>
							</aside>
						</body>
					</html>
				<?php
				/*echo "Charge Credit Card AUTH CODE : " . $tresponse->getAuthCode() . "<br>";
				echo "Charge Credit Card TRANS ID  : " . $tresponse->getTransId() . "<br>";*/
			  }
			  else
			  {
				echo "<script>alert('Charge Credit Card ERROR :  Invalid response!'); window.location.href='index.php';</script>";
			  }
			}  
			else
			{
			  echo  "<script>alert('Charge Credit Card Null response returned!'); window.location.href='index.php';</script>";
			}
		}
		else
		{
			echo "<script>alert('Security Verification Failed!'); window.location.href='index.php';</script>";
		}
	}
	
	elseif(isset($_COOKIE['amount']) && isset($_COOKIE['uid']) && isset($_COOKIE['oid']) && isset($_COOKIE['tx_id']))
	{
		if($_COOKIE['tx_id']=="0x9817907279856729875628975".$_COOKIE['amount'].$_COOKIE['oid']."8174897942856725".$_COOKIE['uid']."891748972457285")
		{
			$cardNumber="";
			$expiry="";
			$cvv="";
			$cardName="";
			
			$amountFinal=$_COOKIE['amount'];

			if(isset($_POST['cc_number']))
			{
				$card=explode(" ",$_POST['cc_number']);

				for($i=0;$i<=count($card)-1;$i++)
				{
					$cardNumber.=$card[$i];
				}
			}
			else
			{
				echo "<script>alert('Please Enter Card Number');window.location.href='index.php';</script>";
			}

			if(isset($_POST['expiry']))
			{
				$expiry1=explode('/',$_POST['expiry'])[0];
				$expiry2=explode('/',$_POST['expiry'])[1];

				$expiry="20".$expiry2."-".$expiry1;
			}
			else
			{
				echo "<script>alert('Please Enter Card Expiry Month');window.location.href='index.php';</script>";
			}

			if(isset($_POST['cvv']))
			{
				$cvv=$_POST['cvv'];
			}
			else
			{
				echo "<script>alert('Please Enter Card CVV Code');window.location.href='index.php';</script>";
			}
			
			if(isset($_POST['name']))
			{
				$cardName=$_POST['name'];
			}
			else
			{
				echo "<script>alert('Please Enter Card Name');window.location.href='index.php';</script>";
			}

				/*echo $cardNumber."<br>";
				echo $expiry."<br>";
				echo $cvv."<br>";
				echo $amountFinal."<br>";
				echo $cardName."<br>";*/

			// Common setup for API credentials  
			  $merchantAuthentication = new AnetAPI\MerchantAuthenticationType();   
			  /*$merchantAuthentication->setName("2rw3RgNjC9X");   
			  $merchantAuthentication->setTransactionKey("4qLk46SV7XTm755D");  */
			  $merchantAuthentication->setName("35jy5SfZ75er");   
			  $merchantAuthentication->setTransactionKey("2wC55Bm38eZZ45rr");    
			  $refId = 'ref' . time();

			// Create the payment data for a credit card
			  $creditCard = new AnetAPI\CreditCardType();
			  $creditCard->setCardNumber($cardNumber); 
			  $creditCard->setExpirationDate($expiry);
			  $creditCard->setCardCode($cvv);
			  $paymentOne = new AnetAPI\PaymentType();
			  $paymentOne->setCreditCard($creditCard);

			// Create a transaction
			  $transactionRequestType = new AnetAPI\TransactionRequestType();
			  $transactionRequestType->setTransactionType("authCaptureTransaction");   
			  $transactionRequestType->setAmount($amountFinal);
			  $transactionRequestType->setPayment($paymentOne);
			  $request = new AnetAPI\CreateTransactionRequest();
			  $request->setMerchantAuthentication($merchantAuthentication);
			  $request->setRefId( $refId);
			  $request->setTransactionRequest($transactionRequestType);
			  $controller = new AnetController\CreateTransactionController($request);
			  $response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX);   

			if ($response != null) 
			{
			  $tresponse = $response->getTransactionResponse();
			  if (($tresponse != null) && ($tresponse->getResponseCode()=="1"))
			  {
				  ?>
					<!doctype html>
					<html>
						<head>
							<meta charset="utf-8">
							<title>Invoice | <?php echo $_COOKIE['uid']; ?></title>
							<style>
							/* reset */
							*
							{
								border: 0;
								box-sizing: content-box;
								color: inherit;
								font-family: inherit;
								font-size: inherit;
								font-style: inherit;
								font-weight: inherit;
								line-height: inherit;
								list-style: none;
								margin: 0;
								padding: 0;
								text-decoration: none;
								vertical-align: top;
							}

							/* content editable */

							*[contenteditable] { border-radius: 0.25em; min-width: 1em; outline: 0; }

							*[contenteditable] { cursor: pointer; }

							*[contenteditable]:hover, *[contenteditable]:focus, td:hover *[contenteditable], td:focus *[contenteditable], img.hover { background: #DEF; box-shadow: 0 0 1em 0.5em #DEF; }

							span[contenteditable] { display: inline-block; }

							/* heading */

							h1 { font: bold 100% sans-serif; letter-spacing: 0.5em; text-align: center; text-transform: uppercase; }

							/* table */

							table { font-size: 75%; table-layout: fixed; width: 100%; }
							table { border-collapse: separate; border-spacing: 2px; }
							th, td { border-width: 1px; padding: 0.5em; position: relative; text-align: left; }
							th, td { border-radius: 0.25em; border-style: solid; }
							th { background: #EEE; border-color: #BBB; }
							td { border-color: #DDD; }

							/* page */

							html { font: 16px/1 'Open Sans', sans-serif; overflow: auto; padding: 0.5in; }
							html { background: #999; cursor: default; }

							body { box-sizing: border-box; height: 11in; margin: 0 auto; overflow: hidden; padding: 0.5in; width: 8.5in; }
							body { background: #FFF; border-radius: 1px; box-shadow: 0 0 1in -0.25in rgba(0, 0, 0, 0.5); }

							/* header */

							header { margin: 0 0 3em; }
							header:after { clear: both; content: ""; display: table; }

							header h1 { background: #000; border-radius: 0.25em; color: #FFF; margin: 0 0 1em; padding: 0.5em 0; }
							header address { float: left; font-size: 75%; font-style: normal; line-height: 1.25; margin: 0 1em 1em 0; }
							header address p { margin: 0 0 0.25em; }
							header span, header img { display: block; float: right; }
							header span { margin: 0 0 1em 1em; max-height: 25%; max-width: 60%; position: relative; }
							header img { max-height: 100%; max-width: 100%; }
							header input { cursor: pointer; -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)"; height: 100%; left: 0; opacity: 0; position: absolute; top: 0; width: 100%; }

							/* article */

							article, article address, table.meta, table.inventory { margin: 0 0 3em; }
							article:after { clear: both; content: ""; display: table; }
							article h1 { clip: rect(0 0 0 0); position: absolute; }

							article address { float: left; font-size: 125%; font-weight: bold; }

							/* table meta & balance */

							table.meta, table.balance { float: right; width: 36%; }
							table.meta:after, table.balance:after { clear: both; content: ""; display: table; }

							/* table meta */

							table.meta th { width: 40%; }
							table.meta td { width: 60%; }

							/* table items */

							table.inventory { clear: both; width: 100%; }
							table.inventory th { font-weight: bold; text-align: center; }

							table.inventory td:nth-child(1) { width: 26%; }
							table.inventory td:nth-child(2) { width: 38%; }
							table.inventory td:nth-child(3) { text-align: right; width: 12%; }
							table.inventory td:nth-child(4) { text-align: right; width: 12%; }
							table.inventory td:nth-child(5) { text-align: right; width: 12%; }

							/* table balance */

							table.balance th, table.balance td { width: 50%; }
							table.balance td { text-align: right; }

							/* aside */

							aside h1 { border: none; border-width: 0 0 1px; margin: 0 0 1em; }
							aside h1 { border-color: #999; border-bottom-style: solid; }

							/* javascript */

							.add, .cut
							{
								border-width: 1px;
								display: block;
								font-size: .8rem;
								padding: 0.25em 0.5em;	
								float: left;
								text-align: center;
								width: 0.6em;
							}

							.add, .cut
							{
								background: #9AF;
								box-shadow: 0 1px 2px rgba(0,0,0,0.2);
								background-image: -moz-linear-gradient(#00ADEE 5%, #0078A5 100%);
								background-image: -webkit-linear-gradient(#00ADEE 5%, #0078A5 100%);
								border-radius: 0.5em;
								border-color: #0076A3;
								color: #FFF;
								cursor: pointer;
								font-weight: bold;
								text-shadow: 0 -1px 2px rgba(0,0,0,0.333);
							}

							.add { margin: -2.5em 0 0; }

							.add:hover { background: #00ADEE; }

							.cut { opacity: 0; position: absolute; top: 0; left: -1.5em; }
							.cut { -webkit-transition: opacity 100ms ease-in; }

							tr:hover .cut { opacity: 1; }

							@media print {
								* { -webkit-print-color-adjust: exact; }
								html { background: none; padding: 0; }
								body { box-shadow: none; margin: 0; }
								span:empty { display: none; }
								.add, .cut { display: none; }
							}

							@page { margin: 0; }
							</style>
						</head>
						<body>
							<header>
								<h1>Invoice</h1>
								<address contenteditable>
									<p><?php echo $cardName; ?></p>
								</address>
								<span><img alt="" src="http://www.jonathantneal.com/examples/invoice/logo.png"><input type="file" accept="image/*"></span>
							</header>
							<article>
								<h1>Recipient</h1>
								<address contenteditable>
									<p><?php echo $cardName; ?><br><?php echo $_COOKIE['tx_id']; ?></p>
								</address>
								<table class="meta">
									<tr>
										<th><span contenteditable>Invoice #</span></th>
										<td><span contenteditable><?php echo $_COOKIE['oid']; ?></span></td>
									</tr>
									<tr>
										<th><span contenteditable>Date</span></th>
										<td><span contenteditable><?php echo  date("F j, Y, g:i a"); ?></span></td>
									</tr>
									<tr>
										<th><span contenteditable>Amount Due:</span></th>
										<td><span id="prefix" contenteditable>$</span><span><?php echo $_COOKIE['amount']; ?></span></td>
									</tr>
								</table>
								<!--<table class="inventory">
									<thead>
										<tr>
											<th><span contenteditable>Item</span></th>
											<th><span contenteditable>Description</span></th>
											<th><span contenteditable>Rate</span></th>
											<th><span contenteditable>Quantity</span></th>
											<th><span contenteditable>Price</span></th>
										</tr>
									</thead>
									<tbody>
										<tr>
											<td><a class="cut">-</a><span contenteditable>Front End Consultation</span></td>
											<td><span contenteditable>Experience Review</span></td>
											<td><span data-prefix>$</span><span contenteditable>150.00</span></td>
											<td><span contenteditable>4</span></td>
											<td><span data-prefix>$</span><span>600.00</span></td>
										</tr>
									</tbody>
								</table>-->
								<a class="add">+</a>
								<table class="balance">
									<tr>
										<th><span contenteditable>Total</span></th>
										<td><span data-prefix>$</span><span><?php echo $_COOKIE['amount']; ?></span></td>
									</tr>
									<tr>
										<th><span contenteditable>Amount Paid</span></th>
										<td><span data-prefix>$</span><span contenteditable><?php echo $_COOKIE['amount']; ?></span></td>
									</tr>
									<tr>
										<th><span contenteditable>Payment Status</span></th>
										<td><span data-prefix></span><span style="color:green; font-weight: bold;">Paid</span></td>
									</tr>
								</table>
							</article>
							<!--<aside>
								<h1><span contenteditable>Additional Notes</span></h1>
								<div contenteditable>
									<p>A finance charge of 1.5% will be made on unpaid balances after 30 days.</p>
								</div>
							</aside>-->
						</body>
					</html>
				<?php
				/*echo "Charge Credit Card AUTH CODE : " . $tresponse->getAuthCode() . "<br>";
				echo "Charge Credit Card TRANS ID  : " . $tresponse->getTransId() . "<br>";*/
			  }
			  else
			  {
				echo "<script>alert('Charge Credit Card ERROR :  Invalid response!'); window.location.href='index.php';</script>";
			  }
			}  
			else
			{
			  echo  "<script>alert('Charge Credit Card Null response returned!'); window.location.href='index.php';</script>";
			}
		}
		else
		{
			echo "<script>alert('Security Verification Failed!'); window.location.href='index.php';</script>";
		}
	}
	
	else
	{
		echo "<script>alert('Security Verification Failed!'); window.location.href='index.php';</script>";
	}
	// localhost/secure/secure/auth/?tx_id=0x9817907279856729875628975 100 7689672986582657 8174897942856725 12 891748972457285 &amount=100&uid=12&oid=7689672986582657&
	
	// localhost/secure/secure/auth/?tx_id=0x98179072798567298756289751007689672986582657817489794285672512891748972457285&amount=100&uid=12&oid=7689672986582657
?>
<?php

/*$cardNumber='4111111111111111';
$expiry="2038-12";
$cvv="123";

$amountFinal="650";*/

?>