Skip to content

Commit

Permalink
Ensure getRawJsonObject returns data for constructed webhooks (#1946)
Browse files Browse the repository at this point in the history
* construct fake response during webhook parsing

* formatting
  • Loading branch information
xavdid-stripe authored Feb 5, 2025
1 parent dfafa65 commit 7c99196
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/main/java/com/stripe/net/Webhook.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.security.NoSuchAlgorithmException;
import java.time.Clock;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
Expand Down Expand Up @@ -72,6 +73,13 @@ public static Event constructEvent(
StripeObject.deserializeStripeObject(
payload, Event.class, ApiResource.getGlobalResponseGetter());
Signature.verifyHeader(payload, sigHeader, secret, tolerance, clock);
// StripeObjects source their raw JSON object from their last response, but constructed webhooks
// don't have that
// in order to make the raw object available on parsed events, we fake the response.
if (event.getLastResponse() == null) {
event.setLastResponse(
new StripeResponse(200, HttpHeaders.of(Collections.emptyMap()), payload));
}

return event;
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/com/stripe/net/WebhookTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.stripe.net;

import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -325,4 +326,13 @@ public void testStripeClientConstructEventWithTolerance()

Mockito.verify(responseGetter).request(Mockito.any(), Mockito.any());
}

@Test
public void testConstructEventWithRawJson()
throws StripeException, NoSuchAlgorithmException, InvalidKeyException {

final Event event = Webhook.constructEvent(payload, generateSigHeader(), secret);

assertNotNull(event.getRawJsonObject());
}
}

0 comments on commit 7c99196

Please sign in to comment.