From 4e27c66878585d5a46d2548fa4bd9bb68b933f62 Mon Sep 17 00:00:00 2001 From: David Brownman Date: Tue, 4 Feb 2025 16:45:15 -0800 Subject: [PATCH 1/2] construct fake response during webhook parsing --- src/main/java/com/stripe/net/Webhook.java | 7 +++++++ src/test/java/com/stripe/net/WebhookTest.java | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/main/java/com/stripe/net/Webhook.java b/src/main/java/com/stripe/net/Webhook.java index 7b5a34febf2..6e05913980c 100644 --- a/src/main/java/com/stripe/net/Webhook.java +++ b/src/main/java/com/stripe/net/Webhook.java @@ -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; @@ -72,6 +73,12 @@ 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; } diff --git a/src/test/java/com/stripe/net/WebhookTest.java b/src/test/java/com/stripe/net/WebhookTest.java index 175f0cd5feb..c6a9c89aa77 100644 --- a/src/test/java/com/stripe/net/WebhookTest.java +++ b/src/test/java/com/stripe/net/WebhookTest.java @@ -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; @@ -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()); + } } From 5fb3e0dc84038536732892404344f1210c041b7c Mon Sep 17 00:00:00 2001 From: David Brownman Date: Tue, 4 Feb 2025 16:51:09 -0800 Subject: [PATCH 2/2] formatting --- src/main/java/com/stripe/net/Webhook.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/stripe/net/Webhook.java b/src/main/java/com/stripe/net/Webhook.java index 6e05913980c..ea67ea34362 100644 --- a/src/main/java/com/stripe/net/Webhook.java +++ b/src/main/java/com/stripe/net/Webhook.java @@ -73,7 +73,8 @@ 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 + // 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(