using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.Configuration;
using istrong.db;
using xyxx_base.Common;
using xyxx.Common;
using xyxx_base.Models;
using MongoDB.Bson;

namespace xyxx.Controllers
{
    public class AlipayController : Controller
    {
        protected readonly DbHelper db = DbHelperFactory.GetDbHelper();
        //支付宝网关地址(新)
        private static string GATEWAY_NEW = "https://mapi.alipay.com/gateway.do?";

        //商户id
        private static string _partner = ConfigurationManager.AppSettings["partner"];

        //商户的私钥
        private static string _key = ConfigurationManager.AppSettings["key"];

        //编码格式
        private static string _input_charset = "utf-8";

        //签名方式
        private static string _sign_type = "MD5";

        //支付类型 必填,不能修改
        private static string payment_type = "1";

        //服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数
        private static string notify_url = ConfigurationManager.AppSettings["notifyUrl"];

        
        private static string return_url = ""; 
//目前只支持一种类型:10(卖家给第三方提成)。当传递了参数royalty_parameters时,提成类型参数不能为空。
        private static string royalty_type = "10";
        public ActionResult Pay()
        {
            //卖家支付宝帐户 必填
            string seller_email = "**************";

            //商户订单号 必填
            string out_trade_no = DateTime.Now.Ticks.ToString();

            //订单名称 必填
            string subject = "*****************";

            //付款金额 必填
            string total_fee = this.GetStringParameter("money", "0.01");

            //订单描述
            string body = "**********************";

            //商品展示地址 需以http://开头的完整路径,例如:http://www.xxx.com/myorder.html
            string show_url = "";

            //防钓鱼时间戳 若要使用请调用类文件submit中的query_timestamp函数
            string anti_phishing_key = "";

            //客户端的IP地址 非局域网的外网IP地址,如:221.0.0.1
            string exter_invoke_ip = "";

            //创建充值记录
            var loginUser = SessionHelper.GetLoginUser();
            var rechargeModel = new RechargeModel()
            {
                CreatorId = loginUser.UserId,
                Creator = loginUser.UserName,
                EnterpriseId = loginUser.UnitId,
                EnterpriseName = loginUser.UnitName,
                Remark = "支付宝充值",
                Data = new Dictionary<string, object>(),
                OrderId = "",
                Deleted = true,
            };
            var rechargeEditModel = new RechargeEditModel()
            {
                Id = rechargeModel.Id,
                CreatorId = rechargeModel.CreatorId,
                Creator = rechargeModel.Creator,
                CreateTime = DateTime.MinValue,
                Attached = null,
                Deleted = false,
            };
            rechargeModel.Edit = rechargeEditModel;
            rechargeModel.EditList.Add(rechargeEditModel);
            rechargeModel.Audit.Money = decimal.Parse(total_fee);
            rechargeModel.Audit.CreateTime = DateTime.MinValue;
            db.Insert(rechargeModel);

            var extra_common_param = rechargeModel.Id.ToString();

            //把请求参数打包成数组
            SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();
            sParaTemp.Add("partner", _partner);
            sParaTemp.Add("_input_charset", _input_charset);
            sParaTemp.Add("service", "create_direct_pay_by_user");
            sParaTemp.Add("payment_type", payment_type);
            sParaTemp.Add("notify_url", notify_url);
            sParaTemp.Add("return_url", return_url);
            sParaTemp.Add("seller_email", seller_email);
            sParaTemp.Add("out_trade_no", out_trade_no);
            sParaTemp.Add("subject", subject);
            sParaTemp.Add("total_fee", total_fee);
            sParaTemp.Add("body", body);
            //sParaTemp.Add("show_url", "ss");
            //sParaTemp.Add("anti_phishing_key", "ss");
            //sParaTemp.Add("exter_invoke_ip", "ss");
            //sParaTemp.Add("it_b_pay", "3m");
            //sParaTemp.Add("royalty_type", royalty_type);//2014-04-10注释
            //sParaTemp.Add("royalty_parameters", royalty_parameters);
            sParaTemp.Add("extra_common_param", extra_common_param);
            //建立请求
            string sHtmlText = BuildRequest(sParaTemp, "post", "确认");
            return Content(sHtmlText);
        }
        /// <summary>
        /// 建立请求,以表单HTML形式构造(默认)
        /// </summary>
        /// <param name="sParaTemp">请求参数数组</param>
        /// <param name="strMethod">提交方式。两个值可选:post、get</param>
        /// <param name="strButtonValue">确认按钮显示文字</param>
        /// <returns>提交表单HTML文本</returns>
        public static string BuildRequest(SortedDictionary<string, string> sParaTemp, string strMethod, string strButtonValue)
        {
            //待请求参数数组
            Dictionary<string, string> dicPara = new Dictionary<string, string>();
            dicPara = BuildRequestPara(sParaTemp);

            StringBuilder sbHtml = new StringBuilder();

            sbHtml.Append("<form id='alipaysubmit' name='alipaysubmit' action='" + GATEWAY_NEW + "_input_charset=" + _input_charset + "' method='" + strMethod.ToLower().Trim() + "'>");

            foreach (KeyValuePair<string, string> temp in dicPara)
            {
                sbHtml.Append("<input type='hidden' name='" + temp.Key + "' value='" + temp.Value + "'/>");
            }

            //submit按钮控件请不要含有name属性
            sbHtml.Append("<input type='submit' value='" + strButtonValue + "' style='display:none;'></form>");

            sbHtml.Append("<script>document.forms['alipaysubmit'].submit();</script>");

            return sbHtml.ToString();
        }

        /// <summary>
        /// 生成要请求给支付宝的参数数组
        /// </summary>
        /// <param name="sParaTemp">请求前的参数数组</param>
        /// <param name="code">字符编码</param>
        /// <returns>要请求的参数数组字符串</returns>
        private static string BuildRequestParaToString(SortedDictionary<string, string> sParaTemp, Encoding code)
        {
            //待签名请求参数数组
            Dictionary<string, string> sPara = new Dictionary<string, string>();
            sPara = BuildRequestPara(sParaTemp);

            //把参数组中所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串,并对参数值做urlencode
            string strRequestData = Core.CreateLinkStringUrlencode(sPara, code);

            return strRequestData;
        }

        /// <summary>
        /// 生成要请求给支付宝的参数数组
        /// </summary>
        /// <param name="sParaTemp">请求前的参数数组</param>
        /// <returns>要请求的参数数组</returns>
        private static Dictionary<string, string> BuildRequestPara(SortedDictionary<string, string> sParaTemp)
        {
            //待签名请求参数数组
            Dictionary<string, string> sPara = new Dictionary<string, string>();
            //签名结果
            string mysign = "";

            //过滤签名参数数组
            sPara = Core.FilterPara(sParaTemp);

            //获得签名结果
            mysign = BuildRequestMysign(sPara);

            //签名结果与签名方式加入请求提交参数组中
            sPara.Add("sign", mysign);
            sPara.Add("sign_type", _sign_type);

            return sPara;
        }

        /// <summary>
        /// 生成请求时的签名
        /// </summary>
        /// <param name="sPara">请求给支付宝的参数数组</param>
        /// <returns>签名结果</returns>
        private static string BuildRequestMysign(Dictionary<string, string> sPara)
        {
            //把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
            string prestr = Core.CreateLinkString(sPara);

            //把最终的字符串签名,获得签名结果
            string mysign = "";
            switch (_sign_type)
            {
                case "MD5":
                    mysign = AlipayMD5.Sign(prestr, _key, _input_charset);
                    break;
                default:
                    mysign = "";
                    break;
            }

            return mysign;
        }
        /// <summary>
        /// 同步回调地址
        /// </summary>
        /// <returns></returns>
        public ActionResult returnUrl()
        {
            return Content("充值成功");
        }
        /// <summary>
        /// 异步回调地址
        /// </summary>
        /// <returns></returns>
        public ActionResult notifyUrl()
        {
            var data = WebClientHelper.GetData(Request);
            var is_success = data.GetStringValue("is_success");
            var trade_status = data.GetStringValue("trade_status");
            var money = data.GetStringValue("total_fee");
            if (trade_status == "TRADE_SUCCESS")
            {
                var extra_common_param = data.GetStringValue("extra_common_param");
                var rechargeModel = db.FindOneByIdAs<RechargeModel>(ObjectId.Parse(extra_common_param));
                rechargeModel.OrderId = data.GetStringValue("trade_no");
                rechargeModel.Data = data;
                if (rechargeModel.Deleted)
                {
                    rechargeModel.Deleted = false;
                    db.Save(rechargeModel);
                    UserEnterpriseHelper.ChangeEnterpriseMoney(EnumTradeType.充值, double.Parse(money), 0, 0, 0, rechargeModel.EnterpriseId, null, "企业充值", db);
                }
                return Content("success");
            }
            return null;

        }



    }
}