Storing order data
Your marketplace is ready for merchants to start selling. You want to save order data to your database for orders made on a marketplace through your channel app.
In this tutorial, you'll create a database table for storing order data. You'll also subscribe to webhook events and create handlers for them.
What you'll learn
Anchor link to section titled "What you'll learn"After finishing this tutorial, you'll know how to do the following:
Handle webhook events for orders
Store order data in your database
Requirements
Anchor link to section titled "Requirements"You've completed the Merchant onboarding tutorials.
You're familiar with webhooks.
Access scopes
Anchor link to section titled "Access scopes"Your channel app requires the following access scopes:
read_orders
Step 1: Add the orders model
Anchor link to section titled "Step 1: Add the orders model"In this step, you'll create an Orders
data table in your schema, with column data reflecting the information you want to store. You'll also run a database migration. For an overview of the available fields for generating your orders model, refer to the Order API reference.
Run the following command to create a new migration file:
In the newly-generated migration file, add the following code:
Create a
models/order.js
file and add the following code:Run the following to apply the migration and create the new data table:
Step 2: Add a webhook handler
Anchor link to section titled "Step 2: Add a webhook handler"In this step, you'll create the framework to subscribe to the ORDERS_CREATE
webhook topic and handle orders/create
webhook events. You'll do the same for the ORDERS_FULFILLED
webhook topic.
In
server/handlers/webhooks
, create anorders.js
file.In
server/handlers/webhooks/orders.js
, add a webhook handler to listen for incoming webhooks and save them to theOrders
data table that you created.In
server/handlers/webhooks/setup.js
, subscribe to theORDERS_CREATE
andORDERS_FULFILLED
webhooks.
Step 3: Register additional webhooks
Anchor link to section titled "Step 3: Register additional webhooks"There are several Order
-adjacent webhook topics that you might want to subscribe to and make database adjustments based on their content.
Learn how to bill merchants for sales they make on your marketplace.
Share feedback on Marketplace Kit.