@pillexa/medusa-subscription-plugin
v0.1.11
Published
A subscription management plugin for Medusa e-commerce
Maintainers
Readme
@pillexa/medusa-subscription-plugin
A comprehensive subscription management plugin for Medusa e-commerce platform, built with TypeScript.
Features
- 🚀 Subscription Management: Create, update, and cancel subscriptions
- 🔄 Recurring Billing: Support for weekly, monthly, and yearly billing cycles
- 📡 Event-Driven: Automatic subscription creation from orders
- 🛡️ TypeScript: Full TypeScript support with type safety
- 🧪 Testing: Comprehensive test coverage
- 📚 RESTful API: Complete API endpoints for subscription management
- 🔔 Event Handling: Subscribe to subscription lifecycle events
Installation
npm install @pillexa/medusa-subscription-pluginConfiguration
Add the plugin to your Medusa configuration:
// medusa-config.js
const plugins = [
// ... other plugins
{
resolve: "@pillexa/medusa-subscription-plugin",
options: {
// Plugin options can be added here
}
}
];
module.exports = {
projectConfig: {
// ... your project config
},
plugins
};Usage
Service Usage
import { SubscriptionService } from "@pillexa/medusa-subscription-plugin";
// Create a subscription
const subscription = await subscriptionService.create({
customer_id: "cust_123",
product_id: "prod_456",
interval: "monthly",
status: "active"
});
// Retrieve a subscription
const subscription = await subscriptionService.retrieve("sub_123");
// List subscriptions
const subscriptions = await subscriptionService.list({
customer_id: "cust_123",
status: "active"
});
// Update a subscription
const updatedSubscription = await subscriptionService.update("sub_123", {
status: "paused",
interval: "yearly"
});
// Cancel a subscription
const cancelledSubscription = await subscriptionService.cancel("sub_123");
// Process billing
const billingResult = await subscriptionService.processBilling("sub_123");API Endpoints
The plugin provides the following REST API endpoints:
GET /subscriptions
List all subscriptions with optional filters.
Query Parameters:
customer_id(string): Filter by customer IDstatus(string): Filter by status (active, cancelled, paused, expired)interval(string): Filter by interval (weekly, monthly, yearly)
Example:
GET /subscriptions?customer_id=cust_123&status=activeGET /subscriptions/:id
Get a specific subscription by ID.
Example:
GET /subscriptions/sub_123POST /subscriptions
Create a new subscription.
Request Body:
{
"customer_id": "cust_123",
"product_id": "prod_456",
"interval": "monthly",
"status": "active"
}PUT /subscriptions/:id
Update a subscription.
Request Body:
{
"status": "paused",
"interval": "yearly"
}DELETE /subscriptions/:id
Cancel a subscription.
POST /subscriptions/:id/billing
Process billing for a subscription.
GET /subscriptions/customer/:customer_id
Get all subscriptions for a specific customer.
GET /subscriptions/status/:status
Get all subscriptions with a specific status.
Event Handling
The plugin automatically handles the following events:
Order Events
order.placed: Creates subscriptions for subscription productsorder.payment_captured: Handles payment processingorder.cancelled: Handles order cancellation
Subscription Events
subscription.created: Triggered when a subscription is createdsubscription.updated: Triggered when a subscription is updatedsubscription.cancelled: Triggered when a subscription is cancelledsubscription.billing_processed: Triggered when billing is processed
Types
interface Subscription {
id: string;
customer_id: string;
product_id: string;
interval: 'weekly' | 'monthly' | 'yearly';
status: 'active' | 'cancelled' | 'paused' | 'expired';
created_at: Date;
updated_at: Date;
next_billing_date: Date;
order_id?: string;
}
interface CreateSubscriptionData {
customer_id: string;
product_id: string;
interval: SubscriptionInterval;
status?: SubscriptionStatus;
order_id?: string;
}
interface UpdateSubscriptionData {
customer_id?: string;
product_id?: string;
interval?: SubscriptionInterval;
status?: SubscriptionStatus;
next_billing_date?: Date;
}Development
Prerequisites
- Node.js 18+
- npm or yarn
- Medusa backend
Setup
Clone the repository
Install dependencies:
cd packages/subscriptions npm installBuild the plugin:
npm run buildRun tests:
npm testRun linting:
npm run lint
Scripts
npm run build: Build the TypeScript codenpm run watch: Watch for changes and rebuildnpm test: Run testsnpm run test:watch: Run tests in watch modenpm run lint: Run ESLintnpm run lint:fix: Fix ESLint issuesnpm run type-check: Run TypeScript type checking
Testing
The plugin includes comprehensive tests covering:
- Service methods (create, retrieve, list, update, cancel, processBilling)
- API endpoints
- Event handling
- Error cases
- Type validation
Run tests with:
npm testRun tests with coverage:
npm test -- --coverageContributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
License
MIT
Support
For support, please open an issue on GitHub or contact the Pillexa team.
Changelog
v0.1.0
- Initial release
- Basic subscription management
- TypeScript support
- RESTful API endpoints
- Event-driven architecture
- Comprehensive testing
