SaaS инвойсинг и выставление счетов
Инвойсинг — документальное подтверждение транзакции для бухгалтерии клиента. Stripe автоматически генерирует инвойсы для подписок. Задача разработчика — правильно настроить реквизиты, налоги и кастомный брендинг.
Stripe: настройка инвойсов
// Настройка Customer с реквизитами для инвойсов
const customer = await stripe.customers.create({
email: '[email protected]',
name: 'Acme Corp',
address: {
line1: 'ул. Ленина, 1',
city: 'Москва',
country: 'RU',
postal_code: '101000',
},
tax_ids: [{
type: 'ru_inn',
value: '7727563778',
}],
metadata: { tenantId },
});
// Кастомный брендинг через Stripe Dashboard:
// Settings → Branding → Logo, colors, footer text
// Обновление реквизитов клиентом
export async function updateBillingDetails(
tenantId: string,
data: BillingDetailsInput
): Promise<void> {
const subscription = await db.subscription.findUnique({
where: { tenantId }
});
await stripe.customers.update(subscription!.stripeCustomerId, {
name: data.companyName,
email: data.billingEmail,
address: {
line1: data.address,
city: data.city,
country: data.country,
postal_code: data.postalCode,
},
});
// Tax ID (ИНН для RU, VAT для EU)
if (data.taxId) {
// Сначала удаляем старые tax IDs
const existingCustomer = await stripe.customers.retrieve(
subscription!.stripeCustomerId,
{ expand: ['tax_ids'] }
) as Stripe.Customer;
for (const taxId of (existingCustomer.tax_ids as Stripe.ApiList<Stripe.TaxId>).data) {
await stripe.customers.deleteTaxId(subscription!.stripeCustomerId, taxId.id);
}
// Добавляем новый
await stripe.customers.createTaxId(subscription!.stripeCustomerId, {
type: data.taxIdType as Stripe.TaxIdCreateParams.Type,
value: data.taxId,
});
}
}
Собственные инвойсы: генерация PDF
// npm install @react-pdf/renderer
import { pdf } from '@react-pdf/renderer';
import { InvoicePDF } from '@/components/pdf/InvoicePDF';
export async function generateInvoicePDF(invoiceId: string): Promise<Buffer> {
const invoice = await db.invoice.findUnique({
where: { id: invoiceId },
include: {
tenant: { include: { branding: true } },
lineItems: true,
}
});
const pdfStream = await pdf(
<InvoicePDF invoice={invoice!} />
).toBuffer();
return pdfStream;
}
// Сохраняем в S3 и возвращаем URL
export async function getInvoicePdfUrl(invoiceId: string): Promise<string> {
const key = `invoices/${invoiceId}.pdf`;
// Проверяем, уже создан ли
try {
await s3.headObject({ Bucket: process.env.AWS_BUCKET!, Key: key }).promise();
return `https://${process.env.AWS_BUCKET}.s3.amazonaws.com/${key}`;
} catch {
// Не найден — генерируем
}
const pdfBuffer = await generateInvoicePDF(invoiceId);
await s3.putObject({
Bucket: process.env.AWS_BUCKET!,
Key: key,
Body: pdfBuffer,
ContentType: 'application/pdf',
ContentDisposition: `attachment; filename="invoice-${invoiceId}.pdf"`,
}).promise();
return `https://${process.env.AWS_BUCKET}.s3.amazonaws.com/${key}`;
}
PDF компонент
// components/pdf/InvoicePDF.tsx
import {
Document, Page, Text, View, Image, StyleSheet
} from '@react-pdf/renderer';
const styles = StyleSheet.create({
page: { padding: 40, fontSize: 11, fontFamily: 'Helvetica' },
header: { flexDirection: 'row', justifyContent: 'space-between', marginBottom: 40 },
title: { fontSize: 24, fontWeight: 'bold' },
table: { marginTop: 20 },
tableRow: { flexDirection: 'row', borderBottom: '1px solid #eee', padding: '8px 0' },
tableHeader: { backgroundColor: '#f5f5f5', fontWeight: 'bold' },
});
export function InvoicePDF({ invoice }: { invoice: Invoice }) {
return (
<Document>
<Page size="A4" style={styles.page}>
<View style={styles.header}>
<View>
{invoice.tenant.branding?.logoUrl && (
<Image src={invoice.tenant.branding.logoUrl} style={{ height: 40 }} />
)}
<Text style={styles.title}>СЧЁТ-ФАКТУРА</Text>
<Text>№ {invoice.number}</Text>
<Text>от {invoice.createdAt.toLocaleDateString('ru-RU')}</Text>
</View>
<View style={{ alignItems: 'flex-end' }}>
<Text style={{ fontSize: 18, color: '#6366f1' }}>
{formatCurrency(invoice.total, invoice.currency)}
</Text>
<Text style={{ color: invoice.status === 'paid' ? '#22c55e' : '#f59e0b' }}>
{invoice.status === 'paid' ? 'Оплачен' : 'Ожидает оплаты'}
</Text>
</View>
</View>
{/* Детали компании */}
<View style={{ flexDirection: 'row', gap: 40, marginBottom: 30 }}>
<View>
<Text style={{ fontWeight: 'bold', marginBottom: 4 }}>От:</Text>
<Text>{process.env.COMPANY_NAME}</Text>
<Text>ИНН: {process.env.COMPANY_INN}</Text>
</View>
<View>
<Text style={{ fontWeight: 'bold', marginBottom: 4 }}>Кому:</Text>
<Text>{invoice.customerName}</Text>
{invoice.taxId && <Text>ИНН: {invoice.taxId}</Text>}
</View>
</View>
{/* Строки инвойса */}
<View style={styles.table}>
<View style={[styles.tableRow, styles.tableHeader]}>
<Text style={{ flex: 3 }}>Описание</Text>
<Text style={{ flex: 1, textAlign: 'right' }}>Кол-во</Text>
<Text style={{ flex: 1, textAlign: 'right' }}>Цена</Text>
<Text style={{ flex: 1, textAlign: 'right' }}>Сумма</Text>
</View>
{invoice.lineItems.map((item) => (
<View key={item.id} style={styles.tableRow}>
<Text style={{ flex: 3 }}>{item.description}</Text>
<Text style={{ flex: 1, textAlign: 'right' }}>{item.quantity}</Text>
<Text style={{ flex: 1, textAlign: 'right' }}>
{formatCurrency(item.unitAmount, invoice.currency)}
</Text>
<Text style={{ flex: 1, textAlign: 'right' }}>
{formatCurrency(item.amount, invoice.currency)}
</Text>
</View>
))}
</View>
{/* Итого */}
<View style={{ alignItems: 'flex-end', marginTop: 20 }}>
<Text style={{ fontSize: 14, fontWeight: 'bold' }}>
Итого: {formatCurrency(invoice.total, invoice.currency)}
</Text>
</View>
</Page>
</Document>
);
}
Webhook: синхронизация инвойсов Stripe
case 'invoice.finalized': {
const stripeInvoice = event.data.object as Stripe.Invoice;
await db.invoice.upsert({
where: { stripeInvoiceId: stripeInvoice.id },
create: {
stripeInvoiceId: stripeInvoice.id,
tenantId: stripeInvoice.metadata.tenantId,
number: stripeInvoice.number!,
total: stripeInvoice.amount_due,
currency: stripeInvoice.currency,
status: 'open',
pdfUrl: stripeInvoice.invoice_pdf,
periodStart: new Date(stripeInvoice.period_start * 1000),
periodEnd: new Date(stripeInvoice.period_end * 1000),
},
update: { status: 'open' }
});
break;
}
Настройка инвойсинга со Stripe, PDF-генерацией через React PDF и хранением в S3 — 3–4 рабочих дня.







