feat(medusa,medusa-react): PaymentCollection support (#2659)

* chore: medusa react, order edit complete fix and single payment session
This commit is contained in:
Carlos R. L. Rodrigues
2022-12-07 12:39:35 -03:00
committed by GitHub
parent 42d9c7222b
commit 15c667fbd3
54 changed files with 2001 additions and 261 deletions
@@ -27,7 +27,7 @@ describe("[MEDUSA_FF_ORDER_EDITING] /admin/payment-collections", () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
const [process, connection] = await startServerWithEnvironment({
cwd,
env: { MEDUSA_FF_ORDER_EDITING: true },
env: { MEDUSA_FF_ORDER_EDITING: true }
})
dbConnection = connection
medusaProcess = process
@@ -66,14 +66,20 @@ describe("[MEDUSA_FF_ORDER_EDITING] /admin/payment", () => {
const api = useApi()
// create payment session
await api.post(`/store/payment-collections/${payCol.id}/sessions`, {
sessions: {
const payColRes = await api.post(
`/store/payment-collections/${payCol.id}/sessions`,
{
provider_id: "test-pay",
customer_id: "customer",
amount: 10000,
},
})
await api.post(`/store/payment-collections/${payCol.id}/authorize`)
}
)
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}`,
@@ -85,6 +91,7 @@ describe("[MEDUSA_FF_ORDER_EDITING] /admin/payment", () => {
)
const payment = paymentCollections.data.payment_collection.payments[0]
expect(payment.captured_at).toBe(null)
const response = await api.post(
@@ -107,14 +114,20 @@ describe("[MEDUSA_FF_ORDER_EDITING] /admin/payment", () => {
const api = useApi()
// create payment session
await api.post(`/store/payment-collections/${payCol.id}/sessions`, {
sessions: {
const payColRes = await api.post(
`/store/payment-collections/${payCol.id}/sessions`,
{
provider_id: "test-pay",
customer_id: "customer",
amount: 10000,
},
})
await api.post(`/store/payment-collections/${payCol.id}/authorize`)
}
)
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}`,
@@ -5,6 +5,9 @@ const startServerWithEnvironment =
const { useApi } = require("../../../helpers/use-api")
const { useDb } = require("../../../helpers/use-db")
const adminSeeder = require("../../helpers/admin-seeder")
const {
getClientAuthenticationCookie,
} = require("../../helpers/client-authentication")
const {
simpleOrderEditFactory,
} = require("../../factories/simple-order-edit-factory")
@@ -16,6 +19,7 @@ const {
simpleLineItemFactory,
simpleProductFactory,
simpleOrderFactory,
simpleCustomerFactory,
} = require("../../factories")
const { OrderEditItemChangeType } = require("@medusajs/medusa")
@@ -33,6 +37,11 @@ describe("[MEDUSA_FF_ORDER_EDITING] /store/order-edits", () => {
})
dbConnection = connection
medusaProcess = process
await simpleCustomerFactory(dbConnection, {
id: "customer",
email: "test@medusajs.com",
})
})
afterAll(async () => {
@@ -163,7 +172,11 @@ describe("[MEDUSA_FF_ORDER_EDITING] /store/order-edits", () => {
it("gets order edit", async () => {
const api = useApi()
const response = await api.get(`/store/order-edits/${orderEditId}`)
const response = await api.get(`/store/order-edits/${orderEditId}`, {
headers: {
Cookie: await getClientAuthenticationCookie(api),
},
})
expect(response.status).toEqual(200)
expect(response.data.order_edit).toEqual(
@@ -217,7 +230,14 @@ describe("[MEDUSA_FF_ORDER_EDITING] /store/order-edits", () => {
const api = useApi()
const err = await api
.get(`/store/order-edits/${orderEditId}?fields=internal_note,order_id`)
.get(
`/store/order-edits/${orderEditId}?fields=internal_note,order_id`,
{
headers: {
Cookie: await getClientAuthenticationCookie(api),
},
}
)
.catch((e) => e)
expect(err.response.data.message).toBe(
@@ -264,6 +284,11 @@ describe("[MEDUSA_FF_ORDER_EDITING] /store/order-edits", () => {
`/store/order-edits/${declineableOrderEdit.id}/decline`,
{
declined_reason: "wrong color",
},
{
headers: {
Cookie: await getClientAuthenticationCookie(api),
},
}
)
@@ -282,6 +307,11 @@ describe("[MEDUSA_FF_ORDER_EDITING] /store/order-edits", () => {
`/store/order-edits/${declinedOrderEdit.id}/decline`,
{
declined_reason: "wrong color",
},
{
headers: {
Cookie: await getClientAuthenticationCookie(api),
},
}
)
@@ -301,9 +331,17 @@ describe("[MEDUSA_FF_ORDER_EDITING] /store/order-edits", () => {
const api = useApi()
await api
.post(`/store/order-edits/${confirmedOrderEdit.id}/decline`, {
declined_reason: "wrong color",
})
.post(
`/store/order-edits/${confirmedOrderEdit.id}/decline`,
{
declined_reason: "wrong color",
},
{
headers: {
Cookie: await getClientAuthenticationCookie(api),
},
}
)
.catch((err) => {
expect(err.response.status).toEqual(400)
expect(err.response.data.message).toEqual(
@@ -345,13 +383,16 @@ describe("[MEDUSA_FF_ORDER_EDITING] /store/order-edits", () => {
return await db.teardown()
})
// TODO once payment collection is done
/*it("complete an order edit", async () => {})*/
it("idempotently complete an already confirmed order edit", async () => {
const api = useApi()
const result = await api.post(
`/store/order-edits/${confirmedOrderEdit.id}/complete`
`/store/order-edits/${confirmedOrderEdit.id}/complete`,
undefined,
{
headers: {
Cookie: await getClientAuthenticationCookie(api),
},
}
)
expect(result.status).toEqual(200)
@@ -367,7 +408,11 @@ describe("[MEDUSA_FF_ORDER_EDITING] /store/order-edits", () => {
it("fails to complete a non requested order edit", async () => {
const api = useApi()
const err = await api
.post(`/store/order-edits/${createdOrderEdit.id}/complete`)
.post(`/store/order-edits/${createdOrderEdit.id}/complete`, undefined, {
headers: {
Cookie: await getClientAuthenticationCookie(api),
},
})
.catch((e) => e)
expect(err.response.status).toEqual(400)
@@ -5,6 +5,9 @@ const startServerWithEnvironment =
const { useApi } = require("../../../helpers/use-api")
const { useDb } = require("../../../helpers/use-db")
const adminSeeder = require("../../helpers/admin-seeder")
const {
getClientAuthenticationCookie,
} = require("../../helpers/client-authentication")
const {
simplePaymentCollectionFactory,
@@ -28,6 +31,11 @@ describe("[MEDUSA_FF_ORDER_EDITING] /store/payment-collections", () => {
})
dbConnection = connection
medusaProcess = process
await simpleCustomerFactory(dbConnection, {
id: "customer",
email: "test@medusajs.com",
})
})
afterAll(async () => {
@@ -71,7 +79,7 @@ describe("[MEDUSA_FF_ORDER_EDITING] /store/payment-collections", () => {
})
})
describe("Manage Payment Sessions", () => {
describe("Manage a Single Payment Session", () => {
beforeEach(async () => {
await adminSeeder(dbConnection)
@@ -97,10 +105,106 @@ describe("[MEDUSA_FF_ORDER_EDITING] /store/payment-collections", () => {
const response = await api.post(
`/store/payment-collections/${payCol.id}/sessions`,
{
sessions: {
provider_id: "test-pay",
customer_id: "customer",
amount: 10000,
provider_id: "test-pay",
}
)
expect(response.data.payment_collection).toEqual(
expect.objectContaining({
id: payCol.id,
type: "order_edit",
amount: 10000,
payment_sessions: expect.arrayContaining([
expect.objectContaining({
amount: 10000,
status: "pending",
}),
]),
})
)
expect(response.status).toEqual(200)
})
it("update a payment session", async () => {
const api = useApi()
let response = await api.post(
`/store/payment-collections/${payCol.id}/sessions`,
{
provider_id: "test-pay",
}
)
expect(response.data.payment_collection.payment_sessions).toHaveLength(1)
const paySessions = response.data.payment_collection.payment_sessions
response = await api.post(
`/store/payment-collections/${payCol.id}/sessions`,
{
provider_id: "test-pay",
}
)
expect(response.data.payment_collection.payment_sessions).toHaveLength(1)
expect(response.data.payment_collection).toEqual(
expect.objectContaining({
id: payCol.id,
type: "order_edit",
amount: 10000,
payment_sessions: expect.arrayContaining([
expect.objectContaining({
id: paySessions[0].id,
amount: 10000,
status: "pending",
}),
]),
})
)
expect(response.status).toEqual(200)
})
})
describe("Manage Multiple Payment Sessions", () => {
beforeEach(async () => {
await adminSeeder(dbConnection)
payCol = await simplePaymentCollectionFactory(dbConnection, {
description: "paycol description",
amount: 10000,
})
await simpleCustomerFactory(dbConnection, {
id: "customer",
email: "test@customer.com",
})
})
afterEach(async () => {
const db = useDb()
return await db.teardown()
})
it("Set a payment session", async () => {
const api = useApi()
const response = await api.post(
`/store/payment-collections/${payCol.id}/sessions/batch`,
{
sessions: [
{
provider_id: "test-pay",
amount: 10000,
},
],
},
{
headers: {
Cookie: await getClientAuthenticationCookie(
api,
"test@customer.com"
),
},
}
)
@@ -126,22 +230,19 @@ describe("[MEDUSA_FF_ORDER_EDITING] /store/payment-collections", () => {
const api = useApi()
const response = await api.post(
`/store/payment-collections/${payCol.id}/sessions`,
`/store/payment-collections/${payCol.id}/sessions/batch`,
{
sessions: [
{
provider_id: "test-pay",
customer_id: "customer",
amount: 2000,
},
{
provider_id: "test-pay",
customer_id: "customer",
amount: 5000,
},
{
provider_id: "test-pay",
customer_id: "customer",
amount: 3000,
},
],
@@ -178,22 +279,19 @@ describe("[MEDUSA_FF_ORDER_EDITING] /store/payment-collections", () => {
const api = useApi()
let response = await api.post(
`/store/payment-collections/${payCol.id}/sessions`,
`/store/payment-collections/${payCol.id}/sessions/batch`,
{
sessions: [
{
provider_id: "test-pay",
customer_id: "customer",
amount: 2000,
},
{
provider_id: "test-pay",
customer_id: "customer",
amount: 5000,
},
{
provider_id: "test-pay",
customer_id: "customer",
amount: 3000,
},
],
@@ -205,18 +303,16 @@ describe("[MEDUSA_FF_ORDER_EDITING] /store/payment-collections", () => {
const multipleSessions = response.data.payment_collection.payment_sessions
response = await api.post(
`/store/payment-collections/${payCol.id}/sessions`,
`/store/payment-collections/${payCol.id}/sessions/batch`,
{
sessions: [
{
provider_id: "test-pay",
customer_id: "customer",
amount: 5000,
session_id: multipleSessions[0].id,
},
{
provider_id: "test-pay",
customer_id: "customer",
amount: 5000,
session_id: multipleSessions[1].id,
},
@@ -249,7 +345,7 @@ describe("[MEDUSA_FF_ORDER_EDITING] /store/payment-collections", () => {
})
})
describe("Authorize a Payment Sessions", () => {
describe("Authorize Payment Sessions", () => {
beforeEach(async () => {
await adminSeeder(dbConnection)
@@ -269,19 +365,62 @@ describe("[MEDUSA_FF_ORDER_EDITING] /store/payment-collections", () => {
return await db.teardown()
})
it("Authorize a payment session", async () => {
it("Authorizes a payment session", async () => {
const api = useApi()
await api.post(`/store/payment-collections/${payCol.id}/sessions`, {
sessions: {
provider_id: "test-pay",
customer_id: "customer",
const payColRes = await api.post(
`/store/payment-collections/${payCol.id}/sessions/batch`,
{
sessions: [
{
provider_id: "test-pay",
amount: 10000,
},
],
}
)
const sessionId = payColRes.data.payment_collection.payment_sessions[0].id
const response = await api.post(
`/store/payment-collections/${payCol.id}/sessions/${sessionId}/authorize`
)
expect(response.data.payment_session).toEqual(
expect.objectContaining({
amount: 10000,
},
})
status: "authorized",
})
)
expect(response.status).toEqual(200)
})
it("Authorize multiple payment sessions", async () => {
const api = useApi()
const payColRes = await api.post(
`/store/payment-collections/${payCol.id}/sessions/batch`,
{
sessions: [
{
provider_id: "test-pay",
amount: 5000,
},
{
provider_id: "test-pay",
amount: 5000,
},
],
}
)
const response = await api.post(
`/store/payment-collections/${payCol.id}/authorize`
`/store/payment-collections/${payCol.id}/sessions/batch/authorize`,
{
session_ids: payColRes.data.payment_collection.payment_sessions.map(
({ id }) => id
),
}
)
expect(response.data.payment_collection).toEqual(
@@ -291,14 +430,18 @@ describe("[MEDUSA_FF_ORDER_EDITING] /store/payment-collections", () => {
amount: 10000,
payment_sessions: expect.arrayContaining([
expect.objectContaining({
amount: 10000,
amount: 5000,
status: "authorized",
}),
expect.objectContaining({
amount: 5000,
status: "authorized",
}),
]),
})
)
expect(response.status).toEqual(200)
expect(response.status).toEqual(207)
})
})
})
+3
View File
@@ -19,3 +19,6 @@ export * from "./simple-batch-job-factory"
export * from "./simple-sales-channel-factory"
export * from "./simple-custom-shipping-option-factory"
export * from "./simple-payment-collection-factory"
export * from "./simple-order-edit-factory"
export * from "./simple-order-item-change-factory"
export * from "./simple-customer-factory"
@@ -11,6 +11,7 @@ export type CustomerFactoryData = {
email?: string
groups?: CustomerGroupFactoryData[]
password_hash?: string
has_account?: boolean
}
export const simpleCustomerFactory = async (
@@ -28,6 +29,10 @@ export const simpleCustomerFactory = async (
const c = manager.create(Customer, {
id: customerId,
email: data.email,
password_hash:
data.password_hash ??
"c2NyeXB0AAEAAAABAAAAAVMdaddoGjwU1TafDLLlBKnOTQga7P2dbrfgf3fB+rCD/cJOMuGzAvRdKutbYkVpuJWTU39P7OpuWNkUVoEETOVLMJafbI8qs8Qx/7jMQXkN", // password matching "test"
has_account: data.has_account ?? true,
})
if (data.password_hash) {
@@ -0,0 +1,20 @@
const AUTH_COOKIE = {}
export async function getClientAuthenticationCookie(
api,
email = null,
password = null
) {
const user = {
email: email ?? "test@medusajs.com",
password: password ?? "test",
}
if (AUTH_COOKIE[user.email]) {
return AUTH_COOKIE[user.email]
}
const authResponse = await api.post("/store/auth", user)
AUTH_COOKIE[user.email] = authResponse.headers["set-cookie"][0].split(";")
return AUTH_COOKIE[user.email]
}