Store Credit Module
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.
Medusa has store-credit related features available through the Store Credit Module. A module 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.
Features#
- Store Credit Accounts: Create and manage customer store credit accounts
- Credit Transactions: Credit and debit operations with full transaction history
- Balance Tracking: Real-time balance calculation and reporting
- Cart Integration: Apply store credits as payment during checkout
How to Use the Store Credit Module#
1. Install the Loyalty Plugin#
The Store Credit Module is part of the Loyalty Plugin. So, install the loyalty plugin:
Then, add it to your medusa-config.js:
2. Run Migrations#
After installing the plugin, run the migrations to create the necessary tables in the database:
3. Use Store Credit Features in Your Application#
In your Medusa application, you build flows around Commerce Modules. A flow is built as a Workflow, which is a special function composed of a series of steps that guarantees data consistency and reliable roll-back mechanism.
You can build custom workflows and steps. You can also re-use the Loyalty Plugin's workflows and steps.
For example:
1import { 2 createWorkflow, 3 WorkflowResponse,4 createStep,5 StepResponse,6} from "@medusajs/framework/workflows-sdk"7 8const createStoreCreditStep = createStep(9 "create-store-credit-account",10 async ({}, { container }) => {11 const storeCreditModuleService = container.resolve("store_credit")12 13 const storeCreditAccount = await storeCreditModuleService.createStoreCreditAccounts({14 code: "test-code",15 customer_id: "customer_123",16 currency_code: "usd",17 })18 19 return new StepResponse({ storeCreditAccount }, storeCreditAccount.id)20 },21 async (storeCreditAccountId, { container }) => {22 const storeCreditModuleService = container.resolve("store_credit")23 24 await storeCreditModuleService.deleteStoreCreditAccounts([storeCreditAccountId])25 }26)27 28export const createStoreCreditWorkflow = createWorkflow(29 "create-store-credit-account",30 () => {31 const { storeCreditAccount } = createStoreCreditStep()32 33 return new WorkflowResponse({34 storeCreditAccount,35 })36 }37)
You can then execute the workflow in your custom API routes, scheduled jobs, or subscribers:
Learn more about workflows in this documentation.