From 1d179e51a95988ba625fc896ff94b4cdabfdde78 Mon Sep 17 00:00:00 2001 From: Du Peng Date: Mon, 11 Mar 2024 11:25:25 +0100 Subject: [PATCH] (bug) set cash for locale payment by default --- .../local-payment/local-payment-form.tsx | 20 +++++++++++-------- app/models/invoice.rb | 2 -- test/services/accounting_service_test.rb | 1 + 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/app/frontend/src/javascript/components/payment/local-payment/local-payment-form.tsx b/app/frontend/src/javascript/components/payment/local-payment/local-payment-form.tsx index a25ab2d0e..897d14ddf 100644 --- a/app/frontend/src/javascript/components/payment/local-payment/local-payment-form.tsx +++ b/app/frontend/src/javascript/components/payment/local-payment/local-payment-form.tsx @@ -13,8 +13,8 @@ import CheckoutAPI from '../../../api/checkout'; import { SelectOption } from '../../../models/select'; import { PaymentMethod } from '../../../models/payment'; -const ALL_SCHEDULE_METHODS = ['card', 'check', 'transfer', 'cash'] as const; -type scheduleMethod = typeof ALL_SCHEDULE_METHODS[number]; +const ALL_PAYMENT_METHODS = ['card', 'check', 'transfer', 'cash'] as const; +type paymentMethod = typeof ALL_PAYMENT_METHODS[number]; /** * A form component to ask for confirmation before cashing a payment directly at the FabLab's reception. @@ -24,7 +24,7 @@ type scheduleMethod = typeof ALL_SCHEDULE_METHODS[number]; export const LocalPaymentForm: React.FC = ({ onSubmit, onSuccess, onError, children, className, paymentSchedule, cart, updateCart, customer, operator, formId, order }) => { const { t } = useTranslation('admin'); - const [method, setMethod] = useState('cash'); + const [method, setMethod] = useState('cash'); const [onlinePaymentModal, setOnlinePaymentModal] = useState(false); useEffect(() => { @@ -44,15 +44,15 @@ export const LocalPaymentForm: React.FC = ({ onSubmit, onSucce /** * Convert all payement methods for schedules to the react-select format */ - const buildMethodOptions = (): Array> => { - return ALL_SCHEDULE_METHODS.map(i => methodToOption(i)); + const buildMethodOptions = (): Array> => { + return ALL_PAYMENT_METHODS.filter(p => p !== 'cash').map(i => methodToOption(i)); }; /** * Convert the given payment-method to the react-select format */ - const methodToOption = (value: scheduleMethod): SelectOption => { - if (!value) return { value, label: '' }; + const methodToOption = (value: paymentMethod): SelectOption => { + if (!value || value === 'cash') return { value, label: '' }; return { value, label: t(`app.admin.local_payment_form.method_${value}`) }; }; @@ -60,7 +60,7 @@ export const LocalPaymentForm: React.FC = ({ onSubmit, onSucce /** * Callback triggered when the user selects a payment method for the current payment schedule. */ - const handleUpdateMethod = (option: SelectOption) => { + const handleUpdateMethod = (option: SelectOption) => { updateCart(Object.assign({}, cart, { payment_method: option.value })); setMethod(option.value); }; @@ -69,6 +69,10 @@ export const LocalPaymentForm: React.FC = ({ onSubmit, onSucce * Handle the submission of the form. It will process the local payment. */ const handleSubmit = async (event: FormEvent): Promise => { + if (paymentSchedule && !ALL_PAYMENT_METHODS.filter(p => p !== 'cash').includes(method)) { + event.preventDefault(); + return; + } event.preventDefault(); onSubmit(); diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 454671423..097e68aa3 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -165,8 +165,6 @@ class Invoice < PaymentDocument if paid_by_card? res.push(means: :card, amount: amount_paid) - elsif paid_by_wallet? - res.push(means: :wallet, amount: amount_paid) elsif paid_by_transfer? res.push(means: :transfer, amount: amount_paid) elsif paid_by_check? diff --git a/test/services/accounting_service_test.rb b/test/services/accounting_service_test.rb index 00dbfb983..a3105c033 100644 --- a/test/services/accounting_service_test.rb +++ b/test/services/accounting_service_test.rb @@ -30,6 +30,7 @@ class AccountingServiceTest < ActionDispatch::IntegrationTest post '/api/local_payment/confirm_payment', params: { customer_id: @vlonchamp.id, coupon_code: 'GIME3EUR', + payment_method: 'cash', items: [ { reservation: {