feat(dashboard,core-flows,js-sdk,types): ability to mark payment as paid (#8679)

* feat(core-flows): create or update payment collections in RMA flows

* chore: change ui to pick payment link from unpaid payment collection

* Apply suggestions from code review

Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com>

* chore: fix mathbn

* feat(dashboard,core-flows,js-sdk,types): ability to mark payment as paid

* chore: add captured bt

---------

Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com>
This commit is contained in:
Riqwan Thamir
2024-08-20 22:58:28 +02:00
committed by GitHub
parent 99eca64c20
commit 8bd284779e
12 changed files with 273 additions and 6 deletions
@@ -35,6 +35,32 @@ export const useCreatePaymentCollection = (
})
}
export const useMarkPaymentCollectionAsPaid = (
paymentCollectionId: string,
options?: UseMutationOptions<
HttpTypes.AdminPaymentCollectionResponse,
Error,
HttpTypes.AdminMarkPaymentCollectionAsPaid
>
) => {
return useMutation({
mutationFn: (payload) =>
sdk.admin.paymentCollection.markAsPaid(paymentCollectionId, payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({
queryKey: ordersQueryKeys.all,
})
queryClient.invalidateQueries({
queryKey: paymentCollectionQueryKeys.all,
})
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useDeletePaymentCollection = (
options?: Omit<
UseMutationOptions<
@@ -814,6 +814,7 @@
"totalPaidByCustomer": "Total paid by customer",
"capture": "Capture payment",
"refund": "Refund",
"markAsPaid": "Mark as paid",
"statusLabel": "Payment status",
"statusTitle": "Payment Status",
"status": {
@@ -830,6 +831,8 @@
},
"capturePayment": "Payment of {{amount}} will be captured.",
"capturePaymentSuccess": "Payment of {{amount}} successfully captured",
"markAsPaidPayment": "Payment of {{amount}} will be marked as paid.",
"markAsPaidPaymentSuccess": "Payment of {{amount}} successfully marked as paid",
"createRefund": "Create Refund",
"refundPaymentSuccess": "Refund of amount {{amount}} successful",
"createRefundWrongQuantity": "Quantity should be a number between 1 and {{number}}",
@@ -27,18 +27,23 @@ import {
Heading,
StatusBadge,
Text,
toast,
Tooltip,
usePrompt,
} from "@medusajs/ui"
import { AdminPaymentCollection } from "../../../../../../../../core/types/dist/http/payment/admin/entities"
import { ActionMenu } from "../../../../../components/common/action-menu"
import { ButtonMenu } from "../../../../../components/common/button-menu/button-menu.tsx"
import { Thumbnail } from "../../../../../components/common/thumbnail"
import { useClaims } from "../../../../../hooks/api/claims.tsx"
import { useExchanges } from "../../../../../hooks/api/exchanges.tsx"
import { useOrderPreview } from "../../../../../hooks/api/orders.tsx"
import { useMarkPaymentCollectionAsPaid } from "../../../../../hooks/api/payment-collections.tsx"
import { useReservationItems } from "../../../../../hooks/api/reservations"
import { useReturns } from "../../../../../hooks/api/returns"
import { useDate } from "../../../../../hooks/use-date"
import { formatCurrency } from "../../../../../lib/format-currency.ts"
import {
getLocaleAmount,
getStylizedAmount,
@@ -54,6 +59,7 @@ type OrderSummarySectionProps = {
export const OrderSummarySection = ({ order }: OrderSummarySectionProps) => {
const { t } = useTranslation()
const navigate = useNavigate()
const prompt = usePrompt()
const { reservations } = useReservationItems(
{
@@ -104,10 +110,54 @@ export const OrderSummarySection = ({ order }: OrderSummarySectionProps) => {
(pc) => pc.status === "not_paid"
)
const { mutateAsync: markAsPaid } = useMarkPaymentCollectionAsPaid(
unpaidPaymentCollection?.id!
)
const showPayment =
unpaidPaymentCollection && (order?.summary?.pending_difference || 0) > 0
const showRefund = (order?.summary?.pending_difference || 0) < 0
const handleMarkAsPaid = async (
paymentCollection: AdminPaymentCollection
) => {
const res = await prompt({
title: t("orders.payment.markAsPaid"),
description: t("orders.payment.markAsPaidPayment", {
amount: formatCurrency(
paymentCollection.amount as number,
order.currency_code
),
}),
confirmText: t("actions.confirm"),
cancelText: t("actions.cancel"),
variant: "confirmation",
})
if (!res) {
return
}
await markAsPaid(
{ order_id: order.id },
{
onSuccess: () => {
toast.success(
t("orders.payment.markAsPaidPaymentSuccess", {
amount: formatCurrency(
paymentCollection.amount as number,
order.currency_code
),
})
)
},
onError: (error) => {
toast.error(error.message)
},
}
)
}
return (
<Container className="divide-y divide-dashed p-0">
<Header order={order} orderPreview={orderPreview} />
@@ -152,6 +202,16 @@ export const OrderSummarySection = ({ order }: OrderSummarySectionProps) => {
order={order}
/>
)}
{showPayment && (
<Button
size="small"
variant="secondary"
onClick={() => handleMarkAsPaid(unpaidPaymentCollection)}
>
{t("orders.payment.markAsPaid")}
</Button>
)}
</div>
)}
</Container>