Fork me on GitHub

ASP.NET MVC 下使用支付宝支付接口 以及 ASP.NET Core 下相关改造支付

通过nuget首先引用AopSdk.dll 包

下面写的是 Asp.Net MVC 下相关的支付接口

APP支付

配置客户端相关的参数,配置成自己的代码就可以了

        private string APPID = string.Empty;
        private string APP_PRIVATE_KEY = string.Empty;
        private string ALIPAY_PUBLIC_KEY = string.Empty;
        private string CHARSET = string.Empty;
IAopClient client;

在支付接口构造函数中初始化Client信息

 client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", APPID, APP_PRIVATE_KEY, "json", "1.0", "RSA2", ALIPAY_PUBLIC_KEY, CHARSET, false);

APP发起支付请求

 /// <summary>
        /// App发起支付请求
        /// </summary>
        /// <param name="tradeno">外部订单号,商户网站订单系统中唯一的订单号</param>
        /// <param name="subject">订单名称</param>
        /// <param name="totalAmout">付款金额</param>
        /// <param name="itemBody">商品描述</param>
        [HttpPost]
        public JObject AppPayRequest(string tradeno, string subject, string totalAmout, string itemBody)
        {


            AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
            //SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
            AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
            model.Body = itemBody; 
            model.Subject = subject;
            model.TotalAmount = totalAmout;
            model.ProductCode = "quick_msecurity_pay";
            model.OutTradeNo = "20170216test01";
            model.TimeoutExpress = "30m";
            request.SetBizModel(model);
            request.SetNotifyUrl("https://xxxxxx/callback");

            //这里和普通的接口调用不同,使用的是sdkExecute
            AlipayTradeAppPayResponse response = client.SdkExecute(request);
            //HttpUtility.HtmlEncode是为了输出到页面时防止被浏览器将关键参数html转义,实际打印到日志以及http传输不会有这个问题
            return JObject.Parse(HttpUtility.HtmlEncode(response.Body));
            //页面输出的response.Body就是orderString 可以直接给客户端请求,无需再做处理。


        }

Web支付调用

        /// <summary>
        ///Web发起支付请求
        /// </summary>
        /// <param name="tradeno">外部订单号,商户网站订单系统中唯一的订单号</param>
        /// <param name="subject">订单名称</param>
        /// <param name="totalAmout">付款金额</param>
        /// <param name="itemBody">商品描述</param>
        [HttpPost]
        public JObject WebPayRequest(string tradeno, string subject, string totalAmout, string itemBody)
        {


            AlipayTradePayRequest request = new AlipayTradePayRequest();
            //SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
            AlipayTradePayModel model = new AlipayTradePayModel();
            model.Body = itemBody;
            model.Subject = subject;
            model.TotalAmount = totalAmout;
            model.ProductCode = "quick_msecurity_pay";
            model.OutTradeNo = "20170216test01";
            model.TimeoutExpress = "30m";
            request.SetBizModel(model);
            request.SetNotifyUrl("https://xxxxxx/callback");
            AlipayTradePayResponse response = client.pageExecute(request); //返回表单信息,js submit
            return JObject.Parse(HttpUtility.HtmlEncode(response.Body));
           


        }

 

支付回调地址

/// <summary>
        ///  异步POST回调
        /// </summary>
        /// <returns></returns>
        public async Task<JObject> NotifyPostAsync()
        {
            int i = 0;
            IDictionary<string, string> sArray = new Dictionary<string, string>();
            NameValueCollection coll;
            coll = await Request.Content.ReadAsFormDataAsync();
            String[] requestItem = coll.AllKeys;

            for (i = 0; i < requestItem.Length; i++)
            {
                sArray.Add(requestItem[i], coll.Get(requestItem[i]));
            }
            //1、获取交易号 在去支付宝校验知否存在该交易号 以及该交易号的支付状态

            //2、验证是否是 我们自己商户 回调的订单信息

            //3、验证知否金额是否是订单金额

            //4、业务通知成功 要告知支付宝 不要继续发动通知回调 ,如果业务处理有异常 记录订单日志 

            //5、最后返回相关信息
            return null;
        }

订单退款

/// <summary>
        /// 订单退款
        /// </summary>
        /// <param name="tradeno">商户订单号</param>
        /// <param name="alipayTradeNo">支付宝交易号</param>
        /// <param name="refundAmount">退款金额</param>
        /// <param name="refundReason">退款原因</param>
        /// <param name="refundNo">退款单号</param>
        /// <returns></returns>
        public JObject OrderBackMoney(string tradeno, string alipayTradeNo, string refundAmount, string refundReason, string refundNo)
        {
            AlipayTradeRefundModel model = new AlipayTradeRefundModel();
            model.OutTradeNo = tradeno;
            model.TradeNo = alipayTradeNo;
            model.RefundAmount = refundAmount;
            model.RefundReason = refundReason;
            model.OutRequestNo = refundNo;

            AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
            request.SetBizModel(model);

            var response =  client.Execute(request);
            return JObject.Parse(HttpUtility.HtmlEncode(response.Body));
        }

退款查询

 /// <summary>
        /// 退款查询
        /// </summary>
        /// <param name="tradeno">商户订单号</param>
        /// <param name="alipayTradeNo">支付宝交易号</param>
        /// <param name="refundNo">退款单号</param>
        /// <returns></returns>
        [HttpPost]
        public  JObject OrderBackMoneyQuery(string tradeno, string alipayTradeNo, string refundNo)
        {
            if (string.IsNullOrEmpty(refundNo))
            {
                refundNo = tradeno;
            }

            AlipayTradeFastpayRefundQueryModel model = new AlipayTradeFastpayRefundQueryModel();
            model.OutTradeNo = tradeno;
            model.TradeNo = alipayTradeNo;
            model.OutRequestNo = refundNo;

            AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
            request.SetBizModel(model);

            var response = client.Execute(request);
            return JObject.Parse(HttpUtility.HtmlEncode(response.Body));
        }

关闭订单

/// <summary>
        /// 关闭订单
        /// </summary>
        /// <param name="tradeno">商户订单号</param>
        /// <param name="alipayTradeNo">支付宝交易号</param>
        /// <returns></returns>
        [HttpPost]
        public JObject OrderClose(string tradeno, string alipayTradeNo)
        {
           
            AlipayTradeCloseModel model = new AlipayTradeCloseModel();
            model.OutTradeNo = tradeno;
            model.TradeNo = alipayTradeNo;

            AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
            request.SetBizModel(model);

            var response = client.Execute(request);
            return JObject.Parse(HttpUtility.HtmlEncode(response.Body));
        }

 

ASP.NET Core 下面的支付,还是使用此SDK, 把DefaultAopClient 封装配置下Options 扩展到 IServiceCollection 方法注册服务即可,后期有时间补上这块的代码

 

posted @ 2019-08-26 13:45  龙码精神  阅读(1325)  评论(0编辑  收藏  举报