0

I am using Stripe in my NextJs project and have tried to get customers list in my app but have not succeeded. If anyone knows how to get it, please instruct me on how to do that.

This is my code:

import { loadStripe } from "@stripe/stripe-js";

async function getStripeCustomers(){
   const stripe = await loadStripe(
   process.env.key
  );
  if (stripe) {
    // there was a toturail for node.js like this. 
    console.log(stripe.customers.list())
  }
}

useEffect(() => {
    getStripeCustomers()

}, []);
Zia Yamin
  • 942
  • 2
  • 10
  • 34

1 Answers1

1

I think you should do this logic in backend so create a route in api folder then try this code.

// api/payment/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 });
}
 }
}

Now from frontend send a POST request to newly created route.

Paiman Rasoli
  • 1,088
  • 1
  • 5
  • 15
  • but it brings just 10. I need all of the users. – Zia Yamin Dec 04 '22 at 04:35
  • 1
    @ZiaYamin please see the doc or code doc it accepts some parameters which I believe you can get more users. const customers = await stripe.customers.list({ /* check the options */ }); – Paiman Rasoli Dec 04 '22 at 04:50