|
1
|
import { HttpTypes } from "@medusajs/types" |
|
2
|
import { |
|
3
|
QueryKey, |
|
4
|
useMutation, |
|
5
|
UseMutationOptions, |
|
6
|
useQuery, |
|
7
|
UseQueryOptions, |
|
8
|
} from "@tanstack/react-query" |
|
9
|
import { sdk } from "../../lib/client" |
|
10
|
import { queryClient } from "../../lib/query-client" |
|
11
|
import { queryKeysFactory } from "../../lib/query-key-factory" |
|
12
|
import { ordersQueryKeys } from "./orders" |
|
13
|
import { FetchError } from "@medusajs/js-sdk" |
|
14
|
|
|
15
|
const PAYMENT_QUERY_KEY = "payment" as const |
|
16
|
export const paymentQueryKeys = queryKeysFactory(PAYMENT_QUERY_KEY) |
|
17
|
|
|
18
|
const PAYMENT_PROVIDERS_QUERY_KEY = "payment_providers" as const |
|
19
|
export const paymentProvidersQueryKeys = queryKeysFactory( |
|
20
|
PAYMENT_PROVIDERS_QUERY_KEY |
|
21
|
) |
|
22
|
|
|
23
|
export const usePaymentProviders = ( |
|
24
|
query?: HttpTypes.AdminGetPaymentProvidersParams, |
|
25
|
options?: Omit< |
|
26
|
UseQueryOptions< |
|
27
|
HttpTypes.AdminGetPaymentProvidersParams, |
|
28
|
FetchError, |
|
29
|
HttpTypes.AdminPaymentProviderListResponse, |
|
30
|
QueryKey |
|
31
|
>, |
|
32
|
"queryKey" | "queryFn" |
|
33
|
> |
|
34
|
) => { |
|
35
|
const { data, ...rest } = useQuery({ |
|
36
|
queryFn: async () => sdk.admin.payment.listPaymentProviders(query), |
|
37
|
queryKey: paymentProvidersQueryKeys.list(query), |
|
38
|
...options, |
|
39
|
}) |
|
40
|
|
|
41
|
return { ...data, ...rest } |
|
42
|
} |
|
43
|
|
|
44
|
export const usePayment = ( |
|
45
|
id: string, |
|
46
|
query?: HttpTypes.AdminPaymentFilters, |
|
47
|
options?: Omit< |
|
48
|
UseQueryOptions< |
|
49
|
HttpTypes.AdminPaymentResponse, |
|
50
|
FetchError, |
|
51
|
HttpTypes.AdminPaymentResponse, |
|
52
|
QueryKey |
|
53
|
>, |
|
54
|
"queryKey" | "queryFn" |
|
55
|
> |
|
56
|
) => { |
|
57
|
const { data, ...rest } = useQuery({ |
|
58
|
queryFn: () => sdk.admin.payment.retrieve(id, query), |
|
59
|
queryKey: paymentQueryKeys.detail(id), |
|
60
|
...options, |
|
61
|
}) |
|
62
|
|
|
63
|
return { ...data, ...rest } |
|
64
|
} |
|
65
|
|
|
66
|
export const useCapturePayment = ( |
|
67
|
orderId: string, |
|
68
|
paymentId: string, |
|
69
|
options?: UseMutationOptions< |
|
70
|
HttpTypes.AdminPaymentResponse, |
|
71
|
FetchError, |
|
72
|
HttpTypes.AdminCapturePayment |
|
73
|
> |
|
74
|
) => { |
|
75
|
return useMutation({ |
|
76
|
mutationFn: (payload) => sdk.admin.payment.capture(paymentId, payload), |
|
77
|
onSuccess: (data, variables, context) => { |
|
78
|
queryClient.invalidateQueries({ |
|
79
|
queryKey: ordersQueryKeys.details(), |
|
80
|
}) |
|
81
|
|
|
82
|
queryClient.invalidateQueries({ |
|
83
|
queryKey: ordersQueryKeys.preview(orderId), |
|
84
|
}) |
|
85
|
|
|
86
|
options?.onSuccess?.(data, variables, context) |
|
87
|
}, |
|
88
|
...options, |
|
89
|
}) |
|
90
|
} |
|
91
|
|
|
92
|
export const useRefundPayment = ( |
|
93
|
orderId: string, |
|
94
|
paymentId: string, |
|
95
|
options?: UseMutationOptions< |
|
96
|
HttpTypes.AdminPaymentResponse, |
|
97
|
FetchError, |
|
98
|
HttpTypes.AdminRefundPayment |
|
99
|
> |
|
100
|
) => { |
|
101
|
return useMutation({ |
|
102
|
mutationFn: (payload) => sdk.admin.payment.refund(paymentId, payload), |
|
103
|
onSuccess: (data, variables, context) => { |
|
104
|
queryClient.invalidateQueries({ |
|
105
|
queryKey: ordersQueryKeys.details(), |
|
106
|
}) |
|
107
|
|
|
108
|
queryClient.invalidateQueries({ |
|
109
|
queryKey: ordersQueryKeys.preview(orderId), |
|
110
|
}) |
|
111
|
|
|
112
|
options?.onSuccess?.(data, variables, context) |
|
113
|
}, |
|
114
|
...options, |
|
115
|
}) |
|
116
|
} |