Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions firestore-stripe-payments/functions/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,16 @@ export const insertInvoiceRecord = async (invoice: Stripe.Invoice) => {
if (customersSnap.size !== 1) {
throw new Error('User not found!');
}

// For upcoming invoices, generate a unique document ID since they don't have an id
const invoiceDocId = invoice.id || `upcoming_${invoice.subscription}_${Date.now()}`;

// Write to invoice to a subcollection on the subscription doc.
await customersSnap.docs[0].ref
.collection('subscriptions')
.doc(invoice.subscription as string)
.collection('invoices')
.doc(invoice.id)
.doc(invoiceDocId)
.set(invoice);

const prices = [];
Expand All @@ -341,15 +345,18 @@ export const insertInvoiceRecord = async (invoice: Stripe.Invoice) => {
);
}

// An Invoice object does not always have an associated Payment Intent
// Only create payment records for invoices that have a payment_intent or id
// Upcoming invoices are previews and don't represent actual payments
const recordId: string = (invoice.payment_intent as string) ?? invoice.id;

// Update subscription payment with price data
await customersSnap.docs[0].ref
.collection('payments')
.doc(recordId)
.set({ prices }, { merge: true });
logs.firestoreDocCreated('invoices', invoice.id);
if (recordId) {
// Update subscription payment with price data
await customersSnap.docs[0].ref
.collection('payments')
.doc(recordId)
.set({ prices }, { merge: true });
}

logs.firestoreDocCreated('invoices', invoiceDocId);
};

/**
Expand Down