服务器之家

服务器之家 > 正文

.net接入支付宝的支付接口

时间:2020-05-05 15:38     来源/作者:勤奋的小鑫0

H5手机网站接入支付宝支付接口,推荐使用支付宝提供的SDK来快速开发

我使用的是SDK开发

引用命名空间

?
1
2
3
4
using Aop.Api;
using Aop.Api.Request;
using Aop.Api.Response;
using Aop.Api.Util;

首页需要定义一些常量

?
1
2
3
4
5
6
7
8
9
10
11
static string serverUrl = "https://openapi.alipaydev.com/gateway.do";
   static string app_id = "**"//开发者的应用ID
   static string format = "JSON";
   static string charset = "utf-8";
   static string sign_type = "RSA2"; //签名格式
   static string version = "1.0";
   string UID = "2088102169707816";//卖家支付宝账户号
   //商户私钥
   static string merchant_private_key = "***";
   //支付宝公钥
   static string alipay_public_key = "***";

这里的app_id,merchant_private_key,alipay_public_key 我就没有列出来了,获取的方法需要自己去支付宝平台完成一些操作进行获取

在用户点击网站付款时,我们需要唤醒支付宝,来进行支付

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public string H5RequestPayWay(OrderPO order)
    {
      IAopClient client = new DefaultAopClient(serverUrl, app_id, merchant_private_key, format, version, sign_type, alipay_public_key, charset, false);
      AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
      string address= "http://m." + PathLogic1.RootDomain;
      request.SetReturnUrl(address+ "/WebPay/AlipayPayResult");//同步请求
      request.SetNotifyUrl(address + "/WebPay/AsyncPay");//异步请求
      var lstDetail = Context.Data.OrderDetail.Where(x => x.OrderNo == order.OrderNo).ToSelectList(x=>new { x.SkuName});
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < lstDetail.Count(); i++)
      {
        sb.Append(lstDetail[i].SkuName + ",");
      }
      request.BizContent = "{" +
  "\"body\":\""+sb.ToString().Substring(0,sb.Length-1)+"\"," +
  "\"subject\":\"袋鼠巴巴商品支付\"," +
  "\"out_trade_no\":\""+order.OrderNo+"\"," +
  "\"timeout_express\":\"90m\"," +
  "\"total_amount\":"+(order.TotalAmount.Value+order.TotalFreight.Value)+"," +
  "\"product_code\":\"QUICK_WAP_PAY\"" +
  " }";
 
      AlipayTradeWapPayResponse response = client.pageExecute(request);
       return response.Body;
    }

具体发送给支付宝的参数,自行去查看

执行方法后,买家输入自己的支付宝账号密码进行支付,支付成功的结果,支付宝会以post的方式异步请求你的SetNotifyUrl的地址

这个SetNotifyUrl的地址必须要外网可以访问,支付宝的请求才能进来

买家支付成功,商家修改订单状态和数据库的操作,都在异步请求中执行

同步请求

?
1
2
3
4
5
public ActionResult AlipayPayResult()
    {
      ViewBag.result = "success";
      return View("PayResult");
    }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
     /// <summary>
    /// 验证通知数据的正确性
    /// </summary>
    /// <param name="out_trade_no"></param>
    /// <param name="total_amount"></param>
    /// <param name="seller_id"></param>
    /// <returns></returns>
private SortedDictionary<string, string> GetRequestPost()
    {
      int i = 0;
      SortedDictionary<string, string> sArray = new SortedDictionary<string, string>();
      NameValueCollection coll;
      //Load Form variables into NameValueCollection variable.
      coll = Request.Form;
 
      // Get names of all forms into a string array.
      String[] requestItem = coll.AllKeys;
 
      for (i = 0; i < requestItem.Length; i++)
      {
        sArray.Add(requestItem[i], Request.Form[requestItem[i]]);
      }
 
      return sArray;
    }
?
1
2
3
4
5
6
7
8
9
10
11
/// <summary>
   /// 验签
   /// </summary>
   /// <param name="inputPara"></param>
   /// <returns></returns>
   public Boolean Verify(SortedDictionary<string, string> inputPara)
   {
     Dictionary<string, string> sPara = new Dictionary<string, string>();   
     Boolean verifyResult = AlipaySignature.RSACheckV1(inputPara, alipay_public_key, charset,sign_type,false);
     return verifyResult;
   }

异步请求:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
[HttpPost]
   public void AsyncPay()
   {
     SortedDictionary<string, string> sPara = GetRequestPost();//将post请求过来的参数传化为SortedDictionary
     if (sPara.Count > 0)
     {
       AlipayTradeWayPayServer pay = new AlipayTradeWayPayServer();
       Boolean VerifyResult = pay.Verify(sPara);//验签if (VerifyResult)
       {
         try
         {
            //商户订单号
           string out_trade_no = Request.Form["out_trade_no"];
           //支付宝交易号
           string trade_no = Request.Form["trade_no"];
           //支付金额
           decimal total_amount = Request.Form["total_amount"].ConvertType(Decimal.Zero);
           //实收金额
           //decimal receipt_amount = Request.Form["receipt_amount"].ConvertType(Decimal.Zero);
           //交易状态
           string trade_status = Request.Form["trade_status"];
           //卖家支付宝账号
           string seller_id = Request.Form["seller_id"];
 
           //商品描述
           string body = Request.Form["body"];
           //交易创建时间
           DateTime gmt_create = DateTime.Parse(Request.Form["gmt_create"]);
           //交易付款时间
           DateTime gmt_payment = DateTime.Parse(Request.Form["gmt_payment"]);
           string appid = Request.Form["app_id"];
           WriteError("验证参数开始");
           Boolean DataValidity = pay.CheckInform(out_trade_no, total_amount, seller_id, appid);//商家判断参数时候是否匹配if (DataValidity)
           {
             if (Request.Form["trade_status"] == "TRADE_FINISHED")
             {
               AlipayWayPayPO model = CreateAlipayWayPay(out_trade_no, trade_no, trade_status, gmt_create, gmt_payment);
               pay.PaySuccess(out_trade_no, model, Server.MapPath("~/" + DateTime.Today.ToString("yyMMdd") + ".txt"));//修改订单
               //注意:
               //退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知
             }
             else if (Request.Form["trade_status"] == "TRADE_SUCCESS")
             {
               AlipayWayPayPO model = CreateAlipayWayPay(out_trade_no, trade_no, trade_status, gmt_create, gmt_payment);
               pay.PaySuccess(out_trade_no, model, Server.MapPath("~/" + DateTime.Today.ToString("yyMMdd") + ".txt"));//修改订单
               //注意:
               //付款完成后,支付宝系统发送该交易状态通知
             }
             else
             {
 
             }
 
             //——请根据您的业务逻辑来编写程序(以上代码仅作参考)——
 
             Response.Write("success"); //请不要修改或删除
 
             /////////////////////////////////////////////////////////////////////////////////////////////////////////////
           }
         }
         catch (Exception ex)
         {
           
         }
       }
       else//验证失败
       {
         Response.Write("fail");
       }
     }
     else
     {
       Response.Write("无通知参数");
     }
   }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

相关文章

热门资讯

沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意 2019-07-07
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
超A是什么意思 你好a表达的是什么
超A是什么意思 你好a表达的是什么 2019-06-06
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
返回顶部