1 import { ChildDocs } from "docs-ui"
2
3 export const metadata = {
4 title: `Store Credit Module Workflows`,
5 }
6
7 # {metadata.title}
8
9 [Workflows](!docs!/learn/fundamentals/workflows) are a series of queries and actions, called steps, that complete a task. By using workflows, you benefit from features like data consistency and a reliable roll-back mechanism.
10
11 The Store Credit Module provides workflows and steps to manage store credit accounts and their usage in carts. These are provided by the `@medusajs/loyalty-plugin` package.
12
13 ## Store Credit Account Management Workflows
14
15 ### Create Store Credit Accounts
16
17 Create new store credit accounts for customers:
18
19 ```ts
20 import { createStoreCreditAccountsWorkflow } from "@medusajs/loyalty-plugin/workflows"
21
22 const { result } = await createStoreCreditAccountsWorkflow(container)
23 .run({
24 input: {
25 customer_id: "cust_123",
26 currency_code: "usd",
27 code: "SC-XXXX", // optional, auto-generated if not provided
28 },
29 })
30 ```
31
32 ### Claim Store Credit Account
33
34 Claim a store credit account for a customer:
35
36 ```ts
37 import { claimStoreCreditAccountWorkflow } from "@medusajs/loyalty-plugin/workflows"
38
39 const { result } = await claimStoreCreditAccountWorkflow(container)
40 .run({
41 input: {
42 customer_id: "cust_123",
43 code: "SC-XXXX",
44 },
45 })
46 ```
47
48 ## Credit and Debit Operations
49
50 ### Credit Store Credit Account
51
52 Add credits to a store credit account:
53
54 ```ts
55 import { creditStoreCreditAccountWorkflow } from "@medusajs/loyalty-plugin/workflows"
56
57 const { result } = await creditStoreCreditAccountWorkflow(container)
58 .run({
59 input: {
60 account_id: "sca_123",
61 amount: 100,
62 note: "Refund for order #1234",
63 reference: "order",
64 reference_id: "order_123",
65 },
66 })
67 ```
68
69 ### Credit Accounts Workflow
70
71 Credit multiple store credit accounts:
72
73 ```ts
74 import { creditAccountsWorkflow } from "@medusajs/loyalty-plugin/workflows"
75
76 const { result } = await creditAccountsWorkflow(container)
77 .run({
78 input: [
79 {
80 account_id: "sca_123",
81 amount: 100,
82 reference: "order",
83 reference_id: "order_123",
84 },
85 {
86 account_id: "sca_456",
87 amount: 50,
88 reference: "refund",
89 reference_id: "refund_123",
90 },
91 ],
92 })
93 ```
94
95 ### Debit Store Credit Account
96
97 Debit a single store credit account by a specified amount. The workflow validates that the amount is greater than zero and that the account exists before debiting. The debit is rejected if the account's balance doesn't cover the amount:
98
99 <Note>
100
101 This feature is available since [Medusa v2.21.1](https://github.com/medusajs/medusa/releases/tag/v2.21.1).
102
103 </Note>
104
105 ```ts
106 import { debitStoreCreditAccountWorkflow } from "@medusajs/loyalty-plugin/workflows"
107
108 await debitStoreCreditAccountWorkflow(container)
109 .run({
110 input: {
111 account_id: "sca_123",
112 amount: 100,
113 note: "Credit issued by mistake",
114 reference: "user",
115 reference_id: "user_123",
116 },
117 })
118 ```
119
120 ### Debit Accounts Workflow
121
122 Debit multiple store credit accounts:
123
124 ```ts
125 import { debitAccountsWorkflow } from "@medusajs/loyalty-plugin/workflows"
126
127 const { result } = await debitAccountsWorkflow(container)
128 .run({
129 input: [
130 {
131 account_id: "sca_123",
132 amount: 25,
133 reference: "cart",
134 reference_id: "cart_123",
135 },
136 ],
137 })
138 ```
139
140 ## Cart Integration Workflows
141
142 ### Add Store Credits to Cart
143
144 Apply store credits to a cart as a credit line:
145
146 ```ts
147 import { addStoreCreditsToCartWorkflow } from "@medusajs/loyalty-plugin/workflows"
148
149 await addStoreCreditsToCartWorkflow(container)
150 .run({
151 input: {
152 cart_id: "cart_123",
153 amount: 50, // optional, uses full balance if not provided
154 },
155 })
156 ```
157
158 ---
159
160 ## Reference Documentation
161
162 For complete API references and additional workflows, refer to the [Medusa Core Workflows reference](/medusa-workflows-reference).
163
164 <ChildDocs type="item" defaultItemsPerRow={2} />