I am implimenting a java servlet, running right now on localhost with connect v2. I am succesfully able to enter a card number on my webpage, then receive the locationID using the nonce, however, when I request that the card be charged, I am getting a 400 error with no other inforamtion returned. I should also note, that I am sending the exact JSON that the documentation suggests, with the nonce being updated.... Any suggestion would be greatly appreciated...
String JSONSample = "{\"idempotency_key\": \"74ae1696-b1e3-4328-af6d-f1e04d947a13\",\"shipping_address\": {\"address_line_1\": \"123 Main St\",\"locality\": \"San Francisco\",\"administrative_district_level_1\": \"CA\",\"postal_code\": \"94114\",\"country\": \"US\"},\"billing_address\": {\"address_line_1\": \"500 Electric Ave\",\"address_line_2\": \"Suite 600\",\"administrative_district_level_1\": \"NY\",\"locality\": \"New York\",\"postal_code\": \"10003\",\"country\": \"US\"},\"amount_money\": {\"amount\": 5000,\"currency\": \"USD\"},\"card_nonce\": \"" + nonce +"\",\"reference_id\": \"some optional reference id\",\"note\": \"some optional note\",\"delay_capture\": false}";
String output = "";
try {
String charset = "UTF-8";
String JSONString = chargeObject.toString();
URL url = new URL("https://connect.squareup.com/v2/locations/" + LocationID + "/transactions");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "Bearer sandbox-<...removed...>");
OutputStream os = conn.getOutputStream();
os.write(JSONSample.getBytes(charset));
os.close();
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
logger.info("Output from Server .... \n");
String JSON = "";
//System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
logger.info(output);
JSON += output;
}
conn.disconnect();
return JSON;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
The whole issue turns out to be the idempotency key much be unique...is there a way to get more detailed error messages than just a 400 error, the documentation says it also sends a JSON error package back, however, I am not able to read this in, the stream is null.
Square Community