Repository navigation
Description
Our current implementation of this column in the database is not stored as an enum but rather the localized text string of the sale type in the language that the sale payment was made in. In monolingual installs where each user uses the same language and the translation for that word never changes it's not that big of a deal, but the current implementation makes it near impossible to programmatically determine the type of payment without using string comparison functions or regex.
For example, if we want to do a report of each payment type showing the amount paid for each type, We are going to get a different amount for "кредитная карта" and "credit card" even though they are the same payment type, but one employee uses the Russian language interface and another the en-US. In my case I'm running into this because I have to send (via RESTful API calls) the payment amounts of each type which requires me to programmatically loop through each payment, determine the payment type and add it to the appropriate value to include in the JSON. Not only does the code for that look horrendous because of the string compares but all it will take is a translation to change or someone to use a new language and the integration is broken until I add that string.
I think the solution for this would be to use constants in the code and where the view needs to display the payment type, to run it through the CI translation engine based on the constant like we do everything else. Normally we would also need to change the column to a tiny int but to do that we would need to convert all existing sales_payments to their appropriate enum value... I'm not sure how to do this programmatically.
This brings up another problem. When the payment type is a Giftcard, a colon, space and the card number is appended to the payment_type field. While that information is helpful to store, the giftcard number should have its own column. Programatically, that conversion can be done fairly easily by any payment type that has a colon replacing it with the enum for giftcard and taking the number to the right of the colon and sticking it in the newly created giftcard_number column. Debit, Credit and Cash payment types would be more difficult though.
@jekkos do you have any thoughts on this?
Activity
jekkos commentedon Aug 5, 2023
Thanks for raising this @objecttothis . As you say it's indeed not good practice to store the payment types using their translated string as
Changing this to an int column is indeed the solution here, but migrating the old data might be a bit of a challenge. What we should try then is map all database values with payment types to their correct int representation. In a first step it can be done by iterating over the key values in the $lang[] array for payment types, then do the value match, get the key from the match and map that to the int constant.
But there is indeed a chance that the migration will not work if in the past translations for payment types were updated. We could go through git history programmatically to retrieve all strings a payment type was mapped to and use these as valid match occurrences. I think this can be scripted easily (iterate over revisions of a language file and extract the value from a key of that file version, and add it to a list in the migration script). If we have this then I think it will go fine 99% of the time. Sounds like a fun challenge.
the giftcard number id suffix feels a bit like a hack to me. I think that we should model it explicitly indeed. Some logic on top of SQL in the migration script might be required to do this. Or perhaps an SQL case match might do the job here. In that case you can fill a column based on a match in another one, eg
UPDATE ospos_payment_type SET giftcard_number (CASE WHEN Type LIKE '%:%' THEN SUBSTRING_INDEX(type, ':', -1); )objecttothis commentedon Aug 5, 2023
@jekkos I think we can probably separate the work into two or three separate modules.
In addition to the database changes via migration, we need to update the business logic to store the payment_type correctly, conduct any business logic related to it and to display it correctly.
@jekkos as long as we can come up with an agreed upon solution, I don't mind taking this on. For now, in my 3.3.2 based production build I'm integrating API calls to the local government cash register and I'll just do a hack based on our database to determine the payment type. Then once we release 3.4.0 I will build a proper migration and rework the business logic in the CI4 framework.
stale commentedon Sep 17, 2023
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
[-]ospos_sales_payments.payment_type stored as a translated string representing the sale type[/-][+][Bug]: Payment type stored as translated string breaks reports[/+]feat: Add payment provider plugin system for external gateway integra…
feat: Add payment provider plugin system integrated with plugin infra…
feat: Add payment provider plugin system integrated with plugin infra…
objecttothis commentedon Sep 1, 2026
Proposed implementation path
1. Replace localized strings with constants/enum IDs
Store
payment_typeas a constant (e.g.PAYMENT_TYPE_CASH,PAYMENT_TYPE_GIFTCARD,PAYMENT_TYPE_REWARDS,PAYMENT_TYPE_CHECK,PAYMENT_TYPE_CREDIT,PAYMENT_TYPE_DEBIT, etc.) instead of the translated string. Only translate at display time vialang(), same pattern used elsewhere in the codebase.2. Add a dedicated
giftcard_numbercolumnsales_payments.payment_typecurrently getsgiftcard:<number>appended via string concatenation (seeSales::postAddPayment()andSale::save_value(), which doesexplode(':', $payment['payment_type'], 2)). Add agiftcard_numbercolumn instead, and stop overloadingpayment_typewith the colon-separated value. Simplifies both the split logic and the DB schema.3. Migration for existing data
Existing
sales_paymentsrows havepayment_typestored as whatever locale string was active when the payment was made. Migration needs to reverse-map known localized strings (across all shipped locale files inapp/Language/*/Sales.php) back to the new constants. Any row that doesn't match a known translation (e.g. from a since-changed translation, or a locale no longer shipped) should be flagged for manual review rather than silently coerced.4. Downstream benefit: enables a real allow-list in the payment controller
Currently
Sales::postAddPayment()(around line 411) guards against spoofed giftcard/rewards payment types usingstr_contains($paymentType, lang('Sales.giftcard'))against the translated string — fragile across locales/case and not a true allow-list (it can't be, since payment type is free-form localized text and other valid types like cash/check/credit must still pass through untouched). Once payment types are IDs, this becomes a straightforwardin_array($paymentTypeId, VALID_PAYMENT_TYPE_IDS)check with no locale dependency, closing that fragility.objecttothis commentedon Nov 4, 2026
Claude Issue Checker Agent (cc @objecttothis)
Investigated against current
master.Status: Still present (fix not implemented), though a related mitigation exists.
sales_payments.payment_typeis still stored as a free-text/localized string rather than a foreign key or enum/constant —Sale::save_value()still doesexplode(':', $payment['payment_type'], 2)to split out an embedded gift-card number, andSale::get_payments_summary()/report filters still match againstlang('Sales.*')-translated strings rather than stable IDs, exactly as described. @objecttothis's September 2026 comment on this thread lays out a concrete migration plan (constants/enum IDs, a dedicatedgiftcard_numbercolumn, and a reverse-mapping migration for historical locale strings), but none of it has been implemented yet — noPAYMENT_TYPE_*constants orgiftcard_numbercolumn exist inapp/Models/Sale.phpor the schema. This is the same underlying issue flagged again in #4548 (payment types for Bank Transfer/Wallet), which also remains open. The one related mitigation that has landed is a dedicatedget_reference_code_payment_types()allow-list helper (see #4093/#4548 threads) for the reference-code UI concern — but the core payment-type-as-translated-string storage problem this issue is about is unresolved.dispeakble commentedon Nov 28, 2026
Data point from a translation PR (#4846, Romanian catalog) — this is the case where the hazard fires without anyone switching languages: the
ropack already shippeddue=Datorat,check=Bon/cec,credit=Card Credit,debit=Card Debit, and it also shippedbank_transfer/wallet/upi/refundas the English fallback. Completing the catalog renames all of them, so on an existing Romanian install every historical row keeps the old string and stops matchingSale::search()(LIKE lang('Sales.due')),Cashups(trans_type == lang('Sales.cash')), the receipt/invoice views andExpense::search()/get_payments_summary()(which filter withExpenses.*while the expense form storesSales.*labels — a cross-catalog mismatch that silently returns nothing; I hit exactly that while auditing).For whoever implements the int/enum column, the data side of the migration for these values is mechanical — this is what I ran against throwaway tables cloned from the real schema, and it round-trips the
:suffix that gift card / reward rows carry (Card Cadou:1234,Reward Points:5):Two notes that matter for the design:
Cash/Dueend up in arodatabase);Sale::save_value(),Sale_lib::addPayment()andSales::postAddPayment().Happy to test a branch of this against a live 3.4.3 instance with real Romanian data if it is useful.