Skip to content

Commit 7c99196

Browse files
Ensure getRawJsonObject returns data for constructed webhooks (#1946)
* construct fake response during webhook parsing * formatting
1 parent dfafa65 commit 7c99196

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/main/java/com/stripe/net/Webhook.java

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.security.NoSuchAlgorithmException;
1010
import java.time.Clock;
1111
import java.util.ArrayList;
12+
import java.util.Collections;
1213
import java.util.List;
1314
import javax.crypto.Mac;
1415
import javax.crypto.spec.SecretKeySpec;
@@ -72,6 +73,13 @@ public static Event constructEvent(
7273
StripeObject.deserializeStripeObject(
7374
payload, Event.class, ApiResource.getGlobalResponseGetter());
7475
Signature.verifyHeader(payload, sigHeader, secret, tolerance, clock);
76+
// StripeObjects source their raw JSON object from their last response, but constructed webhooks
77+
// don't have that
78+
// in order to make the raw object available on parsed events, we fake the response.
79+
if (event.getLastResponse() == null) {
80+
event.setLastResponse(
81+
new StripeResponse(200, HttpHeaders.of(Collections.emptyMap()), payload));
82+
}
7583

7684
return event;
7785
}

src/test/java/com/stripe/net/WebhookTest.java

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.stripe.net;
22

3+
import static org.junit.Assert.assertNotNull;
34
import static org.junit.jupiter.api.Assertions.assertEquals;
45
import static org.junit.jupiter.api.Assertions.assertThrows;
56
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -325,4 +326,13 @@ public void testStripeClientConstructEventWithTolerance()
325326

326327
Mockito.verify(responseGetter).request(Mockito.any(), Mockito.any());
327328
}
329+
330+
@Test
331+
public void testConstructEventWithRawJson()
332+
throws StripeException, NoSuchAlgorithmException, InvalidKeyException {
333+
334+
final Event event = Webhook.constructEvent(payload, generateSigHeader(), secret);
335+
336+
assertNotNull(event.getRawJsonObject());
337+
}
328338
}

0 commit comments

Comments
 (0)