connect_error);
$qcon=mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$tokencheck = "";
error_log($phone);
if($phone != "") {
$query1 = "SELECT token FROM inquiries WHERE inquiry_mobile='".$phone."' ORDER BY token DESC LIMIT 0,1";
$result = mysqli_query($conn, $query1);
$row = mysqli_fetch_assoc($result);
$tokencheck = $row['token'];
$success1 = $conn->query($query1);
if (!$success1) {
$updatetoken = 0;
}
}
}
if( $tokencheck != ""){
// Updates Token then charges token
$apiEndpoint = \Eway\Rapid\Client::MODE_PRODUCTION;
$client = \Eway\ Rapid::createClient($apiKey, $apiPassword, $apiEndpoint);
$tokencustomerid = $tokencheck;
$customer = [
'Email' => trim($_POST['email']),
'TokenCustomerID' => $tokencustomerid,
'CardDetails' => [
'Name' => $fullname,
'Number' => $_POST['EWAY_CARDNUMBER'],
'ExpiryMonth' => $_POST['EWAY_CARDEXPIRYMONTH'],
'ExpiryYear' => $_POST['EWAY_CARDEXPIRYYEAR'],
'CVN' => $_POST['EWAY_CARDCVN']
]
];
/*echo '
';
print_r($customer);
echo '
';
exit;*/
$response = $client->updateCustomer(\Eway\Rapid\Enum\ApiMethod::DIRECT, $customer);
$transaction = [
'Customer' => [
'TokenCustomerID' => $tokencustomerid,
'Reference' => $_POST['reference'],
],
'Payment' => [
'TotalAmount' => $charge,
'InvoiceReference' => $_POST['reference'],
],
'TransactionType' => \Eway\Rapid\Enum\TransactionType::RECURRING,
];
$response = $client->createTransaction(\Eway\Rapid\Enum\ApiMethod::DIRECT, $transaction);
}else{
// Creates token and charges token
$apiEndpoint = \Eway\Rapid\Client::MODE_PRODUCTION;
$client = \Eway\Rapid::createClient($apiKey, $apiPassword, $apiEndpoint);
//create customer
$fullname = trim($_POST['fullname']);
$nameparts = explode(' ', $fullname);
$customer = [
'Title' => 'Mr.',
'FirstName' => trim($nameparts[0]),
'LastName' => trim($nameparts[1]),
'Country' => 'au',
'Email' => trim($_POST['email']),
'CardDetails' => [
'Name' => $fullname,
'Number' => $_POST['EWAY_CARDNUMBER'],
'ExpiryMonth' => $_POST['EWAY_CARDEXPIRYMONTH'],
'ExpiryYear' => $_POST['EWAY_CARDEXPIRYYEAR'],
'CVN' => $_POST['EWAY_CARDCVN']
]
];
$response = $client->createCustomer(\Eway\Rapid\Enum\ApiMethod::DIRECT, $customer);
$tokencustomerid = $response->Customer->TokenCustomerID;
$transaction = [
'Customer' => [
'TokenCustomerID' => $tokencustomerid,
'Reference' => $_POST['reference'],
],
'Payment' => [
'TotalAmount' => $charge,
'InvoiceReference' => $_POST['reference'],
],
'TransactionType' => \Eway\Rapid\Enum\TransactionType::RECURRING,
];
$response = $client->createTransaction(\Eway\Rapid\Enum\ApiMethod::DIRECT, $transaction);
if (isset($response->TransactionStatus) && $response->TransactionStatus) {
$query = "SELECT * FROM customer_tokens WHERE access_token_value='".$_POST['token']."'";
$result = $conn->query($query);
if(!$result) {
error_log("Token error: ".$conn->error);
} else {
if ($result->num_rows > 0)
{
$row = $result->fetch_assoc();
$tokencheck = $row['access_token_value'];
$query = "UPDATE customer_tokens SET modified = NOW() WHERE access_token_value = ".$tokencheck;
$result = $conn->query($query);
if(!$result) {
error_log("Token update error: ".$conn->error);
}
} else {
if($type == "DD" OR $type == "FC" OR $type == "SF" or $type == "MF" or $type == "BF" or $type == "GC" or $type == "SC") {
$query = "INSERT INTO customer_tokens (`id`, `cid`, `access_token_value`, `status`, `created`, `modified`) VALUES (NULL, '', ".$_POST['token'].", 1, NOW(), NOW())";
$result = $conn->query($query);
if(!$result) {
error_log("Token insert error: ".$conn->error);
}
}
}
}
}
}
$nameq = mysqli_real_escape_string($qcon, $fullname);
$dropoffq = mysqli_real_escape_string($qcon, $homeaddress);
if($type == "DD") {
$query = "INSERT into inquiries (inquiry_name, inquiry_mobile, inquiry_dropoff, inquiry_email, token, inquiry_created, welcomemess) VALUES ('" . $nameq . "','" . $phone . "','" . $dropoffq . "','" . $email . "','" . $tokencustomerid . "', NOW(), '') ON DUPLICATE KEY UPDATE inquiry_name='" . $nameq . "', inquiry_dropoff='" . $dropoffq . "', inquiry_email='" . $email . "', token='" . $tokencustomerid . "'";
}
if($type == "FC" or $type == "SF" or $type == "MF" or $type == "BF") {
$table = "customers";
$query = "INSERT into customers (customer_name, customer_mobile, customer_email, token, date_added, welcomemess) VALUES ('" . $nameq . "','" . $phone . "','" . $email . "','" . $tokencustomerid . "', NOW(), '') ON DUPLICATE KEY UPDATE customer_name='" . $nameq . "', customer_email='" . $email . "', token='" . $tokencustomerid . "'";
}
$success = $conn->query($query);
if (!$success) {
die("Card could be processed but error with data/invoice: ".$conn->error);
}
} else {
// One time transaction
$apiEndpoint = \Eway\Rapid\Client::MODE_PRODUCTION;
$client = \Eway\Rapid::createClient($apiKey, $apiPassword, $apiEndpoint);
//create customer
$transaction = [
'Customer' => [
'Email' => trim($_POST['email']),
'Reference' => $_POST['reference'],
'CardDetails' => [
'Name' => $fullname,
'Number' => $_POST['EWAY_CARDNUMBER'],
'ExpiryMonth' => $_POST['EWAY_CARDEXPIRYMONTH'],
'ExpiryYear' => $_POST['EWAY_CARDEXPIRYYEAR'],
'CVN' => $_POST['EWAY_CARDCVN']
]
],
'Payment' => [
'TotalAmount' => $charge,
'InvoiceReference' => $_POST['reference'],
],
'TransactionType' => \Eway\Rapid\Enum\TransactionType::PURCHASE,
];
$response = $client->createTransaction(\Eway\Rapid\Enum\ApiMethod::DIRECT, $transaction);
}
} else if ($_POST['payment_type'] == 'token') {
$sql = "SELECT * FROM invoices WHERE reference = '".$_POST["reference"]."'";
$result = $conn->query($sql);
if (!$result->num_rows > 0) {
$apiEndpoint = \Eway\Rapid\Client::MODE_PRODUCTION;
$client = \Eway\ Rapid::createClient($apiKey, $apiPassword, $apiEndpoint);
$tokencustomerid = $_POST['token'];
$transaction = [
'Customer' => [
'TokenCustomerID' => $_POST['token'],
'Reference' => $_POST['reference'],
],
'Payment' => [
'TotalAmount' => $charge,
'InvoiceReference' => $_POST['reference'],
],
'TransactionType' => \Eway\Rapid\Enum\TransactionType::RECURRING,
];
$response = $client->createTransaction(\Eway\Rapid\Enum\ApiMethod::DIRECT, $transaction);
if (isset($response->TransactionStatus) && $response->TransactionStatus) {
$query = "SELECT * FROM customer_tokens WHERE access_token_value='".$_POST['token']."'";
$result = $conn->query($query);
if(!$result) {
error_log("Token error: ".$conn->error);
} else {
if ($result->num_rows > 0)
{
$row = $result->fetch_assoc();
$tokencheck = $row['access_token_value'];
$query = "UPDATE customer_tokens SET modified = NOW() WHERE access_token_value = ".$tokencheck;
$result = $conn->query($query);
if(!$result) {
error_log("Token update error: ".$conn->error);
}
} else {
if($type == "DD" OR $type == "FC" OR $type == "SF" or $type == "MF" or $type == "BF" or $type = "GC" or $type == "SC") {
$query = "INSERT INTO customer_tokens (`id`, `cid`, `access_token_value`, `status`, `created`, `modified`) VALUES (NULL, '', ".$_POST['token'].", 1, NOW(), NOW())";
$result = $conn->query($query);
if(!$result) {
error_log("Token insert error: ".$conn->error);
}
}
}
}
}
} else {
echo "Customer Already Charged";
}
}
if (isset($response->TransactionStatus) && $response->TransactionStatus) {
$transactionid = $response->TransactionID;
$AuthorisationCode = $response->AuthorisationCode;
$paymentCode = "1cb7bb21-739d-480d-909d-fe564b0e2df8";
$sql = "UPDATE ".$table." SET payment_eway = '".$charge."' WHERE reference = '".$reference."'";
$response = $conn->query($sql);
if(!$response) {
error_log("payment add to database error ".$conn->error);
}
} else {
if($_POST['payment_type'] != 'account'){
$error = true;
if ($response->getErrors()) {
foreach ($response->getErrors() as $error) {
$errormessage.= "Error: $error (".\Eway\Rapid::getMessage($error).")
\n";
}
} else {
$errormessage.= "Sorry, your payment was declined";
$query = "SELECT * FROM desid573_eway_codes.responses WHERE code = '".$response->ResponseMessage."'";
$result = $conn->query($query);
if ($result->num_rows > 0)
{
$row = $result->fetch_assoc();
$responseMessage = $row['message'];
$errormessage = "
".$responseMessage;
}
}
}
}
/*
echo '
';
print_r($response);
echo '
';
*/
if (!$error) {
echo "
Transaction Approved
";
echo "
";
$amount = $_POST['amount'];
$reference = ($_POST['reference'] != '') ? $_POST['reference'] : $_POST['reference'];
$invname = $_POST['invname'];
$paidby = $_POST['payment_type'];
$invdesc = $_POST['invdesc'];
$invmessage = "Invoice Issued
Name $invname
Type $type
Email $email
Amount $amount
Paid By $paidby
Reference $reference
Description $invdesc";
send_mail('booking@perthdesignateddrivers.com.au', 'invoice@perthdesignateddrivers.com.au', 'New Invoice For '.$invname ,$invmessage);
$amountArray["total"] = $amount;
if($_POST['levy'] != "0") {
$amountArray["levy"] = $_POST['levy'];
$sql = "INSERT INTO `desid573_eway_codes`.`levy` (`id`, `ref`, `levy`) VALUES (NULL,'".$reference."','".$_POST['levy']."')";
$result = $conn->query($sql);
if(!$result) {
error_log("levy add error ".$conn->error);
}
}
if($_POST['tollAmount'] != "0") {
error_log("fired tolls");
$amountArray["tolls"] = $_POST['tollAmount'];
}
$qcon=mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$query1 = "SELECT ".$spatulaID.", driver FROM ".$table." WHERE reference='".$reference."'";
$result = mysqli_query($conn, $query1);
$row = mysqli_fetch_assoc($result);
$spat_ref = $row[$spatulaID];
$driver_name = $row['driver'];
$query1 = "SELECT id FROM desid573_eway_codes.drivers WHERE driver='".$driver_name."'";
$result = mysqli_query($conn, $query1);
$row = mysqli_fetch_assoc($result);
$driver_id = $row['id'];
$invnamed = mysqli_real_escape_string($qcon, $invname);
$invdescd = mysqli_real_escape_string($qcon, str_replace("&","", $invdesc));
$invdescb = nl2br($invdesc);
$query1 = "INSERT INTO ".$dbname.".invoices (id, reference, tokenid, invname, amount, tollAmount, email, transactionid, date_created, type, paidby, invdesc, driver) VALUES (NULL,'" . $reference . "','" . $tokencustomerid . "','" . $invnamed . "','" . $amountArray["total"] . "','" . $amountArray["tolls"] . "','" . $email . "','" . $transactionid . "','" . date('Y-m-d H:i:s') . "','" . $type . "','" . $paidby . "','" . $invdescd . "','" . $driver_name . "')";
$success = mysqli_query($conn, $query1);
if (!$success) {
die("Couldn't enter data: ".$conn->error);
}
if($type == "SI" OR $type == "HK" OR $type == "FA" OR $type == "SA" OR $type == "MA" or $type == "BA" or $type == "GA" or $type == "YA") {
$query1 = "SELECT cid FROM booking WHERE reference='".$reference."'";
$result = mysqli_query($conn, $query1);
$row = mysqli_fetch_assoc($result);
$cid = $row['cid'];
if($_REQUEST["referral"] != "none") {
$query1 = "UPDATE `credits` SET `reference` = '".$reference."', `is_used` = 1, `date_used` = NOW() WHERE `customer_id`='".$cid."' AND `is_used` = 0";
$result = mysqli_query($conn, $query1);
$query1 = "UPDATE `referal_claim` SET `reference_claimed` = '".$reference."', `date_last_claimed` = NOW() WHERE `customer_id`='".$cid."'";
$result = mysqli_query($conn, $query1);
}
$query1 = "SELECT promo_code, promo_used FROM customers WHERE id='".$cid."' ";
$result = mysqli_query($conn, $query1);
$row = mysqli_fetch_assoc($result);
$promo_code = $row['promo_code'];
$is_used = $row['promo_used'];
$is_used = $is_used + 1;
if($promo_code != ""){
$query1 = "UPDATE `".$dbname."`.`customers` SET `promo_code` = '', promo_used = '".$is_used."' WHERE `id`='".$cid."'";
$result = mysqli_query($conn, $query1);
if(substr($_REQUEST['promo_code'], 0, 2) === "RF"){
$sql = "UPDATE `".$dbname."`.`referrals` where `code` = '".$promo_code."' and `refered_id` = '".$cid."' SET `reference` = '".$reference."', `date_used` = NOW()";
$result = $conn->query($sql);
if(!$result) {
error_log($conn->error);
}
}
}
}
if (!$success) {
die("Couldnt enter invoice data: ".$conn->error);
} else {
date_default_timezone_set('Australia/Perth');
$date = date("c");
$tollDesc = "";
if($amountArray["tolls"] != null) {
$result = mysqli_query($conn, "SELECT * FROM toll_estimates WHERE reference='".$reference."'");
if(!$result) {
error_log($conn->error);
} else {
if($result->num_rows > 0) {
$row = mysqli_fetch_assoc($result);
$tollDesc = $row['description'];
}
}
}
error_log(json_encode($amountArray, true));
error_log($driver_name);
$invoiceDetails = submitInvoice($invnamed, $email, $date, $invdesc, $amountArray, $reference, $type, $tollDesc, $driver_name);
error_log(json_encode($invoiceDetails, true));
if($invoiceDetails != "") {
$sql = "UPDATE ".$table." SET xero_inv = '".$invoiceDetails['invNum']."' WHERE reference = '".$reference."' ";
$response = $conn->query($sql);
if(!$response) {
error_log("xero update database error ".$conn->error);
}
if($paymentCode != "") {
$totalAmount = $amountArray["total"] + $amountArray["tolls"];
error_log("totalAmount = ".$totalAmount);
submitPayment($invoiceDetails['invoiceID'], $paymentCode, $date, $totalAmount, $reference);
}
$totalAmount = $amountArray["total"] + $amountArray["tolls"];
addToSheets($reference, $invoiceDetails['invNum'], $totalAmount);
} else {
addToSheets($reference, "", $amountArray["total"] + $amountArray["tolls"]);
}
}
if($spat_ref != "") {
require("../vromo.php");
$url = "https://api.vromo.io/v2/graph/role/jobs/$spat_ref/dispatches?access_token=".$vromo;
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, "$url");
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($curlObj, CURLOPT_POST, 1);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, '[{"vehicle":{"id":' . $driver_id . '},"type":"closed"}]');
$response = curl_exec($curlObj);
error_log($response);
curl_close($curlObj);
}
$message = "Type $type
Referecence $reference
";
$_POST = array();
die();
} else {
echo "
Transaction has been declined
\n";
echo $errormessage.
"
\n";
echo '
';
echo "
";
}
?>