#steven_paymentintent-fees

1 messages ยท Page 1 of 1 (latest)

unborn thornBOT
#

๐Ÿ‘‹ Welcome to your new thread!

โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.

โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.

๐Ÿ”— This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1288168254409408595

๐Ÿ“ Have more to share? Add more details, code, screenshots, videos, etc. below.

unkempt comet
#

steven_paymentintent-fees

strange tiger
#

How do I paste my code here?
It supposed to trig the module which doing the job for calculating the commission before forward the money to the connect account

unkempt comet
#

I don't really need your code right now. I need you to explain your issue as a developer

strange tiger
#

but it didn't happen due to webhook didn't receive paymentIntent.succeed event

#

when receive this charge.updated

#

Was it racing?

#

case 'payment_intent.succeeded':
{
try {
const srcToken = event.data.object.id;
let trans = await Transaction.findOne({srcToken: srcToken});
if (trans)
{
trans.srcEvt = event.type;
trans.ums = Date.now();
await trans.save();
}

                return;
            } catch (error) {
                utils2021.logMsg('system', 'webhook', `webhook event 'source.canceled': error:${error.message}`);
            }
            break;
        }
unkempt comet
#

There's nothing "racing". The same exact Events happen in Test mode the same way. The order of Events is never guaranteed

strange tiger
#

case 'charge.updated':
{
const srcToken = event.data.object.payment_intent;
let trans = await Transaction.findOne({srcToken: srcToken});

            const paymentIntent = await stripe.paymentIntents.retrieve(
                srcToken,
                {
                  expand: ['latest_charge.balance_transaction'],
                }
            );
            
            // Calculate net and fee
            const balanceTrans = paymentIntent.latest_charge.balance_transaction;
            const fee = balanceTrans.fee;
            const netAmount = balanceTrans.net;
            
            if (trans.srcEvt === 'payment_intent.succeeded') {
                if (trans)
                {
                    trans.stripeFee = `${fee}`;
                    trans.afterStripeNet = `${netAmount}`;
                    trans.srcType = event.type;
                    trans.ums = Date.now();
                    await trans.save();
                }
                completePaymentIntentSucceed(trans, event.data.object);
            }
            break;
unkempt comet
#

Sorry you're just dumping code with no context. I can't really just debug your code right now without understanding your issue.
My advice is to add real logs to your webhook handler and figure out what's missing

strange tiger
#

If the order not guarranted, the above code won't work due to it assume paymentIntent_succeed happen first

#

The problem didn't happen with StripeCli and it was always working

unkempt comet
strange tiger
#

In that case, if people want to calculate the StripeFee, what's the suggested logic?

#

when I using source, it worked great, not sure why you guys depprecated that

#

I used to calculate the StripeFee by my estimation, and I just need PaymentIntent.succeed, it didn't have this ordering issue, but the fee I calculated is not accurate

unkempt comet
#

I mean the ordering issue will happen for most of your code in a webhook handler really. You more got luck that it didn't affect you until now.
My advice is to debug your code really and handle Events out of order

#

But if you want a quick fix, pass capture_method: 'automatic' so that the fee is calculated synchronously like it used to

strange tiger
#

do you mean in PaymentIntent.succeed?

unkempt comet
#

yes

strange tiger
#

for the Quik fix

unkempt comet
#

no

strange tiger
#

When I debug, it was alway correct as mentioned last time

unkempt comet
#

When you create the PaymentIntent, you can pass the capture_method parameter set to automatic. Doing this means that you will get the BalanceTransaction created at the same time as the Charge and not asynchronously later.

strange tiger
#

I see, oh, it seems I tried that, it has other problem, let me double check

#

Do you mean here:
const pyInt = await stripe.paymentIntents.create({
amount,
currency,
description,
payment_method_types,
metadata: meta,
})

#

?

unkempt comet
#

yes

strange tiger
#

how to pass that?

#

it seems there is no that argument

unkempt comet
#

I'm confused, I linked you to the exact API Reference for that parameter.

strange tiger
#

I see,
automatic_payment_methods: {
enabled: true,
},

#

Actually, it is not about wrapping atest_charge.balance_transaction, it is only about auto support card, alipay or wechat pay, I tried that before

#

It is just replace payment_method_types, right?

unkempt comet
#

I'm sorry but you are passing a parameter that has nothing to do with what I said ๐Ÿ˜…

strange tiger
#

That's what the link showing, any sample code of that if it is not?

unkempt comet
#

Yeah sorry you're mixing up the list of parameters and the basic example code on how to use the API overall. The example code is not for that specific parameter

#

Did you write that code you took a picture of? Are you the developer of the whole integration?

strange tiger
#

Yes

#

The selected one was just pasted, not the original code

unkempt comet
#

okay so if you wrote that code you likely understand that you are passing various parameters to the Create PaymentIntent API https://docs.stripe.com/api/payment_intents/create such as the amount and currency to charge, a description for the payment in the email receipt, etc. right?

strange tiger
#

I'm seeing the response

#

how do I disable the async in the req?

unkempt comet
#

see what I said above

okay so if you wrote that code you likely understand that you are passing various parameters to the Create PaymentIntent API https://docs.stripe.com/api/payment_intents/create such as the amount and currency to charge, a description for the payment in the email receipt, etc. right?

strange tiger
#

That's correct, but how do I make the capture_method to sync instead of async?

#

current is:
const pyInt = await stripe.paymentIntents.create({
amount,
currency,
description,
payment_method_types,
metadata: meta
})

unkempt comet
#

I'm trying to teach you to fish here ๐Ÿ™‚

#

You clearly understand the parameters. You passed them as you needed, you found specific ones such as description and metadata
Now what is the parameter I told you to use earlier in the thread?

strange tiger
#

like this?
const pyInt = await stripe.paymentIntents.create({
amount,
currency,
description,
payment_method_types,
capture_method: automatic,
metadata: meta
})

#

I see, thanks, I'll try it out

unkempt comet
#

it's a string so capture_method: 'automatic' but yes

strange tiger
#

I wished you were the people who told me to capture charged.updated.

#

anyway, thanks a lot

unkempt comet
#

I mean using charge.updated is the right way. That's what I tell dozens of developers here. But you are clearly quite lost with the integration and your code overall so that alternative works, it's just not the best one

strange tiger
#

To confirm: use this parameter capture_method: 'automatic' , we don't need to capture charge.updated, which is the best way, right?

#

I mean for capture the StripeFee in PaymentIntent succeed response