C# 支付宝接口
生成URL链接
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Text;
12
13public partial class pay : System.Web.UI.Page
14{
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 Encoding gb2312 = Encoding.GetEncoding("gb2312");
18 Response.ContentEncoding = gb2312;
19 Request.ContentEncoding = gb2312;
20 }
21 protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
22 {
23 if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
24 {
25 Vlike_DB_read a = new Vlike_DB_read();
26 string zfb_ddh=a.get_zfb_id(Membership.GetUser().UserName,Convert.ToInt32(DropDownList1.SelectedValue));
27 Response.Redirect(img("0001", "包月", "影视包月 10元/月", zfb_ddh, "10.00", Membership.GetUser().UserName.ToUpper(), DropDownList1.SelectedValue));
28 }
29 else
30 {
31 Response.Write("<script type='text/javascript'>alert('请先登陆!');</script>");
32 }
33 }
34
35 private string img(string strcmd, string strSub, string strSubinfo, string strid, string strMoney, string strUser, string strNum)
36 {
37 string strsellerEmail = "341081@qq.com"; //卖家支付宝帐号
38 string strAc = ""; //卖家支付宝安全校验码
39 string INTERFACE_URL = "https://www.alipay.com/payto:";
40 string strCmd = strcmd; //命令字
41 string strSubject = strSub; //商品名
42 string strBody = strSubinfo; //商品描述
43 string strOrder_no = strid; //商户订单号
44 string strPrice = strMoney; //商品单价 0.01~50000.00
45 string rurl = "http://"; //商品展示网址
46 string strType = "2"; //type支付类型 1:商品购买2:服务购买3:网络拍卖4:捐赠
47 string strNumber = strNum; //购买数量
48 string strTransport = "3"; //发货方式 1:平邮2:快递3:虚拟物品
49 string strOrdinary_fee = ""; //平邮运费
50 string strExpress_fee = ""; //快递运费
51 string strReadOnly = "true"; //交易信息是否只读
52 string strBuyer_msg = ""; //买家给卖家的留言
53
54 string strBuyer = ""; //买家EMAIL
55 string strBuyer_name = strUser; //买家姓名
56 string strBuyer_address = ""; //买家地址
57 string strBuyer_zipcode = ""; //买家邮编
58 string strBuyer_tel = ""; //买家电话号码
59 string strBuyer_mobile = ""; //买家手机号码
60 string strPartner = ""; //合作伙伴ID 保留字段
61 return CreatUrl(strsellerEmail, strAc, INTERFACE_URL, strCmd, strSubject, strBody,
62 strOrder_no, strPrice, rurl, strType, strNumber, strTransport,
63 strOrdinary_fee, strExpress_fee, strReadOnly, strBuyer_msg, strBuyer,
64 strBuyer_name, strBuyer_address, strBuyer_zipcode, strBuyer_tel,
65 strBuyer_mobile, strPartner);
66 }
67 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)
68 { //以下参数值不能留空
69
70 string str2CreateAc = "";
71 str2CreateAc += "cmd" + strCmd + "subject" + strSubject;
72 str2CreateAc += "body" + strBody;
73 str2CreateAc += "order_no" + strOrder_no;
74 str2CreateAc += "price" + strPrice;
75 str2CreateAc += "url" + rurl;
76 str2CreateAc += "type" + strType;
77 str2CreateAc += "number" + strNumber;
78 str2CreateAc += "transport" + strTransport;
79 str2CreateAc += "ordinary_fee" + strOrdinary_fee;
80 str2CreateAc += "express_fee" + strExpress_fee;
81 str2CreateAc += "readonly" + strReadOnly;
82 str2CreateAc += "buyer_msg" + strBuyer_msg;
83 str2CreateAc += "seller" + strsellerEmail;
84 str2CreateAc += "buyer" + strBuyer;
85 str2CreateAc += "buyer_name" + strBuyer_name;
86 str2CreateAc += "buyer_address" + strBuyer_address;
87 str2CreateAc += "buyer_zipcode" + strBuyer_zipcode;
88 str2CreateAc += "buyer_tel" + strBuyer_tel;
89 str2CreateAc += "buyer_mobile" + strBuyer_mobile;
90 str2CreateAc += "partner" + strPartner;
91 str2CreateAc += strAc;
92
93
94 string acCode = GetMD5(str2CreateAc);
95
96 string parameter = "";
97
98 parameter += INTERFACE_URL + strsellerEmail + "?cmd=" + strCmd;
99 parameter += "&subject=" + Server.UrlEncode(strSubject);
100 parameter += "&body=" + Server.UrlEncode(strBody);
101 parameter += "&order_no=" + strOrder_no;
102 parameter += "&url=" + rurl;
103 parameter += "&price=" + strPrice;
104 parameter += "&type=" + strType;
105 parameter += "&number=" + strNumber;
106 parameter += "&transport=" + strTransport;
107 parameter += "&ordinary_fee=" + strOrdinary_fee;
108 parameter += "&express_fee=" + strExpress_fee;
109 parameter += "&readonly=" + strReadOnly;
110 parameter += "&buyer_msg=" + strBuyer_msg;
111 parameter += "&buyer=" + strBuyer;
112 parameter += "&buyer_name=" + Server.UrlEncode(strBuyer_name);
113 parameter += "&buyer_address=" + strBuyer_address;
114 parameter += "&buyer_zipcode=" + strBuyer_zipcode;
115 parameter += "&buyer_tel=" + strBuyer_tel;
116 parameter += "&buyer_mobile=" + strBuyer_mobile;
117 parameter += "&partner=" + strPartner;
118 parameter += "&ac=" + acCode;
119
120 return parameter;
121 }
122
123 private static string GetMD5(string s)
124 {
125
126 System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
127 byte[] t = md5.ComputeHash(System.Text.Encoding.GetEncoding("gb2312").GetBytes(s));
128 System.Text.StringBuilder sb = new System.Text.StringBuilder(32);
129 for (int i = 0; i < t.Length; i++)
130 {
131 sb.Append(t[i].ToString("x").PadLeft(2, '0'));
132 }
133 return sb.ToString();
134 }
135}
136
接收支付宝信息并进行相应操作
2<%@ Import NameSpace="System.Data"%>
3<%@ Import NameSpace="System.Web"%>
4<%@ Import NameSpace="System.IO"%>
5<%@ Import NameSpace="System.Net"%>
6<%@ Import NameSpace="System.Text"%>
7<%@ Import NameSpace="System.Security.Cryptography"%>
8<%@ Page Language="C#"%>
9
10<script language="C#" runat="server">
11 private string returnTxt = "N"; //返回给支付宝通知接口的结果
12 private string alipayNotifyURL = "http://notify.alipay.com/trade/notify_query.do?"; //支付宝查询接口URL
13 private string myalipayEmail = "341081@qq.com"; //商户的支付宝Email
14 private string constPaySecurityCode = ""; //码
15 private SqlConnection conn = new SqlConnection();
16
17 private string GetMD5(string s)
18 {
19 System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
20 byte[] t = md5.ComputeHash(System.Text.Encoding.GetEncoding("gb2312").GetBytes(s));
21 StringBuilder sb = new StringBuilder(32);
22 for (int i = 0; i < t.Length; i++)
23 {
24 sb.Append(t[i].ToString("x").PadLeft(2, '0'));
25 }
26 return sb.ToString();
27 }
28 private String Get_Http(String a_strUrl,int timeout)
29 {
30 string strResult ;
31 try
32 {
33 System.Net.HttpWebRequest myReq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(a_strUrl) ;
34 myReq.Timeout = timeout;
35 System.Net.HttpWebResponse HttpWResp = (System.Net.HttpWebResponse)myReq.GetResponse();
36 Stream myStream = HttpWResp.GetResponseStream () ;
37 StreamReader sr = new StreamReader(myStream , System.Text.Encoding.Default);
38 System.Text.StringBuilder strBuilder = new System.Text.StringBuilder();
39 while (-1 != sr.Peek())
40 {
41 strBuilder.Append(sr.ReadLine()+"\r\n");
42 }
43 strResult = strBuilder.ToString();
44 }
45 catch(Exception exp)
46 {
47
48 strResult = "错误:" + exp.Message ;
49 }
50 return strResult ;
51 }
52
53 public void send(string sql)
54 {
55 string dbconn = "data source=127.0.0.1;UID=Cm;PWD=fallenangel;database=text";
56 conn = new SqlConnection(dbconn);
57 conn.Open();
58 SqlCommand cmd = new SqlCommand(sql, conn);
59 int aa = cmd.ExecuteNonQuery();
60 conn.Close();
61 }
62
63 private void Page_Load(object sender, System.EventArgs e)
64 {
65 //检查支付宝通知接口传递过来的参数是否合法
66 string msg_id = System.Web.HttpContext.Current.Request["msg_id"];
67 string order_no = System.Web.HttpContext.Current.Request["order_no"];
68 string gross = System.Web.HttpContext.Current.Request["gross"];
69 string buyer_email = System.Web.HttpContext.Current.Request["buyer_email"];
70 string buyer_name = System.Web.HttpContext.Current.Request["buyer_name"];
71 string buyer_address = System.Web.HttpContext.Current.Request["buyer_address"];
72 string buyer_zipcode = System.Web.HttpContext.Current.Request["buyer_zipcode"];
73 string buyer_tel = System.Web.HttpContext.Current.Request["buyer_tel"];
74 string buyer_mobile = System.Web.HttpContext.Current.Request["buyer_mobile"];
75 string action = System.Web.HttpContext.Current.Request["action"];
76 string s_date = System.Web.HttpContext.Current.Request["date"];
77 string ac = System.Web.HttpContext.Current.Request["ac"];
78 string notify_type = System.Web.HttpContext.Current.Request["notify_type"];
79
80 alipayNotifyURL = alipayNotifyURL + "msg_id=" + msg_id + "&email=" + myalipayEmail + "&order_no=" + order_no;
81
82
83 //获取支付宝ATN返回结果,true和false都是正确的订单信息,invalid 是无效的
84 string responseTxt=Get_Http(alipayNotifyURL,120000);
85
86 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 ;
87
88 string ac_code=GetMD5(Str);
89
90 if(action=="test") //支付宝接口测试是否有效
91 {
92 returnTxt = "Y";
93 }
94 if(action=="sendOff") //发货通知
95 {
96 if(responseTxt.Substring(0,4)=="true" || responseTxt.Substring(0,4)=="fals")//ATN,验证消息是否支付宝发过来
97 {
98 if(ac_code==ac)//验证消息是否被修改
99 {
100 //数据库操作
101 }
102 }
103 }
104 if(action=="checkOut") //交易完成通知
105 {
106 returnTxt = "N";
107 if(responseTxt.Substring(0,4)=="true" || responseTxt.Substring(0,4)=="fals")//ATN,验证消息是否支付宝发过来
108 {
109 if(ac_code==ac)//验证消息是否被修改
110 {
111 //数据库操作
112 }
113 }
114 }
115 System.Web.HttpContext.Current.Response.Write(returnTxt);
116 }
117</script>
118
C# 支付宝接口的更多相关文章
- wap支付宝接口的问题
今天在支付宝接口开发时,遇到的两个坑 第一个: https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.8nHr4i& ...
- ThinkPHP实现支付宝接口功能
最近做系统,需要实现在线支付功能,毫不犹豫,选择的是支付宝的接口支付功能.这里我用的是即时到帐的接口,具体实现的步骤如下:一.下载支付宝接口包下载地址:https://doc.open.alipay. ...
- (实用篇)php支付宝接口用法分析
本文实例讲述了php支付宝接口用法.分享给大家供大家参考.具体分析如下: 现在流行的网站支持平台,支付宝当仁不让的老大了,现在我们就来告诉你如何使用支付宝api来做第三方支付,把支付宝放到自己网站来, ...
- 【转载】关于Alipay支付宝接口(Java版)
转载自:http://blog.163.com/lai_chao/blog/static/70340789201412724619514/ 1.alipay 双功能支付简介 2.alipay 提交支付 ...
- PHP支付宝接口RSA验证
这两天一直困扰的PHP RSA签名验证问题终于解决了,由于之前RSA接触的不多,再加上官方至今还未有PHP的SDK可供参考,因此走了一些弯路,写在这里和大家分享. 虽然支付宝官方还未提供相关SD ...
- android应用程序如何调用支付宝接口
最近在做一个关于购物商城的项目,项目里面付款这块我选的是调用支付宝的接口,因为用的人比较多. 在网上搜索了以下,有很多这方面的教程,但大部分教程过于陈旧,而且描述的过于简单.而且支付宝提供的接口一直在 ...
- [转]支付宝接口程序、文档及解读(ASP.NET)
本文转自:http://www.cnblogs.com/blodfox777/archive/2009/11/03/1595223.html 最近需要为网站加入支付宝的充值接口,而目前关于支付宝接口开 ...
- android应用程序如何调用支付宝接口(转)
最近在做一个关于购物商城的项目,项目里面付款这块我选的是调用支付宝的接口,因为用的人比较多. 在网上搜索了以下,有很多这方面的教程,但大部分教程过于陈旧,而且描述的过于简单.而且支付宝提供的接口一直在 ...
- android开发支付宝接口开发流程(密钥篇)
参考博客:http://blog.it985.com/12276.html 官方下载地址:http://download.alipay.com/public/api/base/WS_MOBILE_PA ...
- Android应用集成支付宝接口的简化
拿到支付宝接口的andriod demo后有点无语,集成一个支付服务而已,要在十几个java类之间引用来引用去,这样不仅容易导致应用本身代码结构的复杂化,调试起来也很累,于是操刀改造之: 该删的删,该 ...
随机推荐
- UE删除空行
- 最新 Arduino 驱动 12接口/户外 LED显示屏/LED点阵屏/LED单元板
起因 现有的驱动LED显示屏的资料,比较好的只有这个.但是它驱动的是08接口的室内显示屏,而我要驱动的是12接口的户外显示屏.两种屏幕的区别在于户外屏幕点阵比较稀疏,而且二者的扫描方式,驱动方式都不太 ...
- 高新服务平台在SUSE系统上重新部署笔录
安装步骤 ./shutdown.sh 停止tomcat (1) 删除 zjgx 目录下 除bbs 和Demos所有目录和文件 (2)上传zjgx0519.zip 到zjgx目录下 (3)unz ...
- php5.2转向 PHP 5.3 的 PHP 开发
PHP 5.3 开始,为了更好的向 PHP 的未来版本(PHP6) 过渡,将未来不再支持的函数标记为 DEPRECATED.在代码中使用这些函数,将毫不留情的在页面中显示警告信息:“使用了过时的函数… ...
- struts2中错误处理
定义一个 package,然后其他package都继承 这个package struts-global 就 有了这个错误处理功能了 然后再自己写个类 struts.xml <constant n ...
- bjfu1253 最大上升子序列和
n^2的算法就行,很简单的动态规划.直接上代码 /* * Author : ben */ #include <cstdio> #include <cstdlib> #inclu ...
- ORA-12516: TNS: 监听程序无法找到匹配协议栈的可用句柄解决方法
1.查看当前连接进程数SQL>select count(*) from v$process;2.查看连接数上限SQL>select value from v$parameter where ...
- js正则表达式中=s.g表示什么意思
//g是全局匹配//中间的内容表示:匹配以=开关,后面是0或多个空格,然后是双引号括起来的任意字符,比如:= "any symble" 匹配 = " asfjaskldf ...
- Request、Request.Form、Request.QueryString 用法的区别
Request.Form:获取以POST方式提交的数据. Request.QueryString:获取地址栏参数(以GET方式提交的数据). Request:包含以上两种方式(优先获取GET方式提交的 ...
- C#的默认编码
C# 的所有源代码文件,默认编码为 UTF-8,注意,是源代码文件,而不是 C# 中的 string. C# 中的所有 string,默认编码均为 Unicode (UTF-16). C# 产生的 A ...