Skip to content
Merged
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
1 change: 1 addition & 0 deletions e2e/wizard-tap-targets.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ for (const width of [320, 360, 375, 390, 430]) {

// Features step: every switch.
await page.getByRole('button', { name: /Next: Features/ }).click();
await expect(page.getByRole('heading', { name: 'Ask guests questions' })).toBeVisible();
const switches = page.getByRole('switch');
expect(await switches.count()).toBeGreaterThanOrEqual(4);
for (const s of await switches.all()) await expect.poll(async () => Math.min(...((await box(s)) ?? [0]))).toBeGreaterThanOrEqual(44);
Expand Down
17 changes: 7 additions & 10 deletions lib/handlers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,16 +435,13 @@ export async function previewReviewerAuth({ req, res, query, ip }) {
if (query && query.get('next')) next = safeNext(query.get('next'));

// Ensure or fetch the synthetic reviewer user (guaranteed guest role, no admin/organiser escalation)
let user = await db.one('SELECT * FROM users WHERE telegram_user_id = $1', [REVIEWER_TG_ID]);
if (!user) {
user = (await db.query(
`INSERT INTO users (telegram_user_id, alias, display_name, age_confirmed_21, age_confirmed_at, status, bot_started, onboarding_completed, onboarding_step)
VALUES ($1, $2, $2, true, now(), 'active', true, true, 6)
ON CONFLICT (telegram_user_id) DO UPDATE SET age_confirmed_21 = true, status = 'active', onboarding_completed = true
RETURNING *`,
[REVIEWER_TG_ID, REVIEWER_ALIAS]
)).rows[0];
}
const user = (await db.query(
`INSERT INTO users (telegram_user_id, alias, display_name, age_confirmed_21, age_confirmed_at, status, bot_started, onboarding_completed, onboarding_step)
VALUES ($1, $2, $2, true, now(), 'active', true, true, 6)
ON CONFLICT (telegram_user_id) DO UPDATE SET age_confirmed_21 = true, status = 'active', onboarding_completed = true
RETURNING *`,
[REVIEWER_TG_ID, REVIEWER_ALIAS]
)).rows[0];
if (user.status !== 'active') throw new HttpError(403, 'account_restricted', 'Reviewer account is not active');

const { csrf } = await startSession(req, res, user);
Expand Down
7 changes: 6 additions & 1 deletion lib/handlers/checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ export async function checkoutRedirect({ req, res, params, query, ip }) {
if (req.method === 'HEAD') return redirect(res, `/events/${slug}`);
await rateLimit(`co:${hashIp(ip)}`, 30, 600);

const ev = await db.one('SELECT checkout_mode FROM events WHERE slug = $1', [slug]);
const ev = await db.one('SELECT checkout_mode, payment_method, organiser_upi_id, organiser_payee_name FROM events WHERE slug = $1', [slug]);
const isDirectUpi = ev?.payment_method === 'direct_upi' || (Boolean(ev?.organiser_upi_id) && Boolean(ev?.organiser_payee_name));
if (isDirectUpi) {
const qStr = query.toString() ? `?${query.toString()}` : '';
return redirect(res, `/checkout/${slug}/upi${qStr}`);
}
if (ev?.checkout_mode === 'upi_direct') {
if (!upiCheckoutEnabled()) return redirect(res, `/events/${slug}`);
const qStr = query.toString() ? `?${query.toString()}` : '';
Expand Down
30 changes: 30 additions & 0 deletions lib/handlers/organiser.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ import {
saveOrganiserPaymentSettings,
deleteOrganiserPaymentSettings
} from '../services/payment-settings.js';
import {
listDirectUpiBookings,
confirmDirectUpiPayment,
rejectDirectUpiPayment
} from '../services/direct-upi.js';

const id = (v) => {
if (!uuid.safeParse(v).success) throw new HttpError(404, 'not_found', 'Not found');
Expand Down Expand Up @@ -586,3 +591,28 @@ export async function deletePaymentSettingsHandler(ctx) {
sendJson(ctx.res, 200, { ok: true, message: 'Payment details removed.', ...res });
}

export async function listDirectUpiPayments(ctx) {
const user = await organiser(ctx);
const eventId = id(ctx.params.id);
const r = await listDirectUpiBookings(user, eventId);
sendJson(ctx.res, 200, r);
}

export async function confirmPayment(ctx) {
const user = await organiser(ctx, { write: true });
const eventId = id(ctx.params.id);
const bookingId = id(ctx.params.bookingId);
const body = await readJson(ctx.req);
const r = await confirmDirectUpiPayment(user, eventId, bookingId, body);
sendJson(ctx.res, 200, r);
}

export async function rejectPayment(ctx) {
const user = await organiser(ctx, { write: true });
const eventId = id(ctx.params.id);
const bookingId = id(ctx.params.bookingId);
const body = await readJson(ctx.req);
const r = await rejectDirectUpiPayment(user, eventId, bookingId, body);
sendJson(ctx.res, 200, r);
}

77 changes: 63 additions & 14 deletions lib/handlers/upi-checkout.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// lib/handlers/upi-checkout.js - HTTP routes for UPI-first checkout, status polling, and PhonePe webhooks.
import { sendJson, sendHtml, redirect, readRawBody, readJson, hashIp, HttpError } from '../http.js';
import { getSessionUser } from '../session.js';
import { getSessionUser, csrfTokenFor } from '../session.js';
import { rateLimit } from '../ratelimit.js';
import { db } from '../db.js';
import {
Expand All @@ -10,6 +10,11 @@ import {
processUpiWebhook,
assertUpiCheckoutEnabled
} from '../services/upi-payments.js';
import {
createDirectUpiBooking,
reportDirectUpiPayment,
getDirectUpiBookingStatus
} from '../services/direct-upi.js';
import { renderUpiCheckout } from '../views/checkout-upi.js';
import { WebhookSignatureError } from '../upi/adapter.js';
import { logger } from '../logger.js';
Expand All @@ -22,7 +27,6 @@ function isMobileUserAgent(ua = '') {
* GET /checkout/:slug/upi - Renders the UPI-First interactive checkout view.
*/
export async function upiCheckoutPage({ req, res, params, query, ip }) {
assertUpiCheckoutEnabled(); // before rate limits and body reads: the route does not exist when off
const slug = params.slug;
if (!/^[a-z0-9]+(-[a-z0-9]+)*$/.test(slug)) throw new HttpError(404, 'not_found', 'Event not found');

Expand All @@ -38,12 +42,19 @@ export async function upiCheckoutPage({ req, res, params, query, ip }) {

// Fetch event details
const event = await db.one(
`SELECT id, slug, title, city, status, start_at, capacity FROM events
WHERE slug = $1 AND status = 'published'`,
`SELECT id, slug, title, city, status, start_at, capacity, payment_method, checkout_mode,
organiser_upi_id, organiser_payee_name, payment_instructions, organiser_user_id
FROM events
WHERE slug = $1 AND status = 'published'`,
[slug]
);
if (!event) throw new HttpError(404, 'event_not_found', 'This event is not available');

const isDirectUpi = event.payment_method === 'direct_upi' || (Boolean(event.organiser_upi_id) && Boolean(event.organiser_payee_name));
if (!isDirectUpi) {
assertUpiCheckoutEnabled();
}

// Determine tier
let tierId = query.get('tier') || query.get('ticket_type_id');
let tier;
Expand All @@ -68,21 +79,33 @@ export async function upiCheckoutPage({ req, res, params, query, ip }) {

const qty = parseInt(query.get('qty') || query.get('quantity'), 10) || 1;

// Create payment intent
const order = await createUpiPaymentIntent({
slug,
user,
ticketTypeId: tier.id,
quantity: qty,
deviceType,
clientIp: ip
});
let order;
if (isDirectUpi) {
order = await createDirectUpiBooking({
slug,
user,
ticketTypeId: tier.id,
quantity: qty
});
} else {
order = await createUpiPaymentIntent({
slug,
user,
ticketTypeId: tier.id,
quantity: qty,
deviceType,
clientIp: ip
});
}

const csrfToken = user ? csrfTokenFor(user.sessionToken) : '';
const html = renderUpiCheckout({
order,
event,
tier,
user
user,
isDirectUpi,
csrfToken
});

sendHtml(res, 200, html);
Expand Down Expand Up @@ -186,3 +209,29 @@ export async function phonepeWebhook({ req, res, ip }) {
sendJson(res, 500, { ok: false, error: 'processing_error' });
}
}

/**
* POST /api/checkout/upi/report-payment - Attendee reports payment UTR reference.
*/
export async function reportPaymentApi({ req, res, ip }) {
await rateLimit(`upi_report:${hashIp(ip)}`, 20, 600);
const user = await getSessionUser(req);
if (!user) throw new HttpError(401, 'unauthorized', 'Please sign in to report payment.');
const body = await readJson(req);
const { bookingId, utr } = body;
if (!bookingId || !utr) throw new HttpError(400, 'validation', 'bookingId and utr are required.');
const r = await reportDirectUpiPayment(user, bookingId, utr);
sendJson(res, 200, r);
}

/**
* GET /api/checkout/upi/booking-status/:id - Real-time polling for direct UPI booking verification.
*/
export async function bookingStatusApi({ req, res, params, ip }) {
const bookingId = params.id;
if (!bookingId) throw new HttpError(400, 'validation', 'bookingId is required.');
await rateLimit(`upi_poll_direct:${hashIp(ip)}`, 180, 60);
const user = await getSessionUser(req);
const r = await getDirectUpiBookingStatus(bookingId, user?.id);
sendJson(res, 200, r);
}
5 changes: 5 additions & 0 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ const ROUTES = [
['POST', '/api/checkout/upi/intent', h('upi-checkout', 'createUpiIntentApi')],
['GET', '/api/checkout/upi/status/:id', h('upi-checkout', 'getUpiStatusApi')],
['POST', '/api/checkout/upi/claim', h('upi-checkout', 'claimUpiBookingApi')],
['POST', '/api/checkout/upi/report-payment', h('upi-checkout', 'reportPaymentApi')],
['GET', '/api/checkout/upi/booking-status/:id', h('upi-checkout', 'bookingStatusApi')],

// Organisations, members, venues (session + can(); outsiders get 404)
['GET', '/api/me/orgs', h('orgs', 'myOrgs')],
Expand Down Expand Up @@ -229,6 +231,9 @@ const ROUTES = [
['GET', '/api/organiser/payment-settings', h('organiser', 'getPaymentSettingsHandler')],
['PUT', '/api/organiser/payment-settings', h('organiser', 'savePaymentSettingsHandler')],
['DELETE', '/api/organiser/payment-settings', h('organiser', 'deletePaymentSettingsHandler')],
['GET', '/api/organiser/events/:id/payments', h('organiser', 'listDirectUpiPayments')],
['POST', '/api/organiser/events/:id/payments/:bookingId/confirm', h('organiser', 'confirmPayment')],
['POST', '/api/organiser/events/:id/payments/:bookingId/reject', h('organiser', 'rejectPayment')],

// Event Promotional Campaigns (Opt-in WhatsApp Marketing)
['POST', '/api/campaigns/preview', h('campaigns', 'previewCampaignHandler')],
Expand Down
Loading
Loading