datewise/app/routes/app.tsx
metatroncubeswdev 54734e8938 feat(billing): switch to Shopify managed pricing; show chosen time on thank-you page
App Store review 1.2.2 ("fails to register the plan"): follow Offerlyst's approved
managed-pricing setup. The plan is read from the Partner API on every app load,
shops without a plan (new install or reinstall) go to Shopify's plan selection
page, and app_subscriptions/update keeps it in sync. Billing API code removed.

Reviewer's checkout video: Shopify's checkout shows pickup by location name, so
the delivery label rename never reached the shopper. New checkout-time extension
shows the chosen date and time on the thank-you page (all plans) and next to
pickup/shipping options (Plus). Checkout display copy now matches.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 16:52:00 -05:00

70 lines
3.2 KiB
TypeScript

import type { HeadersFunction, LoaderFunctionArgs } from "react-router";
import { Outlet, useLoaderData, useRouteError } from "react-router";
import { boundary } from "@shopify/shopify-app-react-router/server";
import { AppProvider } from "@shopify/shopify-app-react-router/react";
import prisma from "../db.server";
import { authenticate } from "../shopify.server";
import { fetchActiveSubscription, partnerApiConfigured, pricingUrl } from "../lib/partner.server";
import { bumpConfig } from "../services/config.server";
import { planFromSubscription } from "../services/plans";
export const loader = async ({ request }: LoaderFunctionArgs) => {
const { admin, redirect, session } = await authenticate.admin(request);
// Shopify App Pricing: a shop without a plan (new install), or whose subscription was cancelled (uninstall,
// then reinstall), goes to Shopify's plan selection page to approve a plan again. Otherwise the stored plan
// follows the subscription. A Partner API failure never redirects a paying merchant.
if (partnerApiConfigured()) {
// the welcome link adds plan_handle after a plan is approved: skip the cache so the new plan shows at once
const fresh = new URL(request.url).searchParams.has("plan_handle");
let sub: Awaited<ReturnType<typeof fetchActiveSubscription>> | undefined;
try {
const r = await admin.graphql(`#graphql
query DatewiseShopId { shop { id } }`);
const { data } = (await r.json()) as { data: { shop: { id: string } } };
sub = await fetchActiveSubscription(data.shop.id, { fresh });
} catch (e) {
console.error("[datewise] subscription check failed", e);
}
if (sub === null || sub?.cancelled) throw redirect(pricingUrl(session.shop), { target: "_top" });
if (sub) {
const plan = planFromSubscription(sub.handles.join(" "));
const row = await prisma.shop.findUnique({ where: { domain: session.shop }, select: { id: true, plan: true } });
if (row && row.plan !== plan) {
await prisma.shop.update({ where: { id: row.id }, data: { plan } });
await bumpConfig(row.id, session.shop);
}
}
}
return { apiKey: process.env.SHOPIFY_API_KEY || "" };
};
export default function App() {
const { apiKey } = useLoaderData<typeof loader>();
return (
<AppProvider apiKey={apiKey}>
<s-app-nav>
<s-link href="/app">Today</s-link>
<s-link href="/app/bookings">Bookings</s-link>
<s-link href="/app/locations">Locations & hours</s-link>
<s-link href="/app/rules">Product rules</s-link>
<s-link href="/app/freshness">Fresh stock</s-link>
<s-link href="/app/peak">Rush control</s-link>
<s-link href="/app/runsheet">Driver run sheet</s-link>
<s-link href="/app/diagnostics">Why unavailable?</s-link>
<s-link href="/app/settings">Settings</s-link>
<s-link href="/app/plans">Plans</s-link>
</s-app-nav>
<Outlet />
</AppProvider>
);
}
// Shopify needs React Router to catch some thrown responses so headers are included.
export function ErrorBoundary() {
return boundary.error(useRouteError());
}
export const headers: HeadersFunction = (headersArgs) => boundary.headers(headersArgs);