Create checkout session with multiple items on Stripe using Java

Viewed 255

I'm getting this error when I click on the URL of the created session:

"Something went wrong You might be having a network connection problem, or the payment provider cannot be reached at the moment."

Code:


 Map<String, Object> paramsPriceIdList1 = new HashMap<>();
        paramsPriceIdList.put2("price", price_id1);
        paramsPriceIdList.put2("quantity", 2);

Map<String, Object> paramsPriceIdList2 = new HashMap<>();
        paramsPriceIdList.put2("price", price_id2);
        paramsPriceIdList.put2("quantity", 2);

List<Object> lineItems = new ArrayList<>();
        lineItems.add(paramsPriceIdList1);
        lineItems.add(paramsPriceIdList2);

Map<String, Object> params = new HashMap<>();
        params.put(
                "success_url",
                "https://example.com/success"
        );
        params.put(
                "cancel_url",
                "https://example.com/cancel"
        );
        params.put("line_items", lineItems);
        params.put("mode", "subscription");

        Session session = Session.create(params);
        System.out.println(session);

If I try to use only 1 item, it works.

1 Answers

I think your variable names are causing the problem. For example, you declare paramsPriceIdList1 but then never add anything to it, and then you have paramsPriceIdList which was never declared. You also have methods that don't exist, like put2().

If you look at the error logs on your server you should see details about these errors.

I cleaned up the variable and method names and this code should work as expected:

Map<String, Object> paramsPriceIdList1 = new HashMap<>();
paramsPriceIdList1.put("price", price_id1);
paramsPriceIdList1.put("quantity", 2);

Map<String, Object> paramsPriceIdList2 = new HashMap<>();
paramsPriceIdList2.put("price", price_id2);
paramsPriceIdList2.put("quantity", 2);

List<Object> lineItems = new ArrayList<>();
lineItems.add(paramsPriceIdList1);
lineItems.add(paramsPriceIdList2);

Map<String, Object> params = new HashMap<>();
params.put(
        "success_url",
        "https://example.com/success"
);
params.put(
        "cancel_url",
        "https://example.com/cancel"
);
params.put("line_items", lineItems);
params.put("mode", "subscription");

Session session = Session.create(params);
System.out.println(session);
Related