using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Receive : System.Web.UI.Page
{
    protected string v_oid;		// 订单号
    protected string v_pstatus;	// 支付状态码
    //20（支付成功，对使用实时银行卡进行扣款的订单）；
    //30（支付失败，对使用实时银行卡进行扣款的订单）；

    protected string v_pstring;	//支付状态描述
    protected string v_pmode;	//支付银行
    protected string v_amount;	//支付金额
    protected string v_moneytype;	//币种		
    protected string remark1;	// 备注1
    protected string remark2;	// 备注1
    protected string v_md5str;
    protected string status_msg;

    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        SqlCommand smd = new SqlCommand("select MerchantNumber,MD5Pwd from Payway where id=" + Session["payid"], conn);
        SqlDataReader sdr = smd.ExecuteReader();
        sdr.Read();
        // MD5密钥要跟订单提交页相同，如Send.asp里的 key = "test" ,修改""号内 test 为您的密钥
        string key = sdr["MD5Pwd"].ToString();	// 如果您还没有设置MD5密钥请登陆我们为您提供商户后台，地址：https://merchant3.chinabank.com.cn/
        // 登陆后在上面的导航栏里可能找到“B2C”，在二级导航栏里有“MD5密钥设置”
        // 建议您设置一个16位以上的密钥或更高，密钥最多64位，但设置16位已经足够了
        sdr.Close();
        conn.Close();
        v_oid = Request["v_oid"];
        v_pstatus = Request["v_pstatus"];
        v_pstring = Request["v_pstring"];
        v_pmode = Request["v_pmode"];
        v_md5str = Request["v_md5str"];
        v_amount = Request["v_amount"];
        v_moneytype = Request["v_moneytype"];
        remark1 = Request["remark1"];

        string str = v_oid + v_pstatus + v_amount + v_moneytype + key;

        str = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5").ToUpper();    

        if (str == v_md5str)
        {

            if (v_pstatus.Equals("20"))
            {
                //支付成功
                //在这里商户可以写上自己的业务逻辑
                int intt = 0;
                SqlConnection conn = CDatabase.wtoconn();
                conn.Open();
                //Doadmin.Execsql("update MemberVIP set Integral=Integral-" +Convert.ToInt32(Session["OrdInt"].ToString())+ " where id=" + Session["userid"].ToString());
                if (Session["Wtouser"] != null)
                {
                    SqlCommand freesmd = new SqlCommand("select freesonfei from SystemBasicInformation", conn);
                    decimal freeint = Convert.ToDecimal(freesmd.ExecuteScalar());
                    if (freeint > Convert.ToDecimal(v_amount))
                    {
                        SqlCommand sonfei = new SqlCommand("select sonfei from SystemBasicInformation", conn);
                        Session["sonfei"] = Convert.ToString(sonfei.ExecuteScalar());
                        this.sonjf.Text = "赠送 " + Convert.ToString((int)(Convert.ToDouble(v_amount) - Convert.ToDouble(Session["sonfei"]))) + " 分";
                        intt = (int)(Convert.ToDouble(v_amount) - Convert.ToDouble(Session["sonfei"]));
                    }
                    else
                    {
                        this.sonjf.Text = "赠送 " + Convert.ToString((int)Convert.ToDouble(v_amount)) + " 分";
                        int intt = (int)Convert.ToDouble(v_amount);
                    }



                    Doadmin.Execsql("update MemberVIP set Integral=Integral+" + intt + ",intin='" + DateTime.Now.ToString() + "' where id=" + Session["userid"].ToString());

                    //int where1, result1, where2, result2, where3, result3, result4;
                    //SqlCommand smdjf;
                    //SqlDataReader sdrjf;
                    //smdjf = new SqlCommand("select * from Integral", Mconn);
                    //sdrjf = smdjf.ExecuteReader();
                    //sdrjf.Read();
                    //where1 = Convert.ToInt32(sdrjf["where1"].ToString());
                    //result1 = Convert.ToInt32(sdrjf["result1"].ToString());
                    //where2 = Convert.ToInt32(sdrjf["where2"].ToString());
                    //result2 = Convert.ToInt32(sdrjf["result2"].ToString());
                    //where3 = Convert.ToInt32(sdrjf["where3"].ToString());
                    //result3 = Convert.ToInt32(sdrjf["result3"].ToString());
                    //result4 = Convert.ToInt32(sdrjf["result4"].ToString());
                    //sdrjf.Close();

                    //if (Convert.ToInt32(v_amount) < where1)
                    //{
                    //    Doadmin.Execsql("update MemberVIP set Integral=Integral+" + result1 + ",intin='" + DateTime.Now.ToString() + "' where id=" + Session["userid"].ToString());
                    //    this.sonjf.Text = "赠送 0 分";
                    //}

                    //if (Convert.ToInt32(v_amount) >= where1 && Convert.ToInt32(v_amount) < where2)
                    //{
                    //    Doadmin.Execsql("update MemberVIP set Integral=Integral+" + result1 + ",intin='" + DateTime.Now.ToString() + "' where id=" + Session["userid"].ToString());
                    //    this.sonjf.Text = "赠送 " + result1.ToString() + " 分";
                    //}

                    //if (Convert.ToInt32(v_amount) >= where2 && Convert.ToInt32(v_amount) < where3)
                    //{
                    //    Doadmin.Execsql("update MemberVIP set Integral=Integral+" + result2 + ",intin='" + DateTime.Now.ToString() + "' where id=" + Session["userid"].ToString());
                    //    this.sonjf.Text = "赠送 " + result2.ToString() + " 分";
                    //}

                    //if (Convert.ToInt32(v_amount) == where3)
                    //{
                    //    Doadmin.Execsql("update MemberVIP set Integral=Integral+" + result1 + ",intin='" + DateTime.Now.ToString() + "' where id=" + Session["userid"].ToString());
                    //    this.sonjf.Text = "赠送 " + result3.ToString() + " 分";
                    //}

                    //if (Convert.ToInt32(v_amount) > where3)
                    //{
                    //    Doadmin.Execsql("update MemberVIP set Integral=Integral+" + result4 + ",intin='" + DateTime.Now.ToString() + "' where id=" + Session["userid"].ToString());
                    //    this.sonjf.Text = "赠送 " + result3.ToString() + " 分";
                    //}
                }
                else
                {
                    sonjf.Text = "只有会员才能赠送积分";
                }
                conn.Close();
                Doadmin.Execsql("update Orderw set OrderState=3 where Orderno='" + v_oid.ToString().Trim() + "'");
            }
        }
        else
        {
            Response.Write("校验失败,数据可疑");
        }
    }
    protected void Page_PreInit(object sender, EventArgs e)
    {
        SqlConnection connsk = CDatabase.wtoconn();
        connsk.Open();
        if (Session["skin"] == null)
        {
            SqlCommand smdskin = new SqlCommand("select skinid from SystemBasicInformation", connsk);
            Session["skin"] = Convert.ToString(smdskin.ExecuteScalar());
        }
        this.Page.Theme = Session["skin"].ToString();
        connsk.Close();
    }
}
