trying to create a subscription for a product on Stripe. The product already has a defined price. I tried with both directly the Price object and creating a new price object, the behavior remains the same.
a fetch request with the following code and JSON body object:
async createSubscription(data) {
try {
const params = new URLSearchParams();
params.append('customer', data.customer);
params.append('cancel_at_period_end', data.cancel_at_period_end.toString());
params.append('currency', data.currency);
params.append('description', data.description);
params.append('items[0][price_data][currency]', data.items[0].price_data.currency);
params.append('items[0][price_data]', data.items[0].price_data.product);
params.append('items[0][price_data][recurring]', data.items[0].price_data.recurring.interval);
params.append('items[0][price_data][unit_amount]', data.items[0].price_data.unit_amount);
const response = await fetch(url, {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.STRIPE_SECRET_KEY}`,
'Content-Type': 'application/x-www-form-urlencoded'
},
body: params});
return {data: response};
} catch (err) {
return {error: err.message };
}
}
JSON:
{
"customer": "cus_QjVBjHwGQiiHie",
"cancel_at_period_end": false,
"currency": "eur",
"description": "my test subscription",
"items": [
{
"price_data": {
"currency": "eur",
"product": "prod_Ql0OtnWNPRhvlh",
"recurring": {
"interval": "month"
},
"unit_amount": 1000
}
}
]
}
API Response:
{
"data": {
"error": {
"message": "Invalid object",
"param": "items[0][price_data][recurring]",
"request_log_url": "https://dashboard.stripe.com/test/logs/req_VuvYP2QECpJRyD?t=1725541424",
"type": "invalid_request_error"
}
}
}
The issue is {"size","timeout}
is not what the Stripe API returned.
You need to also invoke .json()
to see the response(which might be an error that tells you what’s happening). See this related answer:
/a/49842045/9769731
Also consider looking at your Stripe API logs for hints and help: https://docs.stripe.com/development/dashboard/request-logs
I’d also recommend not writing the code this way, you should use the official Stripe Node library. https://github.com/stripe/stripe-node
4