Files
HotLunches/payment_form.php
T
2026-10-19 15:22:52 -07:00

543 lines
20 KiB
PHP

<?php
if (!isset($PAYMENT_FORM_INCLUDED)) $PAYMENT_FORM_INCLUDED = true;
else if ($PAYMENT_FORM_INCLUDED) return;
if (!isset($user_num)) $user_num = intval($_SESSION['UserInfo']['record_num']);
unset($selected_amount);
// Cast to a 2-decimal number: this value is placed directly into SQL below.
if (isset($naction) && $naction == 'deposit' && array_key_exists('selected_amount',$_POST)) $selected_amount = round((float)$_POST['selected_amount'], 2);
?>
<table width='500' cellpadding='5' cellspacing='2' border='0'>
<tr>
<td align='center'>
<?php
// Get processing timestamp
$now = time();
// Get current balance
$balance = db_getVal("select sum(transaction_amount) as balance from user_account where school_num = $school_num and user_num = $user_num AND (deleted != 'Y' OR deleted IS NULL)");
if (!$balance) $balance = 0.00;
$onlineMessage = db_getVal("SELECT text_data FROM text_data WHERE school_num = $school_num AND text_id = 'Online Payment Message'");
if (strlen($onlineMessage)) $onlineMessage .= "<br><br>\n";
switch ($school_num) {
case 251:
case 19438:
$onlineMessage .= "-- Please note that Interac Debit cards are not accepted --";
break;
default:
$onlineMessage .= "-- Please note that MasterCard Debit and Interac Debit cards are not accepted --";
break;
}
$onlineMessageDisplayed = false;
//
// School account processing
//
if ($balance > 0 && !isset($selected_amount)) {
if ($school_record['school_account_enabled'] == 'Y') {
?>
<table width='400' class='content-table'>
<tr>
<td align='center' class='warning'>
<?php if (strlen($school_record['school_account_title'])) echo("<p><b>" . $school_record['school_account_title'] . "</b></p>\n"); ?>
<?php if (strlen($school_record['school_account_text'])) echo("<p>" . $school_record['school_account_text'] . "</p>\n"); ?>
<?php echo("<p><b>" . getLanguageText('TEXT-BALANCE') . ": $currencySymbol" . number_format($balance,2) . "</b></p>\n"); ?>
<p><a href='/?action=charge-school-account' balance='<?php echo($balance) ?>' id='charge-school-account' class='btn btn--primary>'><?php echo($school_record['school_account_button_text']) ?></a></p>
</td>
</tr>
</table>
<br>
<br>
<script>
$(document).ready(function(e) {
<?php include "include/ajax.Setup.php" ?>
$("#charge-school-account").click(function(e) {
e.preventDefault();
var balance = $(this).attr('balance');
var msg = "<?php echo($school_record['school_account_message']) ?>";
msg = msg + "\n\n<?php displayLanguageText('TEXT-AMOUNT'); ?>: <?php echo($currencySymbol) ?>" + balance;
var ans = confirm(msg);
if (ans) {
var data = "&user_num=<?php echo($_SESSION['UserInfo']['record_num']) ?>&balance="+balance;
$.ajax({
url: "ajax.ChargeAccount.php",
data: data
}).done(function(data) {
window.location = '/';
});
}
});
});
</script>
<?php
}
}
//
// Bambora Form
//
if ($school_record['bambora_enabled'] == 'Y' && strlen($school_record['bambora_merchant_id']) > 0) {
// Get current balance
$balance = db_getVal("select sum(transaction_amount) as balance from user_account where school_num = $school_num and user_num = $user_num AND (deleted != 'Y' OR deleted IS NULL)");
if (!$balance) $balance = 0.00;
if (isset($selected_amount) && $selected_amount > 0) $balance = $selected_amount;
if ($balance >= $school_record['bambora_minimum_amount']) {
$trx_time = date("Y-m-d H:i:s",$now);
// Calculate the transaction fee
$trx_time = date("Y-m-d H:i:s",$now);
$trx_fee = intval($balance * $school_record['bambora_percentage']) / 100 + $school_record['bambora_fee'];
$owing = $balance + $trx_fee;
if (!$onlineMessageDisplayed && strlen($onlineMessage)) {
displayStatusMessage($onlineMessage,"warningframedspan");
$onlineMessageDisplayed = true;
}
// Calculate any payment discounts
// Check for an entry to the online payments table
$query = "select * from user_online_payments " .
"where school_num = $school_num " .
"and user_num = $user_num " .
"and status = 'Pending'";
$results = db_query($query);
if (db_num_rows($results) > 0) {
$row = db_fetch_array($results);
$trxnum = $row['record_num'];
$query = "update user_online_payments " .
"set user_account_balance = $balance, " .
"transaction_fee = $trx_fee, " .
"balance_owing = $owing, " .
"transaction_datetime = '$trx_time', " .
"transaction_time = $now " .
"where school_num = $school_num " .
"and user_num = $user_num " .
"and status = 'Pending'";
db_query($query);
}
else {
$query = "insert into user_online_payments (school_num,user_num,transaction_datetime,transaction_time,user_account_balance,transaction_fee,balance_owing,status) " .
"values ($school_num,$user_num,'$trx_time',$now,$balance,$trx_fee,$owing,'Pending')";
db_query($query);
$trxnum = db_insert_id();
}
// If there is a transaction fee
if ($trx_fee != 0 || strlen(trim($school_record['bambora_fee_message'])) > 0) {
$trx_title = getLanguageText('TEXT-TRX-FEE');
if (strlen($school_record['bambora_fee_title'])) $trx_title = $school_record['bambora_fee_title'];
?>
<table width='100%' class='content-table'>
<tr>
<td align='center' class='warning'>
<br>
<?php if (strlen(trim($school_record['bambora_fee_message']))) { ?>
<b><?php echo(nl2br(trim($school_record['bambora_fee_message']))); ?></b><br>
<br>
<?php } ?>
<?php if ($trx_fee != 0) { ?>
<?php if (isset($selected_amount) && $selected_amount > 0) { ?>
<b><nobr><?php echo(getLanguageText('TEXT-DEPOSIT-AMOUNT') . ": " . $currencySymbol . number_format($owing,2)) ?></nobr></b><br>
<?php } else { ?>
<b><nobr><?php echo(getLanguageText('TEXT-ACCOUNT-BALANCE') . ": " . $currencySymbol . number_format($owing,2)) ?></nobr></b><br>
<?php } ?>
<b><nobr><?php echo($trx_title . ": " . $currencySymbol . number_format($trx_fee,2)) ?></nobr></b>
<br>
<br>
<?php } ?>
</td>
</tr>
</table>
<br>
<?php
}
if ($school_num == 0) include "bambora_form2.php";
else {
if ($school_record['display_bambora_form'] != 'Link') include "bambora_form.php";
else {
echo("<a href='?action=bambora-payment' class='btn btn--primary>'>".getLanguageText('TEXT-CREDIT-CARD-PAYMENT')."</a>");
}
}
echo("<br>\n");
echo("<br>\n");
echo("<br>\n");
echo("<br>\n");
}
}
//
// PayPal Account Payments
//
if ($school_record['paypal_enabled'] == 'Y' && strlen($school_record['paypal_code']) > 0) {
// Get current balance
$balance = db_getVal("select sum(transaction_amount) as balance from user_account where school_num = $school_num and user_num = $user_num AND (deleted != 'Y' OR deleted IS NULL)");
if (!$balance) $balance = 0.00;
if (isset($selected_amount) && $selected_amount > 0) $balance = $selected_amount;
if ($balance >= $school_record['paypal_minimum_amount']) {
$trx_time = date("Y-m-d H:i:s",$now);
// if (!$onlineMessageDisplayed && strlen($onlineMessage)) {
// displayStatusMessage($onlineMessage,"warningframedspan");
// $onlineMessageDisplayed = true;
// }
// Calculate the transaction fee
$trx_time = date("Y-m-d H:i:s",$now);
$trx_fee = intval($balance * $school_record['paypal_percentage']) / 100 + $school_record['paypal_fee'];
$owing = $balance + $trx_fee;
// Calculate any payment discounts
// Check for an entry to the online payments table
$query = "select * from user_online_payments " .
"where school_num = $school_num " .
"and user_num = $user_num " .
"and status = 'Pending'";
$results = db_query($query);
if (db_num_rows($results) > 0) {
$row = db_fetch_array($results);
$trxnum = $row['record_num'];
$query = "update user_online_payments " .
"set user_account_balance = $balance, " .
"transaction_fee = $trx_fee, " .
"balance_owing = $owing, " .
"transaction_datetime = '$trx_time', " .
"transaction_time = $now " .
"where school_num = $school_num " .
"and user_num = $user_num " .
"and status = 'Pending'";
db_query($query);
}
else {
$query = "insert into user_online_payments (school_num,user_num,transaction_datetime,transaction_time,user_account_balance,transaction_fee,balance_owing,status) " .
"values ($school_num,$user_num,'$trx_time',$now,$balance,$trx_fee,$owing,'Pending')";
db_query($query);
$trxnum = db_insert_id();
}
$message = getLanguageText('TEXT-PAYPAL-DELAY-MSG');
// displayStatusMessage($message,"warningframedspan");
// If there is a transaction fee
if ($trx_fee != 0) {
$trx_title = getLanguageText('TEXT-TRX-FEE');
if (strlen($school_record['paypal_fee_title'])) $trx_title = $school_record['paypal_fee_title'];
?>
<table width='400' class='content-table'>
<tr>
<td align='center' class='warning'>
<br>
<?php if (strlen(trim($school_record['paypal_fee_message']))) { ?>
<b><?php echo(nl2br(trim($school_record['paypal_fee_message']))); ?></b><br>
<br>
<?php } ?>
<?php if (isset($selected_amount) && $selected_amount > 0) { ?>
<b><nobr><?php echo(getLanguageText('TEXT-DEPOSIT-AMOUNT') . ": " . $currencySymbol . number_format($balance,2)) ?></nobr></b><br>
<?php } else { ?>
<b><nobr><?php echo(getLanguageText('TEXT-ACCOUNT-BALANCE') . ": " . $currencySymbol . number_format($balance,2)) ?></nobr></b><br>
<?php } ?>
<b><nobr><?php echo($trx_title . ": " . $currencySymbol . number_format($trx_fee,2)) ?></nobr></b>
<br>
<br>
</td>
</tr>
</table>
<br>
<?php
}
// Single-account PayPal / credit card button, using PayPal Payments
// Standard: posts straight to PayPal's hosted checkout with the
// school's paypal_code as the receiving account (no client id/secret).
// Always shown alongside the button above, regardless of onboarding status.
// Buffered + wrapped so a failure here is visible in View Source
// instead of silently vanishing or breaking the rest of the page.
$oldPpBtnFile = __DIR__ . "/paypal_standard_button.php";
echo "<!-- old-paypal-button: file=" . htmlspecialchars($oldPpBtnFile, ENT_QUOTES) . " exists=" . (is_file($oldPpBtnFile) ? 'yes' : 'NO') . " -->\n";
try {
ob_start();
include $oldPpBtnFile;
$oldPpBtnOut = ob_get_clean();
echo "<!-- old-paypal-button: rendered " . strlen($oldPpBtnOut) . " bytes -->\n";
echo $oldPpBtnOut;
} catch (\Throwable $e) {
if (ob_get_level() > 0) ob_end_clean();
echo "<!-- old-paypal-button: ERROR " . htmlspecialchars($e->getMessage(), ENT_QUOTES) . " in " . htmlspecialchars($e->getFile(), ENT_QUOTES) . ":" . $e->getLine() . " -->\n";
}
echo("<br>\n");
}
} // End of PayPal Code
//
// Square Account Payments
// Deploy: this file lives at / (home). Square lives at /square/.
// The "Pay with Square" button appears ONLY when this school is onboarded with
// Square (connected, not revoked, Square location able to take payments); for
// every other school nothing is rendered. Payments are charged with the
// school's own Square token, so the money goes to that school's Square account.
// Buffered + wrapped like the PayPal button so a failure here can never break
// the rest of the payment page.
//
$sqRoot = rtrim((string)($_SERVER['DOCUMENT_ROOT'] ?? ''), '/\\');
$sq_button = $sqRoot . '/square/square_button.php';
if (!is_file($sq_button) && is_file(__DIR__ . '/square/square_button.php')) {
$sq_button = __DIR__ . '/square/square_button.php';
}
if (!is_file($sq_button) && is_file(__DIR__ . '/../square_button.php')) {
$sq_button = __DIR__ . '/../square_button.php';
}
if (is_file($sq_button)) {
try {
ob_start();
include $sq_button;
$sqBtnOut = ob_get_clean();
if (strpos($sqBtnOut, 'sq-pay-btn') !== false) $sqBtnOut .= "<br>\n";
echo $sqBtnOut;
} catch (\Throwable $e) {
if (ob_get_level() > 0) ob_end_clean();
if (function_exists('writeLog')) writeLog('square button error: ' . $e->getMessage());
echo "\n<!-- sq-button hidden: error -->\n";
}
} else {
echo "\n<!-- sq-button hidden: square_button.php not found (looked in /square/) -->\n";
}
//
// E-xact Account Payments
//
if ($school_record['exact_enabled'] == 'Y' && strlen($school_record['exact_code']) > 0) {
// Calculate the transaction fee
$trx_time = date("Y-m-d H:i:s",$now);
$trx_fee = intval($balance * $school_record['exact_percentage']) / 100 + $school_record['exact_fee'];
$owing = $balance + $trx_fee;
// Check for an entry to the online payments table
$query = "select * from user_online_payments " .
"where school_num = $school_num " .
"and user_num = $user_num " .
"and status = 'Pending'";
$results = db_query($query);
if (db_num_rows($results) > 0) {
$row = db_fetch_array($results);
$trxnum = $row['record_num'];
$query = "update user_online_payments " .
"set user_account_balance = $balance, " .
"transaction_fee = $trx_fee, " .
"balance_owing = $owing, " .
"transaction_datetime = '$trx_time', " .
"transaction_time = $now " .
"where school_num = $school_num " .
"and user_num = $user_num " .
"and status = 'Pending'";
db_query($query);
}
else {
$query = "insert into user_online_payments (school_num,user_num,transaction_datetime,transaction_time,user_account_balance,transaction_fee,balance_owing,status) " .
"values ($school_num,$user_num,'$trx_time',$now,$balance,$trx_fee,$owing,'Pending')";
db_query($query);
$trxnum = db_insert_id();
}
// If there is a transaction fee
if ($trx_fee > 0) {
?>
<table width='300' class='content-table'>
<tr>
<td align='center' class='warning'>
<br>
<?php if (strlen(trim($school_record['exact_fee_message']))) { ?>
<b><?php echo(nl2br(trim($school_record['exact_fee_message']))); ?></b><br>
<br>
<?php } ?>
<b><nobr><?php echo(getLanguageText('TEXT-ACCOUNT-BALANCE') . ": " . $currencySymbol . number_format($balance,2)) ?></nobr></b><br>
<b><nobr><?php echo(getLanguageText('TEXT-TRX-FEE') . ": " . $currencySymbol . number_format($trx_fee,2)) ?></nobr></b>
<br>
<br>
</td>
</tr>
</table>
<br>
<?php
}
include "exact_form.php";
echo("<br>\n");
} // End of Exact code
//
// Merchant Account Payments
//
if ($school_record['merchant_account_enabled'] != 'None' && strlen($school_record['merchant_account_key_id']) > 0) {
// Get current balance
$balance = db_getVal("select sum(transaction_amount) as balance from user_account where school_num = $school_num and user_num = $user_num AND (deleted != 'Y' OR deleted IS NULL)");
if (!$balance) $balance = 0.00;
$owing = $balance;
// Calculate any payment discounts
$today = date("Y-m-d",$now);
$payment_discount = 0;
$query = "SELECT * FROM payment_discounts WHERE school_num = $school_num AND start_date <= '$today' AND end_date >= '$today'";
$results = db_query($query);
if (db_num_rows($results)) {
$row = db_fetch_assoc($results);
$discount_type = $row['discount_type'];
$discount = $row['discount'];
$discount_message = $row['discount_message'];
switch ($discount_type) {
case 'Amount':
$payment_discount = $discount;
break;
case 'Percentage':
$payment_discount = round($owing * $discount) / 100;
break;
}
$owing = $owing - $payment_discount;
}
// Calculate the transaction fee
$trx_time = date("Y-m-d H:i:s",$now);
$trx_fee = intval($owing * $school_record['merchant_account_percentage']) / 100 + $school_record['merchant_account_fee'];
$owing = $owing + $trx_fee;
// Check for an entry to the online payments table
$query = "select * from user_online_payments " .
"where school_num = $school_num " .
"and user_num = $user_num " .
"and status = 'Pending'";
$results = db_query($query);
if (db_num_rows($results) > 0) {
$row = db_fetch_array($results);
$trxnum = $row['record_num'];
$query = "update user_online_payments " .
"set user_account_balance = $balance, " .
"payment_discount = $payment_discount, " .
"transaction_fee = $trx_fee, " .
"balance_owing = $owing, " .
"transaction_datetime = '$trx_time', " .
"transaction_time = $now " .
"where school_num = $school_num " .
"and user_num = $user_num " .
"and status = 'Pending'";
db_query($query);
}
else {
$query = "insert into user_online_payments (school_num,user_num,transaction_datetime,transaction_time,user_account_balance,payment_discount,transaction_fee,balance_owing,status) " .
"values ($school_num,$user_num,'$trx_time',$now,$balance,$payment_discount,$trx_fee,$owing,'Pending')";
db_query($query);
$trxnum = db_insert_id();
}
// If there is a payment discount message
if ($payment_discount > 0) {
?>
<table width='300' class='content-table'>
<tr>
<td align='center' class='success'>
<br>
<?php if (strlen($discount_message)) { ?>
<b><?php echo(nl2br(trim($discount_message))); ?></b><br>
<br>
<?php } ?>
<table cellpadding='2' cellspacing='0' border='0'>
<tr>
<td align='right'><?php displayLanguageText('TEXT-DISCOUNT'); ?>:&nbsp;&nbsp;</td>
<td align='right'><?php echo('-' . $currencySymbol . number_format($payment_discount,2)) ?></td>
</tr>
<tr>
<td align='right'><?php displayLanguageText('TEXT-BALANCE'); ?>:&nbsp;&nbsp;</td>
<td align='right'><?php echo($currencySymbol . number_format($owing,2)) ?></td>
</tr>
</table>
<br>
</td>
</tr>
</table>
<br>
<?php
}
// If there is a transaction fee
if ($trx_fee > 0) {
?>
<table width='300' class='content-table'>
<tr>
<td align='center' class='warning'>
<br>
<?php if (strlen(trim($school_record['merchant_account_fee_message']))) { ?>
<b><?php echo(nl2br(trim($school_record['merchant_account_fee_message']))); ?></b><br>
<br>
<?php } ?>
<b><nobr><?php echo(getLanguageText('TEXT-ACCOUNT-BALANCE') . ": " . $currencySymbol . number_format($balance,2)) ?></nobr></b><br>
<b><nobr><?php echo(getLanguageText('TEXT-TRX-FEE') . ": " . $currencySymbol . number_format($trx_fee,2)) ?></nobr></b>
<br>
<br>
</td>
</tr>
</table>
<br>
<?php
}
include "merchant_account_form.php";
echo("<br>\n");
} // End of Merchant Account code
if ($balance > 0 && !isset($selected_amount)) {
if ($school_record['display_remittance_button'] == 'Y') {
$btnText = trim($school_record['remittance_button_text']);
if (strlen($btnText) == 0) $btnText = getLanguageText('TEXT-PRINT-REMITTANCE');
?>
<br>
<form method='post' action='/'>
<input type='submit' value='<?php echo($btnText) ?>' class='btn btn--primary'>
<input type='hidden' name='action' value='print-remittance-form'>
</form>
<br>
<?php
if ($school_record['display_remittance_msg_with_button'] == 'Y') {
$text = db_getVal("SELECT text_data FROM text_data WHERE school_num = $school_num AND text_id = 'Remittance Form'");
if (strlen($text) > 0) {
echo("<p>" . nl2br($text) . "</p>\n");
}
}
}
}
?>
</td>
</tr>
</table>