import { Order } from "@medusajs/medusa" import { useAdminRefundPayment } from "medusa-react" import { useMemo, useState } from "react" import { Controller, useForm } from "react-hook-form" import { useTranslation } from "react-i18next" import Button from "../../../../components/fundamentals/button" import AlertIcon from "../../../../components/fundamentals/icons/alert-icon" import CheckIcon from "../../../../components/fundamentals/icons/check-icon" import IconTooltip from "../../../../components/molecules/icon-tooltip" import Modal from "../../../../components/molecules/modal" import Select from "../../../../components/molecules/select" import TextArea from "../../../../components/molecules/textarea" import CurrencyInput from "../../../../components/organisms/currency-input" import useNotification from "../../../../hooks/use-notification" import { Option } from "../../../../types/shared" import { getErrorMessage } from "../../../../utils/error-messages" import FormValidator from "../../../../utils/form-validator" type RefundMenuFormData = { amount: number reason: Option no_notification: boolean note?: string } const reasonOptions = [ { label: "Discount", value: "discount" }, { label: "Other", value: "other" }, ] type RefundMenuProps = { order: Order onDismiss: () => void initialAmount?: number initialReason: "other" | "discount" } const RefundMenu = ({ order, onDismiss, initialAmount, initialReason, }: RefundMenuProps) => { const { t } = useTranslation() const { register, handleSubmit, control } = useForm({ defaultValues: { amount: initialAmount, reason: reasonOptions[initialReason === "other" ? 1 : 0], }, }) const [noNotification, setNoNotification] = useState(order.no_notification) const notification = useNotification() const { mutate, isLoading } = useAdminRefundPayment(order.id) const refundable = useMemo(() => { return order.paid_total - order.refunded_total }, [order]) const handleValidateRefundAmount = (value) => { return value <= refundable } const onSubmit = (data: RefundMenuFormData) => { mutate( { amount: data.amount, reason: data.reason.value, no_notification: noNotification, note: data.note, }, { onSuccess: () => { notification( t("refund-success", "Success"), t( "refund-successfully-refunded-order", "Successfully refunded order" ), "success" ) onDismiss() }, onError: (error) => { notification( t("refund-error", "Error"), getErrorMessage(error), "error" ) }, } ) } const isSystemPayment = order.payments.some((p) => p.provider_id === "system") return (

{t("refund-create-a-refund", "Create a refund")}

{isSystemPayment && (
{t("refund-attention", "Attention!")} {t( "refund-system-payment-disclaimer", "One or more of your payments is a system payment. Be aware, that captures and refunds are not handled by Medusa for such payments." )}
)} {t("refund-details", "Details")}
( )} /> (