Java和AppStore收据验证 [英] Java and AppStore receipt verification

查看:402
本文介绍了Java和AppStore收据验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试验证服务器端的付款收据。
我得到 {status:21002,exception:java.lang.IllegalArgumentException} 作为回报

I am trying to verify a payment receipt on server side. I am getting a {"status":21002, "exception":"java.lang.IllegalArgumentException"} in return

以下是代码:

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

我的收据如下:

{\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}

带有BASE64编码收据的收据数据如下所示:

Receipt data with a BASE64 encoded receipt looks like this:

{"receipt-data" : "[Block_of_chars_76x40+44=3084_chars_total]"}

有人有想法或示例代码我如何从收据字符串中回复JSON,提到此处

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

这篇关于Java和AppStore收据验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆