Ticket Booking System Recipe
This recipe provides the general steps to implement a ticket booking system in your Medusa application.
Overview#
A ticket booking system allows customers to book tickets for events, such as shows or sports games. By using a ticket booking system, you can manage shows, venues, and seat availability, among other features.
Medusa's Framework facilitates building a ticket booking system on top of the existing commerce functionalities, such as products, pricing, inventory, carts, and orders. All of these are provided by Medusa's Commerce Modules.
To support ticket-booking features, you can customize the Medusa application by creating a Ticket Booking Module, linking its data models to Medusa's existing models, and building flows around the module.
Create Ticket Booking Module#
Your custom features and functionalities are implemented inside modules. The module is integrated into the Medusa application without any implications on existing functionalities.
The module will hold your custom data models and the service implementing ticket-booking-related features.
Create Custom Data Models#
A data model represents a table in the database. You can define in your module data models to store data related to your custom features, such as shows, venues, and seats.
For example, you can define:
- A
Venue
model for the venue where the event takes place. - A
TicketProduct
model for a show, and aTicketProductVariant
model for the ticket variants (for example, different seating sections). - A
TicketPurchase
model for a ticket purchase.
Then, you can link your custom data model to data models from other modules. For example, you can link:
- The
TicketProduct
model to the Product Module'sProduct
data model, benefiting from existing features related to products and collections. - The
TicketProductVariant
model to the Product Module'sProductVariant
data model, benefiting from existing features related to inventory and pricing. - The
TicketPurchase
model to the Order Module'sOrder
data model, benefiting from existing features related to orders and payments.
Implement Data Management Features#
Your module’s main service holds data-management and other related features. Then, in other resources, such as an API route, you can resolve the service from the Medusa container and use its functionalities.
Medusa facilitates implementing data-management features using the service factory. Your module's main service can extend this service factory, and it generates data-management methods for your data models.
Implement Ticket Booking Business Flows#
You can implement ticket-booking-related business logic in workflows. A workflow is a series of queries and actions, called steps, that complete a task.
By using workflows, you benefit from features like rollback mechanisms, error handling, and retrying failed steps. You can then execute workflows from other resources, such as an API route.
You can implement workflows that create venues and ticket products, complete carts with ticket products, and more. In the workflow's steps, you can resolve the Ticket Booking Module's service and use its data-management methods to manage ticket-booking-related data.
You can then utilize the API routes that execute these workflows in your client applications, such as your Medusa Admin customizations or your storefront.
Handle Ticket Products Inventory#
By linking the TicketProductVariant
model to the Product Module's ProductVariant
model, you can leverage Medusa's inventory management features, implemented in the Inventory Module, to manage ticket product variants' inventory.
You can set up inventory items for ticket product variants based on show dates and seating sections. You can set the available quantity for each inventory item to reflect the number of seats available in that section for that show date.
This setup ensures that customers can only book tickets for available seats, preventing overbooking.
Disable Shipping on Ticket Product Variants#
By default, Medusa product variants require shipping, which prompts the customer to provide a shipping address and choose a shipping method during checkout.
You can disable shipping on ticket product variants by setting the requires_shipping
property of the product variant's inventory item to false
.
Then, you can remove shipping-related steps from the checkout flow in the storefront.
Customize Admin Dashboard#
You can extend the Medusa Admin to provide merchants with an interface to manage ticket-booking-related data, such as shows and venues. You can inject widgets into existing pages or create new pages.
In your customizations, you send requests to the API routes you created that execute the workflows implementing ticket-booking-related features.
Add Custom Validation on Cart Operations#
Medusa's workflows provide hooks that allow you to inject custom logic at specific points in the workflow execution.
Specifically, workflows like addToCartWorkflow
and completeCartWorkflow
provide a validate
hook that you can consume to add custom validation logic.
For example, you can consume the validate
hook in the addToCartWorkflow
to check if the selected seat is available before adding a ticket product variant to the cart.
Send Order Confirmation Email with QR Code Ticket#
When an order is placed, Medusa emits an order.placed
event. You can listen to this event in a subscriber, which is an asynchronous function executed in the background when its associated event is emitted.
In the subscriber, you can send an email using the Notification Module. You can register a Notification Module Provider, such as SendGrid or Resend, that sends the email.
You can include a QR code representing the ticket in the email, which you can generate using a library like qrcode
.
You can also implement an API route that verifies the QR code at the event entrance.
Customize or Build Storefront#
Customers can book tickets through your storefront. They need features like listing shows, selecting show dates and seats, and placing orders through a checkout flow that supports ticket products.
Medusa provides a Next.js Starter Storefront with standard commerce features including listing products, placing orders, and managing accounts. You can customize the storefront and tailor its functionalities to support ticket booking features.
Alternatively, you can build the storefront with your preferred tech stack.