#mattia_code
1 messages · Page 1 of 1 (latest)
👋 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.
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
What's the in_xxx ID
in_1PkMVhEv6RqipoTEZI1XVKpD
payment_intent is most definitely not null: https://dashboard.stripe.com/test/events/evt_1PkMaVEv6RqipoTERbD1ikRX
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
Why the Java Stripe API wrapper is returning null then?
I suspect your instance of invoice that getPaymentIntentObject is called on the field is null. Perhaps invoice is set before it was paid/finalized
The invoice instance is not null, and that portion of the code is definitely called after the invoice is paid.
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
` @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());
}
}`
the var can't be set before finalization, as the code is executed once my API gets called, and I'm calling it on a paid (and thus finalized) invoice as you can see from the invoice id I provided.
This is getStripeInvoiceById
public com.stripe.model.Invoice getStripeInvoiceById(String invoiceId) { return com.stripe.model.Invoice.retrieve(invoiceId); }
Your code doesn't disprove my theory: no way there am I to know that getInvoiceReceivedAmount is only called on a paid invoice
Not sure about what you mean to be honest. I can confirm I tried to call it again, and it's still returning me an error as I've shown here: #1269961899944972439 message
Looking at the SDK code, I think you may need to actually expand the payment_intent field in when you lookup the invoice: https://docs.stripe.com/api/expanding_objects?lang=java
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Will try, thank you
Otherwise it's just the pi_xxx ID
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
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
oh it's in the payment_intent object, I thought it was under the charge one.
There's no Charge field/object on an Invoice so you do a 'nested' expand on the PI
Oh, guess I am mistaken. But the getLatestChargeObject wants the latest_charge field on the PI
That's on the Charge object so another nested expand: payment_intent.latest_charge.balance_transaction
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?
If by net amount you mean minus the fees, then no. You need the balance transaction
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
Hi! I'm taking over from my colleague. Please, give me a moment to catch up.
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.
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.
So is it just a dashboard issue? Would the API always return ALL the fees in the same currency?
I would suggest checking this for yourself.
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?
Is your Stripe account balance in EUR?
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
Yes
The currency conversion happened before the fees were withheld, that's why they're in your account balance's currency.
The FX rate must be available somewhere.
Let me check.
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?
What do you mean by "correct" exactly?
the net amount is expressed in EUR, so I'm using the provided exchange rate to convert that net EUR amount to USD amount, considering the invoice currency is in USD. Is there any way for me to check the fees Stripe would've taken in USD, rather than in EUR? Just to check everything is correct.
You could potentially create an account with USD balance and try to create the same Invoice in Test mode.
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
I believe so, yes. However, I my team doesn't have much expertise on fees, it's better to check with https://support.stripe.com/?contact=true
Alright thank you, much appreciated
Happy to help.