forked from medusajs/medusa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support payment session cancellation
- Loading branch information
Showing
6 changed files
with
154 additions
and
34 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
packages/core/core-flows/src/payment-collection/steps/cancel-payment-session.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { | ||
IPaymentModuleService, | ||
Logger, | ||
PaymentSessionDTO, | ||
} from "@medusajs/types" | ||
import { | ||
ContainerRegistrationKeys, | ||
ModuleRegistrationName, | ||
promiseAll, | ||
} from "@medusajs/utils" | ||
import { createStep, StepResponse } from "@medusajs/workflows-sdk" | ||
|
||
export type CancelPaymentSessionStepInput = { | ||
id: string | string[] | ||
} | ||
|
||
export const cancelPaymentSessionStepId = "cancel-payment-session-step" | ||
|
||
/** | ||
* This step cancels one or more payment sessions. | ||
*/ | ||
export const cancelPaymentSessionStep = createStep( | ||
cancelPaymentSessionStepId, | ||
async (input: CancelPaymentSessionStepInput, { container }) => { | ||
const logger = container.resolve<Logger>(ContainerRegistrationKeys.LOGGER) | ||
const paymentModule = container.resolve<IPaymentModuleService>( | ||
ModuleRegistrationName.PAYMENT | ||
) | ||
const ids = Array.isArray(input.id) ? input.id : [input.id] | ||
const sessions = await promiseAll( | ||
ids.map((id) => | ||
paymentModule.cancelPaymentSession(id).catch((e) => { | ||
logger.error( | ||
`Error was thrown trying to cancel payment session - ${id} - ${e}` | ||
) | ||
}) | ||
) | ||
) | ||
|
||
return new StepResponse( | ||
Array.isArray(input.id) | ||
? (sessions.filter(Boolean) as PaymentSessionDTO[]) | ||
: sessions[0] | ||
) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/core/core-flows/src/payment-collection/workflows/cancel-payment-session.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { | ||
WorkflowData, | ||
WorkflowResponse, | ||
createWorkflow, | ||
} from "@medusajs/workflows-sdk" | ||
import { cancelPaymentSessionStep } from "../steps/cancel-payment-session" | ||
|
||
export const cancelPaymentSessionWorkflowId = "cancel-payment-session-workflow" | ||
|
||
/** | ||
* This workflow cancels one or more payment sessions. | ||
*/ | ||
export const cancelPaymentSessionWorkflow = createWorkflow( | ||
cancelPaymentSessionWorkflowId, | ||
(input: WorkflowData<{ id: string | string[] }>) => { | ||
return new WorkflowResponse(cancelPaymentSessionStep(input)) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters