asp.net mvc 接入最新支付宝支付+退款 alipay-sdk-NET-20170615110549

2021-06-17 05:04

阅读:382

        #region 服务器异步通知页面

        /// 

        /// 功能:服务器异步通知页面

        /// 创建该页面文件时,请留心该页面文件中无任何HTML代码及空格。

        /// 该页面不能在本机电脑测试,请到服务器上做测试。请确保外部可以访问该页面。

        /// 该页面调试工具请使用写文本函数logResult。

        /// 如果没有收到该页面返回的 success 信息,支付宝会在24小时内按一定的时间策略重发通知

        /// net123    用户名:zouke1220  密码:zouke369189   邮箱:zouke12@163.com

        /// http://www.nat123.com/

        /// GmkCollege

        /// 

        public void NotifyUrl()

        {

            // 获取支付宝Post过来反馈信息

            IDictionary map = GetRequestPost();

 

            if (map.Count > 0) //判断是否有带返回参数

            {

                try

                {

                    //支付宝的公钥

                    string alipayPublicKey = AlipayConfigHelper.alipayPublicKey;

                    string signType = AlipayConfigHelper.signType;

                    string charset = AlipayConfigHelper.charset;

                    bool keyFromFile = false;

                     

                    bool verify_result = AlipaySignature.RSACheckV1(map, alipayPublicKey, charset, signType, keyFromFile);

                    // 验签成功后,按照支付结果异步通知中的描述,对支付结果中的业务内容进行二次校验,校验成功后在response中返回success并继续商户自身业务处理,校验失败返回failure

                    if (verify_result)

                    {

                        //商户订单号

                        string out_trade_no = map["out_trade_no"];

                        //支付宝交易号

                        string trade_no = map["trade_no"];

                        //交易创建时间

                        string gmt_create = map["gmt_create"];

                        //交易付款时间

                        string gmt_payment = map["gmt_payment"];

                        //通知时间

                        string notify_time = map["notify_time"];

                        //通知类型  trade_status_sync

                        string notify_type = map["notify_type"];

                        //通知校验ID

                        string notify_id = map["notify_id"];  

                        //开发者的app_id

                        string app_id = map["app_id"];

                        //卖家支付宝用户号

                        string seller_id = map["seller_id"];

                        //买家支付宝用户号

                        string buyer_id = map["buyer_id"];

                        //实收金额

                        string receipt_amount = map["receipt_amount"];  

                        //交易状态

                        //交易状态TRADE_FINISHED的通知触发条件是商户签约的产品不支持退款功能的前提下,买家付款成功;

                        //或者,商户签约的产品支持退款功能的前提下,交易已经成功并且已经超过可退款期限

                        //状态TRADE_SUCCESS的通知触发条件是商户签约的产品支持退款功能的前提下,买家付款成功

                        if (map["trade_status"] == "TRADE_FINISHED" || map["trade_status"] == "TRADE_SUCCESS")

                        {                 

                            //判断该笔订单是否在商户网站中已经做过处理

                            DataTable dd=collegeService.OrderPayNot(out_trade_no).Tables[0];

                            if (Convert.ToInt32(dd.Rows[0]["Status"]) == 0)

                            {

                                //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序

                                #region 将数据提添加到集合中

                                Dictionary myDic = new Dictionary();

                                myDic.Add("PayTradeNo", trade_no);

                                myDic.Add("Status", "1");

                                myDic.Add("Type", "0");

                                myDic.Add("PayTime", gmt_payment);

                                myDic.Add("BuyerId", buyer_id);

                                myDic.Add("OrderNo", out_trade_no);

                                #endregion

 

                                #region 添加数据到数据库                

                                bool res = collegeService.AddPayInfo(myDic);

                                if (res == false)

                                {

                                    Response.Write("添加支付信息失败!");

                                }

                                #endregion

                                Response.Write("success");  //请不要修改或删除

                            }

                        }                        

                    }

                    // 验签失败则记录异常日志,并在response中返回failure.

                    else

                    {            

                        Response.Write("验证失败");

                    }

                }

                catch (Exception e)

                {

                    throw new Exception(e.Message);

                }

            }

            else

            {

                Response.Write("无返回参数");

            }

        }

        /// 

        /// 获取支付宝POST过来通知消息,并以“参数名=参数值”的形式组成数组

        /// 

        /// request回来的信息组成的数组

        public IDictionary GetRequestPost()

        {

            int i = 0;

            IDictionary sArray = new Dictionary();

            NameValueCollection coll;

            //Load Form variables into NameValueCollection variable.

            coll = Request.Form;

 

            // Get names of all forms into a string array.

            String[] requestItem = coll.AllKeys;

 

            for (i = 0; i

            {

                sArray.Add(requestItem[i], Request.Form[requestItem[i]]);

            }

 

            return sArray;

        }

        #endregion


评论


亲,登录后才可以留言!