0

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?

Alex Libby
  • 81
  • 1
  • Do you get any customers back if you list all of them without the `email` filter? That list call should work as long as you have a customer with that exact same email on that Stripe account in test mode (or live mode when you make the call in live). If no customers are returned without the `email` filter, then it sounds like you are on a different account or in a different mode than you think you are. – Pompey Apr 06 '23 at 20:35
  • I've checked - I don't get any customers back at all, if I use the stripe.customers.list method, with or without the email filter. Is there any way I can tell which mode I'm in, as the dashboard says its test mode? I'm using my Stripe Public/Publishable key, so in theory it should work - is this something I need Stripe to check and advise? – Alex Libby Apr 07 '23 at 07:08
  • As long as you are using your secret key that starts with `sk_test_` that is from the same account you should see the same customers that you see in the dashboard. Have you double checked that you see customers at https://dashboard.stripe.com/test/customers and that you are using your secret key from https://dashboard.stripe.com/test/apikeys ? – Pompey Apr 07 '23 at 18:32
  • Mmm - I've checked both the `sk_test_` key and the customer details; they are both listed in the account, and are both in the code. I am seeing that account listed as a Guest though - would this make any difference? I've not seen how to trigger getting the test customer to register as an account - I suspect though I'm missing something really obvious though! – Alex Libby Apr 08 '23 at 08:20
  • Yes, that is it. Guest customers do not have actual Customer objects associated with them. To have Checkout create Customer objects that you can access via the API, you can pass `customer_creation='always'` when creating your Checkout Sessions https://stripe.com/docs/payments/checkout/guest-customers – Pompey Apr 10 '23 at 14:58
  • I've just tried adding it into my `stripe.sessions.create()` function and it's worked - this is brilliant: thankyou! I've spent hours looking for this fix - adding it now returns values as I need. For anyone who wants to add this setting - it will be in ``` stripe.checkout.sessions.create({ ... customer_creation: "if_required" | customer_creation: "always" }); ``` You can use either example, depending on whether you want to force creation, or have it only when needed. I had to find that value in SO - not sure I've seen it anywhere in the Stripe docs...at least not yet! – Alex Libby Apr 10 '23 at 17:48

0 Answers0