Back to Medusa

{metadata.title}

www/apps/resources/app/commerce-modules/store-credit/page.mdx

2.14.25.6 KB
Original Source

import { CodeTabs, CodeTab } from "docs-ui"

export const metadata = { title: Store Credit Module, }

{metadata.title}

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.

<Note>

The Store Credit Module is part of the Loyalty Plugin. It's compatible with Medusa v2.14.0+.

</Note> <Note title="Looking for no-code docs?">

Refer to the Medusa Admin User Guide to learn how to manage store credit using the dashboard.

</Note>

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.

<Note>

Learn more about why modules are isolated in this documentation.

</Note>

Features


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:

bash
npm install @medusajs/loyalty-plugin

Then, add it to your medusa-config.js:

js
module.exports = defineConfig({
  // ... other configurations
  plugins: [
    {
      resolve: `@medusajs/loyalty-plugin`,
      options: {},
    },
    // ... other plugins
  ],
})

2. Run Migrations

After installing the plugin, run the migrations to create the necessary tables in the database:

bash
npx medusa db:migrate

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:

export const highlights = [ ["11", "store_credit", "Resolve the module in a step."] ]

ts
import { 
  createWorkflow, 
  WorkflowResponse,
  createStep,
  StepResponse,
} from "@medusajs/framework/workflows-sdk"

const createStoreCreditStep = createStep(
  "create-store-credit-account",
  async ({}, { container }) => {
    const storeCreditModuleService = container.resolve("store_credit")

    const storeCreditAccount = await storeCreditModuleService.createStoreCreditAccounts({
      code: "test-code",
      customer_id: "customer_123",
      currency_code: "usd",
    })

    return new StepResponse({ storeCreditAccount }, storeCreditAccount.id)
  },
  async (storeCreditAccountId, { container }) => {
    const storeCreditModuleService = container.resolve("store_credit")

    await storeCreditModuleService.deleteStoreCreditAccounts([storeCreditAccountId])
  }
)

export const createStoreCreditWorkflow = createWorkflow(
  "create-store-credit-account",
  () => {
    const { storeCreditAccount } = createStoreCreditStep()

    return new WorkflowResponse({
      storeCreditAccount,
    })
  }
)

You can then execute the workflow in your custom API routes, scheduled jobs, or subscribers:

<CodeTabs group="resource-types"> <CodeTab label="API Route" value="api-route">
ts
import type {
  MedusaRequest,
  MedusaResponse,
} from "@medusajs/framework/http"
import { createStoreCreditWorkflow } from "../../workflows/create-store-credit"

export async function GET(
  req: MedusaRequest,
  res: MedusaResponse
) {
  const { result } = await createStoreCreditWorkflow(req.scope)
    .run()

  res.send(result)
}
</CodeTab> <CodeTab label="Subscriber" value="subscriber">
ts
import {
  type SubscriberConfig,
  type SubscriberArgs,
} from "@medusajs/framework"
import { createStoreCreditWorkflow } from "../workflows/create-store-credit"

export default async function handleCustomerCreated({
  event: { data },
  container,
}: SubscriberArgs<{ id: string }>) {
  const { result } = await createStoreCreditWorkflow(container)
    .run()

  console.log(result)
}

export const config: SubscriberConfig = {
  event: "customer.created",
}
</CodeTab> <CodeTab label="Scheduled Job" value="scheduled-job">
ts
import { MedusaContainer } from "@medusajs/framework/types"
import { createStoreCreditWorkflow } from "../workflows/create-store-credit"

export default async function myCustomJob(
  container: MedusaContainer
) {
  const { result } = await createStoreCreditWorkflow(container)
    .run()

  console.log(result)
}

export const config = {
  name: "run-once-a-day",
  schedule: `0 0 * * *`,
}
</CodeTab> </CodeTabs>

Learn more about workflows in this documentation.


<CommerceModuleSections />