I am following the tutorial in https://shopify.dev/apps/getting-started/create and have successfully setup a test app that has frontend and backend running.
Now I am trying to test billing. I am following the instructions in https://github.com/Shopify/shopify-api-js/blob/main/docs/guides/billing.md and https://github.com/Shopify/shopify-api-js/blob/main/docs/reference/billing/check.md
I put the following codes in my web/index.js but it is not working.
async function billingMiddleware(req, res, next) {
const session = res.locals.shopify.session;
const hasPayment = await shopify.billing.check({
session,
plans: ['My billing plan'],
isTest: true,
});
if (hasPayment) {
next();
} else {
// Either request payment now (if single plan) or redirect to plan selection page (if multiple plans available), e.g.
const confirmationUrl = await shopify.billing.request({
session,
plan: 'My billing plan',
isTest: true,
});
res.redirect(confirmationUrl);
}
}
app.use('/*', billingMiddleware);
I am not sure if it is the right thing to retrieve the session from
res.locals.shopify.session because the doc said I need to do the following
// use sessionId to retrieve session from app's session storage
// In this example, getSessionFromStorage() must be provided by app
const session = await getSessionFromStorage(sessionId);
I see this error
2023-01-06 14:25:39 | backend | TypeError: Cannot read properties of undefined (reading ‘session’)
Anyone can share or point me to example how this is implemented? I just want to test out a simple global gate where user will be redirected to the billing page if they have not paid.