Search in sources :

Example 1 with InvalidRequestException

use of com.stripe.exception.InvalidRequestException in project stripe-java by stripe.

the class LiveStripeResponseGetter method flattenParamsValue.

private static List<Parameter> flattenParamsValue(Object value, String keyPrefix) throws InvalidRequestException {
    List<Parameter> flatParams = new LinkedList<Parameter>();
    if (value instanceof Map<?, ?>) {
        flatParams = flattenParamsMap((Map<String, Object>) value, keyPrefix);
    } else if (value instanceof List<?>) {
        flatParams = flattenParamsList((List<Object>) value, keyPrefix);
    } else if (value instanceof Object[]) {
        flatParams = flattenParamsArray((Object[]) value, keyPrefix);
    } else if ("".equals(value)) {
        throw new InvalidRequestException("You cannot set '" + keyPrefix + "' to an empty string. " + "We interpret empty strings as null in requests. " + "You may set '" + keyPrefix + "' to null to delete the property.", keyPrefix, null, null, 0, null);
    } else if (value == null) {
        flatParams = new LinkedList<Parameter>();
        flatParams.add(new Parameter(keyPrefix, ""));
    } else {
        flatParams = new LinkedList<Parameter>();
        flatParams.add(new Parameter(keyPrefix, value.toString()));
    }
    return flatParams;
}
Also used : StripeObject(com.stripe.model.StripeObject) InvalidRequestException(com.stripe.exception.InvalidRequestException) HashMap(java.util.HashMap) Map(java.util.Map) LinkedList(java.util.LinkedList)

Example 2 with InvalidRequestException

use of com.stripe.exception.InvalidRequestException in project stripe-java by stripe.

the class TransferTest method testTransferCancel.

@Test
public void testTransferCancel() throws StripeException {
    Transfer created = Transfer.create(getTransferParams());
    try {
        // we expect an InvalidRequestException here (caught by JUnit),
        // because in test mode, transfers are automatically sent.
        created.cancel(RequestOptions.getDefault());
        Assert.fail();
    } catch (InvalidRequestException ire) {
    // do nothing
    }
}
Also used : Transfer(com.stripe.model.Transfer) InvalidRequestException(com.stripe.exception.InvalidRequestException) BaseStripeFunctionalTest(com.stripe.BaseStripeFunctionalTest) Test(org.junit.Test)

Example 3 with InvalidRequestException

use of com.stripe.exception.InvalidRequestException in project stripe-java by stripe.

the class LiveStripeResponseGetter method getMultipartStripeResponse.

private static StripeResponse getMultipartStripeResponse(APIResource.RequestMethod method, String url, Map<String, Object> params, RequestOptions options) throws InvalidRequestException, APIConnectionException, APIException {
    if (method != APIResource.RequestMethod.POST) {
        throw new InvalidRequestException("Multipart requests for HTTP methods other than POST " + "are currently not supported.", null, null, null, 0, null);
    }
    java.net.HttpURLConnection conn = null;
    try {
        conn = createStripeConnection(url, options);
        String boundary = MultipartProcessor.getBoundary();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", String.format("multipart/form-data; boundary=%s", boundary));
        MultipartProcessor multipartProcessor = null;
        try {
            multipartProcessor = new MultipartProcessor(conn, boundary, APIResource.CHARSET);
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                if (value instanceof File) {
                    File currentFile = (File) value;
                    if (!currentFile.exists()) {
                        throw new InvalidRequestException("File for key " + key + " must exist.", null, null, null, 0, null);
                    } else if (!currentFile.isFile()) {
                        throw new InvalidRequestException("File for key " + key + " must be a file and not a directory.", null, null, null, 0, null);
                    } else if (!currentFile.canRead()) {
                        throw new InvalidRequestException("Must have read permissions on file for key " + key + ".", null, null, null, 0, null);
                    }
                    multipartProcessor.addFileField(key, currentFile);
                } else {
                    // We only allow a single level of nesting for params
                    // for multipart
                    multipartProcessor.addFormField(key, (String) value);
                }
            }
        } finally {
            if (multipartProcessor != null) {
                multipartProcessor.finish();
            }
        }
        // trigger the request
        int responseCode = conn.getResponseCode();
        String responseBody;
        Map<String, List<String>> headers;
        if (responseCode >= 200 && responseCode < 300) {
            responseBody = getResponseBody(conn.getInputStream());
        } else {
            responseBody = getResponseBody(conn.getErrorStream());
        }
        headers = conn.getHeaderFields();
        return new StripeResponse(responseCode, responseBody, headers);
    } 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.", Stripe.getApiBase(), e.getMessage()), e);
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) APIConnectionException(com.stripe.exception.APIConnectionException) IOException(java.io.IOException) InvalidRequestException(com.stripe.exception.InvalidRequestException) StripeObject(com.stripe.model.StripeObject) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File)

Example 4 with InvalidRequestException

use of com.stripe.exception.InvalidRequestException in project stripe-java by stripe.

the class OAuth method authorizeURL.

/**
 * Generates a URL to Stripe's OAuth form.
 *
 * @param params  the parameters to include in the URL.
 * @param options the request options.
 * @return the URL to Stripe's OAuth form.
 */
public static String authorizeURL(Map<String, Object> params, RequestOptions options) throws AuthenticationException, InvalidRequestException {
    final String base = Stripe.getConnectBase();
    params.put("client_id", getClientId(params, options));
    if (params.get("response_type") == null) {
        params.put("response_type", "code");
    }
    String query;
    try {
        query = LiveStripeResponseGetter.createQuery(params);
    } catch (UnsupportedEncodingException e) {
        throw new InvalidRequestException("Unable to encode parameters to " + APIResource.CHARSET + ". Please contact support@stripe.com for assistance.", null, null, null, 0, e);
    }
    String url = base + "/oauth/authorize?" + query;
    return url;
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) InvalidRequestException(com.stripe.exception.InvalidRequestException)

Aggregations

InvalidRequestException (com.stripe.exception.InvalidRequestException)4 StripeObject (com.stripe.model.StripeObject)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 BaseStripeFunctionalTest (com.stripe.BaseStripeFunctionalTest)1 APIConnectionException (com.stripe.exception.APIConnectionException)1 Transfer (com.stripe.model.Transfer)1 File (java.io.File)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HttpURLConnection (java.net.HttpURLConnection)1 List (java.util.List)1 Test (org.junit.Test)1