1 ---
2 products:
3 - cart
4 - customer
5 - order
6 - region
7 ---
8
9 import { CodeTabs, CodeTab, Table } from "docs-ui"
10
11 export const metadata = {
12 title: `Links between Payment Module and Other Modules`,
13 }
14
15 # {metadata.title}
16
17 This document showcases the module links defined between the Payment Module and other Commerce Modules.
18
19 ## Summary
20
21 The Payment Module has the following links to other modules:
22
23 <Table>
24 <Table.Header>
25 <Table.Row>
26 <Table.HeaderCell>
27 First Data Model
28 </Table.HeaderCell>
29 <Table.HeaderCell>
30 Second Data Model
31 </Table.HeaderCell>
32 <Table.HeaderCell>
33 Type
34 </Table.HeaderCell>
35 <Table.HeaderCell>
36 Description
37 </Table.HeaderCell>
38 </Table.Row>
39 </Table.Header>
40 <Table.Body>
41 <Table.Row>
42 <Table.Cell>
43 [Cart](/references/cart/models/Cart) in [Cart Module](../../cart/page.mdx)
44 </Table.Cell>
45 <Table.Cell>
46 [PaymentCollection](/references/payment/models/PaymentCollection)
47 </Table.Cell>
48 <Table.Cell>
49 Stored - one-to-one
50 </Table.Cell>
51 <Table.Cell>
52 [Learn more](#cart-module)
53 </Table.Cell>
54 </Table.Row>
55 <Table.Row>
56 <Table.Cell>
57 [Customer](/references/customer/models/Customer) in [Customer Module](../../customer/page.mdx)
58 </Table.Cell>
59 <Table.Cell>
60 [AccountHolder](/references/payment/models/AccountHolder)
61 </Table.Cell>
62 <Table.Cell>
63 Stored - many-to-many
64 </Table.Cell>
65 <Table.Cell>
66 [Learn more](#customer-module)
67 </Table.Cell>
68 </Table.Row>
69 <Table.Row>
70 <Table.Cell>
71 [Order](/references/order/models/Order) in [Order Module](../../order/page.mdx)
72 </Table.Cell>
73 <Table.Cell>
74 [PaymentCollection](/references/payment/models/PaymentCollection)
75 </Table.Cell>
76 <Table.Cell>
77 Stored - one-to-many
78 </Table.Cell>
79 <Table.Cell>
80 [Learn more](#order-module)
81 </Table.Cell>
82 </Table.Row>
83 <Table.Row>
84 <Table.Cell>
85 [OrderClaim](/references/order/models/OrderClaim) in [Order Module](../../order/page.mdx)
86 </Table.Cell>
87 <Table.Cell>
88 [PaymentCollection](/references/payment/models/PaymentCollection)
89 </Table.Cell>
90 <Table.Cell>
91 Stored - one-to-many
92 </Table.Cell>
93 <Table.Cell>
94 [Learn more](#order-module)
95 </Table.Cell>
96 </Table.Row>
97 <Table.Row>
98 <Table.Cell>
99 [OrderExchange](/references/order/models/OrderExchange) in [Order Module](../../order/page.mdx)
100 </Table.Cell>
101 <Table.Cell>
102 [PaymentCollection](/references/payment/models/PaymentCollection)
103 </Table.Cell>
104 <Table.Cell>
105 Stored - one-to-many
106 </Table.Cell>
107 <Table.Cell>
108 [Learn more](#order-module)
109 </Table.Cell>
110 </Table.Row>
111 <Table.Row>
112 <Table.Cell>
113 [Region](/references/region/models/Region) in [Region Module](../../region/page.mdx)
114 </Table.Cell>
115 <Table.Cell>
116 [PaymentProvider](/references/payment/models/PaymentProvider)
117 </Table.Cell>
118 <Table.Cell>
119 Stored - many-to-many
120 </Table.Cell>
121 <Table.Cell>
122 [Learn more](#region-module)
123 </Table.Cell>
124 </Table.Row>
125 </Table.Body>
126 </Table>
127
128 ---
129
130 ## Cart Module
131
132 The Cart Module provides cart-related features, but not payment processing.
133
134 Medusa defines a link between the `Cart` and `PaymentCollection` data models. A cart has a payment collection which holds all the authorized payment sessions and payments made related to the cart.
135
136 Learn more about this relation in [this documentation](../payment-collection/page.mdx#usage-with-the-cart-module).
137
138 ### Retrieve with Query
139
140 To retrieve the cart associated with the payment collection with [Query](!docs!/learn/fundamentals/query), pass `cart.*` in `fields`:
141
142 <CodeTabs group="relation-query">
143 <CodeTab label="query.graph" value="method">
144
145 ```ts
146 const { data: paymentCollections } = await query.graph({
147 entity: "payment_collection",
148 fields: [
149 "cart.*",
150 ],
151 })
152
153 // paymentCollections[0].cart
154 ```
155
156 </CodeTab>
157 <CodeTab label="useQueryGraphStep" value="step">
158
159 ```ts
160 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
161
162 // ...
163
164 const { data: paymentCollections } = useQueryGraphStep({
165 entity: "payment_collection",
166 fields: [
167 "cart.*",
168 ],
169 })
170
171 // paymentCollections[0].cart
172 ```
173
174 </CodeTab>
175 </CodeTabs>
176
177 ### Manage with Link
178
179 To manage the payment collection of a cart, use [Link](!docs!/learn/fundamentals/module-links/link):
180
181 <CodeTabs group="relation-link">
182 <CodeTab label="link.create" value="method">
183
184 ```ts
185 import { Modules } from "@medusajs/framework/utils"
186
187 // ...
188
189 await link.create({
190 [Modules.CART]: {
191 cart_id: "cart_123",
192 },
193 [Modules.PAYMENT]: {
194 payment_collection_id: "paycol_123",
195 },
196 })
197 ```
198
199 </CodeTab>
200 <CodeTab label="createRemoteLinkStep" value="step">
201
202 ```ts
203 import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
204
205 // ...
206
207 createRemoteLinkStep({
208 [Modules.CART]: {
209 cart_id: "cart_123",
210 },
211 [Modules.PAYMENT]: {
212 payment_collection_id: "paycol_123",
213 },
214 })
215 ```
216
217 </CodeTab>
218 </CodeTabs>
219
220 ---
221
222 ## Customer Module
223
224 Medusa defines a link between the `Customer` and `AccountHolder` data models, allowing payment providers to save payment methods for a customer, if the payment provider supports it.
225
226 <Note>
227
228 This link is available starting from Medusa `v2.5.0`.
229
230 </Note>
231
232 ### Retrieve with Query
233
234 To retrieve the customer associated with an account holder with [Query](!docs!/learn/fundamentals/query), pass `customer.*` in `fields`:
235
236 <CodeTabs group="relation-query">
237 <CodeTab label="query.graph" value="method">
238
239 ```ts
240 const { data: accountHolders } = await query.graph({
241 entity: "account_holder",
242 fields: [
243 "customer.*",
244 ],
245 })
246
247 // accountHolders[0].customer
248 ```
249
250 </CodeTab>
251 <CodeTab label="useQueryGraphStep" value="step">
252
253 ```ts
254 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
255
256 // ...
257
258 const { data: accountHolders } = useQueryGraphStep({
259 entity: "account_holder",
260 fields: [
261 "customer.*",
262 ],
263 })
264
265 // accountHolders[0].customer
266 ```
267
268 </CodeTab>
269 </CodeTabs>
270
271 ### Manage with Link
272
273 To manage the account holders of a customer, use [Link](!docs!/learn/fundamentals/module-links/link):
274
275 <CodeTabs group="relation-link">
276 <CodeTab label="link.create" value="method">
277
278 ```ts
279 import { Modules } from "@medusajs/framework/utils"
280
281 // ...
282
283 await link.create({
284 [Modules.CUSTOMER]: {
285 customer_id: "cus_123",
286 },
287 [Modules.PAYMENT]: {
288 account_holder_id: "acchld_123",
289 },
290 })
291 ```
292
293 </CodeTab>
294 <CodeTab label="createRemoteLinkStep" value="step">
295
296 ```ts
297 import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
298
299 // ...
300
301 createRemoteLinkStep({
302 [Modules.CUSTOMER]: {
303 customer_id: "cus_123",
304 },
305 [Modules.PAYMENT]: {
306 account_holder_id: "acchld_123",
307 },
308 })
309 ```
310
311 </CodeTab>
312 </CodeTabs>
313
314 <Note>
315
316 A customer can have many account holders, but each account holder belongs to only one customer. If you try to link an account holder that already belongs to another customer, Medusa throws a `Cannot create multiple links between 'customer' and 'payment'` error. To transfer an account holder to a different customer, remove the existing link first, then create the new one.
317
318 </Note>
319
320 ---
321
322 ## Order Module
323
324 An order's payment details are stored in a payment collection. This also applies for claims and exchanges.
325
326 So, Medusa defines links between the `PaymentCollection` data model and the `Order`, `OrderClaim`, and `OrderExchange` data models.
327
328 ![A diagram showcasing an example of how data models from the Order and Payment modules are linked](https://res.cloudinary.com/dza7lstvk/image/upload/v1716554726/Medusa%20Resources/order-payment_ubdwok.jpg)
329
330 ### Retrieve with Query
331
332 To retrieve the order of a payment collection with [Query](!docs!/learn/fundamentals/query), pass `order.*` in `fields`:
333
334 <CodeTabs group="relation-query">
335 <CodeTab label="query.graph" value="method">
336
337 ```ts
338 const { data: paymentCollections } = await query.graph({
339 entity: "payment_collection",
340 fields: [
341 "order.*",
342 ],
343 })
344
345 // paymentCollections[0].order
346 ```
347
348 </CodeTab>
349 <CodeTab label="useQueryGraphStep" value="step">
350
351 ```ts
352 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
353
354 // ...
355
356 const { data: paymentCollections } = useQueryGraphStep({
357 entity: "payment_collection",
358 fields: [
359 "order.*",
360 ],
361 })
362
363 // paymentCollections[0].order
364 ```
365
366 </CodeTab>
367 </CodeTabs>
368
369 ### Manage with Link
370
371 To manage the payment collections of an order, use [Link](!docs!/learn/fundamentals/module-links/link):
372
373 <CodeTabs group="relation-link">
374 <CodeTab label="link.create" value="method">
375
376 ```ts
377 import { Modules } from "@medusajs/framework/utils"
378
379 // ...
380
381 await link.create({
382 [Modules.ORDER]: {
383 order_id: "order_123",
384 },
385 [Modules.PAYMENT]: {
386 payment_collection_id: "paycol_123",
387 },
388 })
389 ```
390
391 </CodeTab>
392 <CodeTab label="createRemoteLinkStep" value="step">
393
394 ```ts
395 import { Modules } from "@medusajs/framework/utils"
396 import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
397
398 // ...
399
400 createRemoteLinkStep({
401 [Modules.ORDER]: {
402 order_id: "order_123",
403 },
404 [Modules.PAYMENT]: {
405 payment_collection_id: "paycol_123",
406 },
407 })
408 ```
409
410 </CodeTab>
411 </CodeTabs>
412
413 ---
414
415 ## Region Module
416
417 You can specify for each region which payment providers are available. The Medusa application defines a link between the `PaymentProvider` and the `Region` data models.
418
419 ![A diagram showcasing an example of how resources from the Payment and Region modules are linked](https://res.cloudinary.com/dza7lstvk/image/upload/v1711569520/Medusa%20Resources/payment-region_jyo2dz.jpg)
420
421 This increases the flexibility of your store. For example, you only show during checkout the payment providers associated with the cart's region.
422
423 ### Retrieve with Query
424
425 To retrieve the regions of a payment provider with [Query](!docs!/learn/fundamentals/query), pass `regions.*` in `fields`:
426
427 <CodeTabs group="relation-query">
428 <CodeTab label="query.graph" value="method">
429
430 ```ts
431 const { data: paymentProviders } = await query.graph({
432 entity: "payment_provider",
433 fields: [
434 "regions.*",
435 ],
436 })
437
438 // paymentProviders[0].regions
439 ```
440
441 </CodeTab>
442 <CodeTab label="useQueryGraphStep" value="step">
443
444 ```ts
445 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
446
447 // ...
448
449 const { data: paymentProviders } = useQueryGraphStep({
450 entity: "payment_provider",
451 fields: [
452 "regions.*",
453 ],
454 })
455
456 // paymentProviders[0].regions
457 ```
458
459 </CodeTab>
460 </CodeTabs>
461
462 ### Manage with Link
463
464 To manage the payment providers in a region, use [Link](!docs!/learn/fundamentals/module-links/link):
465
466 <CodeTabs group="relation-link">
467 <CodeTab label="link.create" value="method">
468
469 ```ts
470 import { Modules } from "@medusajs/framework/utils"
471
472 // ...
473
474 await link.create({
475 [Modules.REGION]: {
476 region_id: "reg_123",
477 },
478 [Modules.PAYMENT]: {
479 payment_provider_id: "pp_stripe_stripe",
480 },
481 })
482 ```
483
484 </CodeTab>
485 <CodeTab label="createRemoteLinkStep" value="step">
486
487 ```ts
488 import { Modules } from "@medusajs/framework/utils"
489 import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
490
491 // ...
492
493 createRemoteLinkStep({
494 [Modules.REGION]: {
495 region_id: "reg_123",
496 },
497 [Modules.PAYMENT]: {
498 payment_provider_id: "pp_stripe_stripe",
499 },
500 })
501 ```
502
503 </CodeTab>
504 </CodeTabs>