2012-09-14 5 views
6

서버 쪽에서 영수증을 확인하려고합니다.Java 및 AppStore 영수증 확인

내 영수증과 같습니다
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()); 
    } 
} 

: base64로 인코딩 된 영수증

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

영수증 데이터는 다음과 같습니다

내가 여기

반환

{"status":21002, "exception":"java.lang.IllegalArgumentException"}을 얻고 것은 코드입니다
{"receipt-data" : "[Block_of_chars_76x40+44=3084_chars_total]"} 

누군가 아이디어 나 샘플 코드가 있습니까? 영수증 문자열에서 JSON에 응답하려면 here을 언급 했습니까?

+0

는 [이 답변]에 따르면 (http://stackoverflow.com/a/13717476/642706) :'당신이 확인을 위해 보내 생성 한 JSON 개체가 올바른 format'에 있지 않습니다. –

답변

0

나는 그 서비스에 익숙하지 않지만 콘텐츠 유형이나 헤더가 적절하지 않은 경우 다른 서비스와 비슷한 오류를 보았습니다.

con.setRequestProperty("Content-Type", "application/json"); 
con.setRequestProperty("Accept", "application/json"); 

같은

시도 뭔가

+0

감사합니다. 추가하지 않으려 고 시도했습니다. 동일한 오류 : { "status": 21002, "exception": "java.lang.IllegalArgumentException"} – Vetal

1

21002 (. 또는 무엇이든 그들이 기대하고 내가 JSON 있으리라 믿고있어) : 문제는 자바 내부 Base64 인코딩 있었다. IOS 내에서 인코딩을 수행하고 Java에서 인코딩하지 않고 서버의 요청으로 사용하면 작동합니다.

switch (status) { 
     case 21000: 
      msg = "The App Store could not read the JSON object you provided"; 
      logger.info("\n 21000 : The App Store could not read the JSON object you provided. "); 

     break; 
    case 21002: 
     msg = "The data in the receipt-data property was malformed."; 
     logger.info("\n 21002 : The data in the receipt-data property was malformed.. "); 
     break; 
    case 21003: 
     msg = "The data in the receipt-data property was malformed."; 
     logger.info("\n 21003 : The receipt could not be authenticated. "); 
     break; 
    case 21004: 
     msg = "TThe shared secret you provided does not match the shared secret on file for your account."; 
     logger.info("\n 21004 : The shared secret you provided does not match the shared secret on file for your account. "); 
     break; 
    case 21005: 
     msg = "The receipt server is not currently available."; 
     logger.info("\n 21005 : The receipt server is not currently available. "); 
     break; 
    case 21006: 
     msg = "This receipt is valid but the subscription has expired. When this status code is returned to your server, the receipt data is also decoded and returned as part of the response."; 
     logger.info("\n 21006 : This receipt is valid but the subscription has expired. When this status code is returned to your server, the receipt data is also decoded and returned as part of the response. "); 
     break; 
    case 21007: 
     msg = "This receipt is a sandbox receipt, but it was sent to the production service for verification."; 
     logger.info("\n 21007 : This receipt is a sandbox receipt, but it was sent to the production service for verification. "); 
     break; 
    case 21008: 
     msg = "This receipt is a production receipt, but it was sent to the sandbox service for verification."; 
     logger.info("\n 21008 : This receipt is a production receipt, but it was sent to the sandbox service for verification. "); 
     break; 

    default: 
     msg = "Active subscription."; 
     logger.info("\n 0 : valid ....Active subscription. "); 
     break; 
    } 
1
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();