.NET支付宝App支付接入的实例分析

一、前言

       最近也是为了新产品忙得起飞,博客都更新的慢了。新产品为了方便用户支付,需要支付宝扫码接入。这活落到了我的身上。产品是Windows系统下的桌面软件,通过软件生成二维码支付。界面以原生的MVVM编写,下面叙述一下基本的过程,做过的老司机可以直接点关闭了。

二、申请接口

        申请接口是第一步,首先有这么几件事:

公司具有支付宝账户

公司具有营业资质(废话)

创建应用,签约电脑网站支付,手机支付,App支付。

创建私钥、公钥、支付宝公钥

配置网关及回调地址

        需要注意的是以下几点:

创建应用时,名称不要带有“支付”、“pay”等字样,图片建议高清

创建应用时,签约支付需要一些申请材料,如:营业资质照片,公司照片4张,应用的介绍(名称,下载地址,公司网站是否有该应用,该应用出现支付宝支付的界面样式)

签约后需要审核,大致一天,(阿里确实快,腾讯微信要4天),审核通过会发送一份邮件,里面有链接,点击链接完成签约

创建私钥、公钥、支付宝公钥,在支付宝接口网站上有官方工具,下载使用即可

网关与回调地址要与公司网站形成关联,比如是二级域名;如果网关、回调地址与公司网站没什么联系,恐怕不行。

三、代码流程

        有三个构成元素。客户端软件,商户服务器后台,支付宝后台

        客户端软件点击“获取支付二维码”去获得一个可支付的二维码:

        封装客户端的一些必要信息发送给商户服务器后台形成一个商户订单

        ///         /// 获取二维码信息        ///         /// 封装信息        /// 商户产品服务器地址        ///         public static void GetQRCodeInfo(string packageClientInfo, string serverAddress, Action getQRCodeAction)        {            if (!string.IsNullOrEmpty(packageClientInfo))            {                try                {                    HttpClient httpsClient = new HttpClient                    {                        BaseAddress = new Uri(serverAddress),                        Timeout = TimeSpan.FromMinutes(20)                    };                    if (DsClientOperation.ConnectionTest(httpsClient))                    {                        StringContent strData = new StringContent(                                                           packageClientInfo,                                                           Encoding.UTF8,                                                           RcCommonNames.JasonMediaType);                                                                                   string PostUrl = httpsClient.BaseAddress + "api/AlipayForProduct/GetQRCodeString";                        Uri address = new Uri(PostUrl);                        Task response = httpsClient.PostAsync(address, strData);                        response.ContinueWith(                            (postTask) =>                            {                                                            if (postTask.IsFaulted)                                {                                                                    throw postTask.Exception;                                }                                HttpResponseMessage postResponse = postTask.Result;                                postResponse.EnsureSuccessStatusCode();                                                                var result = postResponse.Content.ReadAsStringAsync().Result;                                getQRCodeAction(JsonConvert.DeserializeObject(result)); //注意这个委托                                                                return result;                            });                    }                }                catch                {                    // ignored                }            }        }

         这里的委托方法是用来生成二维码的,当你从这个“api/AlipayForProduct/GetQRCodeString”返回一些字符串(result),比如返回的是:

         “http://xxx.xxx.com/AlipayForProduct/SendInfoToAlipay?ordernumber=” + $”{orderNumber}”;(orderNumber为商户订单号)

         然后使用ThoughtWorks.QRCode.dll去生成二维码

        ///         /// 根据字符串得到相应的二维码        ///         ///         ///         ///         ///         public static Image CreateQRCodeImage(string qrInfo, string productName, string version)        {            try            {                if (!string.IsNullOrEmpty(qrInfo))                {                    QRCodeEncoder encoder = new QRCodeEncoder                    {                        QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE,                        QRCodeScale = 4,                        QRCodeVersion = 0,                        QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M                    };                                        //编码方式(注意:BYTE能支持中文,ALPHA_NUMERIC扫描出来的都是数字)                                        //大小(值越大生成的二维码图片像素越高)                                        //版本(注意:设置为0主要是防止编码的字符串太长时发生错误)                                        //错误效验、错误更正(有4个等级)                    Image image = encoder.Encode(qrInfo, Encoding.GetEncoding("utf-8"));                                        string filename = $"{productName}_{version}.png";                                        var userLocalPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);                                        var docPath = Path.Combine(userLocalPath, @"YourProduct\QRCode");                                        if (!Directory.Exists(docPath))                    {                        Directory.CreateDirectory(docPath);                    }                    string filepath = Path.Combine(docPath, filename);                     using (FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write))                    {                        image.Save(fs, System.Drawing.Imaging.ImageFormat.Png);                        fs.Close();                        image.Dispose();                    }                    return image;                }            }            catch (Exception)            {                return null;            }            return null;        }

           这样就产生了二维码,说白了,就是把一个服务的api由字符串变成了图片,当用户使用支付宝app去扫这个二维码时,会去请求这个api:

            “http://xxx.xxx.com/AlipayForProduct/SendInfoToAlipay?ordernumber=” + $”{orderNumber}”;(orderNumber为商户订单号)    

          orderNumber = Request[ (! matchedItem = db.OrderInfoForProduct.FirstOrDefault(x => x.OrderNumber == (matchedItem !=  && matchedItem.IsPaid ==                      alipayServerURL =  app_id = privateKeyPem = format =  version =  signType =                      out_trade_no = orderNumber;                      product_code = ;                      total_amount = ;                       subject = ;                      body = ; =  returnurl = $ notifyurl == =  +                                      + body +  +                                      + subject +  +                                      + out_trade_no +  +                                      + total_amount +  +                                      + product_code +  +                                                         requestWap.SetReturnUrl(returnurl); = pNone =  + responseWap.Body +

         异步请求一般需要做这么几件事:

用户扫码支付完之后,支付宝后台会把所有需要验证的信息发给你,除了一个参数不需要验签完,其余都需要验签;

如果验签成功且支付状态也是成功交易后,你需要更新商户服务器后台关于此条商户订单的状态,比如将其支付状态变成已支付,填充支付时间等等;

            sPara = (sPara.Count >                  sign_type = Request.Form[                 seller_id = Request.Form[];                  trade_status = Request.Form[];                  notify_time = Request.Form[];                  app_id = Request.Form[];                  out_trade_no = Request.Form[];                  total_amount = Request.Form[];                  receipt_amount = Request.Form[];                  invoice_amount = Request.Form[];                  buyer_pay_amount = Request.Form[];                  body = Request.Form[];                  gmt_payment = Request.Form[];                  tradeGuid =                  isVerfied = AlipaySignature.RSACheckV1(sPara, alipayPublicKey, , sign_type,  (app_id == appID && seller_id == isTradeSuccess = .Equals(trade_status, ) || .Equals(trade_status,  (

         同步请求一般需要做这么几件事:

        1. 当异步调用完后,如果支付成功而且商户服务器后台对此条订单号处理也正确的话;同步请求可以再做一次验证

        2. 如果验证成功,跳转支付成功页面;如果失败,跳转支付失败页面。

        public ActionResult AlipayResult()        {            SortedDictionary sPara = GetRequestGet();            if (sPara.Count > 0)            {                //非验签参数                var sign_type = Request.QueryString["sign_type"];                //接收参数并排序                var seller_id = Request.QueryString["seller_id"]; //卖家支付宝用户号                var app_id = Request.QueryString["app_id"]; //开发者AppId                var out_trade_no = Request.QueryString["out_trade_no"]; //交易订单号                var orderNumberGuid = new Guid(out_trade_no);                try                {                    var isVerfied = AlipaySignature.RSACheckV1(sPara, alipayPublicKey, "utf-8", sign_type, false);                                    if (isVerfied)                    {                        if (app_id == appID && seller_id == sellerID)                        {                           //你的支付成功页面                        }                    }                }                catch                {                   //你的支付失败页面                }            }            else            {               //你的支付失败页面            }            return View();        }        ///         /// 参数排序字典        ///         ///         private SortedDictionary GetRequestGet()        {            SortedDictionary sArray = new SortedDictionary();            NameValueCollection coll = Request.QueryString;            String[] requestItem = coll.AllKeys;            foreach (string t in requestItem)            {                sArray.Add(t, Request.QueryString[t]);            }            return sArray;        }

 

以上就是.NET支付宝App支付接入的实例分析的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1432680.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 08:30:09
下一篇 2025年12月17日 08:30:25

相关推荐

发表回复

登录后才能评论
关注微信