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:
parent
be7145f96c
commit
49cfb47fff
2 changed files with 127 additions and 2 deletions
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue