feat: Add payment collection creation for cart (#6527)

This commit is contained in:
Oli Juhl
2024-03-04 09:02:01 +01:00
committed by GitHub
parent 71ed21de4a
commit 883cb0dca7
11 changed files with 371 additions and 8 deletions

View File

@@ -1,9 +1,11 @@
import {
addToCartWorkflow,
createCartWorkflow,
createPaymentCollectionForCartWorkflow,
deleteLineItemsStepId,
deleteLineItemsWorkflow,
findOrCreateCustomerStepId,
linkCartAndPaymentCollectionsStepId,
updateLineItemInCartWorkflow,
updateLineItemsStepId,
} from "@medusajs/core-flows"
@@ -11,6 +13,7 @@ import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import {
ICartModuleService,
ICustomerModuleService,
IPaymentModuleService,
IPricingModuleService,
IProductModuleService,
IRegionModuleService,
@@ -37,7 +40,8 @@ describe("Carts workflows", () => {
let customerModule: ICustomerModuleService
let productModule: IProductModuleService
let pricingModule: IPricingModuleService
let remoteLink
let paymentModule: IPaymentModuleService
let remoteLink, remoteQuery
let defaultRegion
@@ -52,7 +56,9 @@ describe("Carts workflows", () => {
customerModule = appContainer.resolve(ModuleRegistrationName.CUSTOMER)
productModule = appContainer.resolve(ModuleRegistrationName.PRODUCT)
pricingModule = appContainer.resolve(ModuleRegistrationName.PRICING)
paymentModule = appContainer.resolve(ModuleRegistrationName.PAYMENT)
remoteLink = appContainer.resolve("remoteLink")
remoteQuery = appContainer.resolve("remoteQuery")
})
afterAll(async () => {
@@ -668,4 +674,142 @@ describe("Carts workflows", () => {
})
})
})
describe("createPaymentCollectionForCart", () => {
it("should create a payment collection and link it to cart", async () => {
const region = await regionModuleService.create({
name: "US",
currency_code: "usd",
})
const cart = await cartModuleService.create({
currency_code: "usd",
region_id: region.id,
items: [
{
quantity: 1,
unit_price: 5000,
title: "Test item",
},
],
})
await createPaymentCollectionForCartWorkflow(appContainer).run({
input: {
cart_id: cart.id,
region_id: region.id,
currency_code: "usd",
amount: 5000,
},
throwOnError: false,
})
const result = await remoteQuery(
{
cart: {
fields: ["id"],
payment_collection: {
fields: ["id", "amount", "currency_code"],
},
},
},
{
cart: {
id: cart.id,
},
}
)
expect(result).toEqual([
expect.objectContaining({
id: cart.id,
payment_collection: expect.objectContaining({
amount: 5000,
currency_code: "usd",
}),
}),
])
})
describe("compensation", () => {
it("should dismiss cart <> payment collection link and delete created payment collection", async () => {
const workflow = createPaymentCollectionForCartWorkflow(appContainer)
workflow.appendAction("throw", linkCartAndPaymentCollectionsStepId, {
invoke: async function failStep() {
throw new Error(
`Failed to do something after linking cart and payment collection`
)
},
})
const region = await regionModuleService.create({
name: "US",
currency_code: "usd",
})
const cart = await cartModuleService.create({
currency_code: "usd",
region_id: region.id,
items: [
{
quantity: 1,
unit_price: 5000,
title: "Test item",
},
],
})
const { errors } = await workflow.run({
input: {
cart_id: cart.id,
region_id: region.id,
currency_code: "usd",
amount: 5000,
},
throwOnError: false,
})
expect(errors).toEqual([
{
action: "throw",
handlerType: "invoke",
error: new Error(
`Failed to do something after linking cart and payment collection`
),
},
])
const carts = await remoteQuery(
{
cart: {
fields: ["id"],
payment_collection: {
fields: ["id", "amount", "currency_code"],
},
},
},
{
cart: {
id: cart.id,
},
}
)
const payCols = await remoteQuery({
payment_collection: {
fields: ["id"],
},
})
expect(carts).toEqual([
expect.objectContaining({
id: cart.id,
payment_collection: undefined,
}),
])
expect(payCols.length).toEqual(0)
})
})
})
})

View File

@@ -669,4 +669,35 @@ describe("Store Carts API", () => {
)
})
})
describe("POST /store/carts/:id/payment-collections", () => {
it("should create a payment collection for the cart", async () => {
const region = await regionModuleService.create({
name: "US",
currency_code: "usd",
})
const cart = await cartModuleService.create({
currency_code: "usd",
region_id: region.id,
})
const api = useApi() as any
const response = await api.post(
`/store/carts/${cart.id}/payment-collections`
)
expect(response.status).toEqual(200)
expect(response.data.cart).toEqual(
expect.objectContaining({
id: cart.id,
currency_code: "usd",
payment_collection: expect.objectContaining({
id: expect.any(String),
amount: 0,
}),
})
)
})
})
})