#mattia_code

1 messages · Page 1 of 1 (latest)

jade fieldBOT
#

👋 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/1269961899944972439

📝 Have more to share? Add more details, code, screenshots, videos, etc. below.

marble gazelle
#

I get this error: An error occurred: Cannot invoke "com.stripe.model.PaymentIntent.getLatestChargeObject()" because the return value of "com.stripe.model.Invoice.getPaymentIntentObject()" is null

crystal topaz
#

What's the in_xxx ID

marble gazelle
#

in_1PkMVhEv6RqipoTEZI1XVKpD

crystal topaz
marble gazelle
crystal topaz
#

I suspect your instance of invoice that getPaymentIntentObject is called on the field is null. Perhaps invoice is set before it was paid/finalized

marble gazelle
#

The invoice instance is not null, and that portion of the code is definitely called after the invoice is paid.

crystal topaz
#

I never said the invoice instance is null, I said the payment_intent field may be null which would be the case if the var is set before finalization

#

But yeah, this is likely an issue in your code. You'll need to share more code

marble gazelle
#

` @GetMapping("/payments/{invoiceId}/receivedAmount")
public ResponseEntity<String> getInvoiceReceivedAmount(@PathVariable String invoiceId) {
try{
com.stripe.model.Invoice invoice = invoiceService.getStripeInvoiceById(invoiceId);
long amountPaidInCents = invoice.getAmountPaid();

        List<FeeDetail> fees = invoice.getPaymentIntentObject()
                                      .getLatestChargeObject()
                                      .getBalanceTransactionObject()
                                      .getFeeDetails();

        long paidFeesInCents = fees.stream()
                                   .mapToLong(FeeDetail::getAmount)
                                   .sum();

        double receivedAmount = (amountPaidInCents - paidFeesInCents) / 100.0;
        
        JSONObject response = new JSONObject()
            .put("invoice_id",invoice.getId())
            .put("currency",invoice.getCurrency())
            .put("received_amount", receivedAmount);

        return ResponseEntity.ok(response.toString());
    }catch (JSONException e){
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                             .body("JSON error: " + e.getMessage());
    }catch (Exception e){
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                             .body("An error occurred: " + e.getMessage());
    }
}`
marble gazelle
#

This is getStripeInvoiceById

public com.stripe.model.Invoice getStripeInvoiceById(String invoiceId) { return com.stripe.model.Invoice.retrieve(invoiceId); }

crystal topaz
marble gazelle
crystal topaz
marble gazelle
#

Will try, thank you

crystal topaz
#

Otherwise it's just the pi_xxx ID

marble gazelle
#

Now I get this error: the return value of "com.stripe.model.PaymentIntent.getLatestChargeObject()" is null. I imagine I need to expand the charge object as well, but it's still not working

crystal topaz
#

Yep, your expand field in your retrieve requets should be payment_intent.latest_charge

#

Don't know exact Java syntax but those are the API fields you need

marble gazelle
#

oh it's in the payment_intent object, I thought it was under the charge one.

crystal topaz
#

There's no Charge field/object on an Invoice so you do a 'nested' expand on the PI

marble gazelle
#

this confused me

#

I need to expand the balance transaction object as well

crystal topaz
#

Oh, guess I am mistaken. But the getLatestChargeObject wants the latest_charge field on the PI

crystal topaz
marble gazelle
#

Worked like a charm now, thank you!

Do you know if there's a better way to get the net amount of a payment, compared to what I did?

crystal topaz
#

If by net amount you mean minus the fees, then no. You need the balance transaction

marble gazelle
#

I noticed that the Stipe fees may be in different currencies, which makes it a lot harder to properly calculate the net amount.

#

From Stripe dashboard:

Gross amount: 15,40 USD -> 13,78 €

Taxes: 0,03 €
PayPal fees: 0,31 USD
Stripe processing fees: 0,13 €

The net amount is apparently 13,34 EUR but how was it calculated

jade fieldBOT
analog obsidian
#

Hi! I'm taking over from my colleague. Please, give me a moment to catch up.

marble gazelle
#

Sure, thank you!

#

I assume I could just do getBalanceTransactionObject#getNet but this may be expressed in a different currency compared to the invoice currency.

analog obsidian
#

Hm, I see that the PayPal fee is saved in EUR internally - 0.28. Not sure why it is shown in USD on the Dashboard.

marble gazelle
#

So is it just a dashboard issue? Would the API always return ALL the fees in the same currency?

analog obsidian
#

I would suggest checking this for yourself.

marble gazelle
#

Testing it now, will let you know.

#

You were right, it returns everything in eur

#

May I ask why in EUR if the invoice is in USD?

analog obsidian
#

Is your Stripe account balance in EUR?

marble gazelle
#

If I want to convert EUR to USD for example, what exchange rates should I use? I can't know which exchange rates Stripe used at the time of the payment

marble gazelle
analog obsidian
# marble gazelle Yes

The currency conversion happened before the fees were withheld, that's why they're in your account balance's currency.

analog obsidian
#

Let me check.

marble gazelle
#

Appreciated

#

maybe it's the getExchangeRate of the BalanceTransactionObject?

marble gazelle
#

Ok perfect, let me test real quick

#

Seems to work, but is there any way I can check these calculations are indeed correct? Like a way to know how much fees would Stripe have taken from the payment if my account was in USD?

analog obsidian
#

What do you mean by "correct" exactly?

marble gazelle
analog obsidian
#

You could potentially create an account with USD balance and try to create the same Invoice in Test mode.

marble gazelle
#

Thank you.
Does Stripe take an additional fee on currency conversions? Because 15,4 USD is 14,07 EUR, but on the invoice it was set to 13,78

analog obsidian
marble gazelle
#

Alright thank you, much appreciated

analog obsidian
#

Happy to help.