|
1
|
--- |
|
2
|
products: |
|
3
|
- order |
|
4
|
- payment |
|
5
|
--- |
|
6
|
|
|
7
|
import { AcademicCapSolid, Github } from "@medusajs/icons" |
|
8
|
|
|
9
|
export const metadata = { |
|
10
|
title: `Subscriptions Recipe`, |
|
11
|
} |
|
12
|
|
|
13
|
# {metadata.title} |
|
14
|
|
|
15
|
This recipe provides the general steps to build subscription-based purchase with Medusa. |
|
16
|
|
|
17
|
## Overview |
|
18
|
|
|
19
|
Subscription-based purchase allows customers to purchase products for a specified period, and the payment and fulfillment is processed within a regular interval in that period. |
|
20
|
|
|
21
|
For example, a customer can purchase a book subscription box for a period of three months. Each month, the payment is captured for that order and, if the payment is successful, the fulfillment is processed. |
|
22
|
|
|
23
|
The Medusa [Framework](!docs!/learn/fundamentals/framework) built for customizations facilitates building subscription-based purchases. You can create a Subscription Module that implements data models for subscriptions, and link those data models to existing ones such as products and orders. |
|
24
|
|
|
25
|
You can also expose custom features using API routes, and implement complex flows using workflows. |
|
26
|
|
|
27
|
<Note title="Related use-case"> |
|
28
|
|
|
29
|
[How Goodchef built subscription-based purchases with Medusa](https://medusajs.com/blog/goodchef/). |
|
30
|
|
|
31
|
</Note> |
|
32
|
|
|
33
|
--- |
|
34
|
|
|
35
|
## Save Subscription Details |
|
36
|
|
|
37
|
Subscriptions have details related to the subscription interval, subscription period, and more. |
|
38
|
|
|
39
|
To store the subscription details, you can create a data model in a new subscription module. The module's main service provides data management feature of the data model. |
|
40
|
|
|
41
|
You can link the subscription data model to models of other modules, such as the Order Module's `Order` data model. |
|
42
|
|
|
43
|
<CardList items={[ |
|
44
|
{ |
|
45
|
href: "!docs!/learn/fundamentals/modules", |
|
46
|
title: "Create a Module", |
|
47
|
text: "Learn how to create a module.", |
|
48
|
icon: AcademicCapSolid, |
|
49
|
}, |
|
50
|
{ |
|
51
|
href: "!docs!/learn/fundamentals/modules#1-create-data-model", |
|
52
|
title: "Create a Data Model", |
|
53
|
text: "Learn how to create a data model.", |
|
54
|
icon: AcademicCapSolid, |
|
55
|
}, |
|
56
|
]} /> |
|
57
|
|
|
58
|
--- |
|
59
|
|
|
60
|
## Link Subscription to Existing Data Models |
|
61
|
|
|
62
|
Define a module link that links a data model from your subscription module with a data model from another module. |
|
63
|
|
|
64
|
For example, you can link the subscription data model to the Order Module's `Order` data model. |
|
65
|
|
|
66
|
If you want to create subscriptions on the product level, you can link the subscription data model to the Product Module's `Product` data model. |
|
67
|
|
|
68
|
<Card |
|
69
|
href="!docs!/learn/fundamentals/module-links" |
|
70
|
title="Define a Module Link" |
|
71
|
text="Learn how to define a module link." |
|
72
|
icon={AcademicCapSolid} |
|
73
|
/> |
|
74
|
|
|
75
|
--- |
|
76
|
|
|
77
|
## Implement Subscription Approach |
|
78
|
|
|
79
|
There are different ways to implement subscriptions in your Medusa application. This recipe covers two options. |
|
80
|
|
|
81
|
### Option 1: Custom Subscription Logic |
|
82
|
|
|
83
|
By implementing the subscription logic within your application, you have full control over the subscription logic. You'll also be independent of payment providers, providing customers with more than one payment provider. |
|
84
|
|
|
85
|
Implementing the logic depends on your use case, but you'll mainly implement the following: |
|
86
|
|
|
87
|
1. Create a workflow that completes a cart and creates a subscription for the order. |
|
88
|
1. Create an API route that executes the workflow. |
|
89
|
2. Create a scheduled job that checks daily for subscriptions that need renewal. |
|
90
|
3. Create another scheduled job that checks daily for subscriptions that are expired. |
|
91
|
|
|
92
|
<CardList itemsPerRow={2} items={[ |
|
93
|
{ |
|
94
|
href: "!docs!/learn/fundamentals/workflows", |
|
95
|
title: "Create a Workflow", |
|
96
|
text: "Learn how to create a workflow.", |
|
97
|
icon: AcademicCapSolid, |
|
98
|
}, |
|
99
|
{ |
|
100
|
href: "!docs!/learn/fundamentals/api-routes", |
|
101
|
title: "Create an API Route", |
|
102
|
text: "Learn how to create an API route.", |
|
103
|
icon: AcademicCapSolid, |
|
104
|
}, |
|
105
|
]} /> |
|
106
|
|
|
107
|
<Card |
|
108
|
href="!docs!/learn/fundamentals/scheduled-jobs#1-create-a-scheduled-job" |
|
109
|
title="Create a Scheduled Job" |
|
110
|
text="Learn how to create a scheduled job." |
|
111
|
icon={AcademicCapSolid} |
|
112
|
className="mt-1" |
|
113
|
/> |
|
114
|
|
|
115
|
### Option 2: Using Stripe Subscriptions |
|
116
|
|
|
117
|
Stripe provides a [subscription payments feature](https://stripe.com/docs/billing/subscriptions/overview) that allows you to authorize payment on a subscription basis within Stripe. Stripe then handles checking for recurring payments and capturing payment at the specified interval. |
|
118
|
|
|
119
|
This approach allows you to delegate the complications of implementing the subscription logic to Stripe, but doesn't support using other payment providers. |
|
120
|
|
|
121
|
Although Medusa provides a [Stripe Payment Module Provider](../../commerce-modules/payment/payment-provider/stripe/page.mdx), it doesn't handle subscriptions. You can create a custom Stripe Subscription Module Provider instead. |
|
122
|
|
|
123
|
<Card |
|
124
|
href="/references/payment/provider" |
|
125
|
title="Create Payment Module Provider" |
|
126
|
text="Learn how to create a payment module provider." |
|
127
|
icon={AcademicCapSolid} |
|
128
|
/> |
|
129
|
|
|
130
|
--- |
|
131
|
|
|
132
|
## Customize Admin Dashboard |
|
133
|
|
|
134
|
Based on your use case, you may need to customize the Medusa Admin to add new widgets or pages. |
|
135
|
|
|
136
|
For example, you can create a page that lists all subscriptions or a widget that shows an order's subscription information. |
|
137
|
|
|
138
|
The Medusa Admin is an extensible application within your Medusa application. You can customize it by: |
|
139
|
|
|
140
|
- **Widgets**: Adding widgets to existing pages, such as the order page. |
|
141
|
- **UI Routes**: Adding new pages to the Medusa Admin, such as a page to manage subscriptions. |
|
142
|
- **Settings Pages**: Adding new pages to the Medusa Admin settings, such as a page to manage subscription settings. |
|
143
|
|
|
144
|
<CardList items={[ |
|
145
|
{ |
|
146
|
href: "!docs!/learn/fundamentals/admin/widgets", |
|
147
|
title: "Create Admin Widget", |
|
148
|
text: "Add widgets into existing admin pages.", |
|
149
|
icon: AcademicCapSolid, |
|
150
|
}, |
|
151
|
{ |
|
152
|
href: "!docs!/learn/fundamentals/admin/ui-routes", |
|
153
|
title: "Create Admin UI Routes", |
|
154
|
text: "Add new pages to your Medusa Admin.", |
|
155
|
icon: AcademicCapSolid, |
|
156
|
}, |
|
157
|
]} /> |
|
158
|
|
|
159
|
<Card |
|
160
|
href="!docs!/learn/fundamentals/admin/ui-routes#create-settings-page" |
|
161
|
title="Create Admin Setting Page" |
|
162
|
text="Add new page to the Medusa Admin settings." |
|
163
|
icon={AcademicCapSolid} |
|
164
|
className="mt-1" |
|
165
|
/> |
|
166
|
|
|
167
|
--- |
|
168
|
|
|
169
|
## Customize or Build Storefront |
|
170
|
|
|
171
|
Medusa provides a Next.js Starter Storefront to use with your application. You can customize it for your subscription use case, such as allowing customers to manage their subscriptions. |
|
172
|
|
|
173
|
Alternatively, you can build your own storefront using the Medusa APIs. This headless approach gives you the flexibility to build a custom storefront without limitations on which tech stack you use, or the design of the storefront. |
|
174
|
|
|
175
|
<CardList items={[ |
|
176
|
{ |
|
177
|
href: "/nextjs-starter", |
|
178
|
title: "Next.js Starter Storefront", |
|
179
|
text: "Learn how to install and customize the Next.js Starter Storefront.", |
|
180
|
icon: AcademicCapSolid, |
|
181
|
}, |
|
182
|
{ |
|
183
|
href: "/storefront-development", |
|
184
|
title: "Storefront Development", |
|
185
|
text: "Find guides to build your own storefront.", |
|
186
|
icon: AcademicCapSolid, |
|
187
|
}, |
|
188
|
]} /> |