Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions api/verify-crypto-payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
createEntitlementSession,
entitlementSessionCookie,
getEntitlementStore,
upsertCryptoEntitlement,
claimCryptoEntitlement,
type AccessTier,
} from "../netlify/functions/_lib/entitlements";

Expand Down Expand Up @@ -166,7 +166,7 @@ export default async function handler(req: RequestLike, res: ResponseLike) {
res.status(503).json({ verified: false, reason: "Entitlement backend is not configured." });
return;
}
const entitlement = await upsertCryptoEntitlement(store, {
const entitlement = await claimCryptoEntitlement(store, {
email,
walletAddress,
txHash,
Expand Down Expand Up @@ -196,7 +196,7 @@ export default async function handler(req: RequestLike, res: ResponseLike) {
res.status(503).json({ verified: false, reason: "Entitlement backend is not configured." });
return;
}
const entitlement = await upsertCryptoEntitlement(store, {
const entitlement = await claimCryptoEntitlement(store, {
email,
walletAddress,
txHash,
Expand All @@ -215,6 +215,10 @@ export default async function handler(req: RequestLike, res: ResponseLike) {
});
} catch (error) {
const message = error instanceof Error ? error.message : "Verification failed.";
if (message === "CRYPTO_TRANSACTION_ALREADY_CLAIMED") {
res.status(409).json({ verified: false, reason: "This transaction hash has already been claimed." });
return;
}
res.status(502).json({ verified: false, reason: message });
}
}
30 changes: 25 additions & 5 deletions netlify/functions/_lib/entitlements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ export async function upsertStripeSubscriptionEntitlement(
});
}

export async function upsertCryptoEntitlement(
export async function claimCryptoEntitlement(
store: EntitlementStore,
input: {
email: string;
Expand All @@ -539,16 +539,36 @@ export async function upsertCryptoEntitlement(
},
) {
const tier = input.tier === "premium" ? "premium" : "event";
const txHash = normalizeHash(input.txHash);
const email = normalizeEmail(input.email);
const walletAddress = normalizeWallet(input.walletAddress);
const id = `crypto:${txHash}`;
const existing = await getRecord(store, id);

if (existing) {
const sameClaimant =
existing.email === email &&
existing.walletAddress === walletAddress &&
existing.tier === tier &&
existing.cryptoTxHash === txHash;

if (!sameClaimant) {
throw new Error("CRYPTO_TRANSACTION_ALREADY_CLAIMED");
}

return existing;
}

return upsertEntitlement(store, {
id: `crypto:${normalizeHash(input.txHash)}`,
id,
tier,
source: "crypto",
label: input.label,
status: "active",
activatedAt: new Date().toISOString(),
expiresAt: tier === "event" ? eventAccessExpiry() : undefined,
email: input.email,
walletAddress: input.walletAddress,
cryptoTxHash: input.txHash,
email,
walletAddress,
cryptoTxHash: txHash,
});
}
Loading