how to retrieve the Charges from PaymentIntent object in stripe-java:24.0.0

336 views
Skip to first unread message

Gaurav Singh

unread,
Mar 28, 2024, 3:38:44 AMMar 28
to Stripe API Discussion

Hello everyone,

We recently upgraded our stripe-java version from 20.45.0 to 24.0.0 to utilize the new session expiration feature. However, upon upgrading, we encountered a compilation error. Please see the attached screenshot for reference.

Screenshot 2024-03-28 at 11.01.22 AM.png

Here is the snippet of code where the error occurred:

BalanceTransaction bt = paymentIntent.getCharges().getData().get(0).getBalanceTransactionObject();

Any assistance in resolving this issue would be greatly appreciated.

Thank you.

Gaurav Singh

unread,
Mar 28, 2024, 5:16:12 AMMar 28
to Stripe API Discussion, Gaurav Singh
I can retrieve the BalanceTransaction from PaymentIntent by using the latest_charge property of the PaymentIntent object, now seeing the PaymentIntent details is coming as null when we retrieve the Session object.
My goal is to retrieve the Charge/BalanceTransaction details from the Session object, could you please assist how we can retrieve them with stripe-java:24.0.0 version.
Attached inline screen:
image.png


I tried retrieval with expand parameter but the issue remains the same
Below is snippet of code:

        List<String> expandList = new ArrayList<>();
        expandList.add("payment_intent");
        expandList.add("payment_intent.charges.data.balance_transaction");

        Map<String, Object> params = new HashMap<>();
        params.put("expand", expandList);
       
        Session session = Session.retrieve("cs_test_a1E75LlDC23F9Dz0I8ZlTPSc9FZoh0av3BooXPT3ADJmX9woWuwuxPB45y", params, null);
        PaymentIntent paymentIntent = session.getPaymentIntentObject();
        Charge charge = paymentIntent.getLatestChargeObject();
        BalanceTransaction bt = charge.getBalanceTransactionObject();
        BigDecimal netAmount = valueOf((bt.getAmount() - bt.getFee()) / 100.0);

Remi Jannel

unread,
Mar 28, 2024, 6:40:56 AMMar 28
to Stripe API Discussion, tosg...@gmail.com
Hello,

I'm re-posting from a different email as it looks like my response didn't get to the list earlier sorry. I'll combine a reply to both questions together. You might get my earlier answers later today which might be confusing!

When you upgrade stripe-java to a new major version, it's important to go through the changelog [1] and the migration guide(s) [2] associated with those new versions. This will list all the important breaking changes that you need to deal with as you upgrade.

If you look at the migration guide for 22.* [3], you can see that the `Charges` property was replaced with the `LatestCharge` property that is the id `ch_123` of the most recent Charge on the PaymentIntent. This is related to the API version 2022-11-15 that introduced multiple breaking changes [4] including this one.

It's not possible to get the Charge's BalanceTransaction id directly anymore as we only return a string by default. What you can do is use our Expand feature [5] though which lets you turn that Charge id into the full Charge object when you retrieve the PaymentIntent. If you are not familiar with that feature I recommend watching this video [6] which covers it in detail.

Since you are jumping multiple major versions at once, they each have their own set of breaking changes. For your second question the change is related to the API version 2022-08-01 where one of the breaking changes [7] says this:

A PaymentIntent is no longer created during Checkout Session creation in payment mode. Instead, a PaymentIntent will be created when the Session is confirmed.

So you won't get a PaymentIntent id on creation anymore. What you can do is use `payment_intent_data[metadata]` parameter [8] to pass your own custom id for example to help with reconciliation.

I hope this solves your problem but if you have any other question please reach out to our support team for 1:1 help at https://support.stripe.com/contact

Best,
Remi

[1] https://github.com/stripe/stripe-java/blob/master/CHANGELOG.md
[2] https://github.com/stripe/stripe-java/wiki/
[3] https://github.com/stripe/stripe-java/wiki/Migration-guide-for-v22
[4] https://docs.stripe.com/upgrades#2022-11-15
[5] https://stripe.com/docs/expand
[6] https://www.youtube.com/watch?v=m8Vj_CEWyQc
[7] https://docs.stripe.com/upgrades#2022-08-01
[8] https://docs.stripe.com/api/checkout/sessions/create#create_checkout_session-payment_intent_data-metadata

Gaurav Singh

unread,
Mar 28, 2024, 6:57:23 AMMar 28
to Stripe API Discussion, Remi Jannel
Thank you, Remi, for your thorough advice. Can you recommend a more cautious migration approach that minimizes disruption? My aim is to incorporate the session expiration feature. Could you advise on which version to begin with instead of immediately upgrading to 24.0.0?

Remi Jannel

unread,
Mar 28, 2024, 7:13:38 AMMar 28
to Stripe API Discussion, tosg...@gmail.com
It looks like the `after_expiration` property shipped in stripe-java 20.72.0 [1]. This is on the same major version that you are using so you can upgrade to this one without any breaking changes and that might be the easiest for you.

I'd still recommend upgrading to the latest version in the future as you'd get a lot of new features and overall cleanup though!

Best,
Remi



Reply all
Reply to author
Forward
0 new messages