.net服务端接入支付宝接口

时间:2020-01-18
本文章向大家介绍.net服务端接入支付宝接口,主要包括.net服务端接入支付宝接口使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

创建沙箱测试账号及引入支付宝相应语言的SDK,支付宝开放平台有详细介绍,这里就不做赘述了。

主要代码如下:

创建支付配置类

public class AlipayConfig
    {
        //APPID
        public static string appId = "2016101500694440";
        //支付宝网关
        public static string gateway = "https://openapi.alipaydev.com/gateway.do";
        //用户私钥
        public static string privateKey = "MIIEpQIBAAKCAQEAu+yEUg/5qZD/Q0iDNPzzx+RPTwcjgrX0QlXoOdz7datalPhgDZv1+BIR0No26d1IM9+5kALu0DwQAQ/9bsUN+eat417QOJzsW2NUcLUhHZF6r6+TcJzJXon/yyrTVEIdLm6bSCMTlbO50xJXFWLkYUs0Z8MOMFjESlRbFPpO8cB0satG85GSFXNdBoMxr9rxYnbgFev5162L0xxL5XP9CtTRkRo0JeJsf9PHuZTiiH5NduVATe/piKoUw/2Bk7rjQJsJZLsGh+CAw6Mf6SqgqX0SbjMiiEbPBdv1Nbb+cY3FSNBed3ms4A0coyh4KaL3++PU7q1yOAOL5Zs6Dk1H7QIDAQABAoIBAQCwrYIN4QUZnb/P0ynANJ1Bbm3937lMF6NIhpnpZNBpZauo6QbtZJbRHrPvRmlgX/GVC57bPVanIgDt7smXTd6A95a+cJKU1zwEA5QMzs4OjYrqB8k3k8BiiJ2btmyvB+WFlOma3Zd5JFrtjY2gBrSuoFTpxOHfJGWPeFWVmGuGLolEZVtAwJ4y98HRug+shz0R5LSA8Wg3QiXgie5Za5xnuFIMY2QAAqDqKxbV3/rYZzLdZy1xiWr1Qdsmal7dDV//E+9HwcM6GN1KWCdLyWnroRj5EfP/FLImQBRu/7ZTrQQ0QjTqcEzoyiQ4SuzBzC/1F60kkaF3Lh5GaKlSecrhAoGBAPx1yhTAM+nKI9zfHHM9VOCMz9nfTAGqAXiFMxoKw1UXnerlCMZv/Il5ELzp+KL61UjMgYbNRdusfVuq432kE5QZ+ZntrgHjwutoEgHbQDwWWW6nLAo8KbbVuyI0as5AoThxB7HWdYLDFpCKBYF9PA5EnFMAIfe2UBacOwrUzULJAoGBAL6PEs1WZVGxTKWpZwMoU7o8H77UpnAXqIoBSt30StSEf2Qx6xgugHnaEX5nGKdF47RBLkLImgOxzOLPurGtUQaMJ3s1N6SC5KTc1qZnW54Pk41EEES8vW7DMRRKJMhPdNqmOSyLlsINAzC8W8sMgfZBCdlOkoAESPOHbawTpioFAoGBAOlHtlmCWPX5ETZw+4sI6zsrIo0CJuuKuh0xh8zSkTg5wHRi+r7SkBo/wlWuJVAEU70XK8DyQVEBjDx+JgPc7zJ05g10cMvSODTqT2ljvXFIfY8f7jPu7+TAjlDG+sJnOhI7b47GyoLv3hPlVb0XibztbT3M65ZH4q1h47TYemrxAoGAfVcYU+3G+QP42WJiuoX1BKN1cgznaQV2u0d8Wh/MdJquPFZFP4OeyAdyyETBx4zF6JOS/SR7gYCatnlSYHoSuI7JjVYEwnXCNNp5abzEW2P9dsbVOJE4vLkw+BqItSTmRMdfBfjO8dajSZQrgJQ6i6Jd+wyeegvXfC+RKMVH340CgYEAlB6N7FQU44lqIO9Os1bY06+yPFBUH476R7m8CC2KCW3fc7pHCvWRva4XM8pHnsBf2utF/LgvHpVQIhvieLQaCRwRa24I1RBNyFQ7Z3AFnodHv5fZzxFYbfqS1mZHJF1WnC+jVj6F1oh+DMmPrl69U6dTTppsdM5n1LclXzNhRmc=";
        //支付宝公钥
        public static string alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlUaqoPnb+UVjC/lEUcRytpMQtbVKcTnsT4derbrju/wbvwnL+CjJ8jNgIA8jysAwk2NOzZUY5JU3P24CnW16KiPHiqlWUB4YE1ZJEMmAu5qedu37XjYBmKUFoTtoFq2YtVvFCTQFmRYcFwRARbXFWyUMF30b+mOiD6bldsFtf7VzU6bawOs7YsgCMpUBcA+iIxNIZ8dK8s6PYzn54pKsHmID6Og+VOwNkvjnYG1xOAXqK5Fz6JPaJQRR/XvkHcqVWwBwOFCWx9XXOCNKxgYKY3J/RENSy3JV9IbKtxnWyrb54G32gdT4h+VOaDggqTaH8Pxk7oRCegqBpZdcr+SCqQIDAQAB";
        //支付成功的回调地址
        public static string returnURL = "Student/Index";
        // 签名方式  
        public static string sign_type = "RSA2";
        // 编码格式  
        public static string charset = "UTF-8";

        private static string generateOrderNumber()
        {
            return DateTime.Now.ToString("yyyyMMddHHmmssfff");
        }

        /*
         * 创建支付model
         * price:商品价格
         * title:商品标题
         * description:商品描述
         * **/
        public static AlipayTradePagePayModel creatModel(string price, string title, string description)
        {
            AlipayTradePagePayModel model = new AlipayTradePagePayModel();
            model.Body = description;
            model.Subject = title;
            //付款金额
            model.TotalAmount = price;
            //商户网站中唯一订单号
            model.OutTradeNo = generateOrderNumber();
            model.ProductCode = "FAST_INSTANT_TRADE_PAY";
            return model;
        }

    }

Controller层主要接口:

用户点击支付后的跳转接口

public string Index()
        {
            DefaultAopClient client = new DefaultAopClient(AlipayConfig.gateway, AlipayConfig.appId, AlipayConfig.privateKey, "json", "1.0", AlipayConfig.sign_type, AlipayConfig.alipayPublicKey, AlipayConfig.charset, false);
            //金额格式必须是小数点后两位数或是正整数且不是金额格式(即$123.00),以及非常重要的一个原则,传递的参数要么不传递这个参数(即传递的众多参数中,这个参数完全不存在
            AlipayTradePagePayModel model = AlipayConfig.creatModel("9.90","测试商品","测试商品支付");
            AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
            //支付成功的回调地址
            request.SetReturnUrl("xxxxxxxxxx");
            // 设置异步通知接收地址,需要公网能够访问
            request.SetNotifyUrl("xxxxxxxxxx");
            // 将业务model载入到request
            request.SetBizModel(model);

            AlipayTradePagePayResponse response = null;
            try
            {
                response = client.pageExecute(request);
                return response.Body;
            }
            catch (Exception exp)
            {
                throw exp;
            }

        }

在此过程中支付宝会返回给我们一个表单数据,此表单不需要我们做任何操作,自动提交

同步回调地址为通知用户付款操作后自定义的跳转页面,异步回调地址为支付宝通知后台用户付款操作的结果;具体操作结果以异步通知结果为准。

异步回调接口:

public void notify()
        {
            Dictionary<string, string> sArray = GetRequestPost();
            if(sArray.Count > 0){
                

                //调用SDK验签方法
                bool signVerified = AlipaySignature.RSACheckV1(sArray, AlipayConfig.alipayPublicKey, AlipayConfig.charset);
                //此判断结果中书写业务逻辑代码
                if (signVerified)   //验证支付发过来的消息,签名是否正确
                {
                    //商户订单号
                    string out_trade_no = Request.Form["out_trade_no"];

                    //支付宝交易号
                    string trade_no = Request.Form["trade_no"];

                    //交易状态
                    string trade_status = Request.Form["trade_status"];


                    if (Request.Form["trade_status"] == "TRADE_FINISHED")
                    {
                        //判断该笔订单是否在商户网站中已经做过处理
                        //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
                        //如果有做过处理,不执行商户的业务程序

                        //注意:
                        //退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知
                    }
                    else if (Request.Form["trade_status"] == "TRADE_SUCCESS")
                    {
                        //判断该笔订单是否在商户网站中已经做过处理
                        //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
                        //如果有做过处理,不执行商户的业务程序

                        //注意:
                        //付款完成后,支付宝系统发送该交易状态通知
                        
                        //请在这里加上商户的业务逻辑程序代码

                    }
                    else
                    {
                    }
                    Response.Write("success");     //返回给支付宝消息,成功,这里不要动!!!
                }
                else
                {
                    Response.Write("fail");
                }
            }

        }

        public Dictionary<string, string> GetRequestPost()
        {
            int i = 0;
            Dictionary<string, string> sArray = new Dictionary<string, string>();
            NameValueCollection coll;
            coll = Request.Form;
            String[] requestItem = coll.AllKeys;
            for (i = 0; i < requestItem.Length; i++)
            {
                sArray.Add(requestItem[i], Request.Form[requestItem[i]]);
            }
            return sArray;

        }

至此,支付接口调用完成。

支付宝对于接口调用 的文档说明还是比较清晰详细的,仔细去阅读以下文档,思路还是较为清晰的

$flag 上一页 下一页