<?php
echo "Entered";
// This php file is to receive information from UBF checkout after the payment and it resides in ubf folder (folder different from that of where other php files reside). Hence include other php files into this php accordingly
include_once("CUserSession.php");
include_once("helper.php");
include_once("CUtil.php");
include_once("CProject.php");

// Silent Post Variables - Start here
// print_r($_POST['projectId'], true);
// print_r($_POST['items'][0]['custom']['project_id'], true);

// foreach ($_POST as $key => $value)
//     $message .= "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>";

// mail('sainihar@buffalo.edu', 'POST VALUES', $message);

$project_id =  print_r($_POST['items'][0]['custom']['project_id'], true); /* Obtain Project ID to which the payment transaction occurred*/
$project_submission_type = "isUD Certification";
$authnet_transact_id = print_r($_POST['authnet_transaction_id'], true); /* Authorize.net transaction ID */
$bank_acct_last_4_digits = print_r($_POST['bank_acct_last_4_digits'], true);/* The last 4 digits of the bank account charged.*/
$billing_first_name = print_r($_POST['billing_first_name'], true);/* Payer's billing first name */
$billing_last_name = print_r($_POST['billing_last_name'], true);/* Payer's billing last name*/
$billing_address = print_r($_POST['billing_address'], true);/* Payer's billing street*/
$billing_city = print_r($_POST['billing_city'], true);/* Payer's billing city*/
$billing_state = print_r($_POST['billing_state'], true);/* Payer's billing state*/
$billing_zip = print_r($_POST['billing_zip'], true);/* Payer's billing zip*/
$billing_country = print_r($_POST['billing_country'], true);/* Payer's billing country*/
$billing_email = print_r($_POST['billing_email'], true);/* Payer's billing email*/
$billing_phone = print_r($_POST['billing_phone'], true);/* Payer's billing phone number*/
$card_last_4_digits = print_r($_POST['card_last_4_digits'], true);
$card_type = print_r($_POST['card_type'], true);
$merchant_email = print_r($_POST['merchant_email'], true);
$pay_method = print_r($_POST['pay_method'], true); 
$pre_certification_payment = print_r($_POST['items'][0]['custom']['pre_cert_payment'], true);
$final_certification_payment = print_r($_POST['items'][0]['custom']['final_cert_payment'], true);

// $shipping = print_r($_POST['items']['0']['shipping'], true);/* Order shipping amount*/

// $shipping_first_name = print_r($_POST['shipping_first_name'], true);/* Payer's shipping first name*/
// $shipping_last_name = print_r($_POST['shipping_last_name'], true);/* Payer's shipping last name*/
// $shipping_address = print_r($_POST['shipping_address'], true);/* Payer's shipping street*/
// $shipping_city = print_r($_POST['shipping_city'], true);/* Payer's shipping city*/
// $shipping_state = print_r($_POST['shipping_state'], true);/* Payer's shipping state*/
// $shipping_zip = print_r($_POST['shipping_zip'], true);/* Payer's shipping zip*/
// $shipping_country = print_r($_POST['shipping_country'], true);/* Payer's shipping country*/
$site_code = print_r($_POST['site_code'], true);/* The passed-in site_code*/
$sub_total = print_r($_POST['items']['0']['sub_total'], true);/* Order subtotal. Includes tax and shipping.*/
$tax = print_r($_POST['items']['0']['tax'], true);/* Order tax*/
$total_amount = print_r($_POST['items']['0']['total_amount'], true);/* Order total amount*/

$ubf_transaction_id = print_r($_POST['ubf_transaction_id'], true);/* UBF unique transaction identifier (UBF's Transaction ID)*/
$x_authnet_auth_code = print_r($_POST['x_authnet_auth_code'], true);/* Authorize.net authorization or approval code*/
$x_avs_code = print_r($_POST['x_avs_code'], true);
$x_response_code = print_r($_POST['x_response_code'], true);/* Authorize.net response code:*/

$x_response_reason_text = print_r($_POST['x_response_reason_text'], true);
$x_response_reason_code = print_r($_POST['x_response_reason_code'], true);
$x_response_sub_code = print_r($_POST['x_response_sub_code'], true);

// Silent Post Variables - End here
if($x_response_code == 1){

// Database Insertions
$date_now = new DateTime();
$timestamp = (string)$date_now->getTimestamp();
$order_id = "ord_".$timestamp;

$sql_conn = mysqli_connection();

if(!($stmt = $sql_conn->prepare("INSERT INTO UD_ubf_checkout (order_id,project_id,project_submission_type, authnet_transact_id, bank_acct_last_4_digits, billing_first_name, billing_last_name, billing_address, billing_city, billing_state, billing_zip, 
                                    billing_country,billing_email,billing_phone,card_last_4_digits,card_type,merchant_email,pay_method,site_code,total_amount,ubf_transaction_id,x_authnet_auth_code,x_avs_code,x_response_code,x_response_reason_text,x_response_reason_code,x_response_sub_code) 
                                    VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"))){
    $ret =  "Prepare failed: (" . $sql_conn->errno . ") " . $sql_conn->error." ll";
}

$stmt->bind_param("ssdssssssssssssssssssssssss",
                $order_id,$project_id,$project_submission_type,$authnet_transact_id, $bank_acct_last_4_digits, $billing_first_name, $billing_last_name, $billing_address, $billing_city, $billing_state, $billing_zip, $billing_country,$billing_email,$billing_phone,$card_last_4_digits,$card_type,$merchant_email,$pay_method,$site_code,$total_amount,$ubf_transaction_id,$x_authnet_auth_code,$x_avs_code,$x_response_code,$x_response_reason_text,$x_response_reason_code,$x_response_sub_code);

if(!$stmt->execute()) mail('sainihar@buffalo.edu', 'Thu', $stmt->error);
else mail('sainihar@buffalo.edu', 'Hi', $stmt->error);

$stmt->close();
mysqli_close($sql_conn);

$sql_conn = mysqli_connection();

mail('sainihar@buffalo.edu', ' pre_certification_payment Values', $pre_certification_payment);
mail('sainihar@buffalo.edu', ' final_certification_payment Values', $final_certification_payment);
if($pre_certification_payment == 1){
    if (!($stmt = $sql_conn->prepare("UPDATE `UD_P_Info` SET `pre_certification_paid` = '1', `isud_status` = '2' WHERE `UD_P_Info`.`_id` = ?"))){
        $sql_ret =  "Prepare failed: (" . $sql_conn->errno . ") " . $sql_conn->error;
    }
}

if(!($stmt->bind_param("s",
            $project_id))){
    $sql_ret =  "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}

if(!$stmt->execute()) mail('sainihar@buffalo.edu', 'IntenseTesting', 'Hi');
else mail('sainihar@buffalo.edu', 'IntenseTesting', 'Bye');

$stmt->close();
mysqli_close($sql_conn);

$sql_conn = mysqli_connection();

if($final_certification_payment == 1){
    if (!($stmt = $sql_conn->prepare("UPDATE `UD_P_Info` SET `final_certification_paid` = '1', `isud_status` = '2' WHERE `UD_P_Info`.`_id` = ?"))){
        $sql_ret =  "Prepare failed: (" . $sql_conn->errno . ") " . $sql_conn->error;
    }
 }

if(!($stmt->bind_param("s",
            $project_id))){
    $sql_ret =  "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}

if(!$stmt->execute()) mail('sainihar@buffalo.edu', 'IntenseTesting', 'Hi');
else mail('sainihar@buffalo.edu', 'IntenseTesting', 'Bye');

$stmt->close();
mysqli_close($sql_conn);
}

?>