feat:mollie and footer
This commit is contained in:
@@ -50,7 +50,7 @@ function parseGuestsJson(raw: string | null): Array<{
|
||||
* Credits a watcher's drinkCardValue to their drinkkaart account.
|
||||
*
|
||||
* Safe to call multiple times — the `drinkkaartCreditedAt IS NULL` guard and the
|
||||
* `lemonsqueezy_order_id` UNIQUE constraint together prevent double-crediting even
|
||||
* `mollie_payment_id` UNIQUE constraint together prevent double-crediting even
|
||||
* if called concurrently (webhook + signup racing each other).
|
||||
*
|
||||
* Returns a status string describing the outcome.
|
||||
@@ -160,11 +160,11 @@ export async function creditRegistrationToAccount(
|
||||
return { credited: false, amountCents: 0, status: "already_credited" };
|
||||
}
|
||||
|
||||
// Record the topup. Re-use the registration's lemonsqueezyOrderId so the
|
||||
// Record the topup. Re-use the registration's molliePaymentId so the
|
||||
// topup row is clearly linked to the original payment. We prefix with
|
||||
// "reg_" to distinguish from direct drinkkaart top-up orders if needed.
|
||||
const topupOrderId = reg.lemonsqueezyOrderId
|
||||
? `reg_${reg.lemonsqueezyOrderId}`
|
||||
const topupPaymentId = reg.molliePaymentId
|
||||
? `reg_${reg.molliePaymentId}`
|
||||
: null;
|
||||
|
||||
await db.insert(drinkkaartTopup).values({
|
||||
@@ -175,8 +175,7 @@ export async function creditRegistrationToAccount(
|
||||
balanceBefore,
|
||||
balanceAfter,
|
||||
type: "payment",
|
||||
lemonsqueezyOrderId: topupOrderId,
|
||||
lemonsqueezyCustomerId: reg.lemonsqueezyCustomerId ?? null,
|
||||
molliePaymentId: topupPaymentId,
|
||||
adminId: null,
|
||||
reason: "Drinkkaart bij registratie",
|
||||
paidAt: reg.paidAt ?? new Date(),
|
||||
@@ -742,12 +741,8 @@ export const appRouter = {
|
||||
}),
|
||||
)
|
||||
.handler(async ({ input }) => {
|
||||
if (
|
||||
!env.LEMON_SQUEEZY_API_KEY ||
|
||||
!env.LEMON_SQUEEZY_STORE_ID ||
|
||||
!env.LEMON_SQUEEZY_VARIANT_ID
|
||||
) {
|
||||
throw new Error("Lemon Squeezy is niet geconfigureerd");
|
||||
if (!env.MOLLIE_API_KEY) {
|
||||
throw new Error("Mollie is niet geconfigureerd");
|
||||
}
|
||||
|
||||
const rows = await db
|
||||
@@ -797,60 +792,40 @@ export const appRouter = {
|
||||
? `Vrijwillige gift${drinkCardTotal > 0 ? " + drinkkaart" : ""}`
|
||||
: `Toegangskaart${giftTotal > 0 ? " + gift" : ""}`;
|
||||
|
||||
const response = await fetch(
|
||||
"https://api.lemonsqueezy.com/v1/checkouts",
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Accept: "application/vnd.api+json",
|
||||
"Content-Type": "application/vnd.api+json",
|
||||
Authorization: `Bearer ${env.LEMON_SQUEEZY_API_KEY}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
data: {
|
||||
type: "checkouts",
|
||||
attributes: {
|
||||
custom_price: amountInCents,
|
||||
product_options: {
|
||||
name: "Kunstenkamp Evenement",
|
||||
description: productDescription,
|
||||
redirect_url:
|
||||
input.redirectUrl ??
|
||||
`${env.CORS_ORIGIN}/manage/${input.token}`,
|
||||
},
|
||||
checkout_data: {
|
||||
email: row.email,
|
||||
name: `${row.firstName} ${row.lastName}`,
|
||||
custom: { registration_token: input.token },
|
||||
},
|
||||
checkout_options: {
|
||||
embed: false,
|
||||
locale: "nl",
|
||||
},
|
||||
},
|
||||
relationships: {
|
||||
store: {
|
||||
data: { type: "stores", id: env.LEMON_SQUEEZY_STORE_ID },
|
||||
},
|
||||
variant: {
|
||||
data: { type: "variants", id: env.LEMON_SQUEEZY_VARIANT_ID },
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
const redirectUrl =
|
||||
input.redirectUrl ?? `${env.CORS_ORIGIN}/manage/${input.token}`;
|
||||
|
||||
// Mollie amounts must be formatted as "10.00" (string, 2 decimal places)
|
||||
const amountValue = (amountInCents / 100).toFixed(2);
|
||||
|
||||
const webhookUrl = `${env.CORS_ORIGIN}/api/webhook/mollie`;
|
||||
|
||||
const response = await fetch("https://api.mollie.com/v2/payments", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${env.MOLLIE_API_KEY}`,
|
||||
},
|
||||
);
|
||||
body: JSON.stringify({
|
||||
amount: { value: amountValue, currency: "EUR" },
|
||||
description: `Kunstenkamp Evenement — ${productDescription}`,
|
||||
redirectUrl,
|
||||
webhookUrl,
|
||||
locale: "nl_NL",
|
||||
metadata: { registration_token: input.token },
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json();
|
||||
console.error("Lemon Squeezy checkout error:", errorData);
|
||||
console.error("Mollie checkout error:", errorData);
|
||||
throw new Error("Kon checkout niet aanmaken");
|
||||
}
|
||||
|
||||
const checkoutData = (await response.json()) as {
|
||||
data?: { attributes?: { url?: string } };
|
||||
_links?: { checkout?: { href?: string } };
|
||||
};
|
||||
const checkoutUrl = checkoutData.data?.attributes?.url;
|
||||
const checkoutUrl = checkoutData._links?.checkout?.href;
|
||||
|
||||
if (!checkoutUrl) throw new Error("Geen checkout URL ontvangen");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user