简体   繁体   中英

Create checkout session with multiple items on Stripe using Java

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM