Skip to content

Commit

Permalink
redirecting back to cast after checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
julien51 committed Jan 31, 2024
1 parent 7c00213 commit e070a3e
Showing 1 changed file with 36 additions and 8 deletions.
44 changes: 36 additions & 8 deletions src/app/api/[message]/checkout/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,35 @@ export async function POST(
if (!fcMessage.valid) {
return new Response("Invalid message", { status: 442 });
}

// Get the message URL so we can then redirect to it!
const posterProfile = await getUserProfile(
fcMessage.message.data.frameActionBody?.castId?.fid
);
console.log(JSON.stringify(posterProfile, null, 2));
const checkoutRedirect = new URL(request.url);
checkoutRedirect.searchParams.append("cast", "redirect");

try {
// Get the message URL so we can then redirect to it!
const posterProfile = await getUserProfile(
fcMessage.message.data.frameActionBody?.castId?.fid
);
const userName = posterProfile.messages
.sort((a: any, b: any) => a.data.timestamp > b.data.timestamp)
.find((m: any) => {
return (
m.data.type === "MESSAGE_TYPE_USER_DATA_ADD" &&
m.data.userDataBody.type === "USER_DATA_TYPE_USERNAME"
);
}).data.userDataBody.value;
checkoutRedirect.searchParams.append(
"cast",
`https://warpcast.com/${userName}/${fcMessage.message.data.frameActionBody?.castId.hash}`
);
} catch (error) {
console.error(
`Could not build the redirect URL for ${JSON.stringify(
fcMessage.message.data,
null,
2
)}`
);
console.error(error);
}
return Response.redirect(checkoutRedirect.toString(), 302);
}

Expand All @@ -32,6 +52,14 @@ export async function GET(
{ params }: { params: { message: string } }
) {
const message = await getMessage(params.message);
const checkoutUrl = new URL(message.checkoutUrl);

const u = new URL(request.url);
const cast = u.searchParams.get("cast");
if (cast) {
// We have a cast URL to redirect to!
checkoutUrl.searchParams.append("redirect-url", cast!);
}

return Response.redirect(message.checkoutUrl, 302);
return Response.redirect(checkoutUrl.toString(), 302);
}

0 comments on commit e070a3e

Please sign in to comment.