Skip to content
Merged
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
31 changes: 31 additions & 0 deletions src/main/java/com/stripe/Stripe.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.stripe;

import java.net.PasswordAuthentication;
import java.net.Proxy;

public abstract class Stripe {
public static final String UPLOAD_API_BASE = "https://uploads.stripe.com";
public static final String LIVE_API_BASE = "https://api.stripe.com";
Expand All @@ -8,6 +11,8 @@ public abstract class Stripe {
public static volatile String apiVersion;

private static volatile String apiBase = LIVE_API_BASE;
private static volatile Proxy connectionProxy = null;
private static volatile PasswordAuthentication proxyCredential = null;


/**
Expand All @@ -21,4 +26,30 @@ public static void overrideApiBase(final String overriddenApiBase) {
public static String getApiBase() {
return apiBase;
}

/**
* Set proxy to tunnel all Stripe connections
*
* @param proxy proxy host and port setting
*/
public static void setConnectionProxy(final Proxy proxy) {
connectionProxy = proxy;
}

public static Proxy getConnectionProxy() {
return connectionProxy;
}

/**
* Provide credential for proxy authorization if required
*
* @param auth proxy required userName and password
*/
public static void setProxyCredential(final PasswordAuthentication auth) {
proxyCredential = auth;
}

public static PasswordAuthentication getProxyCredential() {
return proxyCredential;
}
}
20 changes: 16 additions & 4 deletions src/main/java/com/stripe/net/LiveStripeResponseGetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLEncoder;
import java.net.URLStreamHandler;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -118,7 +119,18 @@ private static java.net.HttpURLConnection createStripeConnection(
} else {
stripeURL = new URL(url);
}
java.net.HttpURLConnection conn = (java.net.HttpURLConnection) stripeURL.openConnection();
HttpURLConnection conn;
if (Stripe.getConnectionProxy() != null) {
conn = (HttpURLConnection) stripeURL.openConnection(Stripe.getConnectionProxy());
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return Stripe.getProxyCredential();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is it okay for PasswordAuthentication to return nil?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

yes, the default impl is returning a null PasswordAuthentication. and i've verified.

scala> import java.net._
import java.net._
scala> import com.stripe._
import com.stripe._
scala> import com.stripe.model._
import com.stripe.model._
scala> val m = new java.util.HashMap[String, Object]
m: java.util.HashMap[String,Object] = {}
scala> m.put("description", "xbwu.enable.proxy.test")
res0: Object = null
scala> Stripe.apiKey="sk_test_..."
Stripe.apiKey: String = sk_test_...
scala> Stripe.setConnectionProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", 3128)))
scala> Customer.create(m)
com.stripe.exception.APIConnectionException: IOException during API request to Stripe (https://api.stripe.com): Unable to tunnel through proxy. Proxy returns "HTTP/1.1 407 Proxy Authentication Required" Please check your internet connection and try again. If this problem persists,you should check Stripe's service status at https://twitter.com/stripestatus, or let us know at support@stripe.com.
scala> Stripe.setProxyCredential(new PasswordAuthentication("payment", "......".toCharArray))
scala> Customer.create(m)
res12: com.stripe.model.Customer =
<com.stripe.model.Customer@1074821682 id=cus_6pcgy8R2GZ0D7w> JSON: {......}
scala> Stripe.setProxyCredential(null)
scala> Stripe.setConnectionProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.199.199", 3128)))
scala> Customer.create(m)
res29: com.stripe.model.Customer =
<com.stripe.model.Customer@589773472 id=cus_6pclJkgE5yM3N6> JSON: {......}

in my environment, localhost:3128 is tunneling to a proxy server which requires authorization, while 192.168.199.199:3128 is a proxy server doesn't need auth

}
});
} else {
conn = (HttpURLConnection) stripeURL.openConnection();
}
conn.setConnectTimeout(30 * 1000);
conn.setReadTimeout(80 * 1000);
conn.setUseCaches(false);
Expand All @@ -140,7 +152,7 @@ private static String formatURL(String url, String query) {
}

private static java.net.HttpURLConnection createGetConnection(
String url, String query, RequestOptions options) throws IOException {
String url, String query, RequestOptions options) throws IOException {
String getURL = formatURL(url, query);
java.net.HttpURLConnection conn = createStripeConnection(getURL, options);
conn.setRequestMethod("GET");
Expand Down