一.前期准备
在支付宝沙箱进行信息配置,用密钥生成工具进行生成密钥。密钥工具
1.用工具生成密钥
2.进入开发者中心进行相应的配置
3.设置公钥
3.查看支付宝公钥并保存
二.调用接口进行支付
1.nuget安装AlipaySDKNet
2.配置支付信息
public class AliPayConfig { // APP_ID public static string app_id = "2016092800617380"; //网关 public static string gatewayUrl = "https://openapi.alipaydev.com/gateway.do"; //应用私钥 public static string private_key = "MIIEogIBAAKCAQEiV0gKlWymAomcmCGqoA9JycNuLfmWOJfI3pLsszCYaLOLNtGq/MZOaWbV9idmGwqdTkR+B9eddKgnjS+A2Gc84="; //支付宝公钥 public static string alipay_public_key = "MIIBIjANBgkqhkiG9w0jfgQIDAQAB"; // 签名方式 public static string sign_type = "RSA2"; // 编码格式 public static string charset = "UTF-8"; }
3.前端的简单代码实现
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> <script src="~/Content/jquery-3.3.1/jquery-3.3.1.js"></script> </head> <body> <form action="/Pay/Index" method="post"> <div style="text-align:center;"> <div style="margin-top:50px"> 价格: <input id="amount" name="money"/> </div> <div style="margin-top:50px"> <button type="submit" id="Pay">付款</button> </div> </div> </form> </body> </html>
4.后端代码实现,支付、同步通知、异步通知
using Aop.Api; using Aop.Api.Domain; using Aop.Api.Request; using Aop.Api.Response; using Aop.Api.Util; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Web; using System.Web.Mvc; namespace AliPay.Controllers { public class PayController : Controller { // GET: Pay [HttpGet] public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index(double money) { DefaultAopClient client = new DefaultAopClient(AliPayConfig.gatewayUrl, AliPayConfig.app_id, AliPayConfig.private_key, "json", "1.0", AliPayConfig.sign_type, AliPayConfig.alipay_public_key, AliPayConfig.charset, false); //var s = db.View_OrderDetail.Where(x => x.OrdId == ordNo).FirstOrDefault(); // 外部订单号,商户网站订单系统中唯一的订单号 string out_trade_no =Guid.NewGuid().ToString(); // 订单名称 string subject = "网上购物"; // 付款金额 string total_amout = money.ToString(); // 商品描述 string body = "test"; // 组装业务参数model AlipayTradePagePayModel model = new AlipayTradePagePayModel(); model.Body = body; model.Subject = subject; model.TotalAmount = total_amout; model.OutTradeNo = out_trade_no; model.ProductCode = "FAST_INSTANT_TRADE_PAY"; AlipayTradePagePayRequest request = new AlipayTradePagePayRequest(); // 设置同步回调地址 request.SetReturnUrl("http://localhost:24939/Pay/ReturnUrl"); // 设置异步通知接收地址 request.SetNotifyUrl("http://www.oneteam.com/Pay/NotifyUrl"); //必须为公网地址,能被访问到 // 将业务model载入到request request.SetBizModel(model); AlipayTradePagePayResponse response = null; try { response = client.pageExecute(request, null, "post"); //response = client.Execute(request); Response.Write(response.Body); } catch (Exception exp) { throw exp; } return View(); } /// <summary> /// 同步通知 /// </summary> /// <returns></returns> public ActionResult ReturnUrl() { /* 实际验证过程建议商户添加以下校验。 1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号, 2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额), 3、校验通知中的seller_id(或者seller_email) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email) 4、验证app_id是否为该商户本身。 */ Dictionary<string, string> sArray = GetRequestGet(); if (sArray.Count != 0) { //验签 bool flag = AlipaySignature.RSACheckV1(sArray,AliPayConfig.alipay_public_key,AliPayConfig.charset,AliPayConfig.sign_type,false); if (flag) { return Redirect("/Home/Index"); //return Content("付款成功"); } else { return Content("付款失败"); } } else { return Content("同步验证失败"); } } /// <summary> /// 同步通知 /// </summary> /// <returns></returns> public ActionResult NotifyUrl() { Dictionary<string, string> sArray = GetRequestPost(); if (sArray.Count != 0) { //验签 bool flag = AlipaySignature.RSACheckV1(sArray,AliPayConfig.alipay_public_key,AliPayConfig.charset,AliPayConfig.sign_type,false); if (flag) { //交易状态 //判断该笔订单是否在商户网站中已经做过处理 //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序 //请务必判断请求时的total_amount与通知时获取的total_fee为一致的 //如果有做过处理,不执行商户的业务程序 //注意: //退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知 string trade_status = Request.Form["trade_status"]; Response.Write("success"); } else { Response.Write("fail"); } } return Content("异步通知"); } //获取url中返回的参数,进行后续的同步通知 public Dictionary<string, string> GetRequestGet() { int i = 0; Dictionary<string, string> sArray = new Dictionary<string, string>(); NameValueCollection coll; //coll = Request.Form; coll = Request.QueryString; Console.WriteLine(coll); String[] requestItem = coll.AllKeys; for (i = 0; i < requestItem.Length; i++) { sArray.Add(requestItem[i], Request.QueryString[requestItem[i]]); } return sArray; } //获取post返回的参数,进行后续的异步通知 public Dictionary<string, string> GetRequestPost() { int i = 0; Dictionary<string, string> sArray = new Dictionary<string, string>(); NameValueCollection coll; //coll = Request.Form; coll = Request.Form; String[] requestItem = coll.AllKeys; for (i = 0; i < requestItem.Length; i++) { sArray.Add(requestItem[i], Request.Form[requestItem[i]]); } return sArray; } } }