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;
}
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
}
}
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();
}
}
}
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;
}
Aggregations