|
1
|
--- |
|
2
|
generate_toc: true |
|
3
|
--- |
|
4
|
|
|
5
|
import { CodeTabs, CodeTab } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `Store Credit Module`, |
|
9
|
} |
|
10
|
|
|
11
|
# {metadata.title} |
|
12
|
|
|
13
|
In this section of the documentation, you will find resources to learn more about the Store Credit Module and how to use it in your application. |
|
14
|
|
|
15
|
<Note> |
|
16
|
|
|
17
|
The Store Credit Module is part of the Loyalty Plugin. It's compatible with Medusa v2.14.0+. |
|
18
|
|
|
19
|
</Note> |
|
20
|
|
|
21
|
<Note title="Looking for no-code docs?"> |
|
22
|
|
|
23
|
Refer to the [Medusa Admin User Guide](!user-guide!/loyalty/store-credits) to learn how to manage store credit using the dashboard. |
|
24
|
|
|
25
|
</Note> |
|
26
|
|
|
27
|
Medusa has store-credit related features available through the Store Credit 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 Store Credit Module. |
|
28
|
|
|
29
|
<Note> |
|
30
|
|
|
31
|
Learn more about why modules are isolated in [this documentation](!docs!/learn/fundamentals/modules/isolation). |
|
32
|
|
|
33
|
</Note> |
|
34
|
|
|
35
|
## Features |
|
36
|
|
|
37
|
- [Store Credit Accounts](./concepts/page.mdx#store-credit-account): Create and manage customer store credit accounts |
|
38
|
- [Credit Transactions](./concepts/page.mdx#store-credit-account): Credit and debit operations with full transaction history |
|
39
|
- [Balance Tracking](./concepts/page.mdx#store-credit-balance-calculation): Real-time balance calculation and reporting |
|
40
|
- [Cart Integration](./concepts/page.mdx#during-checkout): Apply store credits as payment during checkout |
|
41
|
|
|
42
|
--- |
|
43
|
|
|
44
|
## How to Use the Store Credit Module |
|
45
|
|
|
46
|
### 1. Install the Loyalty Plugin |
|
47
|
|
|
48
|
The Store Credit Module is part of the Loyalty Plugin. So, install the loyalty plugin: |
|
49
|
|
|
50
|
```bash npm2yarn |
|
51
|
npm install @medusajs/loyalty-plugin |
|
52
|
``` |
|
53
|
|
|
54
|
Then, add it to your `medusa-config.js`: |
|
55
|
|
|
56
|
```js title="medusa-config.js" |
|
57
|
module.exports = defineConfig({ |
|
58
|
// ... other configurations |
|
59
|
plugins: [ |
|
60
|
{ |
|
61
|
resolve: `@medusajs/loyalty-plugin`, |
|
62
|
options: {}, |
|
63
|
}, |
|
64
|
// ... other plugins |
|
65
|
], |
|
66
|
}) |
|
67
|
``` |
|
68
|
|
|
69
|
### 2. Run Migrations |
|
70
|
|
|
71
|
After installing the plugin, run the migrations to create the necessary tables in the database: |
|
72
|
|
|
73
|
```bash npx2yarn |
|
74
|
npx medusa db:migrate |
|
75
|
``` |
|
76
|
|
|
77
|
### 3. Use Store Credit Features in Your Application |
|
78
|
|
|
79
|
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. |
|
80
|
|
|
81
|
You can build custom workflows and steps. You can also re-use the Loyalty Plugin's workflows and steps. |
|
82
|
|
|
83
|
For example: |
|
84
|
|
|
85
|
export const highlights = [ |
|
86
|
["11", "store_credit", "Resolve the module in a step."] |
|
87
|
] |
|
88
|
|
|
89
|
```ts title="src/workflows/create-store-credit.ts" highlights={highlights} |
|
90
|
import { |
|
91
|
createWorkflow, |
|
92
|
WorkflowResponse, |
|
93
|
createStep, |
|
94
|
StepResponse, |
|
95
|
} from "@medusajs/framework/workflows-sdk" |
|
96
|
|
|
97
|
const createStoreCreditStep = createStep( |
|
98
|
"create-store-credit-account", |
|
99
|
async ({}, { container }) => { |
|
100
|
const storeCreditModuleService = container.resolve("store_credit") |
|
101
|
|
|
102
|
const storeCreditAccount = await storeCreditModuleService.createStoreCreditAccounts({ |
|
103
|
code: "test-code", |
|
104
|
customer_id: "customer_123", |
|
105
|
currency_code: "usd", |
|
106
|
}) |
|
107
|
|
|
108
|
return new StepResponse({ storeCreditAccount }, storeCreditAccount.id) |
|
109
|
}, |
|
110
|
async (storeCreditAccountId, { container }) => { |
|
111
|
const storeCreditModuleService = container.resolve("store_credit") |
|
112
|
|
|
113
|
await storeCreditModuleService.deleteStoreCreditAccounts([storeCreditAccountId]) |
|
114
|
} |
|
115
|
) |
|
116
|
|
|
117
|
export const createStoreCreditWorkflow = createWorkflow( |
|
118
|
"create-store-credit-account", |
|
119
|
() => { |
|
120
|
const { storeCreditAccount } = createStoreCreditStep() |
|
121
|
|
|
122
|
return new WorkflowResponse({ |
|
123
|
storeCreditAccount, |
|
124
|
}) |
|
125
|
} |
|
126
|
) |
|
127
|
``` |
|
128
|
|
|
129
|
You can then execute the workflow in your custom API routes, scheduled jobs, or subscribers: |
|
130
|
|
|
131
|
<CodeTabs group="resource-types"> |
|
132
|
<CodeTab label="API Route" value="api-route"> |
|
133
|
|
|
134
|
```ts title="src/api/workflow/route.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports" |
|
135
|
import type { |
|
136
|
MedusaRequest, |
|
137
|
MedusaResponse, |
|
138
|
} from "@medusajs/framework/http" |
|
139
|
import { createStoreCreditWorkflow } from "../../workflows/create-store-credit" |
|
140
|
|
|
141
|
export async function GET( |
|
142
|
req: MedusaRequest, |
|
143
|
res: MedusaResponse |
|
144
|
) { |
|
145
|
const { result } = await createStoreCreditWorkflow(req.scope) |
|
146
|
.run() |
|
147
|
|
|
148
|
res.send(result) |
|
149
|
} |
|
150
|
``` |
|
151
|
|
|
152
|
</CodeTab> |
|
153
|
<CodeTab label="Subscriber" value="subscriber"> |
|
154
|
|
|
155
|
```ts title="src/subscribers/customer-created.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports" |
|
156
|
import { |
|
157
|
type SubscriberConfig, |
|
158
|
type SubscriberArgs, |
|
159
|
} from "@medusajs/framework" |
|
160
|
import { createStoreCreditWorkflow } from "../workflows/create-store-credit" |
|
161
|
|
|
162
|
export default async function handleCustomerCreated({ |
|
163
|
event: { data }, |
|
164
|
container, |
|
165
|
}: SubscriberArgs<{ id: string }>) { |
|
166
|
const { result } = await createStoreCreditWorkflow(container) |
|
167
|
.run() |
|
168
|
|
|
169
|
console.log(result) |
|
170
|
} |
|
171
|
|
|
172
|
export const config: SubscriberConfig = { |
|
173
|
event: "customer.created", |
|
174
|
} |
|
175
|
``` |
|
176
|
|
|
177
|
</CodeTab> |
|
178
|
<CodeTab label="Scheduled Job" value="scheduled-job"> |
|
179
|
|
|
180
|
```ts title="src/jobs/run-daily.ts" highlights={[["7"], ["8"]]} |
|
181
|
import { MedusaContainer } from "@medusajs/framework/types" |
|
182
|
import { createStoreCreditWorkflow } from "../workflows/create-store-credit" |
|
183
|
|
|
184
|
export default async function myCustomJob( |
|
185
|
container: MedusaContainer |
|
186
|
) { |
|
187
|
const { result } = await createStoreCreditWorkflow(container) |
|
188
|
.run() |
|
189
|
|
|
190
|
console.log(result) |
|
191
|
} |
|
192
|
|
|
193
|
export const config = { |
|
194
|
name: "run-once-a-day", |
|
195
|
schedule: `0 0 * * *`, |
|
196
|
} |
|
197
|
``` |
|
198
|
|
|
199
|
</CodeTab> |
|
200
|
</CodeTabs> |
|
201
|
|
|
202
|
Learn more about workflows in [this documentation](!docs!/learn/fundamentals/workflows). |
|
203
|
|
|
204
|
--- |
|
205
|
|
|
206
|
<CommerceModuleSections /> |