I am trying to verify a payment receipt on server side.
I am getting a {"status":21002, "exception":"java.lang.IllegalArgumentException"} in return
Here is the code:
private final static String _sandboxUriStr = "https://sandbox.itunes.apple.com/verifyReceipt";
public static void processPayment(final String receipt) throws SystemException
{
final BASE64Encoder encoder = new BASE64Encoder();
final String receiptData = encoder.encode(receipt.getBytes());
final String jsonData = "{\"receipt-data\" : \"" + receiptData + "\"}";
System.out.println(receipt);
System.out.println(jsonData);
try
{
final URL url = new URL(_sandboxUriStr);
final HttpURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
final OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(jsonData);
wr.flush();
// Get the response
final BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null)
{
System.out.println(line);
}
wr.close();
rd.close();
}
catch (IOException e)
{
throw new SystemException("Error when trying to send request to '%s', %s", _sandboxUriStr, e.getMessage());
}
}
My receipt looks like this:
{\n\t"signature" = "[exactly_1320_characters]";\n\t"purchase-info" =
"[exactly_868_characters]";\n\t"environment" = "Sandbox";\n\t"pod" =
"100";\n\t"signing-status" = "0";\n}
Receipt data with a BASE64 encoded receipt looks like this:
{"receipt-data" : "[Block_of_chars_76x40+44=3084_chars_total]"}
Does someone have an Idea, or sample code how can I get from receipt string to reply JSON, mentioned here?
解决方案CloseableHttpClient client = HttpClients.createDefault();
JSONObject requestData = new JSONObject();
requestData.put("receipt-data", recept);
requestData.put("password", password);
HttpPost httpPost = new HttpPost("https://sandbox.itunes.apple.com/verifyReceipt");
StringEntity entity = new StringEntity(requestData.toString());
httpPost.setEntity(entity);
httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
CloseableHttpResponse response = client.execute(httpPost);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
response.close();