feat(medusa,medusa-react): PaymentCollection support (#2659)
* chore: medusa react, order edit complete fix and single payment session
This commit is contained in:
1 parent
42d9c7222b
commit
15c667fbd3
54 files changed
+2001
-261
No files matched your search
@@ -1154,8 +1154,8 @@
|
||||
"publishable_api_key": {
|
||||
"id": "pubkey_1234",
|
||||
"created_by": "admin_user",
|
||||
"created_at": "2021-11-08 11:58:56.975971+01",
|
||||
"updated_at": "2021-11-08 11:58:56.975971+01",
|
||||
"created_at": "2021-11-08 11:58:56.975971+01",
|
||||
"updated_at": "2021-11-08 11:58:56.975971+01",
|
||||
"revoked_by": null,
|
||||
"revoked_at": null
|
||||
},
|
||||
@@ -1324,6 +1324,76 @@
|
||||
"created_at": "2022-07-05T15:16:01.959Z",
|
||||
"deleted_at": null
|
||||
}
|
||||
]
|
||||
],
|
||||
"payment_collection": {
|
||||
"id": "paycol_01GJK7P9MRHM6XWCJG70JFB8PF",
|
||||
"created_at": "2022-11-23T21:53:19.367Z",
|
||||
"updated_at": "2022-11-24T12:57:08.652Z",
|
||||
"deleted_at": null,
|
||||
"type": "order_edit",
|
||||
"status": "authorized",
|
||||
"description": null,
|
||||
"amount": 900,
|
||||
"authorized_amount": 900,
|
||||
"region_id": "test-region",
|
||||
"currency_code": "usd",
|
||||
"metadata": null,
|
||||
"created_by": "admin_user",
|
||||
"payment_sessions": [
|
||||
{
|
||||
"id": "ps_01GJMVD71Z4WF9FXXGT7DHGPCM",
|
||||
"created_at": "2022-11-24T12:57:07.883Z",
|
||||
"updated_at": "2022-11-24T12:57:08.652Z",
|
||||
"cart_id": null,
|
||||
"provider_id": "test-pay",
|
||||
"is_selected": null,
|
||||
"status": "authorized",
|
||||
"data": {},
|
||||
"idempotency_key": null,
|
||||
"amount": 900,
|
||||
"payment_authorized_at": "2022-11-24T12:57:08.672Z"
|
||||
}
|
||||
]
|
||||
},
|
||||
"payment_sessions": {
|
||||
"id": "ps_01GJMVD71Z4WF9FXXGT7DHGPCM",
|
||||
"created_at": "2022-11-24T12:57:07.883Z",
|
||||
"updated_at": "2022-11-24T12:57:08.652Z",
|
||||
"cart_id": null,
|
||||
"provider_id": "test-pay",
|
||||
"is_selected": null,
|
||||
"status": "authorized",
|
||||
"data": {},
|
||||
"idempotency_key": null,
|
||||
"amount": 900,
|
||||
"payment_authorized_at": "2022-11-24T12:57:08.672Z"
|
||||
},
|
||||
"payment": {
|
||||
"id": "pay_A1GJEVD71Z4WF9FXFGT7D1GP15",
|
||||
"swap_id": null,
|
||||
"cart_id": null,
|
||||
"order_id": null,
|
||||
"amount": 900,
|
||||
"currency_code": "usd",
|
||||
"amount_refunded": 0,
|
||||
"amount_captured": 900,
|
||||
"provider_id": "manual",
|
||||
"data": {},
|
||||
"captured_at": "2022-11-17T21:24:35.871Z",
|
||||
"canceled_at": null,
|
||||
"created_at": "2022-11-16T21:24:35.871Z",
|
||||
"updated_at": "2022-11-16T21:24:35.871Z",
|
||||
"metadata": null,
|
||||
"idempotency_key": null
|
||||
},
|
||||
"refund": {
|
||||
"order_id": null,
|
||||
"payment_id": "pay_A1GJEVD71Z4WF9FXFGT7D1GP15",
|
||||
"amount": 900,
|
||||
"note": null,
|
||||
"reason": "return",
|
||||
"metadata": null,
|
||||
"idempotency_key": null
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2104,4 +2104,103 @@ export const adminHandlers = [
|
||||
})
|
||||
)
|
||||
}),
|
||||
|
||||
rest.get("/admin/payment-collections/:id", (req, res, ctx) => {
|
||||
const { id } = req.params
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
payment_collection: {
|
||||
...fixtures.get("payment_collection"),
|
||||
id,
|
||||
},
|
||||
})
|
||||
)
|
||||
}),
|
||||
|
||||
rest.delete("/admin/payment-collections/:id", (req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
id: req.params.id,
|
||||
object: "payment_collection",
|
||||
deleted: true,
|
||||
})
|
||||
)
|
||||
}),
|
||||
|
||||
rest.post("/admin/payment-collections/:id", (req, res, ctx) => {
|
||||
const { id } = req.params
|
||||
const { description, metadata } = req.body as any
|
||||
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
payment_collection: {
|
||||
...fixtures.get("payment_collection"),
|
||||
description,
|
||||
metadata,
|
||||
id,
|
||||
},
|
||||
})
|
||||
)
|
||||
}),
|
||||
|
||||
rest.post("/admin/payment-collections/:id/authorize", (req, res, ctx) => {
|
||||
const { id } = req.params
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
payment_collection: {
|
||||
...fixtures.get("payment_collection"),
|
||||
id,
|
||||
},
|
||||
})
|
||||
)
|
||||
}),
|
||||
|
||||
rest.get("/admin/payments/:id", (req, res, ctx) => {
|
||||
const { id } = req.params
|
||||
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
payment: {
|
||||
...fixtures.get("payment"),
|
||||
id,
|
||||
},
|
||||
})
|
||||
)
|
||||
}),
|
||||
|
||||
rest.post("/admin/payments/:id/capture", (req, res, ctx) => {
|
||||
const { id } = req.params
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
payment: {
|
||||
...fixtures.get("payment"),
|
||||
id,
|
||||
},
|
||||
})
|
||||
)
|
||||
}),
|
||||
|
||||
rest.post("/admin/payments/:id/refund", (req, res, ctx) => {
|
||||
const { id } = req.params
|
||||
const { amount, reason, note } = req.body as any
|
||||
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
refund: {
|
||||
...fixtures.get("refund"),
|
||||
payment_id: id,
|
||||
amount,
|
||||
reason,
|
||||
note,
|
||||
},
|
||||
})
|
||||
)
|
||||
}),
|
||||
]
|
||||
@@ -446,4 +446,100 @@ export const storeHandlers = [
|
||||
})
|
||||
)
|
||||
}),
|
||||
|
||||
rest.get("/store/payment-collections/:id", (req, res, ctx) => {
|
||||
const { id } = req.params
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
payment_collection: {
|
||||
...fixtures.get("payment_collection"),
|
||||
id,
|
||||
},
|
||||
})
|
||||
)
|
||||
}),
|
||||
|
||||
rest.post(
|
||||
"/store/payment-collections/:id/sessions/batch",
|
||||
(req, res, ctx) => {
|
||||
const { id } = req.params
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
payment_collection: {
|
||||
...fixtures.get("payment_collection"),
|
||||
id,
|
||||
},
|
||||
})
|
||||
)
|
||||
}
|
||||
),
|
||||
|
||||
rest.post(
|
||||
"/store/payment-collections/:id/sessions/batch/authorize",
|
||||
(req, res, ctx) => {
|
||||
const { id } = req.params
|
||||
return res(
|
||||
ctx.status(207),
|
||||
ctx.json({
|
||||
payment_collection: {
|
||||
...fixtures.get("payment_collection"),
|
||||
id,
|
||||
},
|
||||
})
|
||||
)
|
||||
}
|
||||
),
|
||||
|
||||
rest.post("/store/payment-collections/:id/sessions", (req, res, ctx) => {
|
||||
const { id } = req.params
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
payment_collection: {
|
||||
...fixtures.get("payment_collection"),
|
||||
id,
|
||||
},
|
||||
})
|
||||
)
|
||||
}),
|
||||
|
||||
rest.post(
|
||||
"/store/payment-collections/:id/sessions/:session_id",
|
||||
(req, res, ctx) => {
|
||||
const { id, session_id } = req.params
|
||||
const payCol: any = { ...fixtures.get("payment_collection") }
|
||||
|
||||
payCol.payment_sessions[0].id = `new_${session_id}`
|
||||
const session = {
|
||||
payment_session: payCol.payment_sessions[0],
|
||||
}
|
||||
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
...session,
|
||||
})
|
||||
)
|
||||
}
|
||||
),
|
||||
|
||||
rest.post(
|
||||
"/store/payment-collections/:id/sessions/:session_id/authorize",
|
||||
(req, res, ctx) => {
|
||||
const { session_id } = req.params
|
||||
|
||||
const session = fixtures.get("payment_collection").payment_sessions[0]
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
payment_session: {
|
||||
...session,
|
||||
id: session_id,
|
||||
},
|
||||
})
|
||||
)
|
||||
}
|
||||
),
|
||||
]
|
||||
@@ -30,3 +30,5 @@ export * from "./tax-rates"
|
||||
export * from "./uploads"
|
||||
export * from "./users"
|
||||
export * from "./variants"
|
||||
export * from "./payment-collections"
|
||||
export * from "./payments"
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from "./queries"
|
||||
export * from "./mutations"
|
||||
@@ -0,0 +1,85 @@
|
||||
import { useMutation, UseMutationOptions, useQueryClient } from "react-query"
|
||||
import { Response } from "@medusajs/medusa-js"
|
||||
|
||||
import {
|
||||
AdminPaymentCollectionDeleteRes,
|
||||
AdminPaymentCollectionsRes,
|
||||
AdminUpdatePaymentCollectionsReq,
|
||||
} from "@medusajs/medusa"
|
||||
|
||||
import { buildOptions } from "../../utils/buildOptions"
|
||||
import { useMedusa } from "../../../contexts"
|
||||
import { adminPaymentCollectionQueryKeys } from "."
|
||||
|
||||
export const useAdminDeletePaymentCollection = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
Response<AdminPaymentCollectionDeleteRes>,
|
||||
Error,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation(
|
||||
() => client.admin.paymentCollections.delete(id),
|
||||
buildOptions(
|
||||
queryClient,
|
||||
[
|
||||
adminPaymentCollectionQueryKeys.detail(id),
|
||||
adminPaymentCollectionQueryKeys.lists(),
|
||||
],
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
export const useAdminUpdatePaymentCollection = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
Response<AdminPaymentCollectionsRes>,
|
||||
Error,
|
||||
AdminUpdatePaymentCollectionsReq
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation(
|
||||
(payload: AdminUpdatePaymentCollectionsReq) =>
|
||||
client.admin.paymentCollections.update(id, payload),
|
||||
buildOptions(
|
||||
queryClient,
|
||||
[
|
||||
adminPaymentCollectionQueryKeys.detail(id),
|
||||
adminPaymentCollectionQueryKeys.lists(),
|
||||
],
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
export const useAdminMarkPaymentCollectionAsAuthorized = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
Response<AdminPaymentCollectionsRes>,
|
||||
Error,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation(
|
||||
() => client.admin.paymentCollections.markAsAuthorized(id),
|
||||
buildOptions(
|
||||
queryClient,
|
||||
[
|
||||
adminPaymentCollectionQueryKeys.detail(id),
|
||||
adminPaymentCollectionQueryKeys.lists(),
|
||||
],
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { queryKeysFactory } from "../../utils"
|
||||
import { AdminPaymentCollectionsRes } from "@medusajs/medusa"
|
||||
import { useQuery } from "react-query"
|
||||
import { useMedusa } from "../../../contexts"
|
||||
import { UseQueryOptionsWrapper } from "../../../types"
|
||||
import { Response } from "@medusajs/medusa-js"
|
||||
|
||||
const PAYMENT_COLLECTION_QUERY_KEY = `paymentCollection` as const
|
||||
|
||||
export const adminPaymentCollectionQueryKeys = queryKeysFactory<
|
||||
typeof PAYMENT_COLLECTION_QUERY_KEY
|
||||
>(PAYMENT_COLLECTION_QUERY_KEY)
|
||||
|
||||
type AdminPaymentCollectionKey = typeof adminPaymentCollectionQueryKeys
|
||||
|
||||
export const useAdminPaymentCollection = (
|
||||
id: string,
|
||||
options?: UseQueryOptionsWrapper<
|
||||
Response<AdminPaymentCollectionsRes>,
|
||||
Error,
|
||||
ReturnType<AdminPaymentCollectionKey["detail"]>
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const { data, ...rest } = useQuery(
|
||||
adminPaymentCollectionQueryKeys.detail(id),
|
||||
() => client.admin.paymentCollections.retrieve(id),
|
||||
options
|
||||
)
|
||||
|
||||
return { ...data, ...rest } as const
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from "./queries"
|
||||
export * from "./mutations"
|
||||
@@ -0,0 +1,51 @@
|
||||
import { useMutation, UseMutationOptions, useQueryClient } from "react-query"
|
||||
import { Response } from "@medusajs/medusa-js"
|
||||
|
||||
import {
|
||||
AdminPaymentRes,
|
||||
AdminPostPaymentRefundsReq,
|
||||
AdminRefundRes,
|
||||
} from "@medusajs/medusa"
|
||||
|
||||
import { buildOptions } from "../../utils/buildOptions"
|
||||
import { useMedusa } from "../../../contexts"
|
||||
import { adminPaymentQueryKeys } from "."
|
||||
|
||||
export const useAdminPaymentsCapturePayment = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<Response<AdminPaymentRes>, Error, void>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation(
|
||||
() => client.admin.payments.capturePayment(id),
|
||||
buildOptions(
|
||||
queryClient,
|
||||
[adminPaymentQueryKeys.detail(id), adminPaymentQueryKeys.lists()],
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
export const useAdminPaymentsRefundPayment = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
Response<AdminRefundRes>,
|
||||
Error,
|
||||
AdminPostPaymentRefundsReq
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation(
|
||||
(payload: AdminPostPaymentRefundsReq) =>
|
||||
client.admin.payments.refundPayment(id, payload),
|
||||
buildOptions(
|
||||
queryClient,
|
||||
[adminPaymentQueryKeys.detail(id), adminPaymentQueryKeys.lists()],
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { queryKeysFactory } from "../../utils"
|
||||
import { AdminPaymentRes } from "@medusajs/medusa"
|
||||
import { useQuery } from "react-query"
|
||||
import { useMedusa } from "../../../contexts"
|
||||
import { UseQueryOptionsWrapper } from "../../../types"
|
||||
import { Response } from "@medusajs/medusa-js"
|
||||
|
||||
const PAYMENT_QUERY_KEY = `payment` as const
|
||||
|
||||
export const adminPaymentQueryKeys =
|
||||
queryKeysFactory<typeof PAYMENT_QUERY_KEY>(PAYMENT_QUERY_KEY)
|
||||
|
||||
type AdminPaymentKey = typeof adminPaymentQueryKeys
|
||||
|
||||
export const useAdminPayment = (
|
||||
id: string,
|
||||
options?: UseQueryOptionsWrapper<
|
||||
Response<AdminPaymentRes>,
|
||||
Error,
|
||||
ReturnType<AdminPaymentKey["detail"]>
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const { data, ...rest } = useQuery(
|
||||
adminPaymentQueryKeys.detail(id),
|
||||
() => client.admin.payments.retrieve(id),
|
||||
options
|
||||
)
|
||||
|
||||
return { ...data, ...rest } as const
|
||||
}
|
||||
@@ -13,3 +13,4 @@ export * from "./returns/"
|
||||
export * from "./gift-cards/"
|
||||
export * from "./line-items/"
|
||||
export * from "./collections"
|
||||
export * from "./payment-collections"
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from "./queries"
|
||||
export * from "./mutations"
|
||||
@@ -0,0 +1,139 @@
|
||||
import { useMutation, UseMutationOptions, useQueryClient } from "react-query"
|
||||
import { Response } from "@medusajs/medusa-js"
|
||||
|
||||
import {
|
||||
StorePaymentCollectionsRes,
|
||||
StorePostPaymentCollectionsBatchSessionsReq,
|
||||
StorePostPaymentCollectionsBatchSessionsAuthorizeReq,
|
||||
StorePaymentCollectionSessionsReq,
|
||||
StorePaymentCollectionsSessionRes,
|
||||
} from "@medusajs/medusa"
|
||||
|
||||
import { buildOptions } from "../../utils/buildOptions"
|
||||
import { useMedusa } from "../../../contexts"
|
||||
import { paymentCollectionQueryKeys } from "."
|
||||
|
||||
export const useManageMultiplePaymentSessions = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
Response<StorePaymentCollectionsRes>,
|
||||
Error,
|
||||
StorePostPaymentCollectionsBatchSessionsReq
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation(
|
||||
(payload: StorePostPaymentCollectionsBatchSessionsReq) =>
|
||||
client.paymentCollections.managePaymentSessionsBatch(id, payload),
|
||||
buildOptions(
|
||||
queryClient,
|
||||
[
|
||||
paymentCollectionQueryKeys.lists(),
|
||||
paymentCollectionQueryKeys.detail(id),
|
||||
],
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
export const useManagePaymentSession = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
Response<StorePaymentCollectionsRes>,
|
||||
Error,
|
||||
StorePaymentCollectionSessionsReq
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation(
|
||||
(payload: StorePaymentCollectionSessionsReq) =>
|
||||
client.paymentCollections.managePaymentSession(id, payload),
|
||||
buildOptions(
|
||||
queryClient,
|
||||
[
|
||||
paymentCollectionQueryKeys.lists(),
|
||||
paymentCollectionQueryKeys.detail(id),
|
||||
],
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
export const useAuthorizePaymentSession = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
Response<StorePaymentCollectionsRes>,
|
||||
Error,
|
||||
string
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation(
|
||||
(session_id: string) =>
|
||||
client.paymentCollections.authorizePaymentSession(id, session_id),
|
||||
buildOptions(
|
||||
queryClient,
|
||||
[
|
||||
paymentCollectionQueryKeys.lists(),
|
||||
paymentCollectionQueryKeys.detail(id),
|
||||
],
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
export const useAuthorizePaymentSessionsBatch = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
Response<StorePaymentCollectionsRes>,
|
||||
Error,
|
||||
StorePostPaymentCollectionsBatchSessionsAuthorizeReq
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation(
|
||||
(payload) =>
|
||||
client.paymentCollections.authorizePaymentSessionsBatch(id, payload),
|
||||
buildOptions(
|
||||
queryClient,
|
||||
[
|
||||
paymentCollectionQueryKeys.lists(),
|
||||
paymentCollectionQueryKeys.detail(id),
|
||||
],
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
export const usePaymentCollectionRefreshPaymentSession = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
Response<StorePaymentCollectionsSessionRes>,
|
||||
Error,
|
||||
string
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation(
|
||||
(session_id: string) =>
|
||||
client.paymentCollections.refreshPaymentSession(id, session_id),
|
||||
buildOptions(
|
||||
queryClient,
|
||||
[
|
||||
paymentCollectionQueryKeys.lists(),
|
||||
paymentCollectionQueryKeys.detail(id),
|
||||
],
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { queryKeysFactory } from "../../utils"
|
||||
import { StorePaymentCollectionsRes } from "@medusajs/medusa"
|
||||
import { useQuery } from "react-query"
|
||||
import { useMedusa } from "../../../contexts"
|
||||
import { UseQueryOptionsWrapper } from "../../../types"
|
||||
import { Response } from "@medusajs/medusa-js"
|
||||
|
||||
const PAYMENT_COLLECTION_QUERY_KEY = `paymentCollection` as const
|
||||
|
||||
export const paymentCollectionQueryKeys = queryKeysFactory<
|
||||
typeof PAYMENT_COLLECTION_QUERY_KEY
|
||||
>(PAYMENT_COLLECTION_QUERY_KEY)
|
||||
|
||||
type PaymentCollectionKey = typeof paymentCollectionQueryKeys
|
||||
|
||||
export const usePaymentCollection = (
|
||||
id: string,
|
||||
options?: UseQueryOptionsWrapper<
|
||||
Response<StorePaymentCollectionsRes>,
|
||||
Error,
|
||||
ReturnType<PaymentCollectionKey["detail"]>
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const { data, ...rest } = useQuery(
|
||||
paymentCollectionQueryKeys.detail(id),
|
||||
() => client.paymentCollections.retrieve(id),
|
||||
options
|
||||
)
|
||||
|
||||
return { ...data, ...rest } as const
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
import {
|
||||
useAdminDeletePaymentCollection,
|
||||
useAdminUpdatePaymentCollection,
|
||||
useAdminMarkPaymentCollectionAsAuthorized,
|
||||
} from "../../../../src"
|
||||
import { renderHook } from "@testing-library/react-hooks"
|
||||
import { createWrapper } from "../../../utils"
|
||||
|
||||
describe("useAdminDeletePaymentCollection hook", () => {
|
||||
test("Delete a payment collection", async () => {
|
||||
const { result, waitFor } = renderHook(
|
||||
() => useAdminDeletePaymentCollection("payment_collection_id"),
|
||||
{
|
||||
wrapper: createWrapper(),
|
||||
}
|
||||
)
|
||||
|
||||
result.current.mutate()
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.data.response.status).toEqual(200)
|
||||
expect(result.current.data).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "payment_collection_id",
|
||||
deleted: true,
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("useAdminUpdatePaymentCollection hook", () => {
|
||||
test("Update a Payment Collection", async () => {
|
||||
const { result, waitFor } = renderHook(
|
||||
() => useAdminUpdatePaymentCollection("payment_collection_id"),
|
||||
{
|
||||
wrapper: createWrapper(),
|
||||
}
|
||||
)
|
||||
|
||||
result.current.mutate({
|
||||
description: "new description",
|
||||
metadata: { demo: "obj" },
|
||||
})
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.data.response.status).toEqual(200)
|
||||
expect(result.current.data.payment_collection).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "payment_collection_id",
|
||||
description: "new description",
|
||||
metadata: { demo: "obj" },
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("useAdminMarkPaymentCollectionAsAuthorized hook", () => {
|
||||
test("Mark a Payment Collection as Authorized", async () => {
|
||||
const { result, waitFor } = renderHook(
|
||||
() => useAdminMarkPaymentCollectionAsAuthorized("payment_collection_id"),
|
||||
{
|
||||
wrapper: createWrapper(),
|
||||
}
|
||||
)
|
||||
|
||||
result.current.mutate()
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.data.response.status).toEqual(200)
|
||||
expect(result.current.data.payment_collection).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "payment_collection_id",
|
||||
status: "authorized",
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,21 @@
|
||||
import { renderHook } from "@testing-library/react-hooks"
|
||||
import { fixtures } from "../../../../mocks/data"
|
||||
import { createWrapper } from "../../../utils"
|
||||
import { useAdminPaymentCollection } from "../../../../src/hooks/admin/payment-collections"
|
||||
|
||||
describe("useAdminPaymentCollection hook", () => {
|
||||
test("returns a payment collection", async () => {
|
||||
const payment_collection = fixtures.get("payment_collection")
|
||||
const { result, waitFor } = renderHook(
|
||||
() => useAdminPaymentCollection(payment_collection.id),
|
||||
{
|
||||
wrapper: createWrapper(),
|
||||
}
|
||||
)
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.response.status).toEqual(200)
|
||||
expect(result.current.payment_collection).toEqual(payment_collection)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,58 @@
|
||||
import {
|
||||
useAdminPaymentsCapturePayment,
|
||||
useAdminPaymentsRefundPayment,
|
||||
} from "../../../../src"
|
||||
import { renderHook } from "@testing-library/react-hooks"
|
||||
import { createWrapper } from "../../../utils"
|
||||
import { RefundReason } from "@medusajs/medusa"
|
||||
|
||||
describe("useAdminPaymentsCapturePayment hook", () => {
|
||||
test("Capture a payment", async () => {
|
||||
const { result, waitFor } = renderHook(
|
||||
() => useAdminPaymentsCapturePayment("payment_id"),
|
||||
{
|
||||
wrapper: createWrapper(),
|
||||
}
|
||||
)
|
||||
|
||||
result.current.mutate()
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.data.response.status).toEqual(200)
|
||||
expect(result.current.data.payment).toEqual(
|
||||
expect.objectContaining({
|
||||
amount_captured: 900,
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("useAdminPaymentsRefundPayment hook", () => {
|
||||
test("Update a Payment Collection", async () => {
|
||||
const { result, waitFor } = renderHook(
|
||||
() => useAdminPaymentsRefundPayment("payment_id"),
|
||||
{
|
||||
wrapper: createWrapper(),
|
||||
}
|
||||
)
|
||||
|
||||
result.current.mutate({
|
||||
amount: 500,
|
||||
reason: RefundReason.DISCOUNT,
|
||||
note: "note to refund",
|
||||
})
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.data.response.status).toEqual(200)
|
||||
expect(result.current.data.refund).toEqual(
|
||||
expect.objectContaining({
|
||||
payment_id: "payment_id",
|
||||
amount: 500,
|
||||
reason: RefundReason.DISCOUNT,
|
||||
note: "note to refund",
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,18 @@
|
||||
import { renderHook } from "@testing-library/react-hooks"
|
||||
import { fixtures } from "../../../../mocks/data"
|
||||
import { createWrapper } from "../../../utils"
|
||||
import { useAdminPayment } from "../../../../src/hooks/admin/payments"
|
||||
|
||||
describe("useAdminPayment hook", () => {
|
||||
test("returns a payment collection", async () => {
|
||||
const payment = fixtures.get("payment")
|
||||
const { result, waitFor } = renderHook(() => useAdminPayment(payment.id), {
|
||||
wrapper: createWrapper(),
|
||||
})
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.response.status).toEqual(200)
|
||||
expect(result.current.payment).toEqual(payment)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,138 @@
|
||||
import {
|
||||
useManageMultiplePaymentSessions,
|
||||
useManagePaymentSession,
|
||||
useAuthorizePaymentSession,
|
||||
useAuthorizePaymentSessionsBatch,
|
||||
usePaymentCollectionRefreshPaymentSession,
|
||||
} from "../../../../src"
|
||||
import { renderHook } from "@testing-library/react-hooks"
|
||||
import { createWrapper } from "../../../utils"
|
||||
|
||||
describe("useManageMultiplePaymentSessions hook", () => {
|
||||
test("Manage multiple payment sessions of a payment collection", async () => {
|
||||
const { result, waitFor } = renderHook(
|
||||
() => useManageMultiplePaymentSessions("payment_collection_id"),
|
||||
{
|
||||
wrapper: createWrapper(),
|
||||
}
|
||||
)
|
||||
|
||||
result.current.mutate({
|
||||
sessions: {
|
||||
provider_id: "manual",
|
||||
amount: 900,
|
||||
},
|
||||
})
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.data.response.status).toEqual(200)
|
||||
expect(result.current.data?.payment_collection).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "payment_collection_id",
|
||||
amount: 900,
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("useManagePaymentSession hook", () => {
|
||||
test("Manage payment session of a payment collection", async () => {
|
||||
const { result, waitFor } = renderHook(
|
||||
() => useManagePaymentSession("payment_collection_id"),
|
||||
{
|
||||
wrapper: createWrapper(),
|
||||
}
|
||||
)
|
||||
|
||||
result.current.mutate({
|
||||
provider_id: "manual",
|
||||
})
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.data.response.status).toEqual(200)
|
||||
expect(result.current.data?.payment_collection).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "payment_collection_id",
|
||||
amount: 900,
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("useAuthorizePaymentSession hook", () => {
|
||||
test("Authorize a payment session of a Payment Collection", async () => {
|
||||
const { result, waitFor } = renderHook(
|
||||
() => useAuthorizePaymentSession("payment_collection_id"),
|
||||
{
|
||||
wrapper: createWrapper(),
|
||||
}
|
||||
)
|
||||
|
||||
result.current.mutate("123")
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.data.response.status).toEqual(200)
|
||||
expect(result.current.data.payment_session).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "123",
|
||||
amount: 900,
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("authorizePaymentSessionsBatch hook", () => {
|
||||
test("Authorize all payment sessions of a Payment Collection", async () => {
|
||||
const { result, waitFor } = renderHook(
|
||||
() => useAuthorizePaymentSessionsBatch("payment_collection_id"),
|
||||
{
|
||||
wrapper: createWrapper(),
|
||||
}
|
||||
)
|
||||
|
||||
result.current.mutate({
|
||||
session_ids: ["abc"],
|
||||
})
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.data.response.status).toEqual(207)
|
||||
|
||||
expect(result.current.data.payment_collection).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "payment_collection_id",
|
||||
payment_sessions: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
amount: 900,
|
||||
}),
|
||||
]),
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("usePaymentCollectionRefreshPaymentSession hook", () => {
|
||||
test("Refresh a payment sessions of a Payment Collection", async () => {
|
||||
const { result, waitFor } = renderHook(
|
||||
() => usePaymentCollectionRefreshPaymentSession("payment_collection_id"),
|
||||
{
|
||||
wrapper: createWrapper(),
|
||||
}
|
||||
)
|
||||
|
||||
result.current.mutate("session_id")
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.data.response.status).toEqual(200)
|
||||
expect(result.current.data.payment_session).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "new_session_id",
|
||||
amount: 900,
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,21 @@
|
||||
import { renderHook } from "@testing-library/react-hooks"
|
||||
import { fixtures } from "../../../../mocks/data"
|
||||
import { createWrapper } from "../../../utils"
|
||||
import { usePaymentCollection } from "../../../../src/hooks/store/payment-collections"
|
||||
|
||||
describe("usePaymentCollection hook", () => {
|
||||
test("returns a payment collection", async () => {
|
||||
const payment_collection = fixtures.get("payment_collection")
|
||||
const { result, waitFor } = renderHook(
|
||||
() => usePaymentCollection(payment_collection.id),
|
||||
{
|
||||
wrapper: createWrapper(),
|
||||
}
|
||||
)
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.response.status).toEqual(200)
|
||||
expect(result.current.payment_collection).toEqual(payment_collection)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user