예제 #1
0
        public static bool PostInApp(bool bTest, int UserId, string json, out int _ReturnSoce, out string msg)
        {
            _ReturnSoce = 0;
            msg         = "";
            string returnmessage = "";

            try
            {
                LitJson.JsonData newData = new LitJson.JsonData();
                newData["receipt-data"] = json;
                json = newData.ToJson();
                byte[] postBytes = Encoding.UTF8.GetBytes(json);
                System.Net.WebRequest request = null;
                if (bTest)
                {
                    request = System.Net.HttpWebRequest.Create("https://sandbox.itunes.apple.com/verifyReceipt");
                }
                else
                {
                    request = System.Net.HttpWebRequest.Create("https://buy.itunes.apple.com/verifyReceipt");
                }
                request.Method        = "POST";
                request.ContentType   = "application/json";
                request.ContentLength = postBytes.Length;
                using (var stream = request.GetRequestStream())
                {
                    stream.Write(postBytes, 0, postBytes.Length);
                    stream.Flush();
                }
                var    sendresponse     = request.GetResponse();
                string sendresponsetext = "";
                using (var streamReader = new StreamReader(sendresponse.GetResponseStream()))
                {
                    sendresponsetext = streamReader.ReadToEnd().Trim();
                }
                returnmessage = sendresponsetext;
                if (returnmessage.Length < 50)
                {
                    msg = "验证失败";
                    return(false);
                }
                AppleReturnData _AppleReturnData = LitJson.JsonMapper.ToObject <AppleReturnData>(sendresponsetext);
//                 if (_AppleReturnData.receipt.bundle_id != "com.c2dxluagm.RoyalHall")
//                 {
//                     msg = "bundle_id 不正确";
//                     return false;
//                 }
                string[] productString = _AppleReturnData.receipt.in_app[0].product_id.Split('.');
                if (productString.Length <= 2)
                {
                    msg = "验证失败";
                    return(false);
                }
                int Price = 0;
                int.TryParse(productString[productString.Length - 1], out Price);
                Price = Price * 100;
                bool rlt = AddSorce(UserId, Price, _AppleReturnData, out _ReturnSoce);
                if (!rlt)
                {
                    msg = "已经验证过了";
                }

                JsonEMail newEmail = new JsonEMail();
                newEmail.dwUserID   = UserId;
                newEmail.nStatus    = 0;
                newEmail.szTitle    = "支付成功";
                newEmail.szMessage  = "IOS内购交易:支付成功[" + (Price / 100).ToString() + ".00]";
                newEmail.szSender   = "系统";
                newEmail.szSendTime = DateTime.Now.ToString();
                EmailAdd.AddEmail(newEmail);

                return(rlt);
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
            return(false);
        }
예제 #2
0
        public static bool AddSorce(int UserId, int Sroce, AppleReturnData _AppleReturnData, out int _ReturnSoce)
        {
            _ReturnSoce = 0;
            string MyConn = System.Configuration.ConfigurationManager.AppSettings["DBTreasure"];
//             string MyConn = "server=103.105.58.140;uid=testdb;pwd=123abc;database=RYTreasureDB;Trusted_Connection=no";
            SqlConnection MyConnection = new SqlConnection(MyConn);

            try
            {
                MyConnection.Open();
                {
                    string        selStr    = "select transaction_id from ReturnAppDetailInfo where transaction_id=" + _AppleReturnData.receipt.in_app[0].transaction_id;
                    SqlCommand    MyCommand = new SqlCommand(selStr, MyConnection);
                    SqlDataReader _Reader   = MyCommand.ExecuteReader();
                    if (_Reader.HasRows)
                    {
                        _Reader.Close();
                        return(false);
                    }
                    _Reader.Close();
                }
                {
                    string col = @"
UserID,
OrderID,
PayAmount,
Status,
quantity,
product_id,
transaction_id,
purchase_date,
original_transaction_id,
original_purchase_date,
app_item_id,
version_external_identifier,
bid,
bvrs";

                    CsString _Value = new CsString("");
                    _Value.Fill(UserId);
                    _Value.Fill(_AppleReturnData.receipt.download_id);
                    _Value.Fill(Sroce);
                    _Value.Fill("0");
                    _Value.Fill(1);
                    _Value.Fill(_AppleReturnData.receipt.in_app[0].product_id);
                    _Value.Fill(_AppleReturnData.receipt.in_app[0].transaction_id);
                    _Value.Fill(_AppleReturnData.receipt.in_app[0].purchase_date);
                    _Value.Fill(_AppleReturnData.receipt.in_app[0].original_transaction_id);
                    _Value.Fill(_AppleReturnData.receipt.in_app[0].original_purchase_date);
                    _Value.Fill(_AppleReturnData.receipt.app_item_id);
                    _Value.Fill(_AppleReturnData.receipt.version_external_identifier);
                    _Value.Fill(_AppleReturnData.receipt.bundle_id);
                    _Value.FillEx(123);
                    CsString str = CsString.InstertSql("ReturnAppDetailInfo", col, _Value.ToString());

                    SqlCommand MyCommand = new SqlCommand(str.ToString(), MyConnection);
                    MyCommand.ExecuteNonQuery();
                }
                {
                    string MyUpdate = "Update GameScoreInfo set Score=Score+" + Sroce.ToString() +
                                      " where UserID=" + UserId;
                    SqlCommand MyCommand = new SqlCommand(MyUpdate, MyConnection);
                    MyCommand.ExecuteNonQuery();
                }
                {
                    string        selStr    = "select Score from GameScoreInfo where UserID=" + UserId;
                    SqlCommand    MyCommand = new SqlCommand(selStr, MyConnection);
                    SqlDataReader _Reader   = MyCommand.ExecuteReader();
                    if (_Reader.Read())
                    {
                        _ReturnSoce = (int)_Reader.GetInt64(0);
                        _Reader.Close();
                        return(true);
                    }
                    _Reader.Close();
                }
                MyConnection.Close();
                return(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0} Exception caught.", ex);
            }
            return(false);
        }