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