Billing merchants
Merchants can sell their products on your marketplace. Now you want to set up a billing model so that you can charge merchants a sales fee.
In this tutorial, you'll take a fixed fee for each order made in the marketplace. You'll use a capped amount of $20 and charge a $0.5 fee for each order. This provides an approachable billing structure for merchants. It's similar to a monthly subscription of $20, but the merchant doesn’t pay unless your marketplace has provided them with orders.
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:
Create a usage plan for billing merchants who make sales on your marketplace
Add a monthly subscription plan for the marketplace
Bill merchants a usage fee
Requirements
Anchor link to section titled "Requirements"- You've completed the tutorial for storing order data.
Step 1: Set up subscription handlers
Anchor link to section titled "Step 1: Set up subscription handlers"To bill merchants for selling on your marketplace, you need to create the mutation that creates subscriptions and usage charges using the Admin API.
In
server/handlers/mutations
create a file calledcreate-app-subscription.js
.In
server/handlers/mutations/create-app-subscription.js
, add a method that calls theappSubscriptionCreate
mutation and creates a subscription with a usage pricing plan.The plan requires terms that tell merchants how you will be billing them, as well as a capped amount which is the maximum amount that you can charge the merchant per month. It also requires a return URL, which redirects the merchant back to your app after accepting the subscription in the Shopify admin. The method should return the subscription ID, confirmation URL, and line item ID.
In
server/handlers/rest
create a file calledget-app-subscription.js
.In
server/handlers/rest/get-app-subscription.js
, export a method that gets the recurring application charge information given a subscription ID.You can use this to get the app subscription details after the subscription's created using the GraphQL Admin API mutation.
In
server/handlers/index.js
, export the newly-created method from the handler module.
Step 2: Add a subscription confirmation URL to the internal API
Anchor link to section titled "Step 2: Add a subscription confirmation URL to the internal API"In the channel app, you need to redirect merchants to the subscription confirmation page so that they can accept the subscription.
The confirmation URL is returned when you create the subscription, and expires after two days if the merchant has not approved the subscription. You can check the status of the confirmation URL by fetching the app subscription status. If it's stale, then you can create a new subscription.
You'll need to update the Shop
data model to store the subscription ID and line item ID, which you can subsequently use to bill merchants usage charges.
Create a new sequelize migration that you'll use to add
subscriptionId
andsubscriptionLineItemId
columns to yourShop
data model.The command creates a new file in your app's
/migrations
folder with following name:{TIMESTAMP}-add-shop-subscription-info.js
Update the migration file to add the columns to your
Shops
data table.Add the new attributes to your
Shop
data model.Run the migration to write the changes to your database.
In
server/helpers.js
, create a new method calledgetNewSubscriptionConfirmationUrl
that creates a new app subscription, saves the details to the database, and returns a confirmation URL.In
server/graphql/schema.js
, create a newAdminShopSubscription
type that contains fields for the confirmation URL and a Boolean to indicate whether the subscription is accepted.In
server/graphql/resolvers.js
, add asubscription
resolver to theAdminShop
type.The resolver returns a confirmation URL for an app subscription, and whether the app subscription is accepted. Since the
adminShop
resolver on theQuery
type returns the shop details from the database, you can get thesubscriptionId
from the first parameter.
Step 3: Add the subscription to the channel app
Anchor link to section titled "Step 3: Add the subscription to the channel app"You can update the merchant onboarding page to surface information about the billing agreement that you added to the internal GraphQL API.
If the subscription is accepted, then mark the item in the onboarding checklist as completed. Otherwise, you'll redirect the merchant to the subscriptionConfirmationUrl
so that they can accept the subscription.
In
app/src/sections/Onboarding/components/OnboardingInfoCard.jsx
update theOnboardingInfoCard
component to include information about the billing agreement.The onboarding information card should display information about the billing agreement. The following is an example:
In
app/src/sections/Onboarding/components/OnboardingChecklistCard.jsx
, update theOnboardingChecklistCard
component to accept props for the subscription confirmation URL and whether the subscription is accepted.The component should render an item for the subscription agreement, which redirects to the confirmation URL on action.
In
app/src/sections/Onboarding/Onboarding.jsx
update the admin shop query to fetch the subscription details.Update the
Onboarding
component to pass the subscription details from the query to theOnboardingChecklistCard
component. The component should also use thesubscriptionAccepted
Boolean to determine whether the shop meets marketplace requirements.The onboarding page checklist should now include an item for the subscription billing agreement:
Clicking on the billing agreement requirement link should redirect to a subscription approval page:
In
app/src/sections/Settings.jsx
, update the query to fetch the subscription details.The
Settings
component should pass the subscription details from the query into theSettingsChecklist
component. TheSettingsChecklist
component should render an item for the billing requirement.
Step 4: Bill a usage fee
Anchor link to section titled "Step 4: Bill a usage fee"After merchants have approved the subscription, you can start billing usage fees for orders placed on your marketplace.
In
server/handlers/mutations
add a file calledcreate-usage-record.js
.In
server/handlers/mutations/create-usage-record.js
, add a method that creates a usage charge using theappUsageRecordCreate
mutation.It should accept the
subscriptionLineItemId
, which is stored in your database, and bill a $0.5 usage fee.In
server/handlers/index.js
exportcreateUsageRecord
from the handlers module.In
server/handlers/webhooks/orders.js
update thehandleOrderCreate
function to get the shop’ssubscriptionLineItemId
from the database, and call the handler that you created.
- Share feedback on Marketplace Kit.