package com.stripe.android.net; import com.baidu.mapapi.UIMsg; import com.stripe.android.exception.APIConnectionException; import com.stripe.android.exception.APIException; import com.stripe.android.exception.AuthenticationException; import com.stripe.android.exception.CardException; import com.stripe.android.exception.InvalidRequestException; import com.stripe.android.exception.PermissionException; import com.stripe.android.exception.RateLimitException; import com.stripe.android.model.Token; import com.stripe.android.net.ErrorParser; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.security.Security; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Scanner; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLSocketFactory; import org.json.JSONException; import org.json.JSONObject; public class StripeApiHandler { public static final String CHARSET = "UTF-8"; private static final String DNS_CACHE_TTL_PROPERTY_NAME = "networkaddress.cache.ttl"; static final String GET = "GET"; public static final String LIVE_API_BASE = "https://api.stripe.com"; static final String POST = "POST"; private static final SSLSocketFactory SSL_SOCKET_FACTORY = new StripeSSLSocketFactory(); public static final String TOKENS = "tokens"; public static final String VERSION = "3.5.0"; @Retention(RetentionPolicy.SOURCE) @interface RestMethod { } public static Token createToken(Map<String, Object> map, RequestOptions requestOptions) throws AuthenticationException, InvalidRequestException, APIConnectionException, CardException, APIException { return requestToken(POST, getApiUrl(), map, requestOptions); } public static Token retrieveToken(RequestOptions requestOptions, String str) throws AuthenticationException, InvalidRequestException, APIConnectionException, APIException { try { return requestToken(GET, getRetrieveTokenApiUrl(str), null, requestOptions); } catch (CardException e) { throw new APIException(e.getMessage(), e.getRequestId(), e.getStatusCode(), e); } } static String createQuery(Map<String, Object> map) throws UnsupportedEncodingException, InvalidRequestException { StringBuilder sb = new StringBuilder(); for (Parameter parameter : flattenParams(map)) { if (sb.length() > 0) { sb.append("&"); } sb.append(urlEncodePair(parameter.key, parameter.value)); } return sb.toString(); } static Map<String, String> getHeaders(RequestOptions requestOptions) { HashMap hashMap = new HashMap(); String apiVersion = requestOptions.getApiVersion(); hashMap.put("Accept-Charset", "UTF-8"); hashMap.put("Accept", "application/json"); hashMap.put("User-Agent", String.format("Stripe/v1 JavaBindings/%s", VERSION)); hashMap.put("Authorization", String.format("Bearer %s", requestOptions.getPublishableApiKey())); String[] strArr = {"os.name", "os.version", "os.arch", "java.version", "java.vendor", "java.vm.version", "java.vm.vendor"}; HashMap hashMap2 = new HashMap(); for (int i = 0; i < 7; i++) { String str = strArr[i]; hashMap2.put(str, System.getProperty(str)); } hashMap2.put("bindings.version", VERSION); hashMap2.put("lang", "Java"); hashMap2.put("publisher", "Stripe"); hashMap.put("X-Stripe-Client-User-Agent", new JSONObject(hashMap2).toString()); if (apiVersion != null) { hashMap.put("Stripe-Version", apiVersion); } if (requestOptions.getIdempotencyKey() != null) { hashMap.put("Idempotency-Key", requestOptions.getIdempotencyKey()); } return hashMap; } static String getApiUrl() { return String.format("%s/v1/%s", LIVE_API_BASE, TOKENS); } static String getRetrieveTokenApiUrl(String str) { return String.format("%s/%s", getApiUrl(), str); } private static String formatURL(String str, String str2) { if (str2 == null || str2.isEmpty()) { return str; } String str3 = "?"; if (str.contains(str3)) { str3 = "&"; } return String.format("%s%s%s", str, str3, str2); } private static HttpURLConnection createGetConnection(String str, String str2, RequestOptions requestOptions) throws IOException { HttpURLConnection createStripeConnection = createStripeConnection(formatURL(str, str2), requestOptions); createStripeConnection.setRequestMethod(GET); return createStripeConnection; } /* JADX WARNING: Removed duplicated region for block: B:12:0x0036 */ private static HttpURLConnection createPostConnection(String str, String str2, RequestOptions requestOptions) throws IOException { OutputStream outputStream; Throwable th; HttpURLConnection createStripeConnection = createStripeConnection(str, requestOptions); createStripeConnection.setDoOutput(true); createStripeConnection.setRequestMethod(POST); createStripeConnection.setRequestProperty("Content-Type", String.format("application/x-www-form-urlencoded;charset=%s", "UTF-8")); try { outputStream = createStripeConnection.getOutputStream(); try { outputStream.write(str2.getBytes("UTF-8")); if (outputStream != null) { outputStream.close(); } return createStripeConnection; } catch (Throwable th2) { th = th2; if (outputStream != null) { outputStream.close(); } throw th; } } catch (Throwable th3) { th = th3; outputStream = null; if (outputStream != null) { } throw th; } } private static HttpURLConnection createStripeConnection(String str, RequestOptions requestOptions) throws IOException { HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(str).openConnection(); httpURLConnection.setConnectTimeout(UIMsg.m_AppUI.MSG_RADAR_SEARCH_RETURN_RESULT); httpURLConnection.setReadTimeout(80000); httpURLConnection.setUseCaches(false); for (Map.Entry<String, String> entry : getHeaders(requestOptions).entrySet()) { httpURLConnection.setRequestProperty(entry.getKey(), entry.getValue()); } if (httpURLConnection instanceof HttpsURLConnection) { ((HttpsURLConnection) httpURLConnection).setSSLSocketFactory(SSL_SOCKET_FACTORY); } return httpURLConnection; } /* JADX WARNING: Removed duplicated region for block: B:15:0x002b A[SYNTHETIC, Splitter:B:15:0x002b] */ /* JADX WARNING: Removed duplicated region for block: B:51:0x0098 */ private static Token requestToken(String str, String str2, Map<String, Object> map, RequestOptions requestOptions) throws AuthenticationException, InvalidRequestException, APIConnectionException, CardException, APIException { String str3; List<String> list; if (requestOptions == null) { return null; } Boolean bool = true; try { str3 = Security.getProperty(DNS_CACHE_TTL_PROPERTY_NAME); try { Security.setProperty(DNS_CACHE_TTL_PROPERTY_NAME, "0"); } catch (SecurityException unused) { } } catch (SecurityException unused2) { str3 = null; bool = false; if (requestOptions.getPublishableApiKey().trim().isEmpty()) { } } if (requestOptions.getPublishableApiKey().trim().isEmpty()) { try { StripeResponse stripeResponse = getStripeResponse(str, str2, map, requestOptions); int responseCode = stripeResponse.getResponseCode(); String responseBody = stripeResponse.getResponseBody(); Map<String, List<String>> responseHeaders = stripeResponse.getResponseHeaders(); if (responseHeaders == null) { list = null; } else { list = responseHeaders.get("Request-Id"); } String str4 = (list == null || list.size() <= 0) ? null : list.get(0); if (responseCode < 200 || responseCode >= 300) { handleAPIError(responseBody, responseCode, str4); } Token parseToken = TokenParser.parseToken(responseBody); if (bool.booleanValue()) { if (str3 == null) { Security.setProperty(DNS_CACHE_TTL_PROPERTY_NAME, "-1"); } else { Security.setProperty(DNS_CACHE_TTL_PROPERTY_NAME, str3); } } return parseToken; } catch (JSONException unused3) { if (bool.booleanValue()) { if (str3 == null) { Security.setProperty(DNS_CACHE_TTL_PROPERTY_NAME, "-1"); } else { Security.setProperty(DNS_CACHE_TTL_PROPERTY_NAME, str3); } } return null; } catch (Throwable th) { if (bool.booleanValue()) { if (str3 == null) { Security.setProperty(DNS_CACHE_TTL_PROPERTY_NAME, "-1"); } else { Security.setProperty(DNS_CACHE_TTL_PROPERTY_NAME, str3); } } throw th; } } else { throw new AuthenticationException("No API key provided. (HINT: set your API key using 'Stripe.apiKey = <API-KEY>'. You can generate API keys from the Stripe web interface. See https://stripe.com/api for details or email support@stripe.com if you have questions.", null, 0); } } private static StripeResponse getStripeResponse(String str, String str2, Map<String, Object> map, RequestOptions requestOptions) throws InvalidRequestException, APIConnectionException, APIException { try { return makeURLConnectionRequest(str, str2, createQuery(map), requestOptions); } catch (UnsupportedEncodingException e) { throw new InvalidRequestException("Unable to encode parameters to UTF-8. Please contact support@stripe.com for assistance.", null, null, 0, e); } } private static List<Parameter> flattenParams(Map<String, Object> map) throws InvalidRequestException { return flattenParamsMap(map, null); } private static List<Parameter> flattenParamsList(List<Object> list, String str) throws InvalidRequestException { LinkedList linkedList = new LinkedList(); String format = String.format("%s[]", str); if (list.isEmpty()) { linkedList.add(new Parameter(str, "")); } else { for (Object obj : list) { linkedList.addAll(flattenParamsValue(obj, format)); } } return linkedList; } private static List<Parameter> flattenParamsMap(Map<String, Object> map, String str) throws InvalidRequestException { LinkedList linkedList = new LinkedList(); if (map == null) { return linkedList; } for (Map.Entry<String, Object> entry : map.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); if (str != null) { key = String.format("%s[%s]", str, key); } linkedList.addAll(flattenParamsValue(value, key)); } return linkedList; } private static List<Parameter> flattenParamsValue(Object obj, String str) throws InvalidRequestException { if (obj instanceof Map) { return flattenParamsMap((Map) obj, str); } if (obj instanceof List) { return flattenParamsList((List) obj, str); } if ("".equals(obj)) { throw new InvalidRequestException("You cannot set '" + str + "' to an empty string. We interpret empty strings as null in requests. You may set '" + str + "' to null to delete the property.", str, null, 0, null); } else if (obj == null) { LinkedList linkedList = new LinkedList(); linkedList.add(new Parameter(str, "")); return linkedList; } else { LinkedList linkedList2 = new LinkedList(); linkedList2.add(new Parameter(str, obj.toString())); return linkedList2; } } private static void handleAPIError(String str, int i, String str2) throws InvalidRequestException, AuthenticationException, CardException, APIException { ErrorParser.StripeError parseError = ErrorParser.parseError(str); if (i != 429) { switch (i) { case 400: throw new InvalidRequestException(parseError.message, parseError.param, str2, Integer.valueOf(i), null); case 401: throw new AuthenticationException(parseError.message, str2, Integer.valueOf(i)); case 402: throw new CardException(parseError.message, str2, parseError.code, parseError.param, parseError.decline_code, parseError.charge, Integer.valueOf(i), null); case 403: throw new PermissionException(parseError.message, str2, Integer.valueOf(i)); case UIMsg.l_ErrorNo.NETWORK_ERROR_404: throw new InvalidRequestException(parseError.message, parseError.param, str2, Integer.valueOf(i), null); default: throw new APIException(parseError.message, str2, Integer.valueOf(i), null); } } else { throw new RateLimitException(parseError.message, parseError.param, str2, Integer.valueOf(i), null); } } private static String urlEncodePair(String str, String str2) throws UnsupportedEncodingException { return String.format("%s=%s", urlEncode(str), urlEncode(str2)); } private static String urlEncode(String str) throws UnsupportedEncodingException { if (str == null) { return null; } return URLEncoder.encode(str, "UTF-8"); } private static StripeResponse makeURLConnectionRequest(String str, String str2, String str3, RequestOptions requestOptions) throws APIConnectionException { HttpURLConnection httpURLConnection; String str4; char c = 65535; HttpURLConnection httpURLConnection2 = null; try { int hashCode = str.hashCode(); if (hashCode != 70454) { if (hashCode == 2461856) { if (str.equals(POST)) { c = 1; } } } else if (str.equals(GET)) { c = 0; } if (c == 0) { httpURLConnection = createGetConnection(str2, str3, requestOptions); } else if (c == 1) { httpURLConnection = createPostConnection(str2, str3, requestOptions); } else { throw new APIConnectionException(String.format("Unrecognized HTTP method %s. This indicates a bug in the Stripe bindings. Please contact support@stripe.com for assistance.", str)); } int responseCode = httpURLConnection.getResponseCode(); if (responseCode < 200 || responseCode >= 300) { str4 = getResponseBody(httpURLConnection.getErrorStream()); } else { str4 = getResponseBody(httpURLConnection.getInputStream()); } StripeResponse stripeResponse = new StripeResponse(responseCode, str4, httpURLConnection.getHeaderFields()); if (httpURLConnection != null) { httpURLConnection.disconnect(); } return stripeResponse; } catch (IOException e) { throw new APIConnectionException(String.format("IOException during API request to Stripe (%s): %s 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.", getApiUrl(), e.getMessage()), e); } catch (Throwable th) { if (0 != 0) { httpURLConnection2.disconnect(); } throw th; } } private static String getResponseBody(InputStream inputStream) throws IOException { String next = new Scanner(inputStream, "UTF-8").useDelimiter("\\A").next(); inputStream.close(); return next; } /* access modifiers changed from: private */ public static final class Parameter { public final String key; public final String value; public Parameter(String str, String str2) { this.key = str; this.value = str2; } } }