Verify an App Store Transaction Receipt "Apple server verifies the validity of a transaction receipt for an app shop"

Source: Internet
Author: User
Tags http post grails

Transferred from: http://blog.csdn.net/saindy5828/article/details/6414014

1, from the transaction transactionreceipt attribute received data, and Base64 encoding;

2, create JSON object, dictionary format, single key value pair, the key name is "Receiptdata", the value is the last encoded data, the effect:

{"Receipt-data": "Base64 Data after encoding}"

3. Send an HTTP POST request to send the data to the app Store with the address: Https://buy.itunes.apple.com/verifyReceipt

4. The return value of the APP store is also a JSON-formatted object, including two key-value pairs:

{
"Status": 0,
"Receipt": {...}
}
Description

If the value of status is 0, it means that the receipt is valid, otherwise it is invalid.

1  2 3  Public intVerifyreceipt (byte[] receipt) {4        intStatus =-1;5 6         //The the URL of the REST WebService in ITunes App Store7URL url =NewURL ("Https://buy.itunes.apple.com/verifyReceipt");8 9         //Make connection, use post modeTenHttpsurlconnection connection =(httpsurlconnection) url.openconnection (); OneConnection.setrequestmethod ("POST"); AConnection.setdooutput (true); -Connection.setallowuserinteraction (false); -  the         //Encode the binary receipt data into Base -         //Here I ' m using org.apache.commons.codec.binary.Base64 as a encoder, since Commons-codec is already in Grails Classpat H -BASE64 encoder =NewBase64 (); -String Encodedreceipt =NewString (Encoder.encode (receipt)); +  -         //Create a JSON query object +         //Here I ' m using Grails ' Org.codehaus.groovy.grails.web.json.JSONObject AMap map =NewHashMap (); atMap.put ("Receipt-data", encodedreceipt); -Jsonobject Jsonobject =Newjsonobject (map); -  -         //Write The JSON query object to the connection output stream -PrintStream PS =NewPrintStream (Connection.getoutputstream ()); - Ps.print (jsonobject.tostring ()); in ps.close (); -  to         //Call the service +BufferedReader br =NewBufferedReader (NewInputStreamReader (Connection.getinputstream ())); -         //Extract Response the String str; *StringBuffer SB =NewStringBuffer (); $          while(str = br.readline ())! =NULL) {Panax Notoginseng sb.append (str); -Sb.append ("/n"); the         } + br.close (); AString response =sb.tostring (); the  +         //Deserialize Response -Jsonobject result =NewJsonobject (response); $Status = Result.getint ("status"); $         if(Status = = 0) { -             //Provide content -}Else { the             //signal Error, throw an exception, do your stuff honey! -         }Wuyi  the         returnstatus; -  Wu  -}

The authentication can also take the HTTP protocol: HttpClient, download httpcomponents-client, and then import several jar packages in lib.

This is also a way to no longer make redundant explanations. The so-called roads Pass "apples".

Verify an App Store Transaction Receipt "Apple server verifies the validity of a transaction receipt for an app shop"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.