Apple AppStore In-app Purchase internal payment verification code

Source: Internet
Author: User

When payment is made within the app, the AppStore server does not proactively notify the app server of the results of the purchase, and we need to proactively request the AppStore to inquire about the status of order information to handle the corresponding delivery logic after purchase.


Apple Response Data entity class (with Getter,setter):

public class Appstorebuyentity {private string Original_purchase_date_pst;private string purchase_date_ms;private String Unique_identifier;private string Original_transaction_id;private string bvrs;private string transaction_id; private string Quantity;private string Unique_vendor_identifier;private string item_id;private string product_id; private string Purchase_date;private string Original_purchase_date;private string purchase_date_pst;private string bid ;p rivate String original_purchase_date_ms;private int status;}

Apple Server Verification Order code:

/** * Apple Server verification * @param receipt Bill */public static string buyappverify (string receipt) {//test URL private static fin     Al String url_verify = "Https://sandbox.itunes.apple.com/verifyReceipt"; Official URL//private static final String url_verify= "Https://buy.itunes.apple.com/verifyReceipt"; Bufferedoutputstream hurlbufous = Null;inputstream is = null; BufferedReader reader = null; Httpsurlconnection conn = null;try {Sslcontext sc = sslcontext.getinstance ("SSL"); Sc.init (null, new trustmanager[] {new T Rustanytrustmanager ()},new java.security.SecureRandom ()); URL console = new URL (url_verify), conn = (httpsurlconnection) console.openconnection (); Conn.setsslsocketfactory ( Sc.getsocketfactory ()); Conn.sethostnameverifier (new Trustanyhostnameverifier ()); Conn.setrequestmethod ("POST"); Conn.setrequestproperty ("Content-type", "Text/json"), Conn.setrequestproperty ("Proxy-connection", "Keep-Alive"); Conn.setdoinput (True); Conn.setdooutput (true); hurlbufous = new Bufferedoutputstream (conn.getoutputstrEAM ()); String str = String.Format (Locale.china, "{\" receipt-data\ ": \" "+ receipt+" \ "}"); Hurlbufous.write (Str.getbytes ()); Hurlbufous.flush (); is = Conn.getinputstream (), reader = new BufferedReader (new InputStreamReader (IS)); String line = null; StringBuffer sb = new StringBuffer (line = Reader.readline ())! = null) {sb.append (line);} return sb.tostring ();} catch (Exception ex) {logger.error ("-----> post to AppStore Error", ex);} finally {try {if (null! = is) {is.close ()} if (null! = hurlbufous) {hurlbufous.close ();} if (null! = Reader) {reader.close ();}} catch (Exception e) {logger.error (e);}} return null;}

TEST:

public static void Main (string[] args) {//i.e. receiptstring Buycode = ""; String result = Appstorepayverify.buyappverify (Buycode);       Jsonobject job = jsonobject.fromobject (result);       String states=job.getstring ("status");        if (States.equals ("0")) {//verify success String JSON = job.getstring ("receipt");        SYSTEM.OUT.PRINTLN (JSON); try {appstorebuyentity Vo = (appstorebuyentity) Util.decodejson (JSON, appstorebuyentity.class); System.out.println (Vo.getbid ()); System.out.println (Vo.getbvrs ()); System.out.println (vo.getitem_id ()); System.out.println (Vo.getoriginal_purchase_date ()); System.out.println (Vo.getoriginal_purchase_date_ms ()); System.out.println (Vo.getoriginal_purchase_date_pst ()); System.out.println (vo.getoriginal_transaction_id ()); System.out.println (vo.getproduct_id ()); System.out.println (Vo.getpurchase_date ()); System.out.println (Vo.getpurchase_date_ms ()); System.out.println (Vo.getpurchase_date_pst ()); System.out.println (Vo.getquantity ()); System.out.println (Vo.gettransaCTION_ID ()); System.out.println (Vo.getunique_identifier ()); System.out.println (Vo.getunique_vendor_identifier ());} catch (Exception e) {e.printstacktrace ();} Deserialization} else {//Bill invalid}}





Apple AppStore In-app Purchase internal payment verification code

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.