#joelalcedo - charges

1 messages ยท Page 1 of 1 (latest)

wintry bridge
wintry saddle
#

is there to parameterize something like this for a specific paymentintent?

wintry bridge
wintry saddle
#

ok, i see. very helpful. does this flow make sense to you?

1. user checks out -> 2. paymentIntents.create -> 3. charge.succeseded -> 4. stripe.transfers.create

i'm not quite sure this is the most efficient thing to do, since step 3 requires pulling every successful charge over the last 30 days from the API every time a payment is made..if i have 1,000 charges that would be a more time consuming process. isn't there a more efficient way?

#

for example. would this work too?

const paymentIntent = await stripe.paymentIntents.retrieve(
  'pi_3LMJxHGR0hudtWsC0kDo5tMW'
);

if i retrieve a specific payment intent, i can look at paymentIntent.charges

from the documentation of the payment intent data

i see there is a "charges" field

"charges": {
    "object": "list",
    "data": [],
    "has_more": false,
    "url": "/v1/charges?payment_intent=pi_3LMJxHGR0hudtWsC0kDo5tMW"
  },

what is contained in the "data" field?
can i use the URL to fetch the charge ID?

wintry bridge
#

since step 3 requires pulling every successful charge over the last 30 days from the API every time a payment is made..if i have 1,000 charges that would be a more time consuming process. isn't there a more efficient way?
What I mentioned to use is webhooks https://stripe.com/docs/webhooks, the idea is that you are getting notified every time there is a successful charge and then you do your related flow stripe.transfers.create

wintry saddle
#

so if i understand correctly, the webhooks are an event listener that will fire during different events, so the idea would be to listen to the webhook, when a payment intent has succeeded, then i handle the transfer directly inside the webhook?

wintry bridge
#

Yes that's the logic to follow

wintry saddle
#

ok, i will try this out, thanks so much!!!

wintry saddle
#

what is the difference between these webhooks?

checkout.session.completed
and
charge.captured

remote oar
#

Hi there ๐Ÿ‘‹ jumping in as my teammate needed to step away.

The first is triggered whenever a customer completes the checkout flow associated with a Checkout Session, and the event will contain that Checkout Session object.

The latter is triggered whenever a Charge is captured, and the event will contain the associated Charge object.

wintry saddle
#

i see, is the charge event emitted when the checkout is completed? i think the checkout.session.completed event doesn't actually contain the charge ID either

#

thanks for jumping in!!

remote oar
#

The Checkout Session won't have the associated Charge's ID, that is located within the Payment Intent. So the path of objects to follow would be Checkout Session -> Payment Intent -> Charge.

#

charge.captured events won't be triggered unless you're manually capturing payments though. What you trying to be notified about?

wintry saddle
#

i am trying to simply find a corresponding charge ID for a successful payment, then pass that charge ID into the stripe.transfers.create, which needs the charge_id under the following parameter source_transaction: "{CHARGE_ID}",

ornate star
#

If you are using Checkout, you should go through Checkout Session -> Payment Intent -> Charge like toby said. That will be the charge ID that you can use for transfers here

wintry saddle
#

hi Pompey! to make sure i understand correctly, would this step "Payment Intent -> Charge" be done through a webhook event listener?

#
case 'payment_intent.succeeded':
      const paymentIntent = event.data.object;
      // Then define and call a method to handle the successful payment intent.
      // handlePaymentIntentSucceeded(paymentIntent);
      break;
#

i would try to look out for payment_intent.succeeded to get the corresponding charge ID?

ornate star
#

Yes, in your webhook endpoint, you can listen for the checkout.session.completed event or the payment_intent.succeeded event. When you get them you can use the API's retrieve calls to get this info

wintry saddle
#

one moment while i wrap my head around this, thanks so much for the detail

wintry saddle
#

ok, so i was able to successfully ingest an emitted a payment_intent object from my server during the payment_intent.succeeded event. i am noticing the charge_id is contained inside the fraud_details field.

is this always the case? if i want to retrieve the charge_id, i search for payment_intent.object.charges.fraud_details.charge_id

ornate star
#

Can you send me the ID of this payment intent (pi_123)? As far as I know this should be directly in the objects in the charge list as id

wintry saddle
#

sure, one sec

wintry saddle
#

for some reason i can't initialize that payment intent in my app

ripe storm
#

Hi ๐Ÿ‘‹ I'm stepping in for @ornate star

wintry saddle
#

but for the real payment intent that is being generated, i am noticing the following:

when i listen to my webhook after a successful payment intent, the charge_id shows in the stripe webhook page (see second screenshot)

however, i am taking the full data from the event like this

event = stripe.webhooks.constructEvent( req.rawBody, req.headers["stripe-signature"], whSec,);

and then passing it into a firebase database to see all the data that came through (first attachment) - when the data is passed into firebase, there is no corresponding ID for the charge.

does the output from stripe.webhooks.constructEvent( req.rawBody, req.headers["stripe-signature"], whSec,); arrive asynchonously?

ripe storm
#

Can you please paste the charge ID? I cannot do anything with pictures

wintry saddle
#

"id": "ch_3LdcF3GR0hudtWsC0quVPb7s",

ripe storm
#

Okay I've got the payment intent ID. Can you restate the actual question here please?

wintry saddle
#

i figured it out -

the data field inside charges is an array, so to find the charge ID i had to do this:

dataObject.charges.data[0].id

#

my original question was why there wasn't an id field being emitted when i push the data to firestore

ripe storm
#

Which event?

wintry saddle
#

payment_intent.succeeded

ripe storm
#

Okay yeah, in that case the dataObject is a Payment Intent

#

Payment Intents have an array of charges

wintry saddle
#

in other words, when i push the output from event.data.object.charges.data, the corresponding charge "id" isn't contained in the actual data, it's its own separate field that i have to parse by looking at event.data.object.charges.data[0].id

#

if i had multiple charges for a payment intent i would have to do something like... event.data.object.charges.data.forEach((value){value.id})

ripe storm
#

event.data.object.charges.data this is an array so there is no id parameter. That is why you need to specify the index.

#

And there will only ever be 1 successful charge. But there can be multiple failed charges.

wintry saddle
#

right, ok, that all makes sense

#

thanks so much for the help all, really appreciate your patience working with me and hope you have an excellent weekend