feat: Disable projects on failed payment
This commit is contained in:
@@ -5,12 +5,16 @@ import type {Request, Response} from 'express';
|
||||
import signale from 'signale';
|
||||
import type Stripe from 'stripe';
|
||||
|
||||
import {STRIPE_ENABLED, STRIPE_WEBHOOK_SECRET} from '../app/constants.js';
|
||||
import {ProjectDisabledPaymentEmail, sendPlatformEmail} from '@plunk/email';
|
||||
import React from 'react';
|
||||
|
||||
import {DASHBOARD_URI, LANDING_URI, STRIPE_ENABLED, STRIPE_WEBHOOK_SECRET} from '../app/constants.js';
|
||||
import {stripe} from '../app/stripe.js';
|
||||
import {prisma} from '../database/prisma.js';
|
||||
import {BillingLimitService} from '../services/BillingLimitService.js';
|
||||
import {ContactService} from '../services/ContactService.js';
|
||||
import {EventService} from '../services/EventService.js';
|
||||
import {MembershipService} from '../services/MembershipService.js';
|
||||
import {MeterService} from '../services/MeterService.js';
|
||||
import {NtfyService} from '../services/NtfyService.js';
|
||||
import {SecurityService} from '../services/SecurityService.js';
|
||||
@@ -519,6 +523,16 @@ export class Webhooks {
|
||||
const invoice = event.data.object;
|
||||
const customerId = invoice.customer as string;
|
||||
|
||||
// Only disable projects that are already consuming (recurring billing).
|
||||
// If billing_reason is 'subscription_create', this is a first-time payment
|
||||
// attempt and the project has never had an active subscription — don't disable.
|
||||
if (invoice.billing_reason === 'subscription_create') {
|
||||
signale.info(
|
||||
`[WEBHOOK] Payment failed on initial subscription attempt for customer ${customerId}, skipping disable`,
|
||||
);
|
||||
break;
|
||||
}
|
||||
|
||||
// Find project by customer ID
|
||||
const project = await prisma.project.findUnique({
|
||||
where: {customer: customerId},
|
||||
@@ -529,10 +543,33 @@ export class Webhooks {
|
||||
break;
|
||||
}
|
||||
|
||||
signale.warn(`[WEBHOOK] Payment failed for project ${project.name} (${project.id})`);
|
||||
signale.warn(`[WEBHOOK] Payment failed for project ${project.name} (${project.id}), disabling project`);
|
||||
|
||||
// Send notification about payment failure
|
||||
await NtfyService.notifyPaymentFailed(project.name, project.id);
|
||||
await prisma.project.update({
|
||||
where: {id: project.id},
|
||||
data: {disabled: true},
|
||||
});
|
||||
|
||||
await NtfyService.notifyProjectDisabledForPayment(project.name, project.id);
|
||||
|
||||
// Send email notification to project members
|
||||
try {
|
||||
const members = await MembershipService.getMembers(project.id);
|
||||
const emails = members.map(m => m.email);
|
||||
if (emails.length > 0) {
|
||||
const template = React.createElement(ProjectDisabledPaymentEmail, {
|
||||
projectName: project.name,
|
||||
projectId: project.id,
|
||||
dashboardUrl: DASHBOARD_URI,
|
||||
landingUrl: LANDING_URI,
|
||||
});
|
||||
await Promise.all(
|
||||
emails.map(email => sendPlatformEmail(email, 'Project Disabled - Payment Failed', template)),
|
||||
);
|
||||
}
|
||||
} catch (emailError) {
|
||||
signale.error(`[WEBHOOK] Failed to send project disabled email:`, emailError);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -170,6 +170,17 @@ export class NtfyService {
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify about project disabled due to payment failure
|
||||
*/
|
||||
public static async notifyProjectDisabledForPayment(projectName: string, projectId: string): Promise<void> {
|
||||
await this.sendUrgent(
|
||||
'Project Disabled - Payment Failed',
|
||||
`Project "${projectName}" (${projectId}) was automatically disabled due to a failed recurring payment`,
|
||||
[NtfyTag.WARNING, NtfyTag.MONEY, NtfyTag.ERROR],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify about successful invoice payment - MIN priority (routine)
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
import {Heading, Link, Section, Text} from '@react-email/components';
|
||||
import * as React from 'react';
|
||||
import {EmailLayout} from '../common/EmailLayout';
|
||||
import {Footer} from '../common/Footer';
|
||||
import {Header} from '../common/Header';
|
||||
|
||||
interface ProjectDisabledPaymentEmailProps {
|
||||
projectName: string;
|
||||
projectId: string;
|
||||
dashboardUrl?: string;
|
||||
landingUrl?: string;
|
||||
}
|
||||
|
||||
export function ProjectDisabledPaymentEmail({
|
||||
projectName = 'My Project',
|
||||
projectId = 'proj_example123',
|
||||
dashboardUrl = 'https://next-app.useplunk.com',
|
||||
landingUrl = 'https://www.useplunk.com',
|
||||
}: ProjectDisabledPaymentEmailProps) {
|
||||
return (
|
||||
<EmailLayout>
|
||||
<Header />
|
||||
|
||||
<Section className="px-8 pb-10 pt-10">
|
||||
<Heading className="mb-2 mt-0 text-2xl font-semibold tracking-tight text-gray-900">Project disabled</Heading>
|
||||
|
||||
<Text className="mb-8 mt-0 text-base leading-relaxed text-gray-600">
|
||||
Your project <strong className="font-medium text-gray-900">{projectName}</strong> has been automatically
|
||||
disabled because a recurring payment could not be processed.
|
||||
</Text>
|
||||
|
||||
<Section className="mb-8 rounded-lg bg-red-50 px-6 py-4" style={{border: '1px solid #fca5a5'}}>
|
||||
<Text className="mb-0 mt-0 text-sm leading-relaxed text-red-900">
|
||||
All email sending is currently blocked. Please update your payment method to re-enable the project.
|
||||
</Text>
|
||||
</Section>
|
||||
|
||||
<Heading className="mb-4 mt-0 text-lg font-semibold text-gray-900">How to restore your project</Heading>
|
||||
|
||||
<Section className="mb-8">
|
||||
<Section className="mb-3">
|
||||
<Text className="mb-1 mt-0 text-sm font-medium text-gray-900">Update your payment method</Text>
|
||||
<Text className="mb-0 mt-0 text-sm leading-relaxed text-gray-600">
|
||||
Go to your billing settings and add a valid payment method
|
||||
</Text>
|
||||
</Section>
|
||||
|
||||
<Section className="mb-3">
|
||||
<Text className="mb-1 mt-0 text-sm font-medium text-gray-900">Re-enable your project</Text>
|
||||
<Text className="mb-0 mt-0 text-sm leading-relaxed text-gray-600">
|
||||
Once your payment is resolved, contact support to re-enable your project
|
||||
</Text>
|
||||
</Section>
|
||||
|
||||
<Section>
|
||||
<Text className="mb-1 mt-0 text-sm font-medium text-gray-900">Contact support</Text>
|
||||
<Text className="mb-0 mt-0 text-sm leading-relaxed text-gray-600">
|
||||
Need help? Our team is available to assist you
|
||||
</Text>
|
||||
</Section>
|
||||
</Section>
|
||||
|
||||
<Section className="mb-6">
|
||||
<Link
|
||||
href={`${dashboardUrl}/settings?tab=billing`}
|
||||
className="inline-block rounded-md bg-gray-900 px-6 py-3 text-sm font-medium text-white no-underline"
|
||||
>
|
||||
Update payment method
|
||||
</Link>
|
||||
</Section>
|
||||
</Section>
|
||||
|
||||
<Footer projectId={projectId} landingUrl={landingUrl} />
|
||||
</EmailLayout>
|
||||
);
|
||||
}
|
||||
|
||||
export default ProjectDisabledPaymentEmail;
|
||||
@@ -1,4 +1,5 @@
|
||||
export {ProjectDisabledEmail} from './ProjectDisabled';
|
||||
export {ProjectDisabledPaymentEmail} from './ProjectDisabledPayment';
|
||||
export {BillingLimitWarningEmail} from './BillingLimitWarning';
|
||||
export {BillingLimitExceededEmail} from './BillingLimitExceeded';
|
||||
export {EmailVerificationEmail} from './EmailVerification';
|
||||
|
||||
Reference in New Issue
Block a user