1 import { Prerequisites } from "docs-ui"
2
3 export const metadata = {
4 title: `Store Credit Concepts`,
5 }
6
7 # {metadata.title}
8
9 In this guide, you'll learn about store credit accounts and how they work in the Loyalty Plugin.
10
11 <Prerequisites
12 items={[
13 {
14 text: "Loyalty Plugin Installed",
15 link: "../page.mdx",
16 }
17 ]}
18 />
19
20 ## What are Store Credits?
21
22 Store credit provides a way to give customers account-based credit that can be used for future purchases. Store credit is tied to specific customer accounts and provides a running balance that can be credited and debited over time.
23
24 For example, you can use store credit to:
25
26 - Issue refunds that customers can use for future purchases
27 - Reward loyal customers with account credits
28 - Allow customers to accumulate credit from multiple transactions
29
30 ---
31
32 ## Store Credit Account
33
34 A store credit account, represented by the `StoreCreditAccount` data model, is a customer's credit balance. Each account has:
35
36 - `customer_id`: The ID of the customer associated with the account
37 - `code`: A unique code for the account. This is useful when claiming the account.
38 - `currency_code`: The currency for the account balance
39 - `transactions`: A list of all credit and debit transactions for the account, represented by the [AccountTransaction](#account-transactions) data model.
40
41 ### Account Transactions
42
43 All store credit activity is tracked through transactions, which are stored in the `AccountTransaction` data model.
44
45 Each transaction includes:
46
47 - `type`: `credit` (adding funds) or `debit` (spending funds)
48 - `amount`: The transaction amount in the major currency unit (decimal).
49 - If `type` is `credit`, the amount is added to the balance.
50 - If `type` is `debit`, the amount is subtracted from the balance
51 - `reference`: The name of the table that triggered the transaction (for example, `order`, `refund`, `promotion`, etc...)
52 - `reference_id`: The ID of the record in the reference table that triggered the transaction (for example, `order_123`, `refund_456`, etc...)
53
54 ---
55
56 ## Store Credit Balance Calculation
57
58 Store credit account balances are calculated in real-time by:
59
60 1. Summing all credit transactions
61 2. Subtracting all debit transactions
62
63 The result is the current available store credit balance for the customer.
64
65 The balance is not stored directly in the database, but is calculated on demand from the transaction history. This ensures that the balance is always accurate and reflects all account activity.
66
67 ---
68
69 ## Using Store Credit in Orders
70
71 Customers can apply their available store credit during the checkout process to reduce the total amount of their order.
72
73 Medusa validates that sufficient credit is available and reserves the applied credit until the order is confirmed.
74
75 ### During Checkout
76
77 During the checkout process, customers can choose to apply their store credit to the order.
78
79 Medusa will:
80
81 1. Validate that the customer has sufficient available store credit balance
82 2. Apply the amount as a credit line on the cart.
83
84 The cart's total will be reduced by the created credit line.
85
86 Once the customer places the order, the reserved store credit amount will be converted into a debit transaction on the customer's store credit account.
87
88 ### Refunds
89
90 When an admin issues a refund for an order, the refunded amount is added as a credit transaction to the customer's store credit account.
91
92 The customer can then use this store credit for future purchases.
93
94 ---
95
96 ## Claiming Store Credit Accounts
97
98 Customers can claim their store credit accounts using the unique `code` associated with their account. This is useful if the customer's account was created by an admin, or if the customer was a guest at the time the store credit was issued.
99
100 The claimed store credit account will be associated with the customer's account and the customer can view their available store credit balance and transaction history.
101
102 {/* TODO add link to API documentation */}
103
104 Medusa provides a Claim Store Credit Account API to allow customers to claim their store credit accounts.
105
106 Alternatively, you can do it within your custom code. For example:
107
108 ```ts
109 await claimStoreCreditAccountWorkflow(req.scope).run({
110 input: {
111 code: "UNIQUE_ACCOUNT_CODE",
112 // logged-in customer's ID is used to associate the claimed account with the customer's account
113 customer_id: req.auth_context.actor_id,
114 },
115 })