蘋果內購支付對接

來源:互聯網
上載者:User

標籤:get   bool   itunes   trre   log   sts   coding   requests   串流   

  1. public bool ValidateApplePay()  
  2.        {  
  3.                
  4.            //用戶端post過來的參數  
  5.            string appleReceipt = Request.Form["appleReceipt"]; //蘋果內購的驗證收據  
  6.            string orderId = PayHelper.GetOrderIDByPrefix("AP");  //訂單編號  
  7.            string amount = Request.Form["amount"];             //金額  
  8.            string userId = Request.Form["userId"];             //使用者UserID  
  9.              
  10.            // 驗證參數  
  11.            if (appleReceipt.Length < 20)  
  12.            {  
  13.                return false;  
  14.            }  
  15.   
  16.            string strJosn = string.Format("{{\"receipt-data\":\"{0}\"}}", appleReceipt);  
  17.            // 請求驗證  
  18.            string strResult = CreatePostHttpResponse(strJosn, isSandbox);  
  19.            JObject obj = JObject.Parse(strResult);//using Newtonsoft.Json.Linq;  
  20.   
  21.            // 判斷是否購買成功  
  22.            if (obj["status"].ToString() == "0")  
  23.            {  
  24.                return true;  
  25.            }  
  26.            else  
  27.            {  
  28.                return false;  
  29.            }  
  30.        }  
  31.    public string CreatePostHttpResponse(string datas, bool isSandbox = false)  
  32.        {  
  33.            //正式購買地址 沙箱購買地址  
  34.            string url_buy = "https://buy.itunes.apple.com/verifyReceipt";  
  35.            string url_sandbox = "https://sandbox.itunes.apple.com/verifyReceipt";  
  36.            string url = isSandbox ? url_sandbox : url_buy;  
  37.   
  38.            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;  
  39.            request.ProtocolVersion = HttpVersion.Version10;  
  40.            request.Method = "POST";  
  41.            request.ContentType = "application/x-www-form-urlencoded";  
  42.            byte[] data = Encoding.GetEncoding("UTF-8").GetBytes(datas.ToString());  
  43.            using (Stream stream = request.GetRequestStream())  
  44.            {  
  45.                stream.Write(data, 0, data.Length);  
  46.            }  
  47.   
  48.            HttpWebResponse response = request.GetResponse() as HttpWebResponse;  
  49.            Stream responseStream = response.GetResponseStream();   //擷取響應的字串流  
  50.            StreamReader sr = new StreamReader(responseStream); //建立一個stream讀取流  
  51.            var str = sr.ReadToEnd();  
  52.            sr.Close();  
  53.            responseStream.Close();  
  54.            return str.ToString();  
  55.        }  

蘋果內購支付對接

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.