I'm using Stripe in a React demo site.
I'm looking to retrieve customer details and display them on-screen, ahead of adding basic authentication to an area - think "account area of e-commerce site", but very basic!
The problem I have is that while I can create an instance of Stripe, I'm getting zero data back in the response - here is the code I have:
const stripe = require("stripe")(
<MY SK_TEST... ID>
);
const customers = await stripe.customers.list({
email: "alex.test@outlook.com",
});
console.log("CUSTOMERS", JSON.stringify(customers));
This is in a basic React page - I'm using it to trigger retrieving the data each time I refresh the page. The response I get back is this:
CUSTOMERS {"object":"list","data":[],"has_more":false,"url":"/v1/customers"}
My Stripe set up is running in test mode at present - should this cause an issue? I have a customer listed in the Customers section of my Stripe dashboard, so I should have at least one response back.
I've also checked multiple pages in SO which show similar calls to retrieve customer detail, so I'm reasonably sure that the const customers...
is good; not sure why it's returning nothing! I have also tried adding the backend API route from https://stackoverflow.com/a/74669051/9851653, and added this as the POST:
// api/get-all-customers.js
import Stripe from "stripe";
export default async function handler(req, res) {
if (req.method === "POST") {
const { token } = JSON.parse(req.body);
if (!token) {
return res.status(403).json({ msg: "Forbidden" });
}
const stripe = new Stripe(process.env.NEXT_PUBLIC_STRIPE_SECRET, {
apiVersion: "2020-08-27",
});
try {
const customers = await stripe.customers.list(); // returns all customers sorted by createdDate
res.status(200).json(customers);
} catch (err) {
console.log(err);
res.status(500).json({ error: true });
}
}
}
POST:
const response = await fetch("/api/get-all-customers", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
});
This hasn't worked for me - I get this:
error - TypeError: Only absolute URLs are supported
Anyone able to help identify what I'm missing please?