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: add payment authorization workflow
- Loading branch information
Showing
10 changed files
with
93 additions
and
114 deletions.
There are no files selected for viewing
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
50 changes: 50 additions & 0 deletions
50
packages/core/core-flows/src/payment-collection/steps/authorize-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,50 @@ | ||
import { | ||
IPaymentModuleService, | ||
Logger, | ||
PaymentProviderContext, | ||
} from "@medusajs/framework/types" | ||
import { ContainerRegistrationKeys, Modules } from "@medusajs/framework/utils" | ||
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk" | ||
|
||
export type AuthorizePaymentSessionStepInput = { | ||
id: string | ||
provider_token?: string | ||
context?: PaymentProviderContext | ||
} | ||
|
||
export const authorizePaymentSessionStepId = "authorize-payment-session-step" | ||
/** | ||
* This step authorizes a payment session. | ||
*/ | ||
export const authorizePaymentSessionStep = createStep( | ||
authorizePaymentSessionStepId, | ||
async (input: AuthorizePaymentSessionStepInput, { container }) => { | ||
const paymentModule = container.resolve<IPaymentModuleService>( | ||
Modules.PAYMENT | ||
) | ||
const payment = await paymentModule.authorizePaymentSession(input.id, { | ||
provider_token: input.provider_token, | ||
context: input.context, | ||
}) | ||
return new StepResponse(payment) | ||
}, | ||
// If payment or any other part of complete cart fails post payment step, we cancel any payments made | ||
async (payment, { container }) => { | ||
if (!payment) { | ||
return | ||
} | ||
|
||
const logger = container.resolve<Logger>(ContainerRegistrationKeys.LOGGER) | ||
const paymentModule = container.resolve<IPaymentModuleService>( | ||
Modules.PAYMENT | ||
) | ||
|
||
try { | ||
await paymentModule.cancelPayment(payment.id) | ||
} catch (e) { | ||
logger.error( | ||
`Error was thrown trying to cancel payment - ${payment.id} - ${e}` | ||
) | ||
} | ||
} | ||
) |
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
19 changes: 19 additions & 0 deletions
19
packages/core/core-flows/src/payment-collection/workflows/authorize-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,19 @@ | ||
import { | ||
WorkflowData, | ||
WorkflowResponse, | ||
createWorkflow, | ||
} from "@medusajs/framework/workflows-sdk" | ||
import { authorizePaymentSessionStep } from "../steps" | ||
|
||
export const authorizePaymentSessionWorkflowId = | ||
"authorize-payment-session-workflow" | ||
|
||
/** | ||
* This workflow authorizes a payment session. | ||
*/ | ||
export const authorizePaymentSessionWorkflow = createWorkflow( | ||
authorizePaymentSessionWorkflowId, | ||
(input: WorkflowData<{ id: string; provider_token?: string }>) => { | ||
return new WorkflowResponse(authorizePaymentSessionStep(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
93 changes: 0 additions & 93 deletions
93
packages/core/core-flows/src/payment/steps/authorize-payment-session.ts
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export * from "./authorize-payment-session" | ||
export * from "./cancel-payment" | ||
export * from "./capture-payment" | ||
export * from "./refund-payment" |
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