<?php

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

$amountFinal="650";*/

$cardNumber="";
$expiry="";
$cvv="";

$amountFinal="";

if(isset($_POST['cc_number']))
{
	$card=explode(" ",$_POST['cc_number']);
	
	$cardNumber=$card[0].$card[1].$card[2].$card[3];
}
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['amount']))
{
	$amountFinal=$_POST['amount'];
}
else
{
	echo "<script>alert('Please Enter Amount');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>";
}

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

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

define("AUTHORIZENET_LOG_FILE","phplog");

// Common setup for API credentials  
  $merchantAuthentication = new AnetAPI\MerchantAuthenticationType();   
  //FAROOQ SANDBOX
  /*$merchantAuthentication->setName("2rw3RgNjC9X");   
  $merchantAuthentication->setTransactionKey("4qLk46SV7XTm755D");  */
  //TAHA SANDBOX
  $merchantAuthentication->setName("35jy5SfZ75er");   
  $merchantAuthentication->setTransactionKey("2wC55Bm38eZZ45rr");
  //PRODUCTION KEYS
//   $merchantAuthentication->setName("378sV6Ljj");   
//   $merchantAuthentication->setTransactionKey("23362j7b7HnfWZNJ");
  $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"))
  {
    echo "Charge Credit Card AUTH CODE : " . $tresponse->getAuthCode() . "<br>";
    echo "Charge Credit Card TRANS ID  : " . $tresponse->getTransId() . "<br>";
  }
  else
  {
    echo "Charge Credit Card ERROR :  Invalid response\n";
  }
}  
else
{
  echo  "Charge Credit Card Null response returned";
}
?>