From 8ee780566e6b92cda4f4c33f3654dad5cb99a592 Mon Sep 17 00:00:00 2001 From: johnnyq Date: Wed, 22 Jul 2026 17:06:10 -0400 Subject: [PATCH] Cleanup Expense Code in post blocks and remove single use function since stripe payments get reconciled during nightly cron --- agent/post/payment.php | 15 --------------- client/post.php | 15 --------------- cron/cron.php | 24 +++++------------------- functions.php | 1 - functions/payments.php | 22 ---------------------- guest/guest_pay_invoice_stripe.php | 22 +++------------------- 6 files changed, 8 insertions(+), 91 deletions(-) delete mode 100644 functions/payments.php diff --git a/agent/post/payment.php b/agent/post/payment.php index 0cb8cc0f..e1e0be63 100644 --- a/agent/post/payment.php +++ b/agent/post/payment.php @@ -375,8 +375,6 @@ if (isset($_POST['add_payment_stripe'])) { $public_key = escapeSql($row['payment_provider_public_key']); $private_key = escapeSql($row['payment_provider_private_key']); $account_id = intval($row['payment_provider_account']); - $expense_category_id = intval($row['payment_provider_expense_category']); - $expense_vendor_id = intval($row['payment_provider_expense_vendor']); $payment_provider_client = escapeSql($row['payment_provider_client']); $saved_payment_method = escapeSql($row['saved_payment_provider_method']); $saved_payment_description = escapeSql($row['saved_payment_description']); @@ -410,7 +408,6 @@ if (isset($_POST['add_payment_stripe'])) { 'off_session' => true, 'confirm' => true, 'description' => $pi_description, - 'expand' => ['latest_charge.balance_transaction'], 'metadata' => [ 'itflow_client_id' => $client_id, 'itflow_client_name' => $client_name, @@ -487,18 +484,6 @@ if (isset($_POST['add_payment_stripe'])) { $extended_log_desc = '(DEV MODE)'; } - // Create actual Stripe gateway fee as an expense (if configured) - if ($expense_vendor_id > 0 && $expense_category_id > 0) { - $stripe_fee = getStripeGatewayFee($payment_intent); - if ($stripe_fee) { - $gateway_fee = floatval($stripe_fee['fee']); - $gateway_fee_currency = escapeSql($stripe_fee['currency']); - mysqli_query($mysqli,"INSERT INTO expenses SET expense_date = '$pi_date', expense_amount = $gateway_fee, expense_currency_code = '$gateway_fee_currency', expense_account_id = $account_id, expense_vendor_id = $expense_vendor_id, expense_client_id = $client_id, expense_category_id = $expense_category_id, expense_description = 'Stripe fee for Invoice $invoice_prefix$invoice_number payment of $balance_to_pay', expense_reference = 'Stripe - $pi_id $extended_log_desc'"); - } else { - logApp("Stripe", "warning", "Balance transaction unavailable for $pi_id - fee expense not recorded for invoice ID $invoice_id"); - } - } - // Notify/log appNotify("Invoice Paid", "Invoice $invoice_prefix$invoice_number automatically paid", "/agent/invoice.php?invoice_id=$invoice_id", $client_id); logAudit("Invoice", "Payment", "$session_name initiated Stripe payment amount of " . numfmt_format_currency($currency_format, $invoice_amount, $invoice_currency_code) . " added to invoice $invoice_prefix$invoice_number - $pi_id $extended_log_desc", $client_id, $invoice_id); diff --git a/client/post.php b/client/post.php index 7fca4447..3923c1ce 100644 --- a/client/post.php +++ b/client/post.php @@ -548,8 +548,6 @@ if (isset($_GET['add_payment_by_provider'])) { $public_key = escapeSql($row['payment_provider_public_key']); $private_key = escapeSql($row['payment_provider_private_key']); $account_id = intval($row['payment_provider_account']); - $expense_category_id = intval($row['payment_provider_expense_category']); - $expense_vendor_id = intval($row['payment_provider_expense_vendor']); $payment_provider_client = escapeSql($row['payment_provider_client']); $saved_payment_method = escapeSql($row['saved_payment_provider_method']); $saved_payment_description = escapeSql($row['saved_payment_description']); @@ -591,7 +589,6 @@ if (isset($_GET['add_payment_by_provider'])) { 'off_session' => true, 'confirm' => true, 'description' => $pi_description, - 'expand' => ['latest_charge.balance_transaction'], 'metadata' => [ 'itflow_client_id' => $client_id, 'itflow_client_name' => $client_name, @@ -668,18 +665,6 @@ if (isset($_GET['add_payment_by_provider'])) { $extended_log_desc = '(DEV MODE)'; } - // Create actual Stripe gateway fee as an expense (if configured) - if ($expense_vendor_id > 0 && $expense_category_id > 0) { - $stripe_fee = getStripeGatewayFee($payment_intent); - if ($stripe_fee) { - $gateway_fee = floatval($stripe_fee['fee']); - $gateway_fee_currency = escapeSql($stripe_fee['currency']); - mysqli_query($mysqli,"INSERT INTO expenses SET expense_date = '$pi_date', expense_amount = $gateway_fee, expense_currency_code = '$gateway_fee_currency', expense_account_id = $account_id, expense_vendor_id = $expense_vendor_id, expense_client_id = $client_id, expense_category_id = $expense_category_id, expense_description = 'Stripe fee for Invoice $invoice_prefix$invoice_number payment of $balance_to_pay', expense_reference = 'Stripe - $pi_id $extended_log_desc'"); - } else { - logApp("Stripe", "warning", "Balance transaction unavailable for $pi_id - fee expense not recorded for invoice ID $invoice_id"); - } - } - // Notify/log appNotify("Invoice Paid", "Invoice $invoice_prefix$invoice_number automatically paid", "/agent/invoice.php?invoice_id=$invoice_id", $client_id); logAudit("Invoice", "Payment", "$session_name initiated Stripe payment amount of " . numfmt_format_currency($currency_format, $invoice_amount, $invoice_currency_code) . " added to invoice $invoice_prefix$invoice_number - $pi_id $extended_log_desc", $client_id, $invoice_id); diff --git a/cron/cron.php b/cron/cron.php index 4f8f1904..0713245a 100644 --- a/cron/cron.php +++ b/cron/cron.php @@ -818,8 +818,6 @@ while ($row = mysqli_fetch_assoc($sql_recurring_payments)) { $provider_name = escapeSql($saved_payment['payment_provider_name']); $provider_private_key = $saved_payment['payment_provider_private_key']; $account_id = intval($saved_payment['payment_provider_account']); - $expense_category_id = intval($saved_payment['payment_provider_expense_category']); - $expense_vendor_id = intval($saved_payment['payment_provider_expense_vendor']); $saved_payment_description = escapeSql($saved_payment['saved_payment_description']); $stripe_payment_method_id = $saved_payment['saved_payment_provider_method']; @@ -851,7 +849,6 @@ while ($row = mysqli_fetch_assoc($sql_recurring_payments)) { 'off_session' => true, 'confirm' => true, 'description' => $pi_description, - 'expand' => ['latest_charge.balance_transaction'], 'metadata' => [ 'itflow_client_id' => $client_id, 'itflow_client_name' => $client_name, @@ -884,18 +881,6 @@ while ($row = mysqli_fetch_assoc($sql_recurring_payments)) { mysqli_query($mysqli, "INSERT INTO payments SET payment_date = '$pi_date', payment_amount = $pi_amount_paid, payment_currency_code = '$pi_currency', payment_account_id = $account_id, 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 = 'Online Payment added (autopay)', history_invoice_id = $invoice_id"); - // EXPENSE: Actual Stripe gateway fee as an expense (if configured) - if ($expense_vendor_id > 0 && $expense_category_id > 0) { - $stripe_fee = getStripeGatewayFee($payment_intent); - if ($stripe_fee) { - $gateway_fee = floatval($stripe_fee['fee']); - $gateway_fee_currency = escapeSql($stripe_fee['currency']); - mysqli_query($mysqli,"INSERT INTO expenses SET expense_date = '$pi_date', expense_amount = $gateway_fee, expense_currency_code = '$gateway_fee_currency', expense_account_id = $account_id, expense_vendor_id = $expense_vendor_id, expense_client_id = $client_id, expense_category_id = $expense_category_id, expense_description = 'Stripe fee for Invoice $invoice_prefix$invoice_number payment of $balance_to_pay', expense_reference = 'Stripe - $pi_id'"); - } else { - logApp("Stripe", "warning", "Balance transaction unavailable for $pi_id - fee expense not recorded for invoice ID $invoice_id"); - } - } - // RECEIPT EMAIL if (!empty($config_smtp_provider)) { $subject = "Payment Received - Invoice $invoice_prefix$invoice_number"; @@ -1006,10 +991,11 @@ if ($stripe_provider) { continue; } - $stripe_fee = getStripeGatewayFee($payment_intent); - if ($stripe_fee) { - $gateway_fee = floatval($stripe_fee['fee']); - $gateway_fee_currency = escapeSql($stripe_fee['currency']); + // Actual fee from the balance transaction (null until Stripe attaches it - retried next run) + $balance_transaction = $payment_intent->latest_charge->balance_transaction ?? null; + if ($balance_transaction && !is_string($balance_transaction)) { + $gateway_fee = round($balance_transaction->fee / 100, 2); + $gateway_fee_currency = escapeSql(strtoupper($balance_transaction->currency)); mysqli_query($mysqli, "INSERT INTO expenses SET expense_date = '$payment_date', expense_amount = $gateway_fee, expense_currency_code = '$gateway_fee_currency', expense_account_id = $expense_account_id, expense_vendor_id = $expense_vendor_id, expense_client_id = $client_id, expense_category_id = $expense_category_id, expense_description = 'Stripe fee for Invoice $invoice_prefix$invoice_number payment of $payment_amount', expense_reference = '$payment_reference'"); logApp("Stripe", "info", "Fee reconciliation - recorded Stripe fee of $gateway_fee for $pi_id"); } diff --git a/functions.php b/functions.php index 5183cccc..5abd11e9 100644 --- a/functions.php +++ b/functions.php @@ -16,4 +16,3 @@ require_once __DIR__ . '/functions/auth.php'; require_once __DIR__ . '/functions/logging.php'; require_once __DIR__ . '/functions/app.php'; require_once __DIR__ . '/functions/db.php'; -require_once __DIR__ . '/functions/payments.php'; diff --git a/functions/payments.php b/functions/payments.php deleted file mode 100644 index d7bde49c..00000000 --- a/functions/payments.php +++ /dev/null @@ -1,22 +0,0 @@ - ['latest_charge.balance_transaction']. - * Returns ['fee' => float, 'currency' => 'USD'] or false if unavailable. - */ -function getStripeGatewayFee($payment_intent) -{ - $bt = $payment_intent->latest_charge->balance_transaction ?? null; - - // Not expanded or not yet created (async payment methods) - if (!$bt || is_string($bt)) { - return false; - } - - return [ - 'fee' => round($bt->fee / 100, 2), - 'currency' => strtoupper($bt->currency), - ]; -} diff --git a/guest/guest_pay_invoice_stripe.php b/guest/guest_pay_invoice_stripe.php index 317fb7a6..ab87e053 100644 --- a/guest/guest_pay_invoice_stripe.php +++ b/guest/guest_pay_invoice_stripe.php @@ -11,8 +11,6 @@ $stripe_provider = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT * FROM payme $stripe_publishable = escapeHtml($stripe_provider['payment_provider_public_key']); $stripe_secret = escapeHtml($stripe_provider['payment_provider_private_key']); $stripe_account = intval($stripe_provider['payment_provider_account']); -$stripe_expense_vendor = intval($stripe_provider['payment_provider_expense_vendor']); -$stripe_expense_category = intval($stripe_provider['payment_provider_expense_category']); // Show payment form if (isset($_GET['invoice_id'], $_GET['url_key']) && !isset($_GET['payment_intent'])) { @@ -161,10 +159,7 @@ if (isset($_GET['invoice_id'], $_GET['url_key']) && !isset($_GET['payment_intent require_once '../libs/stripe-php/init.php'; \Stripe\Stripe::setApiKey($stripe_secret); - $pi_obj = \Stripe\PaymentIntent::retrieve([ - 'id' => $pi_id, - 'expand' => ['latest_charge.balance_transaction'], - ]); + $pi_obj = \Stripe\PaymentIntent::retrieve($pi_id); if ($pi_obj->client_secret !== $pi_cs) { error_log("Stripe payment error - Payment intent ID/Secret mismatch for $pi_id"); @@ -231,22 +226,11 @@ if (isset($_GET['invoice_id'], $_GET['url_key']) && !isset($_GET['payment_intent // Update Invoice Status mysqli_query($mysqli, "UPDATE invoices SET invoice_status = 'Paid' WHERE invoice_id = $invoice_id"); - // Add Payment to History + // Add Payment to History mysqli_query($mysqli, "INSERT INTO payments SET payment_date = '$pi_date', payment_amount = $pi_amount_paid, payment_currency_code = '$pi_currency', payment_account_id = $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 = 'Online Payment added (client) - $ip - $os - $browser', history_invoice_id = $invoice_id"); - // Stripe expense (actual fee from balance transaction) - if ($stripe_expense_vendor > 0 && $stripe_expense_category > 0) { - $stripe_fee = getStripeGatewayFee($pi_obj); - if ($stripe_fee) { - $gateway_fee = floatval($stripe_fee['fee']); - $gateway_fee_currency = escapeSql($stripe_fee['currency']); - mysqli_query($mysqli, "INSERT INTO expenses SET expense_date = '$pi_date', expense_amount = $gateway_fee, expense_currency_code = '$gateway_fee_currency', expense_account_id = $stripe_account, expense_vendor_id = $stripe_expense_vendor, expense_client_id = $client_id, expense_category_id = $stripe_expense_category, expense_description = 'Stripe fee for Invoice $invoice_prefix$invoice_number payment of $balance_to_pay', expense_reference = 'Stripe - $pi_id'"); - } else { - error_log("Stripe payment warning - balance transaction unavailable for $pi_id, fee expense not recorded"); - } - } - // Notify appNotify("Invoice Paid", "Invoice $invoice_prefix$invoice_number has been paid by $client_name - $ip - $os - $browser", "/agent/invoice.php?invoice_id=$invoice_id", $pi_client_id);