首页 >> 大全

支付宝Payto接口的C#.net实现方法

2023-12-04 大全 32 作者:考证青年

例一:

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("");}}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.00string 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 = "";             //买家EMAILstring 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();}
}接收支付宝信息并进行相应操作<%@ 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" %>

例二:

首先当项目中需要支付宝接口的时候,人家会给你一个Demo,里边代码很重要,基本都可以拿来复制黏贴,只需

改下参数而已。

2个接口的类是一样的,所以需要放在2个文件夹中,你也可以放在类库中。我是这样放的

_支付宝的接口_app支付宝接口

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,一个页面

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("");}else if (Session["App"] != null){Response.Write("");}}
}//下边是返回页面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"];//支付宝用户idstring token = Request.QueryString["token"];    //授权令牌Label4.Text = user_id.ToString();Session["token"] = token;Session["App"] = user_id;}else {Response.Write("");}}else{Response.Write("");}}}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;}
}

还是2个页面,一个是Send,一个还是。传参页面主要是传递需要的:价格,收货人,电话等等信息,

返回页面是当用户支付完毕后,判断支付是否成功,返回到支付网页上来

// 这个是传参层,具体哪些需要填写都很清楚else if (RadioButtonList1.SelectedValue == "支付宝支付"){Session["time2"]= DateTime.Now.ToString("yyyyMMddHHmmss");ZFB();string out_trade_no =Session["time2"].ToString();  //请与贵网站订单系统中的唯一订单号匹配string subject = "蔓越莓支付宝支付:" + Session["time2"].ToString();                    //订单名称,显示在支付宝收银台里的“商品名称”里,显示在支付宝的交易管理的“商品名称”的列表里。string body = "配送方式:"+RadioButtonList2.SelectedValue+",来自:蔓越莓,订单号:"+Session["time2"].ToString();                          //订单描述、订单详细、订单备注,显示在支付宝收银台里的“商品描述”里string price = Label8.Text.Trim();                    //订单总金额,显示在支付宝收银台里的“商品单价”里string logistics_fee = double.Parse(Label7.Text)+"";                                  //物流费用,即运费。string logistics_type = "EXPRESS";                                //物流类型,三个值可选:EXPRESS(快递)、POST(平邮)、EMS(EMS)string logistics_payment = "SELLER_PAY";                        //物流支付方式,两个值可选:SELLER_PAY(卖家承担运费)、BUYER_PAY(买家承担运费)string quantity = "1";                                          //商品数量,建议默认为1,不改变值,把一次交易看成是一次下订单而非购买一件商品。//选填参数////买家收货信息(推荐作为必填)//该功能作用在于买家已经在商户网站的下单流程中填过一次收货信息,而不需要买家在支付宝的付款流程中再次填写收货信息。//若要使用该功能,请至少保证receive_name、receive_address有值//收货信息格式请严格按照姓名、地址、邮编、电话、手机的格式填写string receive_name = UserName.Text;                                //收货人姓名,如:张三string receive_address = ddlProvince.SelectedValue + Request.Form["ddlCity"].ToString() + Address.Text;                            //收货人地址,如:XX省XXX市XXX区XXX路XXX小区XXX栋XXX单元XXX号string receive_zip = "";if (Zip.Text != ""){receive_zip = Zip.Text;                                  //收货人邮编,如:123456
                }else{receive_zip = "未填写";}string receive_phone = "";if (Phone.Text != ""){receive_phone = Phone.Text;                            //收货人电话号码,如:0571-81234567
                }else{receive_phone = "未填写";}string receive_mobile = Mobile.Text;                           //收货人手机号码,如:13312341234//网站商品的展示地址,不允许加?id=123这类自定义参数string show_url = "";//快捷登录用令牌授权码,该参数的值由快捷登录接口(alipay.auth.authorize)的页面跳转同步通知参数中获取string token = "";//注意://token的有效时间为30分钟,过期后需重新执行快捷登录接口(alipay.auth.authorize)获得新的token
//把请求参数打包成数组SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();sParaTemp.Add("body", body);sParaTemp.Add("logistics_fee", logistics_fee);sParaTemp.Add("logistics_payment", logistics_payment);sParaTemp.Add("logistics_type", logistics_type);sParaTemp.Add("out_trade_no", out_trade_no);sParaTemp.Add("payment_type", "1");sParaTemp.Add("price", price);sParaTemp.Add("quantity", quantity);sParaTemp.Add("receive_address", receive_address);sParaTemp.Add("receive_mobile", receive_mobile);sParaTemp.Add("receive_name", receive_name);sParaTemp.Add("receive_phone", receive_phone);sParaTemp.Add("receive_zip", receive_zip);sParaTemp.Add("show_url", show_url);sParaTemp.Add("subject", subject);sParaTemp.Add("token", token);//构造标准双接口表单提交HTML数据,无需修改Service ali = new Service();string sHtmlText = ali.Trade_create_by_buyer(sParaTemp);Response.Write(sHtmlText);}//下边是个返回层,判断是否支付成功,当成功我需要把订单加到数据
using Com.Alipay;
using System.Collections.Specialized;public partial class zfb_success : 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 order_no = Request.QueryString["out_trade_no"];string total_fee = Request.QueryString["price"];if (Session["EOrder"] != null && Session["Cart2"] != null){PageDataBind();}else if (Session["COrder"] != null && Session["Cart"] != null){PageDataBind2();}else{this.table2.Visible = true;this.table1.Visible = false;}}else {this.table1.Visible = true;this.table2.Visible = false;}}else {this.table1.Visible = true;this.table2.Visible = false;}}}
////这个类不能忘记添加进来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;}

其实支付宝的Demo很清晰,示例很详细,很不错。网银接口也一样,都只是需要修改一些参数,其他都是直接

复制黏贴罢了。

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了