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

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

主要代码如下:

创建支付配置类

public class AlipayConfig
    {
        //APPID
        public static string appId = "";
        //支付宝网关
        public static string gateway = "https://openapi.alipaydev.com/gateway.do";
        //用户私钥
        public static string privateKey = "";
        //支付宝公钥
        public static string alipayPublicKey = "";
        //支付成功的回调地址
        public static string returnURL = "";
        // 签名方式  
        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;

        }

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

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

原文地址:https://www.cnblogs.com/xiaowangxiao/p/12209553.html