💳 Stripe API
Typ: REST API
Auth: API Key (Secret + Publishable)
Status: ✅ Marktführer Payments
Übersicht
Stripe für: - Miet-Zahlungen (wiederkehrend) - Einmalzahlungen (Kaution, NK-Nachzahlung) - Zahlungsaufforderungen per Link - Automatisches Mahnwesen
API Endpoints
| Methode | Endpunkt | Beschreibung | Cache TTL |
|---|---|---|---|
GET |
/api/payments/customers |
Alle Kunden | 5min |
GET |
/api/payments/customers/:id |
Kundendetails | 1min |
POST |
/api/payments/customers |
Kunde anlegen | - |
GET |
/api/payments/subscriptions |
Abos (Mieten) | 1min |
POST |
/api/payments/subscriptions |
Abo erstellen | - |
POST |
/api/payments/invoices |
Rechnung erstellen | - |
POST |
/api/payments/payment-links |
Zahlungslink | - |
GET |
/api/payments/balance |
Kontostand | 5min |
GET |
/api/payments/transactions |
Transaktionen | 1min |
Stripe Integration
import Stripe from 'stripe';
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
// Mieter als Kunde anlegen
async function createTenantCustomer(tenant: {
name: string;
email: string;
phone?: string;
address: string;
}) {
return stripe.customers.create({
name: tenant.name,
email: tenant.email,
phone: tenant.phone,
address: {
line1: tenant.address,
country: 'DE'
},
metadata: {
tenantId: tenant.id,
propertyId: tenant.propertyId
}
});
}
// Wiederkehrende Mietzahlung (Subscription)
async function createRentSubscription(
customerId: string,
rentAmount: number, // in Cents
startDate: Date
) {
// Produkt für die Wohnung
const product = await stripe.products.create({
name: `Miete Wohnung ${unitId}`,
metadata: { unitId }
});
// Preis (monatlich)
const price = await stripe.prices.create({
product: product.id,
unit_amount: rentAmount,
currency: 'eur',
recurring: {
interval: 'month',
interval_count: 1
}
});
// Subscription
return stripe.subscriptions.create({
customer: customerId,
items: [{ price: price.id }],
billing_cycle_anchor: Math.floor(startDate.getTime() / 1000),
payment_settings: {
payment_method_types: ['sepa_debit', 'card']
},
metadata: { unitId, type: 'rent' }
});
}
// Einmalzahlung (Kaution, NK)
async function createOneTimePayment(
customerId: string,
amount: number,
description: string
) {
const invoice = await stripe.invoices.create({
customer: customerId,
auto_advance: true,
collection_method: 'send_invoice',
days_until_due: 14
});
await stripe.invoiceItems.create({
customer: customerId,
invoice: invoice.id,
amount,
currency: 'eur',
description
});
return stripe.invoices.sendInvoice(invoice.id);
}
// Zahlungslink für schnelle Zahlung
async function createPaymentLink(
amount: number,
description: string,
tenantEmail: string
) {
const price = await stripe.prices.create({
unit_amount: amount,
currency: 'eur',
product_data: {
name: description
}
});
return stripe.paymentLinks.create({
line_items: [{ price: price.id, quantity: 1 }],
after_completion: {
type: 'redirect',
redirect: { url: 'https://app.example.com/payment-success' }
},
metadata: { tenantEmail }
});
}
Webhook Handler
app.post('/webhooks/stripe', async (req, res) => {
const sig = req.headers['stripe-signature'];
const event = stripe.webhooks.constructEvent(
req.body,
sig,
process.env.STRIPE_WEBHOOK_SECRET
);
switch (event.type) {
case 'invoice.paid':
await recordPayment(event.data.object);
break;
case 'invoice.payment_failed':
await handlePaymentFailure(event.data.object);
break;
case 'customer.subscription.deleted':
await handleSubscriptionCancelled(event.data.object);
break;
}
res.json({ received: true });
});
Umgebungsvariablen
# Stripe
STRIPE_SECRET_KEY="sk_live_..."
STRIPE_PUBLISHABLE_KEY="pk_live_..."
STRIPE_WEBHOOK_SECRET="whsec_..."
# Test-Modus
STRIPE_TEST_SECRET_KEY="sk_test_..."
STRIPE_TEST_PUBLISHABLE_KEY="pk_test_..."
Marktführer • SEPA + Karte • Subscriptions • Webhooks