Friday, November 11, 2022

[FIXED] When I try to create a Stripe Customer using java sdk, it is throwing "Invalid Object"

Issue

I am trying to create a Stripe Customer with address and I not able to do so because it ends up throwing invalid object error. If I remove the address I am able to create the customer. But my requirement is to create the customer with his/her address.

This is the code that I am trying to execute.

Address address = new Address();
address.setPostalCode("250532");
address.setState("Maharashtra");
address.setLine2("20/2, Ganesh Street");
address.setCity("Mumbai");
address.setCountry("IN");

Map<String, Object> customerParams = new HashMap<>();

customerParams.put("email", "test@gmail.com");
customerParams.put("name", "Radhey");
customerParams.put("description", "Test Customer");
customerParams.put("address", address);
customerParams.put("source", token);
Customer customer = Customer.create(customerParams);

It is throwing invalid object error on the last line of the code.

Below given is the stacktrace:

com.stripe.exception.InvalidRequestException: Invalid object
    at com.stripe.net.LiveStripeResponseGetter.handleAPIError(LiveStripeResponseGetter.java:645)
    at com.stripe.net.LiveStripeResponseGetter._request(LiveStripeResponseGetter.java:490)
    at com.stripe.net.LiveStripeResponseGetter.request(LiveStripeResponseGetter.java:73)
    at com.stripe.net.APIResource.request(APIResource.java:161)
    at com.stripe.model.Customer.create(Customer.java:380)
    at com.stripe.model.Customer.create(Customer.java:265)
    at stripe.Test.chargeCreditCard(Test.java:51)
    at stripe.Test.main(Test.java:19)

Solution

The solution by @RyanM in the comment section worked for me.

Have you considered attempting to use the new builder syntax that is in the current Stripe docs? It matches with the current Java doc which includes the specifications when providing an Address while creating a Customer https://stripe.dev/stripe-java/com/stripe/param/CustomerCreateParams.Address.html



Answered By - Radhey Patel
Answer Checked By - Terry (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.