|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const db = require('../../../lib/db'); |
| 4 | + |
| 5 | +function logSafe(message, code, claimId, provider) { |
| 6 | + console.log(JSON.stringify({ message, code, claimId: claimId || null, provider: provider || null })); |
| 7 | +} |
| 8 | + |
| 9 | +module.exports = async function handler(req, res) { |
| 10 | + res.setHeader('Content-Type', 'application/json; charset=utf-8'); |
| 11 | + res.setHeader('Cache-Control', 'no-store'); |
| 12 | + |
| 13 | + if (req.method !== 'POST') { |
| 14 | + res.setHeader('Allow', 'POST'); |
| 15 | + return res.status(405).json({ ok: false, status: 'METHOD_NOT_ALLOWED' }); |
| 16 | + } |
| 17 | + |
| 18 | + const sharedSecret = process.env.COMMERCIAL_WEBHOOK_SHARED_SECRET; |
| 19 | + if (!sharedSecret) { |
| 20 | + logSafe('Internal payment confirmation endpoint not configured', 'INTERNAL_PAYMENT_CONFIRMATION_NOT_CONFIGURED'); |
| 21 | + return res.status(503).json({ ok: false, status: 'INTERNAL_PAYMENT_CONFIRMATION_NOT_CONFIGURED' }); |
| 22 | + } |
| 23 | + |
| 24 | + const authHeader = req.headers.authorization || req.headers.Authorization; |
| 25 | + const expected = `Bearer ${sharedSecret}`; |
| 26 | + if (authHeader !== expected) { |
| 27 | + logSafe('Unauthorized internal payment confirmation request', 'UNAUTHORIZED'); |
| 28 | + return res.status(401).json({ ok: false, status: 'UNAUTHORIZED' }); |
| 29 | + } |
| 30 | + |
| 31 | + const body = req.body || {}; |
| 32 | + const claimId = body.claimId; |
| 33 | + const provider = body.provider; |
| 34 | + const providerPaymentId = body.providerPaymentId; |
| 35 | + const paymentIntentId = body.paymentIntentId || null; |
| 36 | + const amountCents = body.amountCents; |
| 37 | + const currency = (body.currency || 'usd').toLowerCase(); |
| 38 | + |
| 39 | + if (!claimId) return res.status(400).json({ ok: false, status: 'CLAIM_ID_REQUIRED' }); |
| 40 | + if (!provider) return res.status(400).json({ ok: false, status: 'PROVIDER_REQUIRED' }); |
| 41 | + if (provider !== 'stripe') return res.status(400).json({ ok: false, status: 'PROVIDER_NOT_SUPPORTED' }); |
| 42 | + if (!providerPaymentId) return res.status(400).json({ ok: false, status: 'PROVIDER_PAYMENT_ID_REQUIRED' }); |
| 43 | + if (!Number.isInteger(amountCents) || amountCents <= 0) return res.status(400).json({ ok: false, status: 'INVALID_AMOUNT_CENTS' }); |
| 44 | + |
| 45 | + try { |
| 46 | + const claimResult = await db.query('select claim_id, status from claim_requests where claim_id = $1 limit 1', [claimId]); |
| 47 | + const claim = claimResult.rows[0]; |
| 48 | + if (!claim) return res.status(404).json({ ok: false, status: 'CLAIM_NOT_FOUND' }); |
| 49 | + |
| 50 | + if (claim.status !== 'payment_pending' && claim.status !== 'paid') { |
| 51 | + return res.status(400).json({ ok: false, status: 'CLAIM_NOT_READY_FOR_PAYMENT' }); |
| 52 | + } |
| 53 | + |
| 54 | + await db.query( |
| 55 | + `update claim_payments |
| 56 | + set status = 'paid', provider_payment_id = $3, payment_intent_id = $4, amount_cents = $5, currency = $6, updated_at = now() |
| 57 | + where claim_id = $1 and provider = $2`, |
| 58 | + [claimId, provider, providerPaymentId, paymentIntentId, amountCents, currency] |
| 59 | + ); |
| 60 | + |
| 61 | + await db.query( |
| 62 | + `update claim_payments |
| 63 | + set status = 'paid', claim_id = $1, provider = $2, payment_intent_id = $4, amount_cents = $5, currency = $6, updated_at = now() |
| 64 | + where provider_payment_id = $3`, |
| 65 | + [claimId, provider, providerPaymentId, paymentIntentId, amountCents, currency] |
| 66 | + ); |
| 67 | + |
| 68 | + if (claim.status === 'paid') { |
| 69 | + logSafe('Claim already paid; returning idempotent success', 'CLAIM_MARKED_PAID', claimId, provider); |
| 70 | + return res.status(200).json({ ok: true, status: 'CLAIM_MARKED_PAID', claimId }); |
| 71 | + } |
| 72 | + |
| 73 | + await db.query( |
| 74 | + `update claim_requests |
| 75 | + set status = 'paid', payment_status = 'paid', payment_amount_cents = $2, |
| 76 | + payment_currency = $3, stripe_checkout_session_id = $4, |
| 77 | + stripe_payment_intent_id = $5, paid_at = now(), updated_at = now() |
| 78 | + where claim_id = $1`, |
| 79 | + [claimId, amountCents, currency, providerPaymentId, paymentIntentId] |
| 80 | + ); |
| 81 | + |
| 82 | + await db.query( |
| 83 | + `insert into claim_events (claim_id, event_type, message, metadata_json) |
| 84 | + values ($1, 'payment.completed', 'Payment completed.', $2::jsonb)`, |
| 85 | + [claimId, JSON.stringify({ provider })] |
| 86 | + ); |
| 87 | + |
| 88 | + const transitionExists = await db.query( |
| 89 | + `select 1 from claim_status_transitions |
| 90 | + where claim_id = $1 and from_status = 'payment_pending' and to_status = 'paid' |
| 91 | + limit 1`, |
| 92 | + [claimId] |
| 93 | + ); |
| 94 | + if (!transitionExists.rows.length) { |
| 95 | + await db.query( |
| 96 | + `insert into claim_status_transitions (claim_id, from_status, to_status) |
| 97 | + values ($1, 'payment_pending', 'paid')`, |
| 98 | + [claimId] |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + logSafe('Claim marked paid', 'CLAIM_MARKED_PAID', claimId, provider); |
| 103 | + return res.status(200).json({ ok: true, status: 'CLAIM_MARKED_PAID', claimId }); |
| 104 | + } catch (error) { |
| 105 | + logSafe('Failed to confirm claim payment', 'PAYMENT_CONFIRMATION_FAILED', claimId, provider); |
| 106 | + return res.status(500).json({ ok: false, status: 'PAYMENT_CONFIRMATION_FAILED' }); |
| 107 | + } |
| 108 | +}; |
0 commit comments