From 77535934d5458159f670c04e596ca93c59c892bd Mon Sep 17 00:00:00 2001
From: hamid zarghami
Date: Wed, 3 Dec 2025 16:16:18 +0330
Subject: [PATCH] payment section
---
.../checkout/[id]/components/AddressSection.tsx | 10 +++++-----
.../checkout/[id]/components/PaymentSection.tsx | 17 ++++++++++++++---
.../order/checkout/hooks/useOrderData.ts | 7 +++++++
.../order/checkout/service/OrderService.ts | 7 +++++++
4 files changed, 33 insertions(+), 8 deletions(-)
diff --git a/src/app/[name]/(Dialogs)/order/checkout/[id]/components/AddressSection.tsx b/src/app/[name]/(Dialogs)/order/checkout/[id]/components/AddressSection.tsx
index 629cf2d..247b972 100644
--- a/src/app/[name]/(Dialogs)/order/checkout/[id]/components/AddressSection.tsx
+++ b/src/app/[name]/(Dialogs)/order/checkout/[id]/components/AddressSection.tsx
@@ -92,9 +92,9 @@ export const AddressSection = () => {
}
// اگر آدرس انتخاب شده وجود ندارد، نمایش نده
- if (!selectedAddress) {
- return null;
- }
+ // if (!selectedAddress) {
+ // return null;
+ // }
return (
<>
@@ -112,9 +112,9 @@ export const AddressSection = () => {
- {selectedAddress.title}
+ {selectedAddress?.title}
- {formatAddress(selectedAddress)}
+ {selectedAddress && formatAddress(selectedAddress)}
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 7410c33..1c6bb91 100644
--- a/src/app/[name]/(Dialogs)/order/checkout/[id]/components/PaymentSection.tsx
+++ b/src/app/[name]/(Dialogs)/order/checkout/[id]/components/PaymentSection.tsx
@@ -3,14 +3,16 @@
import { Card, Icon, Wallet2 } from 'iconsax-react';
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
-import { useGetPaymentMethod } from '../../hooks/useOrderData';
+import { useGetPaymentMethod, useSetPaymentMethod } from '../../hooks/useOrderData';
+import { useCheckoutStore } from '../../store/Store';
type PaymentSectionProps = {
paymentType: string;
onPaymentTypeChange: (id: string) => void;
};
-const getIconByMethod = (method: "CardOnDelivery" | "Cash" | "Online", gateway: string | null): Icon => {
+// eslint-disable-next-line @typescript-eslint/no-unused-vars
+const getIconByMethod = (method: "CardOnDelivery" | "Cash" | "Online", _gateway: string | null): Icon => {
if (method === "Online" || method === "CardOnDelivery") {
return Card;
}
@@ -24,6 +26,8 @@ export const PaymentSection = ({ paymentType, onPaymentTypeChange }: PaymentSect
const { t } = useTranslation('parallels', { keyPrefix: 'OrderDetail' });
const { data: paymentMethod } = useGetPaymentMethod();
+ const { mutate: setPaymentMethod } = useSetPaymentMethod();
+ const { setIsSelectedPayment } = useCheckoutStore();
const paymentOptions = useMemo(() => {
if (!paymentMethod?.data) return [];
@@ -56,7 +60,14 @@ export const PaymentSection = ({ paymentType, onPaymentTypeChange }: PaymentSect
onPaymentTypeChange(id)}
+ onChange={() => {
+ onPaymentTypeChange(id);
+ setPaymentMethod(id, {
+ onSuccess: () => {
+ setIsSelectedPayment(true);
+ },
+ });
+ }}
type='radio'
name='paymentMethod'
id={`paymentMethod${id}`}
diff --git a/src/app/[name]/(Dialogs)/order/checkout/hooks/useOrderData.ts b/src/app/[name]/(Dialogs)/order/checkout/hooks/useOrderData.ts
index b390439..66c26c9 100644
--- a/src/app/[name]/(Dialogs)/order/checkout/hooks/useOrderData.ts
+++ b/src/app/[name]/(Dialogs)/order/checkout/hooks/useOrderData.ts
@@ -4,6 +4,7 @@ import {
getShipmentMethod,
setAddressCart,
setDeliveryMethod,
+ setPaymentMethod,
} from "../service/OrderService";
export const useGetShipmentMethod = () => {
@@ -31,3 +32,9 @@ export const useSetDeliveryMethod = () => {
mutationFn: setDeliveryMethod,
});
};
+
+export const useSetPaymentMethod = () => {
+ return useMutation({
+ mutationFn: setPaymentMethod,
+ });
+};
diff --git a/src/app/[name]/(Dialogs)/order/checkout/service/OrderService.ts b/src/app/[name]/(Dialogs)/order/checkout/service/OrderService.ts
index 7bf3056..bb3722a 100644
--- a/src/app/[name]/(Dialogs)/order/checkout/service/OrderService.ts
+++ b/src/app/[name]/(Dialogs)/order/checkout/service/OrderService.ts
@@ -29,3 +29,10 @@ export const setDeliveryMethod = async (id: string) => {
});
return data;
};
+
+export const setPaymentMethod = async (id: string) => {
+ const { data } = await api.patch("/public/cart/payment-method", {
+ paymentMethodId: id,
+ });
+ return data;
+};