Skip to content

Commit

Permalink
Add STRIPE-ACCOUNT header support
Browse files Browse the repository at this point in the history
  • Loading branch information
danwang-stripe committed Mar 12, 2015
1 parent 9c78fed commit ece8a0d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/stripe/net/APIResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ static Map<String, String> getHeaders(RequestOptions options) {
if (options.getIdempotencyKey() != null) {
headers.put("Idempotency-Key", options.getIdempotencyKey());
}
if (options.getStripeAccount() != null) {
headers.put("Stripe-Account", options.getStripeAccount());
}
return headers;
}

Expand Down
43 changes: 39 additions & 4 deletions src/main/java/com/stripe/net/RequestOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@

public class RequestOptions {
public static RequestOptions getDefault() {
return new RequestOptions(Stripe.apiKey, Stripe.apiVersion, null);
return new RequestOptions(Stripe.apiKey, Stripe.apiVersion, null, null);
}

private final String apiKey;
private final String stripeVersion;
private final String idempotencyKey;
private final String stripeAccount;

private RequestOptions(String apiKey, String stripeVersion, String idempotencyKey) {
private RequestOptions(String apiKey, String stripeVersion, String idempotencyKey, String stripeAccount) {
this.apiKey = apiKey;
this.stripeVersion = stripeVersion;
this.idempotencyKey = idempotencyKey;
this.stripeAccount = stripeAccount;
}

public String getApiKey() {
Expand All @@ -29,6 +31,10 @@ public String getIdempotencyKey() {
return idempotencyKey;
}

public String getStripeAccount() {
return stripeAccount;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down Expand Up @@ -62,13 +68,14 @@ public static RequestOptionsBuilder builder() {
}

public RequestOptionsBuilder toBuilder() {
return new RequestOptionsBuilder().setApiKey(this.apiKey).setStripeVersion(this.stripeVersion);
return new RequestOptionsBuilder().setApiKey(this.apiKey).setStripeVersion(this.stripeVersion).setStripeAccount(this.stripeAccount);
}

public static final class RequestOptionsBuilder {
private String apiKey;
private String stripeVersion;
private String idempotencyKey;
private String stripeAccount;

public RequestOptionsBuilder() {
this.apiKey = Stripe.apiKey;
Expand Down Expand Up @@ -113,8 +120,25 @@ public String getIdempotencyKey() {
return this.idempotencyKey;
}

public String getStripeAccount() {
return this.stripeAccount;
}

public RequestOptionsBuilder setStripeAccount(String stripeAccount) {
this.stripeAccount = stripeAccount;
return this;
}

public RequestOptionsBuilder clearStripeAccount() {
return setStripeAccount(null);
}

public RequestOptions build() {
return new RequestOptions(normalizeApiKey(this.apiKey), normalizeStripeVersion(this.stripeVersion), normalizeIdempotencyKey(this.idempotencyKey));
return new RequestOptions(
normalizeApiKey(this.apiKey),
normalizeStripeVersion(this.stripeVersion),
normalizeIdempotencyKey(this.idempotencyKey),
normalizeStripeAccount(this.stripeAccount));
}
}

Expand Down Expand Up @@ -156,6 +180,17 @@ private static String normalizeIdempotencyKey(String idempotencyKey) {
return normalized;
}

private static String normalizeStripeAccount(String stripeAccount) {
if (stripeAccount == null) {
return null;
}
String normalized = stripeAccount.trim();
if (normalized.isEmpty()) {
throw new InvalidRequestOptionsException("Empty stripe account specified!");
}
return normalized;
}

public static class InvalidRequestOptionsException extends RuntimeException {
public InvalidRequestOptionsException(String message) {
super(message);
Expand Down

0 comments on commit ece8a0d

Please sign in to comment.