feat(payments): log initial Helcim charge to Payment on subscription creation

After a paid subscription is created and the Member row is flipped to
active, fetches the newest paid transaction from Helcim and upserts a
Payment row. Passes paymentType from the chosen cadence and
sendConfirmation: true.

Wrapped in try/catch: a logging failure here never breaks subscription
creation — the reconcile-helcim-payments script will pick up any
misses on the next run.
This commit is contained in:
Jennie Robinson Faber 2026-04-20 13:16:53 +01:00
parent be7145f96c
commit 49cfb47fff
2 changed files with 127 additions and 2 deletions

View file

@ -4,8 +4,9 @@ import Member from '../../models/member.js'
import { connectDB } from '../../utils/mongoose.js'
import { getSlackService } from '../../utils/slack.ts'
import { requireAuth } from '../../utils/auth.js'
import { createHelcimSubscription, generateIdempotencyKey } from '../../utils/helcim.js'
import { createHelcimSubscription, generateIdempotencyKey, listHelcimCustomerTransactions } from '../../utils/helcim.js'
import { sendWelcomeEmail } from '../../utils/resend.js'
import { upsertPaymentFromHelcim } from '../../utils/payments.js'
// Function to invite member to Slack
async function inviteToSlack(member) {
@ -189,6 +190,19 @@ export default defineEventHandler(async (event) => {
logActivity(member._id, 'subscription_created', { amount: body.contributionAmount })
try {
const txs = await listHelcimCustomerTransactions(body.customerCode)
const latestPaid = txs.find((t) => t.status === 'paid')
if (latestPaid) {
await upsertPaymentFromHelcim(member, latestPaid, {
paymentType: cadence,
sendConfirmation: true
})
}
} catch (err) {
console.error('[payments] initial charge log failed, will be picked up by reconciliation:', err?.message || err)
}
await inviteToSlack(member)
if (isFirstActivation) await sendWelcomeEmail(member)