Merge pull request #810 from twetech/client-pays-fees

Enable passing stripe fee to clients
This commit is contained in:
Johnny 2023-11-20 16:49:16 -05:00 committed by GitHub
commit 26b439554b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 117 additions and 7 deletions

View File

@ -1498,12 +1498,12 @@ if (LATEST_DATABASE_VERSION > CURRENT_DATABASE_VERSION) {
}
//if (CURRENT_DATABASE_VERSION == '0.9.4') {
if (CURRENT_DATABASE_VERSION == '0.9.4') {
// Insert queries here required to update to DB version 0.9.5
mysqli_query($mysqli, "ALTER TABLE `settings` ADD `config_stripe_client_pays_fees` TINYINT(1) NOT NULL DEFAULT 0 AFTER `config_stripe_account`");
// Then, update the database to the next sequential version
// mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '0.9.5'");
//}
mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '0.9.5'");
}
} else {
// Up-to-date
}

View File

@ -5,5 +5,5 @@
* It is used in conjunction with database_updates.php
*/
DEFINE("LATEST_DATABASE_VERSION", "0.9.4");
DEFINE("LATEST_DATABASE_VERSION", "0.9.5");

1
db.sql
View File

@ -1267,6 +1267,7 @@ CREATE TABLE `settings` (
`config_stripe_publishable` varchar(255) DEFAULT NULL,
`config_stripe_secret` varchar(255) DEFAULT NULL,
`config_stripe_account` tinyint(1) NOT NULL DEFAULT 0,
`config_stripe_client_pays_fees` tinyint(1) NOT NULL DEFAULT 0,
`config_azure_client_id` varchar(200) DEFAULT NULL,
`config_azure_client_secret` varchar(200) DEFAULT NULL,
`config_module_enable_itdoc` tinyint(1) NOT NULL DEFAULT 1,

View File

@ -84,6 +84,7 @@ $config_stripe_enable = intval($row['config_stripe_enable']);
$config_stripe_publishable = $row['config_stripe_publishable'];
$config_stripe_secret = $row['config_stripe_secret'];
$config_stripe_account = $row['config_stripe_account'];
$config_stripe_client_pays_fees = intval($row['config_stripe_client_pays_fees']);
// Modules
$config_module_enable_itdoc = intval($row['config_module_enable_itdoc']);

View File

@ -51,12 +51,28 @@ if (isset($_GET['stripe_create_pi'])) {
$client_id = intval($row['client_id']);
$client_name = nullable_htmlentities($row['client_name']);
$config_sql = mysqli_query($mysqli, "SELECT * FROM settings WHERE company_id = 1");
$config_row = mysqli_fetch_array($config_sql);
$config_stripe_client_pays_fees = intval($config_row['config_stripe_client_pays_fees']);
// Add up all the payments for the invoice and get the total amount paid to the invoice
$sql_amount_paid = mysqli_query($mysqli, "SELECT SUM(payment_amount) AS amount_paid FROM payments WHERE payment_invoice_id = $invoice_id");
$row = mysqli_fetch_array($sql_amount_paid);
$amount_paid = floatval($row['amount_paid']);
$balance_to_pay = $invoice_amount - $amount_paid;
// Check config to see if client pays fees is enabled
if ($config_stripe_client_pays_fees == 1) {
// Get fees from config
$percentage_fee = 0.029; // Default Stripe fee
$flat_fee = 0.30; // Default Stripe fee
// Calculate the amount to charge the client
$balance_to_pay = ($balance_to_pay + $flat_fee) / (1 - $percentage_fee);
}
$balance_to_pay = round($balance_to_pay, 2);
if (intval($balance_to_pay) == 0) {
exit("No balance outstanding");
}

View File

@ -2,6 +2,14 @@
require_once 'guest_header.php';
function log_to_console($message)
{
$message = date("H:i:s") . " - $message - ".PHP_EOL;
print($message);
flush();
ob_flush();
}
// Define wording
DEFINE("WORDING_PAYMENT_FAILED", "<br><h2>There was an error verifying your payment. Please contact us for more information.</h2>");
@ -66,6 +74,7 @@ if (isset($_GET['invoice_id'], $_GET['url_key']) && !isset($_GET['payment_intent
$sql = mysqli_query($mysqli, "SELECT * FROM companies, settings WHERE companies.company_id = settings.company_id AND companies.company_id = 1");
$row = mysqli_fetch_array($sql);
$company_locale = nullable_htmlentities($row['company_locale']);
$config_stripe_client_pays_fees = intval($row['config_stripe_client_pays_fees']);
// Add up all the payments for the invoice and get the total amount paid to the invoice
$sql_amount_paid = mysqli_query($mysqli, "SELECT SUM(payment_amount) AS amount_paid FROM payments WHERE payment_invoice_id = $invoice_id");
@ -73,6 +82,21 @@ if (isset($_GET['invoice_id'], $_GET['url_key']) && !isset($_GET['payment_intent
$amount_paid = floatval($row['amount_paid']);
$balance_to_pay = $invoice_amount - $amount_paid;
// Check config to see if client pays fees is enabled
if ($config_stripe_client_pays_fees == 1) {
$balance_before_fees = $balance_to_pay;
$percentage_fee = 0.029;
$flat_fee = 0.30;
// Calculate the amount to charge the client
$balance_to_pay = ($balance_to_pay + $flat_fee) / (1 - $percentage_fee);
// Calculate the fee amount
$gateway_fee = round($balance_to_pay - $balance_before_fees, 2);
}
//Round balance to pay to 2 decimal places
$balance_to_pay = round($balance_to_pay, 2);
// Get invoice items
$sql_invoice_items = mysqli_query($mysqli, "SELECT * FROM invoice_items WHERE item_invoice_id = $invoice_id ORDER BY item_id ASC");
@ -119,8 +143,18 @@ if (isset($_GET['invoice_id'], $_GET['url_key']) && !isset($_GET['payment_intent
<td class="text-right"><?php echo numfmt_format_currency($currency_format, $item_total, $invoice_currency_code); ?></td>
</tr>
<?php }
if ($config_stripe_client_pays_fees == 1) { ?>
<tr>
<td>Gateway Fees</td>
<td class="text-center">-</td>
<td class="text-right"><?php echo numfmt_format_currency($currency_format, $gateway_fee, $invoice_currency_code); ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
@ -243,6 +277,17 @@ if (isset($_GET['invoice_id'], $_GET['url_key']) && !isset($_GET['payment_intent
$amount_paid_previously = $row['amount_paid'];
$balance_to_pay = $invoice_amount - $amount_paid_previously;
// Check config to see if client pays fees is enabled
if ($config_stripe_client_pays_fees == 1) {
$percentage_fee = 0.029;
$flat_fee = 0.30;
// Calculate the amount to charge the client
$balance_to_pay = ($balance_to_pay + $flat_fee) / (1 - $percentage_fee);
}
// Round balance to pay to 2 decimal places
$balance_to_pay = round($balance_to_pay, 2);
// Sanity check that the amount paid is exactly the invoice outstanding balance
if (intval($balance_to_pay) !== intval($pi_amount_paid)) {
exit("Something went wrong confirming this payment. Please get in touch.");
@ -257,6 +302,11 @@ if (isset($_GET['invoice_id'], $_GET['url_key']) && !isset($_GET['payment_intent
mysqli_query($mysqli, "INSERT INTO payments SET payment_date = '$pi_date', payment_amount = $pi_amount_paid, payment_currency_code = '$pi_currency', payment_account_id = $config_stripe_account, payment_method = 'Stripe', payment_reference = 'Stripe - $pi_id', payment_invoice_id = $invoice_id");
mysqli_query($mysqli, "INSERT INTO history SET history_status = 'Paid', history_description = 'Payment added - $ip - $os - $browser', history_invoice_id = $invoice_id");
// Add Gateway fees to history if applicable
if ($config_stripe_client_pays_fees == 1) {
mysqli_query($mysqli, "INSERT INTO history SET history_status = 'Paid', history_description = 'Gateway fees of $gateway_fee has been billed', history_invoice_id = $invoice_id");
}
// Notify
mysqli_query($mysqli, "INSERT INTO notifications SET notification_type = 'Invoice Paid', notification = 'Invoice $invoice_prefix$invoice_number has been paid - $ip - $os - $browser', notification_action = 'invoice.php?invoice_id=$invoice_id', notification_client_id = $pi_client_id");
@ -265,8 +315,13 @@ if (isset($_GET['invoice_id'], $_GET['url_key']) && !isset($_GET['payment_intent
if (!$pi_livemode) {
$extended_log_desc = '(DEV MODE)';
}
if ($config_stripe_client_pays_fees == 1) {
$extended_log_desc .= ' (Client Pays Fees [' . numfmt_format_currency($currency_format, $gateway_fee, $invoice_currency_code) . ']])';
}
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Payment', log_action = 'Create', log_description = 'Stripe payment of $pi_currency $pi_amount_paid against invoice $invoice_prefix$invoice_number - $pi_id $extended_log_desc', log_ip = '$ip', log_user_agent = '$user_agent', log_client_id = $pi_client_id");
// Send email receipt
$sql_settings = mysqli_query($mysqli, "SELECT * FROM settings WHERE company_id = 1");
$row = mysqli_fetch_array($sql_settings);

View File

@ -80,6 +80,7 @@ if (!empty($company_logo)) {
$company_locale = nullable_htmlentities($row['company_locale']);
$config_invoice_footer = nullable_htmlentities($row['config_invoice_footer']);
$config_stripe_enable = intval($row['config_stripe_enable']);
$config_stripe_client_pays_fees = intval($row['config_stripe_client_pays_fees']);
//Set Currency Format
$currency_format = numfmt_create($company_locale, NumberFormatter::CURRENCY);
@ -110,6 +111,15 @@ $amount_paid = floatval($row['amount_paid']);
$balance = $invoice_amount - $amount_paid;
// Check config to see if client pays fees is enabled
if ($config_stripe_client_pays_fees == 1) {
$percentage_fee = 0.029;
$flat_fee = 0.30;
// Calculate the amount to charge the client
$balance_to_pay = ($balance + $flat_fee) / (1 - $percentage_fee);
$stripe_fee = $balance_to_pay - $balance;
}
//check to see if overdue
$invoice_color = $invoice_badge_color; // Default
if ($invoice_status !== "Paid" && $invoice_status !== "Draft" && $invoice_status !== "Cancelled") {
@ -293,6 +303,12 @@ $sql_invoice_items = mysqli_query($mysqli, "SELECT * FROM invoice_items WHERE it
<td class="text-right"><strong><?php echo numfmt_format_currency($currency_format, $balance, $invoice_currency_code); ?></strong></td>
</tr>
</tbody>
<?php if (isset($stripe_fee)) {?>
<tr class="border-bottom">
<td>Gateway Fee:</td>
<td class="text-right"><?php echo numfmt_format_currency($currency_format, $stripe_fee, $invoice_currency_code); ?></td>
</tr>
<?php } ?>
</table>
</div>
</div>

View File

@ -1098,3 +1098,19 @@ if (isset($_GET['update_db'])) {
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if (isset($_POST['config_stripe_client_pays_fees'])) {
validateAdminRole();
$config_stripe_client_pays_fees = intval($_POST['config_stripe_client_pays_fees']);
mysqli_query($mysqli,"UPDATE settings SET config_stripe_client_pays_fees = $config_stripe_client_pays_fees WHERE company_id = 1");
//Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Settings', log_action = 'Modify', log_description = '$session_name modified stripe client pays fees', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id");
$_SESSION['alert_message'] = "Stripe client pays fees updated";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}

View File

@ -61,10 +61,15 @@ require_once "inc_all_settings.php";
}
?>
</select>
</div>
</div>
<div class="form-group">
<label>Client Pays Fees</label>
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" name="config_stripe_client_pays_fees" <?php if ($config_stripe_client_pays_fees == 1) { echo "checked"; } ?> value="1" id="clientPaysFeesSwitch">
<label class="custom-control-label" for="clientPaysFeesSwitch">Enable</label>
</div>
<?php } ?>