Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripe Java API - Cannot parse Event object from Webhook

I upgraded to the latest Stripe API (2017-06-05) and noticed that test webhooks being sent to my server worked perfectly, but live ones did not. After looking into it, I noticed that the Event object contains the following in LIVE mode:

  ...
  "pending_webhooks": 1,
  "request": {
    "id": null,
    "idempotency_key": null
  },
  ...

And the following in TEST mode:

  ...
  "pending_webhooks": 1,
  "request": null
  ...

This results in the following error in LIVE mode:

Expected a string but was BEGIN_OBJECT at line 105 column 15 path $.request

Clearly, the error occurs because test webhooks can be "mistaken" as a null String instead of a null object, which is why tests do NOT throw an exception.

The current version of the Stripe client library (5.6.0) has the following code for the EventRequest.class de-serialization:

public EventRequest deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
    throws JsonParseException {
    Gson gson = new GsonBuilder()
        .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
        .create();

    // API versions 2017-05-25 and earlier render `request` as a string
    // instead of a JSON object
    if (json.isJsonPrimitive()) {
        EventRequest request = new EventRequest();
        request.setId(json.getAsString());
        return request;
    }
    else {
        return gson.fromJson(json, typeOfT);
    }
}

So, I'm really not sure of why this issue is happening or how to remedy this, since it is caused internally in the Stripe Java client library. Any help is appreciated!

like image 414
Corey Caplan Avatar asked Jul 12 '26 22:07

Corey Caplan


1 Answers

I know this is an old question, but I ran into a similar problem. The JSON sent by Stripe has a number of properties that are serialized as IDs instead of objects. You need to use the GSON instance built into Stripe to decode it:

com.stripe.net.ApiResource.GSON.fromJson(payload, Event.class)

Where payload is the JSON string.

like image 188
Frank Riccobono Avatar answered Jul 15 '26 10:07

Frank Riccobono



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!