Java and AppStore receipt verification
- by user1672461
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(_productionUriStr);
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:
Blockquote
{"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: [http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/StoreKitGuide/VerifyingStoreReceipts/VerifyingStoreReceipts.html#//apple_ref/doc/uid/TP40008267-CH104-SW1]?
Thank you