转载地址:http://bbs.csdn.net/topics/370260628
很早以前我在CSDN论坛上问过这个问题,一个项目中同时使用这两个接口的问题。
终于有时间上来逛逛,分享下支付宝接口示例,给不会的朋友做个介绍,会的朋友请不要喷,谢谢合作!
首先当项目中需要支付宝接口的时候,人家会给你一个Demo,里边代码很重要,基本都可以拿来复制黏贴,只需
改下参数而已。
2个接口的类是一样的,所以需要放在2个文件夹中,你也可以放在类库中。我是这样放的
App中是支付接口的类,外部是登录接口的类
首先需要把阴影的类中的几个参数修改下
static Config()
{
//↓↓↓↓↓↓↓↓↓↓请在这里配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
//合作身份者ID,以2088开头由16位纯数字组成的字符串
partner = "2088601323326437";
//交易安全检验码,由数字和字母组成的32位字符串
key = "";
//签约支付宝账号或卖家支付宝帐户
seller_email = "";
//页面跳转同步返回页面文件路径 要用 http://格式的完整路径,不允许加?id=123这类自定义参数
return_url = "http://ozmb.inicp.com/zfbreturn.aspx";
//服务器通知的页面文件路径 要用 http://格式的完整路径,不允许加?id=123这类自定义参数
notify_url = "http://ozmb.inicp.com/zfbreturn.aspx";
说明部分很清楚,我就不解释了
其他部分都不需要动!
支付宝快捷登录
这个接口很简单,2个页面,一个Send,一个return页面
using Com.Alipay;
///Send页面,除了我的判断,其他都可以复制黏贴,类的引用要注意,根据你的文件夹所在来引用
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Member_Info"] == null && Session["App"]==null)
{
string anti_phishing_key = "";
//获取客户端的IP地址,建议:编写获取客户端IP地址的程序
string exter_invoke_ip = "";
//注意:
//请慎重选择是否开启防钓鱼功能
//exter_invoke_ip、anti_phishing_key一旦被设置过,那么它们就会成为必填参数
//建议使用POST方式请求数据
//示例:
//exter_invoke_ip = "";
//Service aliQuery_timestamp = new Service();
//anti_phishing_key = aliQuery_timestamp.Query_timestamp(); //获取防钓鱼时间戳函数
//把请求参数打包成数组
SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();
sParaTemp.Add("anti_phishing_key", anti_phishing_key);
sParaTemp.Add("exter_invoke_ip", exter_invoke_ip);
//构造快捷登录接口表单提交HTML数据,无需修改
Service ali = new Service();
string sHtmlText = ali.Alipay_auth_authorize(sParaTemp);
Response.Write(sHtmlText);
}
else if (Session["Member_Info"] != null)
{
Response.Write("<script>alert('你已经登录,请先退出在进行支付宝登录!');location.href='index.aspx';</script>");
}
else if (Session["App"] != null)
{
Response.Write("<script>alert('你已经登录过了!');location.href='index.aspx';</script>");
}
}
}
//下边是返回页面return。一个泛型方法不要忘记。除了我的判断其他可以直接复制黏贴
using Com.Alipay;
public partial class zfblogin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
SortedDictionary<string, string> sPara = GetRequestGet();
if (sPara.Count > 0)//判断是否有带返回参数
{
Notify aliNotify = new Notify();
bool verifyResult = aliNotify.Verify(sPara, Request.QueryString["notify_id"], Request.QueryString["sign"]);
if (verifyResult)//验证成功
{
/
//请在这里加上商户的业务逻辑程序代码
//——请根据您的业务逻辑来编写程序(以下代码仅作参考)——
//获取支付宝的通知返回参数,可参考技术文档中页面跳转同步通知参数列表
string user_id = Request.QueryString["user_id"];//支付宝用户id
string token = Request.QueryString["token"]; //授权令牌
Label4.Text = user_id.ToString();
Session["token"] = token;
Session["App"] = user_id;
}
else {
Response.Write("<script>alert('登录失败');location.href='index.aspx'</script>");
}
}
else
{
Response.Write("<script>alert('登录失败');location.href='index.aspx'</script>");
}
}
}
public SortedDictionary<string, string> GetRequestGet()
{
int i = 0;
SortedDictionary<string, string> sPara = new SortedDictionary<string, string>();
NameValueCollection coll;
//Load Form variables into NameValueCollection variable.
coll = Request.QueryString;
// Get names of all forms into a string array.
String[] requestItem = coll.AllKeys;
for (i = 0; i < requestItem.Length; i++)
{
sPara.Add(requestItem[i], Request.QueryString[requestItem[i]]);
}
return sPara;
}
}