1 ---
2 generate_toc: true
3 ---
4
5 import { CodeTabs, CodeTab, ChildDocs } from "docs-ui"
6
7 export const metadata = {
8 title: `Payment Module`,
9 }
10
11 # {metadata.title}
12
13 In this section of the documentation, you will find resources to learn more about the Payment Module and how to use it in your application.
14
15 <Note title="Looking for no-code docs?">
16
17 Refer to the [Medusa Admin User Guide](!user-guide!/orders/payments) to learn how to manage order payments using the dashboard.
18
19 </Note>
20
21 Medusa has payment related features available out-of-the-box through the Payment Module. A [module](!docs!/learn/fundamentals/modules) is a standalone package that provides features for a single domain. Each of Medusa's commerce features are placed in Commerce Modules, such as this Payment Module.
22
23 <Note>
24
25 Learn more about why modules are isolated in [this documentation](!docs!/learn/fundamentals/modules/isolation).
26
27 </Note>
28
29 ## Payment Features
30
31 - [Authorize, Capture, and Refund Payments](./payment/page.mdx): Authorize, capture, and refund payments for a single resource.
32 - [Payment Collection Management](./payment-collection/page.mdx): Store and manage all payments of a single resources, such as a cart, in payment collections.
33 - [Integrate Third-Party Payment Providers](./payment-provider/page.mdx): Use payment providers like [Stripe](./payment-provider/stripe/page.mdx) to handle and process payments, or integrate custom payment providers.
34 - [Saved Payment Methods](./account-holder/page.mdx): Save payment methods for customers in third-party payment providers.
35 - [Handle Webhook Events](./webhook-events/page.mdx): Handle webhook events from third-party providers and process the associated payment.
36
37 ---
38
39 ## How to Use the Payment Module
40
41 In your Medusa application, you build flows around Commerce Modules. A flow is built as a [Workflow](!docs!/learn/fundamentals/workflows), which is a special function composed of a series of steps that guarantees data consistency and reliable roll-back mechanism.
42
43 You can build custom workflows and steps. You can also re-use Medusa's workflows and steps, which are provided by the `@medusajs/medusa/core-flows` package.
44
45 For example:
46
47 export const highlights = [
48 ["12", "Modules.PAYMENT", "Resolve the module in a step."]
49 ]
50
51 ```ts title="src/workflows/create-payment-collection.ts" highlights={highlights}
52 import {
53 createWorkflow,
54 WorkflowResponse,
55 createStep,
56 StepResponse,
57 } from "@medusajs/framework/workflows-sdk"
58 import { Modules } from "@medusajs/framework/utils"
59
60 const createPaymentCollectionStep = createStep(
61 "create-payment-collection",
62 async ({}, { container }) => {
63 const paymentModuleService = container.resolve(Modules.PAYMENT)
64
65 const paymentCollection = await paymentModuleService.createPaymentCollections({
66 currency_code: "usd",
67 amount: 50,
68 })
69
70 return new StepResponse({ paymentCollection }, paymentCollection.id)
71 },
72 async (paymentCollectionId, { container }) => {
73 if (!paymentCollectionId) {
74 return
75 }
76 const paymentModuleService = container.resolve(Modules.PAYMENT)
77
78 await paymentModuleService.deletePaymentCollections([paymentCollectionId])
79 }
80 )
81
82 export const createPaymentCollectionWorkflow = createWorkflow(
83 "create-payment-collection",
84 () => {
85 const { paymentCollection } = createPaymentCollectionStep()
86
87 return new WorkflowResponse({
88 paymentCollection,
89 })
90 }
91 )
92 ```
93
94 You can then execute the workflow in your custom API routes, scheduled jobs, or subscribers:
95
96 <CodeTabs group="resource-types">
97 <CodeTab label="API Route" value="api-route">
98
99 ```ts title="src/api/workflow/route.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports"
100 import type {
101 MedusaRequest,
102 MedusaResponse,
103 } from "@medusajs/framework/http"
104 import { createPaymentCollectionWorkflow } from "../../workflows/create-payment-collection"
105
106 export async function GET(
107 req: MedusaRequest,
108 res: MedusaResponse
109 ) {
110 const { result } = await createPaymentCollectionWorkflow(req.scope)
111 .run()
112
113 res.send(result)
114 }
115 ```
116
117 </CodeTab>
118 <CodeTab label="Subscriber" value="subscriber">
119
120 ```ts title="src/subscribers/user-created.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports"
121 import {
122 type SubscriberConfig,
123 type SubscriberArgs,
124 } from "@medusajs/framework"
125 import { createPaymentCollectionWorkflow } from "../workflows/create-payment-collection"
126
127 export default async function handleUserCreated({
128 event: { data },
129 container,
130 }: SubscriberArgs<{ id: string }>) {
131 const { result } = await createPaymentCollectionWorkflow(container)
132 .run()
133
134 console.log(result)
135 }
136
137 export const config: SubscriberConfig = {
138 event: "user.created",
139 }
140 ```
141
142 </CodeTab>
143 <CodeTab label="Scheduled Job" value="scheduled-job">
144
145 ```ts title="src/jobs/run-daily.ts" highlights={[["7"], ["8"]]}
146 import { MedusaContainer } from "@medusajs/framework/types"
147 import { createPaymentCollectionWorkflow } from "../workflows/create-payment-collection"
148
149 export default async function myCustomJob(
150 container: MedusaContainer
151 ) {
152 const { result } = await createPaymentCollectionWorkflow(container)
153 .run()
154
155 console.log(result)
156 }
157
158 export const config = {
159 name: "run-once-a-day",
160 schedule: `0 0 * * *`,
161 }
162 ```
163
164 </CodeTab>
165 </CodeTabs>
166
167 Learn more about workflows in [this documentation](!docs!/learn/fundamentals/workflows).
168
169 ---
170
171 ## Configure Payment Module
172
173 The Payment Module accepts options for further configurations. Refer to [this documentation](./module-options/page.mdx) for details on the module's options.
174
175 ---
176
177 ## Providers
178
179 Medusa provides the following payment providers out-of-the-box. You can use them to process payments for orders, returns, and other resources.
180
181 <ChildDocs showItems={["Providers"]} hideTitle />
182
183 ---
184
185 <CommerceModuleSections />