<?php
require_once $path . 'shop/checkout/sagepay/protx.php';

$vendorTxBase = config('site.tnx_prefix') . $newbasket->order_id;

try {
    $vendorTxToken = bin2hex(random_bytes(4));
} catch (Exception $exception) {
    $vendorTxToken = substr(md5(microtime(true)), -8);
}

$vendorTxCode = $vendorTxBase . '-' . $vendorTxToken;

\Mtc\Shop\Order::query()
    ->where('id', $newbasket->order_id)
    ->update(['order_ref' => $vendorTxCode]);

$newbasket->order_ref = $vendorTxCode;

$stuff = "VendorTxCode=" . $vendorTxCode . "&";
$stuff .= "Amount=" . sprintf("%.2f", $newbasket->cost_total) . "&";
$stuff .= "Currency=GBP&";
$stuff .= "ReferrerID=968FB4B0-E929-45E4-A5BC-41A4FFE873F2&";
$stuff .= "Description=" . config('app.name') . " Merchandise&";
$stuff .= "SuccessURL=" . SITE_URL . "/shop/checkout/sagepay/order_protx.php&";
$stuff .= "FailureURL=" . SITE_URL . "/shop/checkout/sagepay/order_protx.php&";
$stuff .= "VendorEMail=" . SAGEPAY_EMAIL . "&";
$stuff .= "CustomerEmail=" . $newbasket->info['email'] . "&";
$stuff .= "CustomerName=" . stripslashes($newbasket->address['billing']['firstname']) . " " . stripslashes($newbasket->address['billing']['lastname']) . "&";
$stuff .= "BillingSurname=" . stripslashes($newbasket->address['billing']['lastname']) . "&";
$stuff .= "BillingFirstnames=" . stripslashes($newbasket->address['billing']['firstname']) . "&";
$stuff .= "BillingAddress1=" . stripslashes($newbasket->address['billing']['address1']) . "\n";
if ($newbasket->address['billing']['address2'] != "") {
    $stuff .= "&BillingAddress2=" . stripslashes($newbasket->address['billing']['address2']);
}
$stuff .= "&BillingCity=" . stripslashes($newbasket->address['billing']['city']) . "\n";
$stuff .= "&BillingCountry=" . stripslashes($newbasket->address['billing']['country']);
if ($newbasket->address['billing']['state'] != "") {
    $stuff .= "&BillingState=" . stripslashes($newbasket->address['billing']['state']);
}
$stuff .= "&BillingPostCode=" . stripslashes($newbasket->address['billing']['postcode']) . "&";
$stuff .= "DeliverySurname=" . stripslashes($newbasket->address['shipping']['lastname']) . "&";
$stuff .= "DeliveryFirstnames=" . stripslashes($newbasket->address['shipping']['firstname']) . "&";
$stuff .= "DeliveryAddress1=" . stripslashes($newbasket->address['shipping']['address1']) . "\n";
if ($newbasket->address['shipping']['address2'] != "") {
    $stuff .= "&DeliveryAddress2=" . stripslashes($newbasket->address['shipping']['address2']);
}
$stuff .= "&DeliveryCity=" . stripslashes($newbasket->address['shipping']['city']) . "\n";
$stuff .= "&DeliveryCountry=" . trim($newbasket->address['shipping']['country']);
if ($newbasket->address['shipping']['state'] != "") {
    $stuff .= "&DeliveryState=" . trim($newbasket->address['shipping']['state']);
}
$stuff .= "&DeliveryPostCode=" . stripslashes($newbasket->address['shipping']['postcode']) . "&";
$stuff .= "ContactNumber=" . stripslashes($newbasket->info['contact_no']) . "&";
$stuff .= "ContactFax=";

if (empty($EncryptionPassword)) {
    // If you get this error, you most likely don't have SAGEPAY_ENCRYPTION
    // defined.
    throw new Exception("No Encryption Password defined.");
}

$crypt = encryptAndEncode($stuff, $EncryptionPassword);
?>

<form action="<?= $vspsite ?>" method="post" name="form1" id="SagePayForm">

    <input type="hidden" name="VPSProtocol" value="<?= SAGEPAY_PROTOCOL ?>" />
    <input type="hidden" name="TxType" value="PAYMENT" />
    <input type="hidden" name="Vendor" value=<?= $VendorName ?> />
    <input type="hidden" name="Crypt" value="<?= $crypt ?>" />

    <button type="submit" name="payment" class="checkoutButton">
        Make payment securely <i class="fa fa-lock"></i>
    </button>

</form>

<?php

if (!SAGEPAY_LIVE && !SAGEPAY_TEST) {
    ?>
    <div class="error">
        This site is currently processing orders through the Sagepay DEVELOPER SIMULATOR!
    </div>
    <?php
} elseif (!SAGEPAY_LIVE && SAGEPAY_TEST) {
    ?>
    <div class="error">
        This site is currently processing orders through the Sagepay TEST gateway!
    </div>
    <?php
}
