From a165189cc3d5fabaf6d804236a9f1efb68c3b061 Mon Sep 17 00:00:00 2001 From: johnnyq Date: Tue, 20 Oct 2026 14:16:27 -0400 Subject: [PATCH] Payment Providers stage 1: list rebuilt in the standard pattern showing on or off, live or test mode from the keys, where payments go, the card limit, whether fees are recorded, saved cards with autopay and the last card payment; providers can be turned off and on, and delete states the saved cards, autopay and client links that go with it; Add and Edit share one window that no longer shows the secret key and checks the keys, the live and test mode match, the card limit and the fee vendor and category, and checks new keys with Stripe before anything is saved, showing problems beside each field; fees use a single record-fees switch with vendor and category; changes are logged from old to new value with keys logged only as changed; fixes a rejected Stripe key being saved, the empty provider name and PHP warnings on edit, the ignored enable expense switch, and the stale Online Payment path in the account archive message --- CHANGELOG.md | 8 + .../payment_provider/payment_provider_add.php | 158 +------ .../payment_provider_edit.php | 387 ++++++++++++------ admin/payment_providers.php | 245 ++++++----- admin/post/payment_provider.php | 164 +++++--- admin/post/payment_provider_model.php | 9 - functions.php | 1 + functions/accounts.php | 2 +- functions/payment_provider.php | 291 +++++++++++++ 9 files changed, 827 insertions(+), 438 deletions(-) delete mode 100644 admin/post/payment_provider_model.php create mode 100644 functions/payment_provider.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f707d471..620446a39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1037,6 +1037,11 @@ If you are coming from 26.08 or earlier, the database update still has to be run - Why an email failed is kept with it - the mail server's own answer, an address that isn't one, no from address, a domain with no mail server - and shown under its status and searched. Cancelling keeps who cancelled it. The alert for a failed email says why and when it is tried again, and when ITFlow gives up after the last try there is an alert for that too, each opening the Mail Queue on its tab. - Opening an email shows where it stands and why, who it is to and from, when it was queued, how many tries it has had, when it was sent or last failed, its attachments and calendar file - and the email itself as it will arrive, rather than stripped of its layout. A sent one says what it said was cleared on sending, and why. Retry now, Cancel and Delete are in the window too. - Without opening it: the Settings overview says when email failed to send in the last day, and when the queue isn't moving, and the Mail tile how many failed this week; the Mail Queue menu item carries the same count; Mail's Tests tab shows when mail was last sent and what is waiting, retrying or failed. The Mail Queue is in the search window (Ctrl K) for admins, and the test email says where to watch it go. +- **Payment Providers** (rebuilt) + - Admin > Payment Providers lists each provider with whether it is on, live or test (as its keys say), the account its payments go into, its card limit, whether its fees are recorded as expenses, its saved cards and the autopay on them, and when a card payment last came through it. Search, sort by any of them, and Saved Cards from the top. + - A provider can be turned off and on again: off, clients can't pay by card and autopay with its cards is skipped, while its cards are kept. Delete says first what goes with it - its saved cards, the autopay on them and its links to clients - and suggests turning it off instead. + - Add and Edit are one window. The secret key is no longer put into it - left empty, it stays as saved. Save checks first: keys that aren't Stripe's, one live and one test, a card limit that isn't money, or fees with no vendor or category are said beside the field, and new keys are checked with Stripe - before anything is saved, and without closing the window or losing what was typed. Fees have one switch, Record its fees as expenses, with who they are paid to and their category. The card limit is shown in your currency. + - The audit log says what changed, from what to what - "Card limit No limit → Under $2,500.00", "Mode test → live" - with the keys only as changed. ### Bug Fixes @@ -1192,6 +1197,8 @@ If you are coming from 26.08 or earlier, the database update still has to be run - Settings > Mail: a From address that wasn't an email address was saved blank, and From names lost their punctuation - "PittPC, LLC" was saved as "PittPC LLC". - Settings > Mail: a password or secret lost a space at either end when saved, and anything from a < up to the next > (or to its end). - Settings > Mail: pressing Enter on the OAuth tab went off to Microsoft to connect, rather than saving. Changing the OAuth app or its refresh token went on using the old access token until it ran out. +- Payment Providers: a Stripe key that Stripe turned away was saved anyway, with an error after; the Edit window showed the secret key; saving it logged and said an empty provider name, with PHP warnings; the Enable Expense switch did nothing; and there was no way to turn a provider off. +- Accounts: archiving the account online payments go into pointed to Admin > Settings > Online Payment, which no longer exists - it is Admin > Payment Providers. - Settings > Notifications, and recurring invoices: the note that recurring invoices are made as drafts pointed to the billing settings, but the switch is in Notifications. - Mail Queue: cancelling an email marked it Failed, and an email to a bad address or with no from address showed Failed with no time. - Mail Queue: Resend was only offered once all four tries were used up, and the next failure quietly gave a resent email three more. @@ -1760,6 +1767,7 @@ If you are coming from 26.08 or earlier, the database update still has to be run - Settings: Tickets, Projects, Quotes, Invoices and billing and Defaults use the frame. New in `functions/settings.php`: `settingsNumbering($kind)`, `settingsNumberInUse()`, `settingsNumberingCheck($kind, $post, $saved)` (prefix and next number checked against the highest in use with that prefix - skipped when both are as saved, so a clash already there doesn't block the rest of the page) and `settingsNumberingFields()` (with `[data-settings-next]` in `js/settings.js`, and `[data-settings-depends-on=name]` to fade fields while a switch is off); `settingsEmailProblem()`; `settingsStartPages()`, `settingsStartPageProblem()` and `settingsNetTerms()`. `settingsSectionStart()` takes the section's element id, so `#reminders`, `#billing-time`, `#billable-expenses` and `#mileage` still work; `settingsSelect()` takes `options_html`, `append` and `search => false`; `settingsSwitch()` takes `id`. `settingsSame()` treats NULL as 0 or empty and ignores Windows line ends; the log names accounts, calendars and categories. Values kept from a refused save are only used for five minutes. These handlers keep a setting that isn't sent (`$_POST + saved`), a switch not sent being off. Defaults' fields are renamed to their columns (`config_start_page`, `config_default_*`). The start page is escaped where it is printed. - Settings: Notifications, Security, Client portal, Single sign-on (`admin/identity_providers.php`), Mail and Logs use the frame. `settingsSearch($query, $limit)` scores settings as Find a setting does, leaving out pages for a module that is off; `agent/ajax.php?global_search` returns them as `settings` for admins and `js/global_search.js` shows them under Settings. Single sign-on's fields are renamed to their columns (`config_azure_client_id`, `config_azure_client_secret`). A field marked `quiet` in `settingsFields()` is logged as changed without its value (the login key). The Logs handler is unchanged. - Settings: Mail's Sending, Receiving, OAuth and From addresses tabs use the frame, and every Mail column is in `settingsFields()`. Its handlers save through `settingsSave()` and `settingsWrite()`, logging from -> to; `edit_mail_smtp_settings` and `edit_mail_imap_settings` share one check, and both come back to their tab when refused. A password, secret or refresh token posted empty keeps what is saved; an empty OAuth client ID forgets its secret and refresh token; a changed app or refresh token clears `config_mail_oauth_access_token` and its expiry, so it is fetched again. The access token is no longer a field. `oauth_connect_microsoft_mail` saves the same way before going to Microsoft, and `admin/oauth_microsoft_mail_callback.php` now stores only the tokens. New in `functions/settings.php`: `settingsMailProviders()`, `settingsMailEncryption()` and `settingsEnterSaves($button)` - a hidden Save placed first in a form whose first button would otherwise be another (Mail's OAuth tab, Logs). `settingsRefuse()` takes the page to go back to; `settingsInput()`, `settingsSelect()` and `settingsTextarea()` take `id`; `settingsWrite()` writes a `secret` field exactly as given, rather than through `escapeSql()`, which trims and strips tags. +- Payment Providers: new `functions/payment_provider.php` - `paymentProviderKinds()` (the providers ITFlow takes payments through and what their keys look like), `paymentProviderMode()`, `paymentProviderRow()`, `paymentProviderTies()` and `paymentProviderTiesText()`, `paymentProviderLimitText()`, `paymentProviderCheck($post, $before)`, `paymentProviderKeysChanged()`, `paymentProviderAsk($name, $secret_key)` (Stripe's account, from the secret key), `paymentProviderChangeLines()` and `paymentProviderWrite()`, which saves the keys exactly as given. `check_payment_provider` answers in JSON - `ok`, `errors` by field and the Stripe `account` - and the window asks it before saving; a key it has had taken isn't asked about again on the save, for ten minutes. New handlers `turn_payment_provider_off` and `turn_payment_provider_on` (GET with the CSRF token). `payment_provider_add.php` opens `payment_provider_edit.php` with no id. Field `record_fees` replaces `enable_expense`; `active` and `description` are new. `admin/post/payment_provider_model.php` is gone. - Removed, as nothing reached them or they couldn't run: `admin/settings_custom_fields.php` (it required a modal that no longer existed) with `admin/post/custom_field.php`, `admin/post/custom_field_model.php` and `admin/modals/custom_field/`; `admin/post/settings_ai.php` and `admin/post/settings_online_payment.php`, which wrote the `config_ai_*` and `config_stripe_*` columns dropped in 2.3.2; `admin/post/settings_online_payment_clients.php`, and `agent/client_autopay.php`, which read the dropped Stripe columns and was linked from nowhere. The `custom_fields` and `custom_values` tables stay as they are, and the client export still includes asset custom field values. - Mail Queue: new `functions/mail_queue.php` - `MAIL_QUEUE_TRIES` (4), `MAIL_QUEUE_RETRY_MINUTES` (30) and `MAIL_QUEUE_KEEP_DAYS` (90), which `cron/mail_queue.php`'s retry pass and the nightly clean-up now use too; `mailQueueTabs()`, `mailQueueState($row)`, `mailQueueCan($row, $action)`, `mailQueueAct($action, $ids)` and `mailQueueHealth()`. `email_status` 4 is Cancelled - never selected by the worker. Retry now sets status 0, no attempts and no failure time. Handlers: `retry_mail`, `cancel_mail`, `delete_mail` (GET with the CSRF token) and `bulk_retry_emails`, `bulk_cancel_emails`, `bulk_delete_emails`; `send_failed_mail` still works. The worker sets `email_failed_at` on the bad sender, bad recipient and no-MX paths. - Mail Queue: database 3.3.7 adds `email_queue.email_error` varchar(500) - why it last failed, or "Cancelled by ..."; cleared when sent or retried. The worker writes it on every failure path, through `mailQueueErrorSql()`, which leaves it out until the update has run, so an install between updating its files and its database still sends; `mailQueueErrorColumn()` does the same for reads. The retry pass's bad-recipient path now sets `email_failed_at`, so its backoff counts from that try. An alert (`appNotify`) when the retry pass gives up. The email's window shows the queued HTML in an iframe `srcdoc` with `sandbox="allow-popups allow-popups-to-escape-sandbox"` (no scripts, no same origin) and a `script-src 'none'` policy, instead of HTMLPurifier in the page. diff --git a/admin/modals/payment_provider/payment_provider_add.php b/admin/modals/payment_provider/payment_provider_add.php index 70664d8a3..c59c168b7 100644 --- a/admin/modals/payment_provider/payment_provider_add.php +++ b/admin/modals/payment_provider/payment_provider_add.php @@ -1,155 +1,9 @@ Payment Providers - Add. The same window as Edit + * (payment_provider_edit.php), with no provider yet. + */ -ob_start(); - -?> - -
- - - - -
- - Payment Providers - one provider's window: Add (no id, or from + * payment_provider_add.php) or Edit. Its keys - the secret one never put back + * into the page, so left empty it stays - whether it is on, the account its + * payments go into, the card limit, and whether its fees are recorded as + * expenses. Save asks first (check_payment_provider, + * admin/post/payment_provider.php): what is wrong shows beside each field and + * the window stays open; once all is right, it saves. + */ + require_once '../../includes/modal_header.php'; -$provider_id = intval($_GET['id']); +$provider_id = intval($_GET['id'] ?? 0); +$row = $provider_id ? paymentProviderRow($provider_id) : null; +if ($provider_id && !$row) { + echo json_encode(['error' => 'That payment provider no longer exists.']); + exit; +} -$sql = mysqli_query($mysqli, "SELECT payment_provider_account, payment_provider_expense_category, - payment_provider_expense_vendor, payment_provider_name, payment_provider_private_key, - payment_provider_public_key, payment_provider_threshold FROM payment_providers WHERE payment_provider_id = $provider_id LIMIT 1"); +$kinds = paymentProviderKinds(); +$taken = []; +$sql = mysqli_query($mysqli, "SELECT payment_provider_name FROM payment_providers"); +while ($taken_row = mysqli_fetch_row($sql)) { + $taken[$taken_row[0]] = true; +} +$free_kinds = array_diff_key($kinds, $taken); -$row = mysqli_fetch_assoc($sql); -$provider_name = escapeHtml($row['payment_provider_name']); -$public_key = escapeHtml($row['payment_provider_public_key']); -$private_key = escapeHtml($row['payment_provider_private_key']); -$account_id = intval($row['payment_provider_account']); -$threshold = floatval($row['payment_provider_threshold']); -$vendor_id = intval($row['payment_provider_expense_vendor']); -$category_id = intval($row['payment_provider_expense_category']); +$name = $row ? (string) $row['payment_provider_name'] : (string) (array_key_first($free_kinds) ?? array_key_first($kinds)); +$kind = $kinds[$name] ?? reset($kinds); +$mode = $row ? paymentProviderMode($row['payment_provider_private_key']) : ''; +$account_id = intval($row['payment_provider_account'] ?? 0); +$vendor_id = intval($row['payment_provider_expense_vendor'] ?? 0); +$category_id = intval($row['payment_provider_expense_category'] ?? 0); +$threshold = round(floatval($row['payment_provider_threshold'] ?? 0), 2); +$records_fees = $row ? ($vendor_id > 0 && $category_id > 0) : true; + +// A new one starts on an account and a vendor named for it, and Processing Fee, where there are those +if (!$row) { + $starts = mysqli_fetch_row(mysqli_query($mysqli, "SELECT + (SELECT account_id FROM accounts WHERE account_name = '" . escapeSql($name) . "' AND account_archived_at IS NULL LIMIT 1), + (SELECT vendor_id FROM vendors WHERE vendor_name = '" . escapeSql($name) . "' AND vendor_client_id = 0 AND vendor_archived_at IS NULL LIMIT 1), + (SELECT category_id FROM categories WHERE category_type = 'Expense' AND category_name = 'Processing Fee' AND category_archived_at IS NULL LIMIT 1)")); + $account_id = intval($starts[0] ?? 0); + $vendor_id = intval($starts[1] ?? 0); + $category_id = intval($starts[2] ?? 0); +} + +$accounts = mysqli_query($mysqli, "SELECT account_id, account_name, account_archived_at FROM accounts WHERE account_archived_at IS NULL OR account_id = $account_id ORDER BY account_name ASC"); +$vendors = mysqli_query($mysqli, "SELECT vendor_id, vendor_name, vendor_archived_at FROM vendors WHERE vendor_client_id = 0 AND (vendor_archived_at IS NULL OR vendor_id = $vendor_id) ORDER BY vendor_name ASC"); +$currency = (string) ($session_company_currency ?? ''); + +// Where what is wrong with a field shows +$error_for = function ($field) { + return '
'; +}; ob_start(); ?> - -
+ -