#quantum1125 - Node Listing Payment Intents

1 messages ยท Page 1 of 1 (latest)

meager arch
#

Hello! I believe require() needs to be called outside of a function. If you move that line outside the function, then pass in stripe as an argument to the function instead does it work as expected?

winged crown
#

no difference, same stripe error

meager arch
#

Can you share the code you're using?

winged crown
#
const stripe = require('stripe')('sk_test_');


async function getPaymentIntents() {
   
    const paymentIntents = await stripe.paymentIntents.list({
        limit: 3,
      });
    console.log('paymentIntents',paymentIntents)
    return paymentIntents
}

getPaymentIntents();
```javascript
meager arch
#

Is it still throwing the error Error: Cannot find module 'stripe' or a different one?

winged crown
#

same error, Cannot find module 'stripe'

meager arch
#

Did you install the Stripe Node library using NPM?

winged crown
#

oh my, you're right. I was using the stripe-search-beta library, which i assumed included the regular stripe node library

meager arch
#

๐Ÿ˜„

#

A quick npm install stripe --save should sort out that error. ๐Ÿ™‚

#

And, actually, you can probably put it back in the function.

#

If you leave it out of the function you may need to modify both getPaymentIntents() bits to be getPaymentIntents(stripe)

winged crown
#

Ok i got an output working, but I'm curious how to correctly iterate through the result of
await stripe.paymentIntents.list();

I'm getting an error that paymentIntents.forEach is not a function even though stripe returns a list

#

here's my code:

let orders = []
async function getPaymentIntents() {
   
    const paymentIntents = await stripe.paymentIntents.list();
   
    paymentIntents.forEach(paymentIntent => {
        if (paymentIntent["status"] =='succeeded') {
            orders.push(paymentIntent)
        }
    });
    return orders
}

getPaymentIntents();
```javascript
meager arch
winged crown
#

when i try auto-pagination, i get an error saying TypeError: paymentIntents.autoPagingEach is not a function

let orders = []
async function getPaymentIntents() {
   
    const paymentIntents = await stripe.paymentIntents.list();

    paymentIntents.autoPagingEach((paymentIntent) => {
        if (paymentIntent["status"] =='succeeded') {
            orders.push(paymentIntent)
        }
    })
    return orders
}

getPaymentIntents();```
meager arch
#

What version of Node are you using?

winged crown
#

v16.9.0, you want me to use

for await (const customer of stripe.customers.list({limit: 3})) {
  // Do something with customer
}
meager arch
#

Yeah, that's what you need to use since you're on Node 10+.

winged crown
#

because the syntax of auto-pagination for stripe search beta is what i did above

meager arch
#

Not sure what you mean, can you provide more details?

winged crown
meager arch
#

The search API is in beta and may work differently, I'm not honestly sure. Does the Node 10+ syntax work as expected?

winged crown
#

i believe this syntax works, but it does take longer than expected to filter out.

meager arch
#

Try using a higher limit.

#

The limit applies to each individual request behind the scenes. For example, if you specify a limit of 10 and you have 100 total items to list that will require 10 API requests. If you specify a limit of 50 you'll only need two.

winged crown
meager arch
#

It does limit it for each individual API call, but when using auto-pagination the library will make as many API calls as needed to give you the full list.

winged crown
#

so it seems like I should always opt for limit:100 if i am returning more than 100 items in a list?

meager arch
#

No, depending on what you're listing a high limit might make the request time out. I recommend using a limit of around 50 to start with, then adjust up or down from there depending on your specific use case.

#

Also note that specifying a limit greater than the number of results is fine.

winged crown
meager arch
#

If your account has 10 Customers you can ask the API to list all Customers with limit set to 100 and it will work just fine. The limit defines the maximum amount of results per API request, but you may get back fewer.

#

On the other hand, if your account has 1,000 Customers and you have limit set to 100 you'll get 100 back with a single, non-auto-paginating list request or all 1,000 if you use auto-pagination.

#

Does that make sense?

winged crown
meager arch
#

No, the customer property there is supposed to be a Customer ID (typically something like cus_123).

winged crown
meager arch
winged crown
#

got it, and auto-pagination is suppose to work across all supported Stripe APIs, including Customer?

meager arch
#

Yeah, auto-pagination works for any list endpoint.

winged crown
#

hmm why is customerID null so often? make it impossible to look up the customer name. shouldn't generate a new customer id per paymentIntent if customer doesn't exist?

meager arch
#

Where are you seeing it be null?

#

On the customer property on a Payment Intent?

#

Payment Intents can exist on their own without being associated with a Customer.

winged crown
#

yeah i am seeing it on Payment Intent. got an error that my customer api call was not working and it was because it was being passed null, which i pulled from the payment Intent

meager arch
#

That's normal. You should check for null values before passing them back to the API.

winged crown
#

i know to check now, but i am wondering how stripe assigns customer id in paymentIntent

meager arch
#

In what scenario?

#

Payment Intents can be created directly via the API, or they can be created by other things (like Checkout or Invoices). Can you give me more details about what you're trying to do so I can help unblock you?

winged crown
#

my goal is pull 3 pieces of info from stripe via your endpoints: description (via paymentIntent), customer name (via customer via paymentIntent), and order amount (via paymentIntent)

meager arch
#

A Payment Intent may not have a Customer associated with it, and thus there will be no name.

#

Even if there is a Customer associated that Customer may not have a name set.

winged crown
#

i am wondering how sometimes during checkout (where i believe our paymentIntents are created) a customer ID is associated and sometimes not. Seems mostly random.

meager arch
#

Can you give me an example Payment Intent ID without a Customer associated?

winged crown
#

pi_3JvKp2BEzuI4g3Px1s6t9uSL

meager arch
#

Looking...

#

No Customer was associated.

winged crown
#

interesting, what about this payment Intent? can you tell how it was created? pi_3JYD4pBEzuI4g3Px1b2ecGav

#

this has an associated customer

meager arch
#

I can, but let me teach you how to find out yourself. ๐Ÿ™‚

winged crown
#

i'm currently unable to access my stripe account which is why i ask

meager arch
#

Ah, okay.

#

That one was created by the API as well, but this time a Customer was specified. Checkout was again not involved.

#

You should try to get your Dashboard access sorted out, as that will help you investigate and debug.

winged crown
#

I have access to another stripe account that i'm looking at to see where you are referencing the origin of the paymentIntent, but I do not see it

meager arch
#

What do you mean?

winged crown
#

where do you see that any of these requests originated via API vs Checkout?

#

this is an old stripe account btw

meager arch
#

Click on one of those, then on the right you should see detail about the request. The Source will indicate where the request came from.

winged crown
#

Source gives the device info
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36

meager arch
#

That means a web browser made the request. Chrome v94, specifically.

#

Here's an example from my test account showing a Payment Intent created with the Stripe PHP library.

winged crown
#

ah ok, this is what was i was curious about

#

You have been incredibly helpful @meager arch . Thank you for answering all my questions!