feat: Add payment collection creation for cart (#6527)
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IPaymentModuleService } from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
type StepInput = {
|
||||
region_id: string
|
||||
currency_code: string
|
||||
amount: number
|
||||
metadata?: Record<string, unknown>
|
||||
}
|
||||
|
||||
export const createPaymentCollectionsStepId = "create-payment-collections"
|
||||
export const createPaymentCollectionsStep = createStep(
|
||||
createPaymentCollectionsStepId,
|
||||
async (data: StepInput[], { container }) => {
|
||||
const service = container.resolve<IPaymentModuleService>(
|
||||
ModuleRegistrationName.PAYMENT
|
||||
)
|
||||
|
||||
const created = await service.createPaymentCollections(data)
|
||||
|
||||
return new StepResponse(
|
||||
created,
|
||||
created.map((collection) => collection.id)
|
||||
)
|
||||
},
|
||||
async (createdIds, { container }) => {
|
||||
if (!createdIds?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IPaymentModuleService>(
|
||||
ModuleRegistrationName.PAYMENT
|
||||
)
|
||||
|
||||
await service.deletePaymentCollections(createdIds)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,41 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
type StepInput = {
|
||||
links: {
|
||||
cart_id: string
|
||||
payment_collection_id: string
|
||||
}[]
|
||||
}
|
||||
|
||||
export const linkCartAndPaymentCollectionsStepId =
|
||||
"link-cart-payment-collection"
|
||||
export const linkCartAndPaymentCollectionsStep = createStep(
|
||||
linkCartAndPaymentCollectionsStepId,
|
||||
async (data: StepInput, { container }) => {
|
||||
const remoteLink = container.resolve("remoteLink")
|
||||
|
||||
const links = data.links.map((d) => ({
|
||||
[Modules.CART]: { cart_id: d.cart_id },
|
||||
[Modules.PAYMENT]: { payment_collection_id: d.payment_collection_id },
|
||||
}))
|
||||
|
||||
await remoteLink.create(links)
|
||||
|
||||
return new StepResponse(void 0, data)
|
||||
},
|
||||
async (data, { container }) => {
|
||||
if (!data) {
|
||||
return
|
||||
}
|
||||
|
||||
const remoteLink = container.resolve("remoteLink")
|
||||
|
||||
const links = data.links.map((d) => ({
|
||||
[Modules.CART]: { cart_id: d.cart_id },
|
||||
[Modules.PAYMENT]: { payment_collection_id: d.payment_collection_id },
|
||||
}))
|
||||
|
||||
await remoteLink.dismiss(links)
|
||||
}
|
||||
)
|
||||
@@ -4,7 +4,7 @@ import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
interface StepInput {
|
||||
id: string
|
||||
config: FindConfig<CartDTO>
|
||||
config?: FindConfig<CartDTO>
|
||||
}
|
||||
|
||||
export const retrieveCartStepId = "retrieve-cart"
|
||||
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
import {
|
||||
CartDTO,
|
||||
CreatePaymentCollectionForCartWorkflowInputDTO,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
WorkflowData,
|
||||
createWorkflow,
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { retrieveCartStep } from "../steps"
|
||||
import { createPaymentCollectionsStep } from "../steps/create-payment-collection"
|
||||
import { linkCartAndPaymentCollectionsStep } from "../steps/link-cart-payment-collection"
|
||||
|
||||
export const createPaymentCollectionForCartWorkflowId =
|
||||
"create-payment-collection-for-cart"
|
||||
export const createPaymentCollectionForCartWorkflow = createWorkflow(
|
||||
createPaymentCollectionForCartWorkflowId,
|
||||
(
|
||||
input: WorkflowData<CreatePaymentCollectionForCartWorkflowInputDTO>
|
||||
): WorkflowData<CartDTO> => {
|
||||
const created = createPaymentCollectionsStep([input])
|
||||
|
||||
const link = transform({ cartId: input.cart_id, created }, (data) => ({
|
||||
links: [
|
||||
{
|
||||
cart_id: data.cartId,
|
||||
payment_collection_id: data.created[0].id,
|
||||
},
|
||||
],
|
||||
}))
|
||||
|
||||
linkCartAndPaymentCollectionsStep(link)
|
||||
|
||||
const cart = retrieveCartStep({ id: input.cart_id })
|
||||
|
||||
return cart
|
||||
}
|
||||
)
|
||||
@@ -1,6 +1,6 @@
|
||||
export * from "./add-to-cart"
|
||||
export * from "./create-carts"
|
||||
export * from "./create-payment-collection-for-cart"
|
||||
export * from "./update-cart"
|
||||
export * from "./update-cart-promotions"
|
||||
export * from "./update-line-item-in-cart"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user