-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.tsx
73 lines (64 loc) · 2.5 KB
/
page.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { baseUSDC } from "@daimo/contract";
import { redirect } from "next/navigation";
import { getAddress, parseUnits } from "viem";
import items from "./items.json";
export default async function GenerateAndRedirect(
props: {
searchParams: Promise<{ [key: string]: string }>;
}
) {
const searchParams = await props.searchParams;
// Fixed params for all payers
const networkSchoolAddress = getAddress("0xCdDc0Ed3dC148a9E5D0a92a3d2015FFDB26b2d53"); // thenetworkschool.eth
const recipientToken = baseUSDC;
// Variable params per payer
const apiKey = searchParams["apiKey"];
const usd = searchParams["usd"];
const selectedItemsStr = searchParams["selectedItems"];
const mealPlanStr = searchParams["mealPlan"];
const isTest = searchParams["isTest"];
const submissionId = searchParams["submissionId"];
const usdcAmount = parseUnits(usd, recipientToken.decimals) / (isTest ? BigInt(100) : BigInt(1));
const mealPlan = decodeURIComponent(mealPlanStr);
const daimoPayDisplayItems: typeof items[number][] = [];
if (selectedItemsStr && selectedItemsStr.length > 0) {
const selectedItems = decodeURIComponent(selectedItemsStr).split(",").map(item => items.find(i => item.toLowerCase().trim().startsWith(i.name.toLowerCase())));
if (selectedItems) {
daimoPayDisplayItems.push(...selectedItems.filter(item => item !== undefined));
}
}
if (mealPlan && mealPlan.length > 0) {
const mealPlanItem = items.find(i => mealPlan.toLowerCase().startsWith(i.name.split(" ")[0].toLowerCase()));
if (mealPlanItem) {
daimoPayDisplayItems.push(mealPlanItem);
}
}
if (!apiKey || !usd || !daimoPayDisplayItems || !submissionId) {
throw new Error("apiKey, usd, submissionId and selectedItems or mealPlan are required");
}
console.log(`daimoPayDisplayItems: ${JSON.stringify(daimoPayDisplayItems)}`);
const response = await fetch('https://pay.daimo.com/api/generate', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Idempotency-Key': submissionId,
'Api-Key': apiKey,
},
body: JSON.stringify({
style: {
background: "",
},
orgLogo: "https://daimo-pay-redirect.vercel.app/ns.svg",
intent: `Pay Network School Food`,
items: daimoPayDisplayItems,
recipient: {
address: networkSchoolAddress,
amount: usdcAmount.toString(),
token: recipientToken.token,
chain: recipientToken.chainId,
},
}),
});
const data = await response.json();
redirect(data.url);
}