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.
As 2024 wraps up, the dropshipping landscape is already shifting towards 2025's trends....
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024In today’s interview, we sat down with @BSS-Commerce to discuss practical strategies f...
By JasonH Nov 13, 2024