feat:mollie and footer
This commit is contained in:
@@ -14,7 +14,6 @@
|
||||
"@kk/auth": "workspace:*",
|
||||
"@kk/db": "workspace:*",
|
||||
"@kk/env": "workspace:*",
|
||||
"@lemonsqueezy/lemonsqueezy.js": "^4.00.0",
|
||||
"@orpc/client": "catalog:",
|
||||
"@orpc/openapi": "catalog:",
|
||||
"@orpc/server": "catalog:",
|
||||
|
||||
@@ -134,82 +134,49 @@ export const drinkkaartRouter = {
|
||||
.handler(async ({ input, context }) => {
|
||||
const { env: serverEnv, session } = context;
|
||||
|
||||
if (
|
||||
!serverEnv.LEMON_SQUEEZY_API_KEY ||
|
||||
!serverEnv.LEMON_SQUEEZY_STORE_ID ||
|
||||
!serverEnv.LEMON_SQUEEZY_DRINKKAART_VARIANT_ID
|
||||
) {
|
||||
if (!serverEnv.MOLLIE_API_KEY) {
|
||||
throw new ORPCError("INTERNAL_SERVER_ERROR", {
|
||||
message: "Lemon Squeezy Drinkkaart variant is niet geconfigureerd",
|
||||
message: "Mollie is niet geconfigureerd",
|
||||
});
|
||||
}
|
||||
|
||||
const card = await getOrCreateDrinkkaart(session.user.id);
|
||||
|
||||
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 ${serverEnv.LEMON_SQUEEZY_API_KEY}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
data: {
|
||||
type: "checkouts",
|
||||
attributes: {
|
||||
custom_price: input.amountCents,
|
||||
product_options: {
|
||||
name: "Drinkkaart Opladen",
|
||||
description: `Drinkkaart top-up — ${formatCents(input.amountCents)}`,
|
||||
redirect_url: `${serverEnv.BETTER_AUTH_URL}/account?topup=success`,
|
||||
},
|
||||
checkout_data: {
|
||||
email: session.user.email,
|
||||
name: session.user.name,
|
||||
custom: {
|
||||
type: "drinkkaart_topup",
|
||||
drinkkaartId: card.id,
|
||||
userId: session.user.id,
|
||||
},
|
||||
},
|
||||
checkout_options: {
|
||||
embed: false,
|
||||
locale: "nl",
|
||||
},
|
||||
},
|
||||
relationships: {
|
||||
store: {
|
||||
data: {
|
||||
type: "stores",
|
||||
id: serverEnv.LEMON_SQUEEZY_STORE_ID,
|
||||
},
|
||||
},
|
||||
variant: {
|
||||
data: {
|
||||
type: "variants",
|
||||
id: serverEnv.LEMON_SQUEEZY_DRINKKAART_VARIANT_ID,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
// Mollie amounts must be formatted as "10.00" (string, 2 decimal places)
|
||||
const amountValue = (input.amountCents / 100).toFixed(2);
|
||||
|
||||
const response = await fetch("https://api.mollie.com/v2/payments", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${serverEnv.MOLLIE_API_KEY}`,
|
||||
},
|
||||
);
|
||||
body: JSON.stringify({
|
||||
amount: { value: amountValue, currency: "EUR" },
|
||||
description: `Drinkkaart Opladen — ${formatCents(input.amountCents)}`,
|
||||
redirectUrl: `${serverEnv.BETTER_AUTH_URL}/account?topup=success`,
|
||||
webhookUrl: `${serverEnv.BETTER_AUTH_URL}/api/webhook/mollie`,
|
||||
locale: "nl_NL",
|
||||
metadata: {
|
||||
type: "drinkkaart_topup",
|
||||
drinkkaartId: card.id,
|
||||
userId: session.user.id,
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json();
|
||||
console.error("Lemon Squeezy Drinkkaart checkout error:", errorData);
|
||||
console.error("Mollie Drinkkaart checkout error:", errorData);
|
||||
throw new ORPCError("INTERNAL_SERVER_ERROR", {
|
||||
message: "Kon checkout niet aanmaken",
|
||||
});
|
||||
}
|
||||
|
||||
const data = (await response.json()) as {
|
||||
data?: { attributes?: { url?: string } };
|
||||
_links?: { checkout?: { href?: string } };
|
||||
};
|
||||
const checkoutUrl = data.data?.attributes?.url;
|
||||
const checkoutUrl = data._links?.checkout?.href;
|
||||
if (!checkoutUrl) {
|
||||
throw new ORPCError("INTERNAL_SERVER_ERROR", {
|
||||
message: "Geen checkout URL ontvangen",
|
||||
|
||||
@@ -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");
|
||||
|
||||
|
||||
17
packages/db/src/migrations/0006_mollie_migration.sql
Normal file
17
packages/db/src/migrations/0006_mollie_migration.sql
Normal file
@@ -0,0 +1,17 @@
|
||||
-- Migrate from Lemonsqueezy to Mollie
|
||||
-- Renames payment provider columns in registration and drinkkaart_topup tables.
|
||||
|
||||
-- registration table:
|
||||
-- lemonsqueezy_order_id -> mollie_payment_id
|
||||
-- lemonsqueezy_customer_id -> dropped (Mollie does not return a persistent
|
||||
-- customer ID for one-off payments)
|
||||
|
||||
ALTER TABLE registration RENAME COLUMN lemonsqueezy_order_id TO mollie_payment_id;
|
||||
ALTER TABLE registration DROP COLUMN lemonsqueezy_customer_id;
|
||||
|
||||
-- drinkkaart_topup table:
|
||||
-- lemonsqueezy_order_id -> mollie_payment_id
|
||||
-- lemonsqueezy_customer_id -> dropped
|
||||
|
||||
ALTER TABLE drinkkaart_topup RENAME COLUMN lemonsqueezy_order_id TO mollie_payment_id;
|
||||
ALTER TABLE drinkkaart_topup DROP COLUMN lemonsqueezy_customer_id;
|
||||
@@ -46,8 +46,7 @@ export const drinkkaartTopup = sqliteTable("drinkkaart_topup", {
|
||||
balanceBefore: integer("balance_before").notNull(),
|
||||
balanceAfter: integer("balance_after").notNull(),
|
||||
type: text("type", { enum: ["payment", "admin_credit"] }).notNull(),
|
||||
lemonsqueezyOrderId: text("lemonsqueezy_order_id").unique(), // nullable; only for type="payment"
|
||||
lemonsqueezyCustomerId: text("lemonsqueezy_customer_id"),
|
||||
molliePaymentId: text("mollie_payment_id").unique(), // nullable; only for type="payment"
|
||||
adminId: text("admin_id"), // nullable; only for type="admin_credit"
|
||||
reason: text("reason"),
|
||||
paidAt: integer("paid_at", { mode: "timestamp_ms" }).notNull(),
|
||||
|
||||
@@ -29,8 +29,7 @@ export const registration = sqliteTable(
|
||||
paymentStatus: text("payment_status").notNull().default("pending"),
|
||||
paymentAmount: integer("payment_amount").default(0),
|
||||
giftAmount: integer("gift_amount").default(0),
|
||||
lemonsqueezyOrderId: text("lemonsqueezy_order_id"),
|
||||
lemonsqueezyCustomerId: text("lemonsqueezy_customer_id"),
|
||||
molliePaymentId: text("mollie_payment_id"),
|
||||
paidAt: integer("paid_at", { mode: "timestamp_ms" }),
|
||||
// Set when the drinkCardValue has been credited to the user's drinkkaart.
|
||||
// Null means not yet credited (either unpaid, account doesn't exist yet, or
|
||||
@@ -50,6 +49,6 @@ export const registration = sqliteTable(
|
||||
index("registration_managementToken_idx").on(table.managementToken),
|
||||
index("registration_paymentStatus_idx").on(table.paymentStatus),
|
||||
index("registration_giftAmount_idx").on(table.giftAmount),
|
||||
index("registration_lemonsqueezyOrderId_idx").on(table.lemonsqueezyOrderId),
|
||||
index("registration_molliePaymentId_idx").on(table.molliePaymentId),
|
||||
],
|
||||
);
|
||||
|
||||
6
packages/env/src/server.ts
vendored
6
packages/env/src/server.ts
vendored
@@ -25,11 +25,7 @@ export const env = createEnv({
|
||||
NODE_ENV: z
|
||||
.enum(["development", "production", "test"])
|
||||
.default("development"),
|
||||
LEMON_SQUEEZY_API_KEY: z.string().min(1).optional(),
|
||||
LEMON_SQUEEZY_STORE_ID: z.string().min(1).optional(),
|
||||
LEMON_SQUEEZY_VARIANT_ID: z.string().min(1).optional(),
|
||||
LEMON_SQUEEZY_DRINKKAART_VARIANT_ID: z.string().min(1).optional(),
|
||||
LEMON_SQUEEZY_WEBHOOK_SECRET: z.string().min(1).optional(),
|
||||
MOLLIE_API_KEY: z.string().min(1).optional(),
|
||||
},
|
||||
runtimeEnv: process.env,
|
||||
emptyStringAsUndefined: true,
|
||||
|
||||
@@ -30,14 +30,8 @@ export const web = await TanStackStart("web", {
|
||||
SMTP_USER: getEnvVar("SMTP_USER"),
|
||||
SMTP_PASS: getEnvVar("SMTP_PASS"),
|
||||
SMTP_FROM: getEnvVar("SMTP_FROM"),
|
||||
// Payments (Lemon Squeezy)
|
||||
LEMON_SQUEEZY_API_KEY: getEnvVar("LEMON_SQUEEZY_API_KEY"),
|
||||
LEMON_SQUEEZY_STORE_ID: getEnvVar("LEMON_SQUEEZY_STORE_ID"),
|
||||
LEMON_SQUEEZY_VARIANT_ID: getEnvVar("LEMON_SQUEEZY_VARIANT_ID"),
|
||||
LEMON_SQUEEZY_DRINKKAART_VARIANT_ID: getEnvVar(
|
||||
"LEMON_SQUEEZY_DRINKKAART_VARIANT_ID",
|
||||
),
|
||||
LEMON_SQUEEZY_WEBHOOK_SECRET: getEnvVar("LEMON_SQUEEZY_WEBHOOK_SECRET"),
|
||||
// Payments (Mollie)
|
||||
MOLLIE_API_KEY: getEnvVar("MOLLIE_API_KEY"),
|
||||
},
|
||||
domains: ["kunstenkamp.be", "www.kunstenkamp.be"],
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user