From cc5d2745963fbd728cdf5bd5c0d1872da7e1c1fd Mon Sep 17 00:00:00 2001 From: Andrew Malsbury Date: Fri, 17 Nov 2023 22:44:10 +0000 Subject: [PATCH 1/3] Update database version to 0.9.5 and add config_stripe_client_pays_fees field --- database_updates.php | 8 ++++---- database_version.php | 2 +- db.sql | 1 + guest_ajax.php | 11 +++++++++++ post/setting.php | 16 ++++++++++++++++ settings_online_payment.php | 11 +++++++++-- 6 files changed, 42 insertions(+), 7 deletions(-) diff --git a/database_updates.php b/database_updates.php index 9bf550a7..a822b609 100644 --- a/database_updates.php +++ b/database_updates.php @@ -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 } diff --git a/database_version.php b/database_version.php index 102f8005..e8349860 100644 --- a/database_version.php +++ b/database_version.php @@ -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"); diff --git a/db.sql b/db.sql index a0da4305..e135890a 100644 --- a/db.sql +++ b/db.sql @@ -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, diff --git a/guest_ajax.php b/guest_ajax.php index 5eb12b39..47418c55 100644 --- a/guest_ajax.php +++ b/guest_ajax.php @@ -57,6 +57,17 @@ if (isset($_GET['stripe_create_pi'])) { $amount_paid = floatval($row['amount_paid']); $balance_to_pay = $invoice_amount - $amount_paid; + // Check config to see if client pays fees is enabled + $row = mysqli_fetch_array(mysqli_query($mysqli, "SELECT config_stripe_client_pays_fees FROM settings WHERE company_id = 1")); + if ($row['config_client_pays_fees'] == 1) { + // Get fees from config + $row = mysqli_fetch_array(mysqli_query($mysqli, "SELECT config_stripe_percentage_fee, config_stripe_flat_fee FROM settings WHERE company_id = 1")); + $percentageFee = floatval($row['config_stripe_percentage_fee']); + $flatFee = floatval($row['config_stripe_flat_fee']); + // Calculate the amount to charge the client + $balance_to_pay = ($balance_to_pay + $flatFee) / (1 - $percentageFee); + } + if (intval($balance_to_pay) == 0) { exit("No balance outstanding"); } diff --git a/post/setting.php b/post/setting.php index bba3113a..3df54650 100644 --- a/post/setting.php +++ b/post/setting.php @@ -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"]); +} diff --git a/settings_online_payment.php b/settings_online_payment.php index 8e20148e..1dc55733 100644 --- a/settings_online_payment.php +++ b/settings_online_payment.php @@ -2,6 +2,8 @@ require_once "inc_all_settings.php"; +// Get Stripe Setting for config client pays + ?> @@ -61,10 +63,15 @@ require_once "inc_all_settings.php"; } ?> - - +
+ +
+ value="1" id="clientPaysFeesSwitch"> + +
+ From 6ab96b3b1618f900abae36787d068d1c95266e55 Mon Sep 17 00:00:00 2001 From: Andrew Malsbury Date: Mon, 20 Nov 2023 16:42:11 +0000 Subject: [PATCH 2/3] Update settings --- get_settings.php | 1 + settings_online_payment.php | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/get_settings.php b/get_settings.php index b05d5268..7cd0d233 100644 --- a/get_settings.php +++ b/get_settings.php @@ -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']); diff --git a/settings_online_payment.php b/settings_online_payment.php index 1dc55733..f8eb017e 100644 --- a/settings_online_payment.php +++ b/settings_online_payment.php @@ -2,8 +2,6 @@ require_once "inc_all_settings.php"; -// Get Stripe Setting for config client pays - ?> From 88392d0dea72296d74c2d2565893bb706682cd1b Mon Sep 17 00:00:00 2001 From: Andrew Malsbury Date: Mon, 20 Nov 2023 16:42:29 +0000 Subject: [PATCH 3/3] Add Stripe fee calculation to guest_ajax.php and guest_view_invoice.php --- guest_ajax.php | 17 +++++++---- guest_pay_invoice_stripe.php | 55 ++++++++++++++++++++++++++++++++++++ guest_view_invoice.php | 16 +++++++++++ 3 files changed, 82 insertions(+), 6 deletions(-) diff --git a/guest_ajax.php b/guest_ajax.php index 47418c55..44402413 100644 --- a/guest_ajax.php +++ b/guest_ajax.php @@ -51,6 +51,10 @@ 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); @@ -58,16 +62,17 @@ if (isset($_GET['stripe_create_pi'])) { $balance_to_pay = $invoice_amount - $amount_paid; // Check config to see if client pays fees is enabled - $row = mysqli_fetch_array(mysqli_query($mysqli, "SELECT config_stripe_client_pays_fees FROM settings WHERE company_id = 1")); - if ($row['config_client_pays_fees'] == 1) { + if ($config_stripe_client_pays_fees == 1) { // Get fees from config - $row = mysqli_fetch_array(mysqli_query($mysqli, "SELECT config_stripe_percentage_fee, config_stripe_flat_fee FROM settings WHERE company_id = 1")); - $percentageFee = floatval($row['config_stripe_percentage_fee']); - $flatFee = floatval($row['config_stripe_flat_fee']); + $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 + $flatFee) / (1 - $percentageFee); + $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"); } diff --git a/guest_pay_invoice_stripe.php b/guest_pay_invoice_stripe.php index 604a8828..b25395bc 100644 --- a/guest_pay_invoice_stripe.php +++ b/guest_pay_invoice_stripe.php @@ -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", "

There was an error verifying your payment. Please contact us for more information.

"); @@ -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 + + + + Gateway Fees + - + + + +
@@ -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); diff --git a/guest_view_invoice.php b/guest_view_invoice.php index 981da396..27c2aa0e 100644 --- a/guest_view_invoice.php +++ b/guest_view_invoice.php @@ -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 + + + Gateway Fee: + + +