public DataTable GetAccountInfoDT(string userID)
        {
            string sql = "select * from tb_AccountInfo";
            if (!String.IsNullOrEmpty(userID))
                sql += " where UserID like '%" + userID + "%'";
            DataTable dt = da.GetDataTable(sql);
            dt.Columns.Add("PaymentType");
            dt.Columns.Add("PaymentTime");

            PaymentInfo payment = new PaymentInfo();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                try
                {
                    payment = SerializeHelper<PaymentInfo>.Deserialize(dt.Rows[i]["PaymentInfo"].ToString());
                    dt.Rows[i]["PaymentType"] = payment.PaymentType;
                    dt.Rows[i]["PaymentTime"] = payment.PaymentTime;
                }
                catch
                {
                    dt.Rows[i]["PaymentType"] = "";
                    dt.Rows[i]["PaymentTime"] = "";
                }
            }
            return dt;
        }
        public static PaymentInfo PaymentTransactionDataToPaymentInfo(PaymentTransactionData transData)
        {
            PaymentInfo paymentInfo = null;

            if (transData != null && transData.ProductData != null)
            {
                paymentInfo = new PaymentInfo();

                try
                {
                    paymentInfo.ProductID = transData.ProductData.Product.ProductId;
                    paymentInfo.ProductName = transData.ProductData.Product.ProductName;
                    paymentInfo.SetPrice(transData.ProductData.Amount.ToString());
                    paymentInfo.TransactionID = transData.TransactionId;
                    paymentInfo.PaymentTime = transData.ChargeDate;
                    paymentInfo.PaymentTimeSpecified = true;
                    paymentInfo.SetPaymentType(transData.ProductData.Term.TermType);

                }
                catch (Exception e)
                {
                    // ignore.
                }
            }

            return paymentInfo;
        }
 public bool SavePaymentTransactionData(PaymentInfo paymentinfo)
 {
     PaymentTransactionData data = AccountInfoProvider.PaymentInfoToPaymentTransactionData(paymentinfo);
     return SavePaymentTransactionData(data);
 }
        private static PaymentTransactionData PaymentInfoToPaymentTransactionData(PaymentInfo paymentInfo)
        {
            PaymentTransactionData transData = null;

            if (paymentInfo != null)
            {
                transData = new PaymentTransactionData();

                try
                {
                    ProductData productdata = new ProductData();
                    Product product = new Product();
                    product.ProductId = paymentInfo.ProductID;
                    product.ProductName = paymentInfo.ProductName;
                    product.IsVip = paymentInfo.ProductID.ToLower() == "vip";
                    productdata.Product = product;

                    productdata.Amount = Convert.ToDecimal(paymentInfo.Price.Amount);

                    Term term = new Term();
                    DataAccess da = new DataAccess();
                    DataTable dtterm = da.GetDataTable("select * from Terms where [Type]='" + productdata.Term.TermType + "'");
                    term.TermId = Convert.ToInt32(dtterm.Rows[0]["TermId"].ToString());
                    term.TermType = dtterm.Rows[0]["Type"].ToString();
                    term.Description = dtterm.Rows[0]["Description"].ToString();
                    productdata.Term = term;

                    productdata.ProductTermId = prefix + productdata.Product.ProductId.ToLower() + "_" + productdata.Term.Description.ToLower();
                    transData.TransactionId = paymentInfo.TransactionID;
                    transData.ChargeDate = paymentInfo.PaymentTime;
                    transData.ProductData = productdata;
                }
                catch { }
            }

            return transData;
        }
        //tested
        public bool UpdatePurchaseInfo(string userID, string applicationID, PaymentInfo paymentInfo)
        {
            bool updated = false;
            if (userID != null && applicationID != null && paymentInfo != null)
            {
                try
                {
                    string sql = "UPDATE [tb_AccountInfo] SET [PaymentInfo] =@paymentinfo,[AccountType] =@acctype"
                       + " WHERE [ApplicationID] =@appid and [UserID] =@userid";
                    SqlCommand sc = new SqlCommand(sql);
                    sc.Parameters.AddWithValue("@userid", userID);
                    sc.Parameters.AddWithValue("@appid", applicationID);
                    sc.Parameters.AddWithValue("@acctype", PaymentTypeToAccountType(paymentInfo.PaymentType).ToString());
                    sc.Parameters.AddWithValue("@paymentinfo", XMLToString(paymentInfo));
                    da.ExecuteNonQuery(sql, sc);

                    updated = true;
                }
                catch (Exception ex)
                {
                }
            }

            return updated;
        }
        public StatusCode UpdateApplicationPayment(string userID, string applicationID, PaymentInfo paymentInfo)
        {
            StatusCode status = StatusCode.Fail;
            if (paymentInfo != null)
            {
                if (paymentInfo.PaymentType == PaymentType.VIP)
                {
                    applicationID = "VIP";
                    if (HasApplicationAccountInfo(userID, applicationID))
                    {
                        if (UpdatePurchaseInfo(userID, applicationID, paymentInfo))
                        {
                            status = StatusCode.Success;
                        }
                    }
                    else
                    {
                        AccountInfo vipAccountInfo = new AccountInfo();
                        vipAccountInfo.ApplicationID = applicationID;
                        vipAccountInfo.UserID = userID;
                        vipAccountInfo.AccountType = AccountType.VIP;
                        vipAccountInfo.PaymentInfo = paymentInfo;

                        status = AddAccountInfo(vipAccountInfo);
                    }

                }
                else
                {
                    if (HasApplicationAccountInfo(userID, applicationID))
                    {
                        if (UpdatePurchaseInfo(userID, applicationID, paymentInfo))
                        {
                            status = StatusCode.Success;
                        }
                    }
                    else
                    {
                        AccountInfo paidAccountInfo = new AccountInfo();
                        paidAccountInfo.ApplicationID = applicationID;
                        paidAccountInfo.UserID = userID;
                        paidAccountInfo.AccountType = AccountType.Paid;
                        paidAccountInfo.PaymentInfo = paymentInfo;

                        status = AddAccountInfo(paidAccountInfo);
                    }
                }
            }
            return status;
        }
        public bool PaymentSummaryNotification(string userID, PaymentInfo paymentInfo)
        {
            bool success = false;
            if (userID != null && paymentInfo != null)
            {

                if (UpdateApplicationPayment(userID, paymentInfo.ProductID, paymentInfo) == StatusCode.Success)
                {
                    success = true;
                }

            }

            return success;
        }