From d30806533c181be39b97b0321a44390cd31acd3c Mon Sep 17 00:00:00 2001 From: William Bouchard <46496014+willbouch@users.noreply.github.com> Date: Tue, 30 Sep 2025 13:17:43 -0400 Subject: [PATCH] fix(dashboard): create refund form broken when no payment id defined (#13631) * fix(dashboard): create refund form broken when no payment id defined * Create funny-donuts-destroy.md --- .changeset/funny-donuts-destroy.md | 5 + .../create-refund-form/create-refund-form.tsx | 105 +++++++++++++++--- 2 files changed, 97 insertions(+), 13 deletions(-) create mode 100644 .changeset/funny-donuts-destroy.md diff --git a/.changeset/funny-donuts-destroy.md b/.changeset/funny-donuts-destroy.md new file mode 100644 index 0000000000..9427b7798f --- /dev/null +++ b/.changeset/funny-donuts-destroy.md @@ -0,0 +1,5 @@ +--- +"@medusajs/dashboard": patch +--- + +fix(dashboard): create refund form broken when no payment id defined diff --git a/packages/admin/dashboard/src/routes/orders/order-create-refund/components/create-refund-form/create-refund-form.tsx b/packages/admin/dashboard/src/routes/orders/order-create-refund/components/create-refund-form/create-refund-form.tsx index 38f19a0331..a55641d082 100644 --- a/packages/admin/dashboard/src/routes/orders/order-create-refund/components/create-refund-form/create-refund-form.tsx +++ b/packages/admin/dashboard/src/routes/orders/order-create-refund/components/create-refund-form/create-refund-form.tsx @@ -1,7 +1,14 @@ import { zodResolver } from "@hookform/resolvers/zod" import { HttpTypes } from "@medusajs/types" -import { Button, CurrencyInput, Select, Textarea, toast } from "@medusajs/ui" -import { useMemo, useState } from "react" +import { + Button, + CurrencyInput, + Label, + Select, + Textarea, + toast, +} from "@medusajs/ui" +import { useEffect, useMemo, useState } from "react" import { formatValue } from "react-currency-input-field" import { useForm } from "react-hook-form" import { useTranslation } from "react-i18next" @@ -16,6 +23,7 @@ import { formatCurrency } from "../../../../../lib/format-currency" import { getLocaleAmount } from "../../../../../lib/money-amount-helpers" import { getPaymentsFromOrder } from "../../../../../lib/orders" import { useDocumentDirection } from "../../../../../hooks/use-document-direction" +import { formatProvider } from "../../../../../lib/format-provider.ts" type CreateRefundFormProps = { order: HttpTypes.AdminOrder @@ -36,11 +44,12 @@ export const CreateRefundForm = ({ order }: CreateRefundFormProps) => { const { refund_reasons } = useRefundReasons() const [searchParams] = useSearchParams() + const hasPaymentIdInSearchParams = !!searchParams.get("paymentId") const [paymentId, setPaymentId] = useState( searchParams.get("paymentId") || undefined ) const payments = getPaymentsFromOrder(order) - const payment = payments.find((p) => p.id === paymentId)! + const payment = payments.find((p) => p.id === paymentId) const paymentAmount = payment?.amount || 0 const currency = useMemo( @@ -59,6 +68,23 @@ export const CreateRefundForm = ({ order }: CreateRefundFormProps) => { resolver: zodResolver(CreateRefundSchema), }) + useEffect(() => { + const pendingDifference = order.summary.pending_difference as number + const paymentAmount = (payment?.amount || 0) as number + const pendingAmount = + pendingDifference < 0 + ? Math.min(Math.abs(pendingDifference), paymentAmount) + : paymentAmount + + const normalizedAmount = + pendingAmount < 0 ? pendingAmount * -1 : pendingAmount + + form.setValue("amount", { + value: normalizedAmount.toFixed(currency.decimal_digits), + float: normalizedAmount, + }) + }, [payment?.id || ""]) + const { mutateAsync, isPending } = useRefundPayment(order.id, payment?.id!) const handleSubmit = form.handleSubmit(async (data) => { @@ -96,16 +122,69 @@ export const CreateRefundForm = ({ order }: CreateRefundFormProps) => { >
-
- - {getLocaleAmount( - payment.amount as number, - payment.currency_code - )} - -  -  - (#{payment.id.substring(23)}) -
+ {!hasPaymentIdInSearchParams && ( + + )} + {hasPaymentIdInSearchParams && ( +
+ + {getLocaleAmount( + payment!.amount as number, + payment!.currency_code + )} + +  -  + (#{payment!.id.substring(23)}) +
+ )}