test: Introduce V2 tests in Admin Payments API (#6621)
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
const { medusaIntegrationTestRunner } = require("medusa-test-utils")
|
const { medusaIntegrationTestRunner } = require("medusa-test-utils")
|
||||||
const { createAdminUser } = require("../../../helpers/create-admin-user")
|
const { createAdminUser } = require("../../../helpers/create-admin-user")
|
||||||
|
const { breaking } = require("../../../helpers/breaking")
|
||||||
|
const { ModuleRegistrationName } = require("@medusajs/modules-sdk")
|
||||||
|
|
||||||
jest.setTimeout(30000)
|
jest.setTimeout(30000)
|
||||||
|
|
||||||
@@ -11,9 +13,47 @@ const adminHeaders = {
|
|||||||
|
|
||||||
let { simpleCustomerFactory, simplePaymentCollectionFactory } = {}
|
let { simpleCustomerFactory, simplePaymentCollectionFactory } = {}
|
||||||
|
|
||||||
|
const createV1PaymentSetup = async (dbConnection, payCol, api) => {
|
||||||
|
// create payment collection
|
||||||
|
payCol = await simplePaymentCollectionFactory(dbConnection, {
|
||||||
|
description: "paycol description",
|
||||||
|
amount: 1000,
|
||||||
|
})
|
||||||
|
|
||||||
|
// create payment session
|
||||||
|
const payColRes = await api.post(
|
||||||
|
`/store/payment-collections/${payCol.id}/sessions`,
|
||||||
|
{
|
||||||
|
provider_id: "test-pay",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// authorize payment session
|
||||||
|
await api.post(
|
||||||
|
`/store/payment-collections/${payCol.id}/sessions/batch/authorize`,
|
||||||
|
{
|
||||||
|
session_ids: payColRes.data.payment_collection.payment_sessions.map(
|
||||||
|
({ id }) => id
|
||||||
|
),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// get payment collection
|
||||||
|
const response = await api.get(
|
||||||
|
`/admin/payment-collections/${payCol.id}`,
|
||||||
|
adminHeaders
|
||||||
|
)
|
||||||
|
|
||||||
|
// return payment
|
||||||
|
return response.data.payment_collection.payments[0]
|
||||||
|
}
|
||||||
|
|
||||||
medusaIntegrationTestRunner({
|
medusaIntegrationTestRunner({
|
||||||
env: { MEDUSA_FF_PRODUCT_CATEGORIES: true },
|
// env: { MEDUSA_FF_MEDUSA_V2: true },
|
||||||
testSuite: ({ dbConnection, getContainer, api }) => {
|
testSuite: ({ dbConnection, getContainer, api }) => {
|
||||||
|
let container
|
||||||
|
let paymentService
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
;({
|
;({
|
||||||
simplePaymentCollectionFactory,
|
simplePaymentCollectionFactory,
|
||||||
@@ -24,11 +64,13 @@ medusaIntegrationTestRunner({
|
|||||||
})
|
})
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const container = getContainer()
|
container = getContainer()
|
||||||
await createAdminUser(dbConnection, adminHeaders, container)
|
await createAdminUser(dbConnection, adminHeaders, container)
|
||||||
|
|
||||||
|
paymentService = container.resolve(ModuleRegistrationName.PAYMENT)
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("POST /admin/payment-collections/:id", () => {
|
describe("Admin Payments API", () => {
|
||||||
let payCol
|
let payCol
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
@@ -36,42 +78,44 @@ medusaIntegrationTestRunner({
|
|||||||
id: "customer",
|
id: "customer",
|
||||||
email: "test@customer.com",
|
email: "test@customer.com",
|
||||||
})
|
})
|
||||||
|
|
||||||
payCol = await simplePaymentCollectionFactory(dbConnection, {
|
|
||||||
description: "paycol description",
|
|
||||||
amount: 10000,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it("Captures an authorized payment", async () => {
|
it("Captures an authorized payment", async () => {
|
||||||
// create payment session
|
const payment = await breaking(
|
||||||
const payColRes = await api.post(
|
async () => {
|
||||||
`/store/payment-collections/${payCol.id}/sessions`,
|
const v1Payment = await createV1PaymentSetup(
|
||||||
{
|
dbConnection,
|
||||||
provider_id: "test-pay",
|
payCol,
|
||||||
|
api
|
||||||
|
)
|
||||||
|
return v1Payment
|
||||||
|
},
|
||||||
|
async () => {
|
||||||
|
const paymentCollection =
|
||||||
|
await paymentService.createPaymentCollections({
|
||||||
|
region_id: "test-region",
|
||||||
|
amount: 1000,
|
||||||
|
currency_code: "usd",
|
||||||
|
})
|
||||||
|
|
||||||
|
const paymentSession = await paymentService.createPaymentSession(
|
||||||
|
paymentCollection.id,
|
||||||
|
{
|
||||||
|
provider_id: "pp_system_default",
|
||||||
|
amount: 1000,
|
||||||
|
currency_code: "usd",
|
||||||
|
data: {},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
const payment = await paymentService.authorizePaymentSession(
|
||||||
|
paymentSession.id,
|
||||||
|
{}
|
||||||
|
)
|
||||||
|
|
||||||
|
return payment
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
await api.post(
|
|
||||||
`/store/payment-collections/${payCol.id}/sessions/batch/authorize`,
|
|
||||||
{
|
|
||||||
session_ids: payColRes.data.payment_collection.payment_sessions.map(
|
|
||||||
({ id }) => id
|
|
||||||
),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
const paymentCollections = await api.get(
|
|
||||||
`/admin/payment-collections/${payCol.id}`,
|
|
||||||
adminHeaders
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(
|
|
||||||
paymentCollections.data.payment_collection.payments
|
|
||||||
).toHaveLength(1)
|
|
||||||
|
|
||||||
const payment = paymentCollections.data.payment_collection.payments[0]
|
|
||||||
|
|
||||||
expect(payment.captured_at).toBe(null)
|
|
||||||
|
|
||||||
const response = await api.post(
|
const response = await api.post(
|
||||||
`/admin/payments/${payment.id}/capture`,
|
`/admin/payments/${payment.id}/capture`,
|
||||||
@@ -83,69 +127,134 @@ medusaIntegrationTestRunner({
|
|||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: payment.id,
|
id: payment.id,
|
||||||
captured_at: expect.any(String),
|
captured_at: expect.any(String),
|
||||||
amount: 10000,
|
...breaking(
|
||||||
|
() => ({}),
|
||||||
|
() => ({
|
||||||
|
captures: [
|
||||||
|
expect.objectContaining({
|
||||||
|
id: expect.any(String),
|
||||||
|
amount: 1000,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
refunds: [],
|
||||||
|
})
|
||||||
|
),
|
||||||
|
amount: 1000,
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
expect(response.status).toEqual(200)
|
expect(response.status).toEqual(200)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("Refunds an captured payment", async () => {
|
it("Refunds an captured payment", async () => {
|
||||||
// create payment session
|
const payment = await breaking(
|
||||||
const payColRes = await api.post(
|
async () => {
|
||||||
`/store/payment-collections/${payCol.id}/sessions`,
|
const v1Payment = await createV1PaymentSetup(
|
||||||
{
|
dbConnection,
|
||||||
provider_id: "test-pay",
|
payCol,
|
||||||
}
|
api
|
||||||
)
|
)
|
||||||
await api.post(
|
|
||||||
`/store/payment-collections/${payCol.id}/sessions/batch/authorize`,
|
|
||||||
{
|
|
||||||
session_ids: payColRes.data.payment_collection.payment_sessions.map(
|
|
||||||
({ id }) => id
|
|
||||||
),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
const paymentCollections = await api.get(
|
await api.post(
|
||||||
`/admin/payment-collections/${payCol.id}`,
|
`/admin/payments/${v1Payment.id}/capture`,
|
||||||
adminHeaders
|
undefined,
|
||||||
)
|
adminHeaders
|
||||||
const payment = paymentCollections.data.payment_collection.payments[0]
|
)
|
||||||
await api.post(
|
|
||||||
`/admin/payments/${payment.id}/capture`,
|
return v1Payment
|
||||||
undefined,
|
},
|
||||||
adminHeaders
|
async () => {
|
||||||
|
const paymentCollection =
|
||||||
|
await paymentService.createPaymentCollections({
|
||||||
|
region_id: "test-region",
|
||||||
|
amount: 1000,
|
||||||
|
currency_code: "usd",
|
||||||
|
})
|
||||||
|
|
||||||
|
const paymentSession = await paymentService.createPaymentSession(
|
||||||
|
paymentCollection.id,
|
||||||
|
{
|
||||||
|
provider_id: "pp_system_default",
|
||||||
|
amount: 1000,
|
||||||
|
currency_code: "usd",
|
||||||
|
data: {},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
const payment = await paymentService.authorizePaymentSession(
|
||||||
|
paymentSession.id,
|
||||||
|
{}
|
||||||
|
)
|
||||||
|
|
||||||
|
await paymentService.capturePayment({
|
||||||
|
payment_id: payment.id,
|
||||||
|
amount: 1000,
|
||||||
|
})
|
||||||
|
|
||||||
|
return payment
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// refund
|
// refund
|
||||||
const response = await api.post(
|
const response = await api.post(
|
||||||
`/admin/payments/${payment.id}/refund`,
|
`/admin/payments/${payment.id}/refund`,
|
||||||
{
|
{
|
||||||
amount: 5000,
|
amount: 500,
|
||||||
reason: "return",
|
...breaking(
|
||||||
note: "Do not like it",
|
() => ({
|
||||||
|
// TODO: We should probably introduce this in V2 too
|
||||||
|
reason: "return",
|
||||||
|
note: "Do not like it",
|
||||||
|
}),
|
||||||
|
() => ({})
|
||||||
|
),
|
||||||
},
|
},
|
||||||
adminHeaders
|
adminHeaders
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(response.data.refund).toEqual(
|
|
||||||
expect.objectContaining({
|
|
||||||
payment_id: payment.id,
|
|
||||||
reason: "return",
|
|
||||||
amount: 5000,
|
|
||||||
})
|
|
||||||
)
|
|
||||||
expect(response.status).toEqual(200)
|
expect(response.status).toEqual(200)
|
||||||
|
|
||||||
const savedPayment = await api.get(
|
breaking(
|
||||||
`/admin/payments/${payment.id}`,
|
async () => {
|
||||||
adminHeaders
|
expect(response.data.refund).toEqual(
|
||||||
)
|
expect.objectContaining({
|
||||||
|
payment_id: payment.id,
|
||||||
|
reason: "return",
|
||||||
|
amount: 500,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
expect(savedPayment.data.payment).toEqual(
|
const savedPayment = await api.get(
|
||||||
expect.objectContaining({
|
`/admin/payments/${payment.id}`,
|
||||||
amount_refunded: 5000,
|
adminHeaders
|
||||||
})
|
)
|
||||||
|
|
||||||
|
expect(savedPayment.data.payment).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
amount_refunded: 500,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
},
|
||||||
|
() => {
|
||||||
|
expect(response.data.payment).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
id: payment.id,
|
||||||
|
captured_at: expect.any(String),
|
||||||
|
captures: [
|
||||||
|
expect.objectContaining({
|
||||||
|
id: expect.any(String),
|
||||||
|
amount: 1000,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
refunds: [
|
||||||
|
expect.objectContaining({
|
||||||
|
id: expect.any(String),
|
||||||
|
amount: 500,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
amount: 1000,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { capturePaymentWorkflow } from "@medusajs/core-flows"
|
import { capturePaymentWorkflow } from "@medusajs/core-flows"
|
||||||
import { Modules } from "@medusajs/modules-sdk"
|
|
||||||
import {
|
import {
|
||||||
ContainerRegistrationKeys,
|
ContainerRegistrationKeys,
|
||||||
remoteQueryObjectFromString,
|
remoteQueryObjectFromString,
|
||||||
@@ -9,9 +8,10 @@ import {
|
|||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../../../types/routing"
|
} from "../../../../../types/routing"
|
||||||
import { defaultAdminPaymentFields } from "../../query-config"
|
import { defaultAdminPaymentFields } from "../../query-config"
|
||||||
|
import { AdminPostPaymentsCapturesReq } from "../../validators"
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest<AdminPostPaymentsCapturesReq>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
@@ -22,6 +22,7 @@ export const POST = async (
|
|||||||
input: {
|
input: {
|
||||||
payment_id: id,
|
payment_id: id,
|
||||||
captured_by: req.auth?.actor_id,
|
captured_by: req.auth?.actor_id,
|
||||||
|
amount: req.validatedBody.amount,
|
||||||
},
|
},
|
||||||
throwOnError: false,
|
throwOnError: false,
|
||||||
})
|
})
|
||||||
@@ -31,7 +32,7 @@ export const POST = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
const query = remoteQueryObjectFromString({
|
const query = remoteQueryObjectFromString({
|
||||||
entryPoint: Modules.PAYMENT,
|
entryPoint: "payments",
|
||||||
variables: { id },
|
variables: { id },
|
||||||
fields: defaultAdminPaymentFields,
|
fields: defaultAdminPaymentFields,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { refundPaymentWorkflow } from "@medusajs/core-flows"
|
import { refundPaymentWorkflow } from "@medusajs/core-flows"
|
||||||
import { Modules } from "@medusajs/modules-sdk"
|
|
||||||
import {
|
import {
|
||||||
ContainerRegistrationKeys,
|
ContainerRegistrationKeys,
|
||||||
remoteQueryObjectFromString,
|
remoteQueryObjectFromString,
|
||||||
@@ -9,9 +8,10 @@ import {
|
|||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../../../../../types/routing"
|
} from "../../../../../types/routing"
|
||||||
import { defaultAdminPaymentFields } from "../../query-config"
|
import { defaultAdminPaymentFields } from "../../query-config"
|
||||||
|
import { AdminPostPaymentsRefundsReq } from "../../validators"
|
||||||
|
|
||||||
export const POST = async (
|
export const POST = async (
|
||||||
req: AuthenticatedMedusaRequest,
|
req: AuthenticatedMedusaRequest<AdminPostPaymentsRefundsReq>,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) => {
|
) => {
|
||||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
@@ -22,6 +22,7 @@ export const POST = async (
|
|||||||
input: {
|
input: {
|
||||||
payment_id: id,
|
payment_id: id,
|
||||||
created_by: req.auth?.actor_id,
|
created_by: req.auth?.actor_id,
|
||||||
|
amount: req.validatedBody.amount,
|
||||||
},
|
},
|
||||||
throwOnError: false,
|
throwOnError: false,
|
||||||
})
|
})
|
||||||
@@ -31,7 +32,7 @@ export const POST = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
const query = remoteQueryObjectFromString({
|
const query = remoteQueryObjectFromString({
|
||||||
entryPoint: Modules.PAYMENT,
|
entryPoint: "payments",
|
||||||
variables: { id },
|
variables: { id },
|
||||||
fields: defaultAdminPaymentFields,
|
fields: defaultAdminPaymentFields,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
import { transformQuery } from "../../../api/middlewares"
|
import { transformBody, transformQuery } from "../../../api/middlewares"
|
||||||
import { MiddlewareRoute } from "../../../types/middlewares"
|
import { MiddlewareRoute } from "../../../types/middlewares"
|
||||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||||
import * as queryConfig from "./query-config"
|
import * as queryConfig from "./query-config"
|
||||||
import { AdminGetPaymentsParams } from "./validators"
|
import {
|
||||||
|
AdminGetPaymentsParams,
|
||||||
|
AdminPostPaymentsCapturesReq,
|
||||||
|
AdminPostPaymentsRefundsReq,
|
||||||
|
} from "./validators"
|
||||||
|
|
||||||
export const adminPaymentRoutesMiddlewares: MiddlewareRoute[] = [
|
export const adminPaymentRoutesMiddlewares: MiddlewareRoute[] = [
|
||||||
{
|
{
|
||||||
@@ -33,11 +37,11 @@ export const adminPaymentRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
{
|
{
|
||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/payments/:id/capture",
|
matcher: "/admin/payments/:id/capture",
|
||||||
middlewares: [],
|
middlewares: [transformBody(AdminPostPaymentsCapturesReq)],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/payments/:id/refund",
|
matcher: "/admin/payments/:id/refund",
|
||||||
middlewares: [],
|
middlewares: [transformBody(AdminPostPaymentsRefundsReq)],
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -2,8 +2,13 @@ export const defaultAdminPaymentFields = [
|
|||||||
"id",
|
"id",
|
||||||
"currency_code",
|
"currency_code",
|
||||||
"amount",
|
"amount",
|
||||||
|
"captured_at",
|
||||||
"payment_collection_id",
|
"payment_collection_id",
|
||||||
"payment_session_id",
|
"payment_session_id",
|
||||||
|
"captures.id",
|
||||||
|
"captures.amount",
|
||||||
|
"refunds.id",
|
||||||
|
"refunds.amount",
|
||||||
]
|
]
|
||||||
|
|
||||||
export const defaultAdminPaymentRelations = ["captures", "refunds"]
|
export const defaultAdminPaymentRelations = ["captures", "refunds"]
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Type } from "class-transformer"
|
import { Type } from "class-transformer"
|
||||||
import { IsOptional, ValidateNested } from "class-validator"
|
import { IsInt, IsOptional, ValidateNested } from "class-validator"
|
||||||
import {
|
import {
|
||||||
DateComparisonOperator,
|
DateComparisonOperator,
|
||||||
FindParams,
|
FindParams,
|
||||||
@@ -44,3 +44,15 @@ export class AdminGetPaymentsParams extends extendedFindParamsMixin({
|
|||||||
@Type(() => DateComparisonOperator)
|
@Type(() => DateComparisonOperator)
|
||||||
deleted_at?: DateComparisonOperator
|
deleted_at?: DateComparisonOperator
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class AdminPostPaymentsCapturesReq {
|
||||||
|
@IsInt()
|
||||||
|
@IsOptional()
|
||||||
|
amount?: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export class AdminPostPaymentsRefundsReq {
|
||||||
|
@IsInt()
|
||||||
|
@IsOptional()
|
||||||
|
amount?: number
|
||||||
|
}
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ export const joinerConfig: ModuleJoinerConfig = {
|
|||||||
name: ["payment", "payments"],
|
name: ["payment", "payments"],
|
||||||
args: {
|
args: {
|
||||||
entity: Payment.name,
|
entity: Payment.name,
|
||||||
|
methodSuffix: "Payments",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user