The previous answer is not entirely correct. Perhaps the Stripe API documentation has changed. So what needs to be done:
- Create an invoice by passing the customer_id. Docs
- Create an invoice element by passing the customer_id, invoice_id (from step 1) + either price_id or amount and currency. Docs
- Then finalize the invoice (it will receive the status="open") and automatically receive the payment intent. Docs
- Request the desired payment intent, and get its secret_key. Docs
// Step 1: Create an invoice by passing the customer_id.
const stripe = require('stripe')('YOUR_STRIPE_SECRET_KEY');
async function createInvoice(customer_id) {
try {
const invoice = await stripe.invoices.create({
customer: customer_id,
});
return invoice;
} catch (error) {
console.error('Error creating invoice:', error);
throw error;
}
}
// Step 2: Create an invoice item for the invoice obtained from the previous step, by passing the customer_id, invoice_id, and either price_id or amount and currency.
async function createInvoiceItem(customer_id, invoice_id, price_id, amount, currency) {
try {
const invoiceItem = await stripe.invoiceItems.create({
customer: customer_id,
invoice: invoice_id,
price: price_id, // or amount and currency, depending on your choice
});
return invoiceItem;
} catch (error) {
console.error('Error creating invoice item:', error);
throw error;
}
}
// Step 3: Finalize the invoice to set its status to "open" and automatically receive the payment intent.
async function finalizeInvoice(invoice_id) {
try {
const finalizedInvoice = await stripe.invoices.finalizeInvoice(invoice_id);
return finalizedInvoice;
} catch (error) {
console.error('Error finalizing invoice:', error);
throw error;
}
}
// Step 4: Request the payment intent for the invoice.
async function getPaymentIntent(invoice_id) {
try {
const paymentIntent = await stripe.paymentIntents.create({
invoice: invoice_id,
});
return paymentIntent;
} catch (error) {
console.error('Error getting payment intent:', error);
throw error;
}
}
// Usage of all steps:
async function main() {
try {
const customer_id = 'YOUR_CUSTOMER_ID';
// Step 1: Create an invoice
const invoice = await createInvoice(customer_id);
console.log('Created invoice:', invoice);
// Step 2: Create an invoice item
const price_id = 'YOUR_PRICE_ID'; // or use amount and currency if not using Price API
const invoiceItem = await createInvoiceItem(customer_id, invoice.id, price_id, 1000, 'usd');
console.log('Created invoice item:', invoiceItem);
// Step 3: Finalize the invoice and get the payment intent
const finalizedInvoice = await finalizeInvoice(invoice.id);
console.log('Finalized invoice:', finalizedInvoice);
// Step 4: Request the payment intent
const paymentIntent = await getPaymentIntent(invoice.id);
console.log('Payment intent:', paymentIntent);
} catch (error) {
console.error('Error:', error);
}
}
main();