I am testing the managed price using a test store, and my app has not been published yet.
I have set up a Pro plan and a Free plan.
When I switched from the Free plan to the Pro plan, I received the webhook, and everything worked correctly.
However, when I switched from Pro back to Free, a confirmation page popped up stating that the plan would change to Free only after the Pro plan expires.
This is also the behavior I expected — switching to Free after the Pro plan expires.
However, I immediately received a subscription update webhook, and my plan changed to Free right away.
This is very strange, it means my test account directly switched to Free.
Is this behavior only in the test store, and official merchants won’t encounter this issue?
Or do I need to do some additional work to ensure it runs correctly?
My understanding is that I have integrated managed pricing, and then by using webhooks and providing refreshes, I can handle it properly, isn’t that right?
here is my code:
import type { ActionFunctionArgs } from "react-router";
import { authenticate } from "../shopify.server";
import * as SubscriptionDb from "../db/subscriptions.server";
import { refreshSubscription } from "app/services/subscription-service.server";
export const action = async ({ request }: ActionFunctionArgs) => {
const { shop, session, topic, admin } = await authenticate.webhook(request);
console.log(
`[SUB_UPDATE] Received ${topic} webhook for ${shop}. session exists:${session !== undefined}`,
);
if (!session) {
console.log(`[SUB_UPDATE] Deleting subscription for ${shop}`);
await SubscriptionDb.deleteSubscriptionsByShop(shop);
} else if (admin) {
console.log(`[SUB_UPDATE] Refreshing subscription for ${shop}`);
await refreshSubscription(session.shop, admin);
}
return new Response();
};
// app/services/subscription-service.server
export async function refreshSubscription(shop: string, admin?: AdminApiType) {
try {
const adminToUse = admin || (await unauthenticated.admin(shop)).admin;
// Get the latest active subscription from Shopify
const { currentAppInstallation } = await getActiveSubscriptions(adminToUse);
const activeSubscription = currentAppInstallation.activeSubscriptions[0];
console.log("activeSubscription", shop, activeSubscription);
// Delete all existing subscriptions for the shop
await SubscriptionDb.deleteSubscriptionsByShop(shop);
// If there's an active subscription, create it in our database
if (activeSubscription) {
// upsert
await SubscriptionDb.createSubscription({
shop,
subscriptionId: activeSubscription.id,
status: activeSubscription.status,
planName: activeSubscription.name,
currentPeriodEnd: new Date(activeSubscription.currentPeriodEnd),
});
return activeSubscription;
}
} catch (exception) {
console.error(
`refreshSubscription - failed. shop:${shop}. exception: ${exception}`,
);
}
return null;
}


