All things Shopify and commerce
import { redirect } from "@remix-run/node";
import { authenticate, BASIC_PLAN, BUSINESS_PLAN, PRO_PLAN } from "../shopify.server";
export const loader = async ({ request }) => {
try {
const { billing, session } = await authenticate.admin(request);
const { shop } = session;
const myShop = shop.replace(".myshopify.com", "");
// Extract the plan from the URL query parameters
const url = new URL(request.url);
const selectedPlan = url.searchParams.get("plan");
const plansNames = {
basic: BASIC_PLAN,
business: BUSINESS_PLAN,
pro: PRO_PLAN,
};
const plan = plansNames[selectedPlan];
if (!plan) {
console.log("Invalid plan selected");
return new Response("Invalid plan selected", { status: 400 });
}
try {
await billing.require({
plans: [plan],
onFailure: async () => {
await billing.request({
plan: plan,
isTest: true,
returnUrl: `https://admin.shopify.com/store/${myShop}/apps/${process.env.APP_NAME}/app/pricing`
});
}
});
// If we reach here, it means the billing.require didn't throw and didn't return true
return new Response("Unexpected billing result", { status: 500 });
} catch (error) {
if (error instanceof Response && error.status === 302) {
// This is the redirect to the payment URL
return error;
}
console.error("Error upgrading plan:", error);
return new Response("Error processing upgrade request", { status: 500 });
}
} catch (authError) {
console.error("Authentication failed:", authError);
return new Response("Authentication failed", { status: 401 });
}
};
This is my code why I am getting this error , When i try to change the plan
this is the error :
Error upgrading plan: Response {
15:41:43 │ remix │ [Symbol(realm)]: { settingsObject: {} },
15:41:43 │ remix │ [Symbol(state)]: {
15:41:43 │ remix │ aborted: false,
15:41:43 │ remix │ rangeRequested: false,
15:41:43 │ remix │ timingAllowPassed: false,
15:41:43 │ remix │ requestIncludesCredentials: false,
15:41:43 │ remix │ type: 'default',
15:41:43 │ remix │ status: 401,
15:41:43 │ remix │ timingInfo: null,
15:41:43 │ remix │ cacheState: '',
15:41:43 │ remix │ statusText: 'Unauthorized',
15:41:43 │ remix │ headersList: HeadersList {
15:41:43 │ remix │ cookies: null,
15:41:43 │ remix │ [Symbol(headers map)]: [Map],
15:41:43 │ remix │ [Symbol(headers map sorted)]: null
15:41:43 │ remix │ },
15:41:43 │ remix │ urlList: []
15:41:43 │ remix │ },
15:41:43 │ remix │ [Symbol(headers)]: HeadersList {
15:41:43 │ remix │ cookies: null,
15:41:43 │ remix │ [Symbol(headers map)]: Map(1) {
15:41:43 │ remix │ 'x-shopify-api-request-failure-reauthorize-url' => [Object]
15:41:43 │ remix │ },
15:41:43 │ remix │ [Symbol(headers map sorted)]: null
15:41:43 │ remix │ }
15:41:43 │ remix │ }
the thing is that ,
when i refresh the page it took me to the approval page
The error may be due to these reasons:
Invalid selectedPlan: Ensure the plan query parameter in the URL is valid (e.g., "basic", "business", or "pro"). If it's invalid, the code returns a 400 error.
Log values of selectedPlan and check the results from billing.require to pinpoint the issue.
Learn how to build powerful custom workflows in Shopify Flow with expert guidance from ...
By Jacqui May 7, 2025Did You Know? May is named after Maia, the Roman goddess of growth and flourishing! ...
By JasonH May 2, 2025Discover opportunities to improve SEO with new guidance available from Shopify’s growth...
By Jacqui May 1, 2025