.NET支付宝接口

简介:

 

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using  System;
using  System.Data;
using  System.Configuration;
using  System.Collections;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.Text;
public  partial  class  pay : System.Web.UI.Page
{
     protected  void  Page_Load( object  sender, EventArgs e)
     {
         Encoding gb2312 = Encoding.GetEncoding( "gb2312" );
         Response.ContentEncoding = gb2312;
         Request.ContentEncoding = gb2312;
     }
     protected  void  ImageButton1_Click( object  sender, ImageClickEventArgs e)
     {
         if  (Request.Cookies[FormsAuthentication.FormsCookieName] != null )
         {
             Vlike_DB_read a = new  Vlike_DB_read();
             string  zfb_ddh = a.get_zfb_id(Membership.GetUser().UserName, Convert.ToInt32(DropDownList1.SelectedValue));
             Response.Redirect(img( "0001" , "包月" , "影视包月  10元/月" , zfb_ddh, "10.00" , Membership.GetUser().UserName.ToUpper(), DropDownList1.SelectedValue));
         }
         else
         {
             Response.Write( "<script type=" text/javascript ">alert(" 请先登陆! ");</script>" );
         }
     }
     private  string  img( string  strcmd, string  strSub, string  strSubinfo, string  strid, string  strMoney, string  strUser, string  strNum)
     {
         string  strsellerEmail = "341081@qq.com" ;          //卖家支付宝帐号
         string  strAc = "" ;            //卖家支付宝安全校验码
         string  INTERFACE_URL = "https://www.alipay.com/payto:" ;
         string  strCmd = strcmd;           //命令字
         string  strSubject = strSub;       //商品名
         string  strBody = strSubinfo;      //商品描述
         string  strOrder_no = strid;       //商户订单号
         string  strPrice = strMoney;       //商品单价 0.01~50000.00
         string  rurl = "http://" ;          //商品展示网址
         string  strType = "2" ;             //type支付类型    1:商品购买2:服务购买3:网络拍卖4:捐赠
         string  strNumber = strNum;        //购买数量
         string  strTransport = "3" ;        //发货方式        1:平邮2:快递3:虚拟物品
         string  strOrdinary_fee = "" ;      //平邮运费
         string  strExpress_fee = "" ;       //快递运费
         string  strReadOnly = "true" ;      //交易信息是否只读
         string  strBuyer_msg = "" ;         //买家给卖家的留言
         string  strBuyer = "" ;             //买家EMAIL
         string  strBuyer_name = strUser;   //买家姓名
         string  strBuyer_address = "" ;     //买家地址
         string  strBuyer_zipcode = "" ;     //买家邮编
         string  strBuyer_tel = "" ;         //买家电话号码
         string  strBuyer_mobile = "" ;      //买家手机号码
         string  strPartner = "" ;           //合作伙伴ID    保留字段
         return  CreatUrl(strsellerEmail, strAc, INTERFACE_URL, strCmd, strSubject, strBody,
             strOrder_no, strPrice, rurl, strType, strNumber, strTransport,
             strOrdinary_fee, strExpress_fee, strReadOnly, strBuyer_msg, strBuyer,
             strBuyer_name, strBuyer_address, strBuyer_zipcode, strBuyer_tel,
             strBuyer_mobile, strPartner);
     }
     private  string  CreatUrl(
         string  strsellerEmail,
         string  strAc,
         string  INTERFACE_URL,
         string  strCmd,
         string  strSubject,
         string  strBody,
         string  strOrder_no,
         string  strPrice,
         string  rurl,
         string  strType,
         string  strNumber,
         string  strTransport,
         string  strOrdinary_fee,
         string  strExpress_fee,
         string  strReadOnly,
         string  strBuyer_msg,
         string  strBuyer,
         string  strBuyer_name,
         string  strBuyer_address,
         string  strBuyer_zipcode,
         string  strBuyer_tel,
         string  strBuyer_mobile,
         string  strPartner)
     {   //以下参数值不能留空
         string  str2CreateAc = "" ;
         str2CreateAc += "cmd"  + strCmd + "subject"  + strSubject;
         str2CreateAc += "body"  + strBody;
         str2CreateAc += "order_no"  + strOrder_no;
         str2CreateAc += "price"  + strPrice;
         str2CreateAc += "url"  + rurl;
         str2CreateAc += "type"  + strType;
         str2CreateAc += "number"  + strNumber;
         str2CreateAc += "transport"  + strTransport;
         str2CreateAc += "ordinary_fee"  + strOrdinary_fee;
         str2CreateAc += "express_fee"  + strExpress_fee;
         str2CreateAc += "readonly"  + strReadOnly;
         str2CreateAc += "buyer_msg"  + strBuyer_msg;
         str2CreateAc += "seller"  + strsellerEmail;
         str2CreateAc += "buyer"  + strBuyer;
         str2CreateAc += "buyer_name"  + strBuyer_name;
         str2CreateAc += "buyer_address"  + strBuyer_address;
         str2CreateAc += "buyer_zipcode"  + strBuyer_zipcode;
         str2CreateAc += "buyer_tel"  + strBuyer_tel;
         str2CreateAc += "buyer_mobile"  + strBuyer_mobile;
         str2CreateAc += "partner"  + strPartner;
         str2CreateAc += strAc;
         string  acCode = GetMD5(str2CreateAc);
         string  parameter = "" ;
         parameter += INTERFACE_URL + strsellerEmail + "?cmd="  + strCmd;
         parameter += "&subject="  + Server.UrlEncode(strSubject);
         parameter += "&body="  + Server.UrlEncode(strBody);
         parameter += "&order_no="  + strOrder_no;
         parameter += "&url="  + rurl;
         parameter += "&price="  + strPrice;
         parameter += "&type="  + strType;
         parameter += "&number="  + strNumber;
         parameter += "&transport="  + strTransport;
         parameter += "&ordinary_fee="  + strOrdinary_fee;
         parameter += "&express_fee="  + strExpress_fee;
         parameter += "&readonly="  + strReadOnly;
         parameter += "&buyer_msg="  + strBuyer_msg;
         parameter += "&buyer="  + strBuyer;
         parameter += "&buyer_name="  + Server.UrlEncode(strBuyer_name);
         parameter += "&buyer_address="  + strBuyer_address;
         parameter += "&buyer_zipcode="  + strBuyer_zipcode;
         parameter += "&buyer_tel="  + strBuyer_tel;
         parameter += "&buyer_mobile="  + strBuyer_mobile;
         parameter += "&partner="  + strPartner;
         parameter += "&ac="  + acCode;
         return  parameter;
     }
     private  static  string  GetMD5( string  s)
     {
         System.Security.Cryptography.MD5 md5 = new  System.Security.Cryptography.MD5CryptoServiceProvider();
         byte [] t = md5.ComputeHash(System.Text.Encoding.GetEncoding( "gb2312" ).GetBytes(s));
         System.Text.StringBuilder sb = new  System.Text.StringBuilder(32);
         for  ( int  i = 0; i < t.Length; i++)
         {
             sb.Append(t[i].ToString( "x" ).PadLeft(2, "0" ));
         }
         return  sb.ToString();
     }
}

 

 

接收支付宝信息并进行相应操作

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<%@ Page Language= "C#"  %>
<%@ Import Namespace= "System.Data.SqlClient"  %>
<%@ Import Namespace= "System.Data"  %>
<%@ Import Namespace= "System.Web"  %>
<%@ Import Namespace= "System.IO"  %>
<%@ Import Namespace= "System.Net"  %>
<%@ Import Namespace= "System.Text"  %>
<%@ Import Namespace= "System.Security.Cryptography"  %>
<script type= "text/C#"  runat= "server" >
private  string  returnTxt = "N" ;              //返回给支付宝通知接口的结果
private  string  alipayNotifyURL = "http://notify.alipay.com/trade/notify_query.do?" ;    //支付宝查询接口URL
private  string  myalipayEmail = "341081@qq.com" ;            //商户的支付宝Email
private  string  constPaySecurityCode = "" ;                  //码
private  SqlConnection conn = new  SqlConnection();
private  string  GetMD5( string  s)
{
     System.Security.Cryptography.MD5 md5 = new  System.Security.Cryptography.MD5CryptoServiceProvider();
     byte [] t = md5.ComputeHash(System.Text.Encoding.GetEncoding( "gb2312" ).GetBytes(s));
     StringBuilder sb = new  StringBuilder(32);
     for  ( int  i = 0; i < t.Length; i++)
     {
         sb.Append(t[i].ToString( "x" ).PadLeft(2, "0" ));
     }
     return  sb.ToString();
}
private  String Get_Http(String a_strUrl, int  timeout)
{
     string  strResult;
     try
     {
         System.Net.HttpWebRequest myReq = (System.Net.HttpWebRequest) System.Net.HttpWebRequest.Create(a_strUrl);
         myReq.Timeout = timeout;
         System.Net.HttpWebResponse HttpWResp = (System.Net.HttpWebResponse) myReq.GetResponse();
         Stream myStream = HttpWResp.GetResponseStream();
         StreamReader sr = new  StreamReader(myStream, System.Text.Encoding.Default);
         System.Text.StringBuilder strBuilder = new  System.Text.StringBuilder();
         while  (-1 != sr.Peek())
         {
             strBuilder.Append(sr.ReadLine() + "\r\n" );
         }
         strResult = strBuilder.ToString();
     }
     catch  (Exception exp)
     {
         strResult = "错误:"  + exp.Message;
     }
     return  strResult;
}
public  void  send( string  sql)
{
     string  dbconn = "data source=127.0.0.1;UID=Cm;PWD=fallenangel;database=text" ;
     conn = new  SqlConnection(dbconn);
     conn.Open();
     SqlCommand cmd = new  SqlCommand(sql, conn);
     int  aa = cmd.ExecuteNonQuery();
     conn.Close();
}
private  void  Page_Load( object  sender, System.EventArgs e)
{
     //检查支付宝通知接口传递过来的参数是否合法
     string  msg_id = System.Web.HttpContext.Current.Request[ "msg_id" ];
     string  order_no = System.Web.HttpContext.Current.Request[ "order_no" ];
     string  gross = System.Web.HttpContext.Current.Request[ "gross" ];
     string  buyer_email = System.Web.HttpContext.Current.Request[ "buyer_email" ];
     string  buyer_name = System.Web.HttpContext.Current.Request[ "buyer_name" ];
     string  buyer_address = System.Web.HttpContext.Current.Request[ "buyer_address" ];
     string  buyer_zipcode = System.Web.HttpContext.Current.Request[ "buyer_zipcode" ];
     string  buyer_tel = System.Web.HttpContext.Current.Request[ "buyer_tel" ];
     string  buyer_mobile = System.Web.HttpContext.Current.Request[ "buyer_mobile" ];
     string  action = System.Web.HttpContext.Current.Request[ "action" ];
     string  s_date = System.Web.HttpContext.Current.Request[ "date" ];
     string  ac = System.Web.HttpContext.Current.Request[ "ac" ];
     string  notify_type = System.Web.HttpContext.Current.Request[ "notify_type" ];
     alipayNotifyURL = alipayNotifyURL + "msg_id="  + msg_id + "&email="  + myalipayEmail + "&order_no="  + order_no;
     //获取支付宝ATN返回结果,true和false都是正确的订单信息,invalid 是无效的
     string  responseTxt = Get_Http(alipayNotifyURL, 120000);
     string  Str = "msg_id"  + msg_id + "order_no"  + order_no + "gross"  + gross + "buyer_email"  + buyer_email + "buyer_name"  + buyer_name + "buyer_address"  + buyer_address + "buyer_zipcode"  + buyer_zipcode + "buyer_tel"  + buyer_tel + "buyer_mobile"  + buyer_mobile + "action"  + action + "date"  + s_date + constPaySecurityCode;
     string  ac_code = GetMD5(Str);
     if  (action == "test" ) //支付宝接口测试是否有效
     {
         returnTxt = "Y" ;
     }
     if  (action == "sendOff" //发货通知
     {
         if  (responseTxt.Substring(0, 4) == "true"
             || responseTxt.Substring(0, 4) == "fals" ) //ATN,验证消息是否支付宝发过来
         {
             if  (ac_code == ac) //验证消息是否被修改
             {
                 //数据库操作
             }
         }
     }
     if  (action == "checkOut" //交易完成通知
     {
         returnTxt = "N" ;
         if  (responseTxt.Substring(0, 4) == "true"
             || responseTxt.Substring(0, 4) == "fals" ) //ATN,验证消息是否支付宝发过来
         {
             if  (ac_code == ac) //验证消息是否被修改
             {
                 //数据库操作   
             }
         }
     }
     System.Web.HttpContext.Current.Response.Write(returnTxt);
}
</script>

    本文转自曾祥展博客园博客,原文链接:http://www.cnblogs.com/zengxiangzhan/archive/2009/12/31/1636915.html ,如需转载请自行联系原作者

相关文章
.net开发支付宝接口,公私钥正确但一直报私钥格式错误排查方案
报错描述 最近有用户测试.net测试,公私钥明明没问题,可是一直报私钥格式错误,如下图所示  报错代码   排查方案 1、检查私钥格式是否是pkcs1格式; 2、检查私钥是否是一行,以及没有空格、换行等; 3、若确定自己生成的公私钥是否匹配; 如何验证公钥正确性测试步骤:https://openclub.
1090 0
|
2月前
|
开发框架 前端开发 JavaScript
ASP.NET MVC 教程
ASP.NET 是一个使用 HTML、CSS、JavaScript 和服务器脚本创建网页和网站的开发框架。
38 7
|
2月前
|
存储 开发框架 前端开发
ASP.NET MVC 迅速集成 SignalR
ASP.NET MVC 迅速集成 SignalR
53 0
|
3月前
|
开发框架 前端开发 .NET
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
46 0
|
3月前
|
开发框架 前端开发 安全
ASP.NET MVC 如何使用 Form Authentication?
ASP.NET MVC 如何使用 Form Authentication?
|
3月前
|
开发框架 .NET
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
118 0
|
6月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
187 0
|
6月前
|
开发框架 前端开发 JavaScript
JavaScript云LIS系统源码ASP.NET CORE 3.1 MVC + SQLserver + Redis医院实验室信息系统源码 医院云LIS系统源码
实验室信息系统(Laboratory Information System,缩写LIS)是一类用来处理实验室过程信息的软件,云LIS系统围绕临床,云LIS系统将与云HIS系统建立起高度的业务整合,以体现“以病人为中心”的设计理念,优化就诊流程,方便患者就医。
78 0
|
6月前
|
开发框架 前端开发 .NET
C# .NET面试系列六:ASP.NET MVC
<h2>ASP.NET MVC #### 1. MVC 中的 TempData\ViewBag\ViewData 区别? 在ASP.NET MVC中,TempData、ViewBag 和 ViewData 都是用于在控制器和视图之间传递数据的机制,但它们有一些区别。 <b>TempData:</b> 1、生命周期 ```c# TempData 的生命周期是短暂的,数据只在当前请求和下一次请求之间有效。一旦数据被读取,它就会被标记为已读,下一次请求时就会被清除。 ``` 2、用途 ```c# 主要用于在两个动作之间传递数据,例如在一个动作中设置 TempData,然后在重定向到另
324 5