Skip to content

Instantly share code, notes, and snippets.

@daguitosama
Created July 27, 2023 18:39
Show Gist options
  • Save daguitosama/1c47bdd2f2115a00f31e7be994b87d8e to your computer and use it in GitHub Desktop.
Save daguitosama/1c47bdd2f2115a00f31e7be994b87d8e to your computer and use it in GitHub Desktop.
Wix Headless Checkout handler (Remix Route)
import { defer, json, redirect } from "@remix-run/cloudflare";
import type { ActionArgs, LinksFunction, LoaderArgs, V2_MetaFunction } from "@remix-run/cloudflare";
import { thirty_days_from_now } from "~/lib/lib.server";
export async function loader({ params, request, context }: LoaderArgs) {
const { session_storage, wix_cart_api } = context;
const session = await session_storage.getSession(request.headers.get("Cookie"));
const stored_cat_id = session.get("cart_id") as string;
console.log(`\r\t -> cart_id: `, stored_cat_id);
if (typeof stored_cat_id != "string") {
return defer({
url: null,
domain: typeof context.DOMAIN == "string" ? context.DOMAIN : "localhost"
});
}
const cart = await wix_cart_api.get(stored_cat_id);
const create_checkout_options = {
channelType: "WEB",
lineItems: cart?.lineItems
};
// TODO: Fix this create_checkout_options type
const { checkout_url, checkout_id } = await wix_cart_api.get_checkout_url_and_id(
//@ts-ignore
create_checkout_options
);
// save checkout ref
session.set("checkout_id", checkout_id);
return redirect(checkout_url, {
headers: {
"Set-Cookie": await session_storage.commitSession(session, {
expires: thirty_days_from_now()
})
}
});
}
type ActionData = {
delResult: string;
};
export async function action({ request, context }: ActionArgs) {
const formData = await request.formData();
const session = await context.session_storage.getSession(request.headers.get("Cookie"));
const stored_cat_id = session.get("cart_id") as string;
if (!stored_cat_id) {
return json({
delResult: "stored_cat_id not found"
});
}
const intent = await formData.get("intent");
const headers = new Headers();
if (intent == "DELETE_CART") {
console.log("(checkout.action) DELETE_CART hit");
// just clear the cookie my friend
headers.set("Set-Cookie", await context.session_storage.destroySession(session));
}
return json<ActionData>(
{
delResult: "ok"
},
{
headers
}
);
}
export const links: LinksFunction = () => [];
export const meta: V2_MetaFunction<typeof loader> = ({ data }) => {
if (!data || !("domain" in data)) {
throw new Error("No data or data.domain");
}
return [
{ title: "Checkout | DAMANCI" },
{
property: "og:type",
content: "website"
},
{
property: "og:url",
content: `https://${data.domain}/checkout`
},
{
property: "og:title",
content: "Checkout | DAMANCI"
},
// {
// property: "og:description",
// content:
// "DAMANCI offers Great quality products for haircare, crafted with 100% organic ingredients.",
// },
{
property: "og:image",
content: `https://${data.domain}/img/global/social.jpg`
}
];
};
export default function CheckOutRoute() {
return (
<div className="w-full h-full min-h-screen">
<div className="mt-[30px]">Checkout</div>
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment