apple java验证 支付_Java和AppStore收据验证

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值