Evening,
When a user signs up for a subscription the checkout.session.completed event is fired and I can test that everything is working correctly via the CLI command: stripe trigger checkout.session.completed --add checkout_session:customer="REPLACE WITH CUSTOMER ID"
.
I want to be able to do the same to test when a SPECIFIC customer has failed to make a payment and the invoice.payment_failed
event is triggered.
I need the email value from event.data.object to match an email address on my database so I can check that the status value is correctly changing from ‘member’ to ‘pastMember’.
My intuition is the command I want is something like stripe trigger invoice.payment_failed --add invoice:customer="REPLACE WITH CUSTOMER ID"
but unfortunately this doesn’t work.
Editing the fixture for invoice doesn’t work either. Changing the customer id has no effect and I cannot add a customer_email parameter to the params object despite the fact the field exists as part of the invoice schema defined here
I have tried everything I can think of but I can’t make sense of stripe. I find the documentation regarding testing absolutely appalling.
export async function postWebhook(req: Request, res: Response) {
const event = req.body
const type = event.type
switch (type) {
// -- Working
case 'checkout.session.completed': {
const {customer_details, customer: customerId} = event.data.object
const {email} = customer_details
const user = await User.findOneAndUpdate({email}, {status: 'member', stripeId: customerId})
await instance.customers.update(customerId, {
metadata: {
mongoId: user?.id,
},
})
break
}
// -- Not working
case 'invoice.payment_failed': {
const email = event.data.object[???]
await User.findOneAndUpdate({email}, {status: 'pastMember'})
break
}
default:
break
}
res.sendStatus(200)
}
2
Answers
karllekko provided 95% of the answer I needed. I have provided the complete json fixture below to help the next poor sod trying to achieve this. The params
off_session
andconfirm
passed to payment intent were the missing magic for me:The complication here is that you have to use
--override
here to set the Customer ID(--add
only works to add a parameter there was not already present in the fixture, you need tooverride
when the fixture already sets that parameter to something, as the invoice trigger fixtures do).However that in itself won’t work out of the box, because the fixture will try to attach and use a certain PaymentMethod, so you’d end up having to try and override many things. So really I would just write my own fixture for this, instead.
That would work, but hard to say without seeing what exact edits you made. Here’s how I would do it. Basically edit the existing fixture so it loads an existing Customer, and then uses that for all the subsequent steps.
custom_invoice_payment_failed.json
ENV CUSTOMER_ID="cus_QX87JKPRilxAOU" stripe fixtures ./custom_invoice_payment_failed.json
and it should just work.https://docs.stripe.com/cli/fixtures