Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion src/main/java/com/stripe/StripeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,50 @@ protected StripeResponseGetter getResponseGetter() {
return responseGetter;
}

/**
* Gets the current StripeContext from the client's configuration.
*
* @return the current StripeContext, or null if none is set
*/
protected String getContext() {
// TODO(major): add getOptions to the StripeResponseGetter interface? that would simplify this
if (!(responseGetter instanceof LiveStripeResponseGetter)) {
return null;
}

LiveStripeResponseGetter liveGetter = (LiveStripeResponseGetter) responseGetter;
StripeResponseGetterOptions options = liveGetter.getOptions();

return options.getStripeContext();
}

/**
* Sets the StripeContext for this client. This updates the client configuration to use the
* provided context for all subsequent requests.
*/
protected void setContext(String context) {
// TODO(major): add getOptions to the StripeResponseGetter interface? that would simplify this
if (!(responseGetter instanceof LiveStripeResponseGetter)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love this and am all ears for a better suggestion!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meh, not much you can do. Seems like the wrong abstractions were put in place (or they made the common Java mistake of creating an interface when there was only ever going to be a single implementation)

throw new IllegalStateException(
"Cannot set context on a StripeClient with a non-Live response getter");
}

LiveStripeResponseGetter liveGetter = (LiveStripeResponseGetter) responseGetter;
StripeResponseGetterOptions options = liveGetter.getOptions();

if (!(options instanceof ClientStripeResponseGetterOptions)) {
throw new IllegalStateException(
"Cannot set context on a StripeClient with non-standard options");
}

ClientStripeResponseGetterOptions clientOptions = (ClientStripeResponseGetterOptions) options;
clientOptions.setStripeContext(context);
}

protected void setContext(StripeContext context) {
setContext(context.toString());
}

/**
* Returns an StripeEvent instance using the provided JSON payload. Throws a JsonSyntaxException
* if the payload is not valid JSON, and a SignatureVerificationException if the signature
Expand Down Expand Up @@ -1073,7 +1117,7 @@ static class ClientStripeResponseGetterOptions extends StripeResponseGetterOptio
private final String stripeAccount;

@Getter(onMethod_ = {@Override})
private final String stripeContext;
private volatile String stripeContext;

ClientStripeResponseGetterOptions(
Authenticator authenticator,
Expand Down Expand Up @@ -1103,6 +1147,15 @@ static class ClientStripeResponseGetterOptions extends StripeResponseGetterOptio
this.stripeAccount = stripeAccount;
this.stripeContext = stripeContext;
}

/**
* Updates the stripe context.
*
* @param stripeContext the new stripe context value
*/
protected void setStripeContext(String stripeContext) {
this.stripeContext = stripeContext;
}
}

/**
Expand Down Expand Up @@ -1412,4 +1465,9 @@ public StripeResponse rawRequest(
public StripeObject deserialize(String rawJson, ApiMode apiMode) throws StripeException {
return StripeObject.deserializeStripeObject(rawJson, this.getResponseGetter(), apiMode);
}

public StripeEventRouter router(
String webhookSecret, StripeEventRouter.UnhandledEventHandler handler) {
return new StripeEventRouter(webhookSecret, this, handler);
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/stripe/StripeContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public StripeContext pop() {
/**
* Converts the context to a string by joining segments with '/'.
*
* @return string representation of the context segments joined by '/', `null` if there are no
* segments (useful for clearing context)
* @return string representation of the context segments joined by '/'. If there are no segments,
* returns an empty string (useful for clearing context).
*/
@Override
public String toString() {
Expand Down
Loading
Loading