Mitchell Cartwright
Mitchell Cartwright

Reputation: 191

Stripe No such customer (Express Connected Account)

I'm trying to allow users to create one-off invoices after they have onboarded to the platform via an Express account.

I have a Node route set up to create and send an invoice.

I'm getting the error

 StripeInvalidRequestError: No such customer: 'cus_I3Xra0juO9x2Iu'

However the customer does exist in the user's connect account.

enter image description here

The route is below

app.post('/api/new_invoice', async (req, res) => {
        try {
            const { customer, deliverables, amount, payableBy } = req.body

            const product = await stripe.products.create({
                name: deliverables,
            })

            const price = await stripe.prices.create({
                unit_amount: amount,
                currency: 'aud',
                product: product.id,
            })

            const invoiceItem = await stripe.invoiceItems.create({
                customer,
                price: price.id,
            })

            const stripeInvoice = await stripe.invoices.create(
                {
                    customer,
                    collection_method: 'send_invoice',
                    days_until_due: 30,
                    invoiceItem,
                },
                {
                    stripeAccount: req.user.stripeAcct,
                }
            )

            const invoice = await new Invoice({
                customer,
                deliverables,
                amount,
                paid: false,
                issueDate: Date.now(),
                payableBy,
                _user: req.user.id,
            }).save()
            res.send(invoice.data)
            console.log(invoiceItem, stripeInvoice)
        } catch (err) {
            console.log(err)
            res.status(400)
            res.send({ error: err })
            return
        }
    })

From what I understand adding a second object to stripe.invoices.create with the connect account's id then it should look into their Stripe account for the customer?

Thanks in advance

Upvotes: 3

Views: 2145

Answers (1)

koopajah
koopajah

Reputation: 25552

When making a call on behalf of a connected account, you need to set the Stripe-Account header as their account id as documented here. This header has to be set on every API request you make on behalf of that connected account.

In your code, you are only setting the header on the Invoice Create API request but not the other one(s) such as the Invoice Item Create. Make sure to pass it everywhere and your code will start working.

Upvotes: 4

Related Questions