pay order

This commit is contained in:
hamid zarghami
2025-12-17 12:45:19 +03:30
parent c7c3c04ce4
commit 58ee5af8f9
4 changed files with 29 additions and 9 deletions
@@ -54,7 +54,7 @@ export const ShippingSection = ({ deliveryType, onDeliveryTypeChange }: Shipping
onSuccess: () => { onSuccess: () => {
setTimeout(() => { setTimeout(() => {
setIsSelectedShipment(true); setIsSelectedShipment(true);
}, 500); }, 200);
}, },
}); });
} }
@@ -6,7 +6,7 @@ import { ChevronLeft } from 'lucide-react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { useCheckoutStore } from '../../store/Store'; import { useCheckoutStore } from '../../store/Store';
import { toast } from '@/components/Toast'; import { toast } from '@/components/Toast';
import { useCreateOrder } from '../../hooks/useOrderData'; import { useConfirmOrder, useCreateOrder } from '../../hooks/useOrderData';
import { extractErrorMessage } from '@/lib/func'; import { extractErrorMessage } from '@/lib/func';
import { useParams } from 'next/navigation'; import { useParams } from 'next/navigation';
import { useGetCartItems } from '@/app/[name]/(Dialogs)/cart/hooks/useCartData'; import { useGetCartItems } from '@/app/[name]/(Dialogs)/cart/hooks/useCartData';
@@ -26,6 +26,7 @@ export const SummarySection = ({ cartModal, onToggleCartModal, deliveryType }: S
const { t } = useTranslation('parallels', { keyPrefix: 'OrderDetail' }); const { t } = useTranslation('parallels', { keyPrefix: 'OrderDetail' });
const { isSelectedAddress, isSelectedShipment, isSelectedPayment } = useCheckoutStore(); const { isSelectedAddress, isSelectedShipment, isSelectedPayment } = useCheckoutStore();
const { mutate: createOrder } = useCreateOrder(); const { mutate: createOrder } = useCreateOrder();
const { mutate: confirmOrder } = useConfirmOrder();
const params = useParams(); const params = useParams();
const name = params.name as string; const name = params.name as string;
const { isSuccess } = useGetProfile(); const { isSuccess } = useGetProfile();
@@ -68,13 +69,20 @@ export const SummarySection = ({ cartModal, onToggleCartModal, deliveryType }: S
createOrder(undefined, { createOrder(undefined, {
onSuccess: (data) => { onSuccess: (data) => {
if (data?.data?.paymentUrl) { confirmOrder(data?.data?.order?.id, {
window.location.href = data?.data?.paymentUrl; onSuccess: (payment) => {
toast('در حال ریدایرکت به صفحه پرداخت...', 'success'); if (payment?.data?.paymentUrl) {
} else { window.location.href = payment?.data?.paymentUrl;
window.location.href = `/${name}/verify/${data?.data?.order?.id}`; toast('در حال ریدایرکت به صفحه پرداخت...', 'success');
toast('سفارش با موفقیت ثبت شد', 'success'); } else {
} window.location.href = `/${name}/verify/${data?.data?.order?.id}`;
toast('سفارش با موفقیت ثبت شد', 'success');
}
},
onError: (error) => {
toast(extractErrorMessage(error), 'error');
}
});
}, },
onError: (error) => { onError: (error) => {
toast(extractErrorMessage(error), 'error'); toast(extractErrorMessage(error), 'error');
@@ -11,6 +11,7 @@ import {
setTableNumber, setTableNumber,
getOrderDetail, getOrderDetail,
cancelOrder, cancelOrder,
confirmOrder,
} from "../service/OrderService"; } from "../service/OrderService";
export const useGetShipmentMethod = () => { export const useGetShipmentMethod = () => {
@@ -92,3 +93,9 @@ export const useCancelOrder = () => {
mutationFn: cancelOrder, mutationFn: cancelOrder,
}); });
}; };
export const useConfirmOrder = () => {
return useMutation({
mutationFn: confirmOrder,
});
};
@@ -72,3 +72,8 @@ export const cancelOrder = async (id: string) => {
const { data } = await api.patch(`/public/orders/${id}/cancel`); const { data } = await api.patch(`/public/orders/${id}/cancel`);
return data; return data;
}; };
export const confirmOrder = async (id: string) => {
const { data } = await api.get(`/public/payments/pay-order/${id}`);
return data;
};