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

經過nuget首先引用AopSdk.dll 包html

下面寫的是 Asp.Net MVC 下相關的支付接口json

APP支付

配置客戶端相關的參數,配置成本身的代碼就能夠了api

        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]));
            }
            //一、獲取交易號 在去支付寶校驗知否存在該交易號 以及該交易號的支付狀態

            //二、驗證是不是 咱們本身商戶 回調的訂單信息

            //三、驗證知否金額是不是訂單金額

            //四、業務通知成功 要告知支付寶 不要繼續發動通知回調 ,若是業務處理有異常 記錄訂單日誌 

            //五、最後返回相關信息
            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 方法註冊服務便可,後期有時間補上這塊的代碼異步

相關文章
相關標籤/搜索