近来也是为了新产物忙得腾飞,博客都更新的慢了。新产物为了轻易用户付出,须要付出宝扫码接入。这活落到了我的身上。产物是Windows体系下的桌面软件,经由历程软件生成二维码付出。界面以原生的MVVM编写,下面叙说一下基础的历程,做过的老司机能够直接点封闭了。
二、要求接口
要求接口是第一步,起首有这么几件事:
公司具有付出宝账户
公司具有业务天资(空话)
建立运用,签约电脑网站付出,手机付出,App付出。
建立私钥、公钥、付出宝公钥
设置网关及回调地点
须要注重的是以下几点:
建立运用时,称号不要带有“付出”、“pay”等字样,图片发起高清
建立运用时,签约付出须要一些要求材料,如:业务天资照片,公司照片4张,运用的引见(称号,下载地点,公司网站是不是有该运用,该运用涌现付出宝付出的界面款式)
签约后须要考核,大抵一天,(阿里确切快,腾讯微信要4天),考核经由历程会发送一份邮件,内里有链接,点击链接完成签约
建立私钥、公钥、付出宝公钥,在付出宝接口网站上有官方东西,下载运用即可
网关与回调地点要与公司网站组成关联,比方是二级域名;假如网关、回调地点与公司网站没什么联络,生怕不可。
三、代码流程
有三个组成元素。客户端软件,商户效劳器背景,付出宝背景
客户端软件点击“猎取付出二维码”去取得一个可付出的二维码:
封装客户端的一些必要信息发送给商户效劳器背景组成一个商户定单
/// <summary> /// 猎取二维码信息 /// </summary> /// <param name="packageClientInfo">封装信息</param> /// <param name="serverAddress">商户产物效劳器地点</param> /// <returns></returns> public static void GetQRCodeInfo(string packageClientInfo, string serverAddress, Action<string> 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<HttpResponseMessage> 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<string>(result)); //注重这个托付 return result; }); } } catch { // ignored } } }
这里的托付要领是用来生成二维码的,当你从这个“api/AlipayForProduct/GetQRCodeString”返回一些字符串(result),比方返回的是:
"http://xxx.xxx.com/AlipayForProduct/SendInfoToAlipay?ordernumber=" + $"{orderNumber}";(orderNumber为商户定单号)
然后运用ThoughtWorks.QRCode.dll去生成二维码
/// <summary> /// 依据字符串获得响应的二维码 /// </summary> /// <param name="qrInfo"></param> /// <param name="productName"></param> /// <param name="version"></param> /// <returns></returns> 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<string, string> 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(); } /// <summary> /// 参数排序字典 /// </summary> /// <returns></returns> private SortedDictionary<string, string> GetRequestGet() { SortedDictionary<string, string> sArray = new SortedDictionary<string, string>(); NameValueCollection coll = Request.QueryString; String[] requestItem = coll.AllKeys; foreach (string t in requestItem) { sArray.Add(t, Request.QueryString[t]); } return sArray; }
以上就是.NET付出宝App付出接入的实例剖析的细致内容,更多请关注ki4网别的相干文章!