From 7a9dc0c891082cd09369bfd75c4ee8a850dabfbd Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Mon, 29 Dec 2025 10:11:52 +0330 Subject: [PATCH] wallet manage in checkout --- .../[id]/components/PaymentSection.tsx | 66 +++++++++++++++---- 1 file changed, 54 insertions(+), 12 deletions(-) diff --git a/src/app/[name]/(Dialogs)/order/checkout/[id]/components/PaymentSection.tsx b/src/app/[name]/(Dialogs)/order/checkout/[id]/components/PaymentSection.tsx index 04e8864..5db769c 100644 --- a/src/app/[name]/(Dialogs)/order/checkout/[id]/components/PaymentSection.tsx +++ b/src/app/[name]/(Dialogs)/order/checkout/[id]/components/PaymentSection.tsx @@ -6,6 +6,9 @@ import { useTranslation } from 'react-i18next'; import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'; import { useGetPaymentMethod } from '../../hooks/useOrderData'; import { useCheckoutStore } from '../../store/Store'; +import { useGetUserWallet } from '@/app/[name]/(Main)/transactions/hooks/useTransactionData'; +import { useGetCartItems } from '@/app/[name]/(Dialogs)/cart/hooks/useCartData'; +import { useGetProfile } from '@/app/[name]/(Profile)/profile/hooks/userProfileData'; type PaymentSectionProps = { paymentType: string; @@ -23,12 +26,28 @@ const getIconByMethod = (method: "CardOnDelivery" | "Cash" | "Online", _gateway: return Card; }; +const isWalletPayment = (method: string, gateway: string | null): boolean => { + return gateway?.toLowerCase() === 'wallet' || method.toLowerCase() === 'wallet'; +}; + export const PaymentSection = ({ paymentType, onPaymentTypeChange }: PaymentSectionProps) => { const { t: tOrderDetail } = useTranslation('parallels', { keyPrefix: 'OrderDetail' }); const { t: tOrders } = useTranslation('orders'); const { data: paymentMethod } = useGetPaymentMethod(); const { setIsSelectedPayment } = useCheckoutStore(); + const { data: userWallet } = useGetUserWallet(); + const { isSuccess } = useGetProfile(); + const { data: cartData } = useGetCartItems(isSuccess); + + const walletAmount = userWallet?.data?.wallet ?? 0; + const totalAmount = cartData?.data?.total ?? 0; + const isWalletInsufficient = walletAmount < totalAmount; + + const formatPrice = useCallback( + (value: number) => value.toLocaleString('fa-IR'), + [] + ); const getPaymentMethodTranslation = useCallback((method: string, fallbackTitle: string) => { const translationKey = `paymentMethod.${method}`; @@ -46,6 +65,9 @@ export const PaymentSection = ({ paymentType, onPaymentTypeChange }: PaymentSect id: item.id, label: getPaymentMethodTranslation(item.method, item.title), icon: getIconByMethod(item.method, item.gateway), + method: item.method, + gateway: item.gateway, + isWallet: isWalletPayment(item.method, item.gateway), })); }, [paymentMethod, getPaymentMethodTranslation]); @@ -62,19 +84,39 @@ export const PaymentSection = ({ paymentType, onPaymentTypeChange }: PaymentSect }} className='flex flex-col gap-6 mt-6' > - {paymentOptions.map(({ id, label, icon: Icon }) => ( -
- -
+ ); + })}