2012-09-14 13 views
6

मैं सर्वर पक्ष पर भुगतान रसीद सत्यापित करने का प्रयास कर रहा हूं।जावा और ऐपस्टोर रसीद सत्यापन

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

कोशिश कुछ (। या जो कुछ भी वे उम्मीद कर रहे हैं मैं json संभालने कर रहा हूँ)

+0

धन्यवाद, इन्हें जोड़ने का प्रयास किया, काम नहीं किया। एक ही त्रुटि: {"स्थिति": 21002, "अपवाद": "java.lang.IllegalArgumentException"} – Vetal

1

21002: समस्या जावा अंदर Base64 एन्कोडिंग पर था। जब मैं आईओएस के अंदर एन्कोडिंग करता हूं और जावा में किसी भी एन्कोडिंग के बिना सर्वर से अनुरोध के रूप में उपयोग करता हूं, तो यह काम करता है।

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(); 
संबंधित मुद्दे