.net下如何使用支付宝的“即时到帐”接口
前段时间在做一个B2C的商城,在订单支付模块需要接入支付宝和微信即时到帐,在网上查了一些资料多是层次不齐,后来在支付宝官网上下载了一个Demo案例研究了一下,发现并没有想象中的那么难,开发者可以在支付宝官网上下载Demo案例,Demo的下载链接 URL:https://b.alipay.com/order/productDetail.htm?productId=2015110218012942&tabId=4#ps-tabinfo-hash
1.支付事件:
protected void BtnAlipay_Click(object sender, EventArgs e)
{
////////////////////////////////////////////请求参数////////////////////////////////////////////
//商户订单号,商户网站订单系统中唯一订单号,必填
string out_trade_no = WIDout_trade_no.Text.Trim();
//订单名称,必填
string subject = WIDsubject.Text.Trim();
//付款金额,必填
string total_fee = WIDtotal_fee.Text.Trim();
//商品描述,可空
string body = WIDbody.Text.Trim();
////////////////////////////////////////////////////////////////////////////////////////////////
//把请求参数打包成数组
SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();
sParaTemp.Add("service", Config.service);
sParaTemp.Add("partner", Config.partner);
sParaTemp.Add("seller_id", Config.seller_id);
sParaTemp.Add("_input_charset", Config.input_charset.ToLower());
sParaTemp.Add("payment_type", Config.payment_type);
sParaTemp.Add("notify_url", Config.notify_url);
sParaTemp.Add("return_url", Config.return_url);
sParaTemp.Add("anti_phishing_key", Config.anti_phishing_key);
sParaTemp.Add("exter_invoke_ip", Config.exter_invoke_ip);
sParaTemp.Add("out_trade_no", out_trade_no);
sParaTemp.Add("subject", subject);
sParaTemp.Add("total_fee", total_fee);
sParaTemp.Add("body", body);
//其他业务参数根据在线开发文档,添加参数.文档地址:https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.O9yorI&treeId=62&articleId=103740&docType=1
//如sParaTemp.Add("参数名","参数值");
//建立请求:以表单HTML形式构造
string sHtmlText = Submit.BuildRequest(sParaTemp, "get", "确认");
Response.Write(sHtmlText);
}
2.配置类WebConfig:
public class Config
{
//↓↓↓↓↓↓↓↓↓↓请在这里配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
// 必填:合作身份者ID,签约账号,以2088开头由16位纯数字组成的字符串,查看地址:https://b.alipay.com/order/pidAndKey.htm
public static string partner = "";
// 收款支付宝账号,以2088开头由16位纯数字组成的字符串,一般情况下收款账号就是签约账号
public static string seller_id = partner;
// 必填:MD5密钥,安全检验码,由数字和字母组成的32位字符串,查看地址:https://b.alipay.com/order/pidAndKey.htm
public static string key = "";
// 服务器异步通知页面路径,需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
public static string notify_url = "http://商户网关地址/create_direct_pay_by_user-CSHARP-UTF-8/notify_url.aspx";
// 页面跳转同步通知页面路径,需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
public static string return_url = "http://商户网关地址/create_direct_pay_by_user-CSHARP-UTF-8/return_url.aspx";
// 签名方式
public static string sign_type = "MD5";
// 调试用,创建TXT日志文件夹路径,见AlipayCore.cs类中的LogResult(string sWord)打印方法。
public static string log_path = HttpRuntime.AppDomainAppPath.ToString() + "log\\";
// 字符编码格式 目前支持 gbk 或 utf-8
public static string input_charset = "utf-8";
// 支付类型 ,无需修改
public static string payment_type = "1";
// 调用的接口名,无需修改
public static string service = "create_direct_pay_by_user";
//↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
//↓↓↓↓↓↓↓↓↓↓请在这里配置防钓鱼信息,如果没开通防钓鱼功能,请忽视不要填写 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
//防钓鱼时间戳 若要使用请调用类文件submit中的Query_timestamp函数
public static string anti_phishing_key = "";
//客户端的IP地址 非局域网的外网IP地址,如:221.0.0.1
public static string exter_invoke_ip = "";
//↑↑↑↑↑↑↑↑↑↑请在这里配置防钓鱼信息,如果没开通防钓鱼功能,请忽视不要填写 ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
}
3.请求提交类:
public class Submit
{
#region 字段
//支付宝网关地址(新)
private static string GATEWAY_NEW = "https://mapi.alipay.com/gateway.do?";
//商户的私钥:
private static string _key = "";
//编码格式
private static string _input_charset = "";
//签名方式
private static string _sign_type = "";
#endregion
static Submit()
{
_key = Config.key.Trim();
_input_charset = Config.input_charset.Trim().ToLower();
_sign_type = Config.sign_type.Trim().ToUpper();
}
/// <summary>
/// 生成请求时的签名
/// </summary>
/// <param name="sPara">请求给支付宝的参数数组</param>
/// <returns>签名结果</returns>
private static string BuildRequestMysign(Dictionary<string, string> sPara)
{
//把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
string prestr = Core.CreateLinkString(sPara);
//把最终的字符串签名,获得签名结果
string mysign = "";
switch (_sign_type)
{
case "MD5":
mysign = AlipayMD5.Sign(prestr, _key, _input_charset);
break;
default:
mysign = "";
break;
}
return mysign;
}
/// <summary>
/// 生成要请求给支付宝的参数数组
/// </summary>
/// <param name="sParaTemp">请求前的参数数组</param>
/// <returns>要请求的参数数组</returns>
private static Dictionary<string, string> BuildRequestPara(SortedDictionary<string, string> sParaTemp)
{
//待签名请求参数数组
Dictionary<string, string> sPara = new Dictionary<string, string>();
//签名结果
string mysign = "";
//过滤签名参数数组
sPara = Core.FilterPara(sParaTemp);
//获得签名结果
mysign = BuildRequestMysign(sPara);
//签名结果与签名方式加入请求提交参数组中
sPara.Add("sign", mysign);
sPara.Add("sign_type", _sign_type);
return sPara;
}
/// <summary>
/// 生成要请求给支付宝的参数数组
/// </summary>
/// <param name="sParaTemp">请求前的参数数组</param>
/// <param name="code">字符编码</param>
/// <returns>要请求的参数数组字符串</returns>
private static string BuildRequestParaToString(SortedDictionary<string, string> sParaTemp, Encoding code)
{
//待签名请求参数数组
Dictionary<string, string> sPara = new Dictionary<string, string>();
sPara = BuildRequestPara(sParaTemp);
//把参数组中所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串,并对参数值做urlencode
string strRequestData = Core.CreateLinkStringUrlencode(sPara, code);
return strRequestData;
}
/// <summary>
/// 建立请求,以表单HTML形式构造(默认)
/// </summary>
/// <param name="sParaTemp">请求参数数组</param>
/// <param name="strMethod">提交方式。两个值可选:post、get</param>
/// <param name="strButtonValue">确认按钮显示文字</param>
/// <returns>提交表单HTML文本</returns>
public static string BuildRequest(SortedDictionary<string, string> sParaTemp, string strMethod, string strButtonValue)
{
//待请求参数数组
Dictionary<string, string> dicPara = new Dictionary<string, string>();
dicPara = BuildRequestPara(sParaTemp);
StringBuilder sbHtml = new StringBuilder();
sbHtml.Append("<form id='alipaysubmit' name='alipaysubmit' action='" + GATEWAY_NEW + "_input_charset=" + _input_charset + "' method='" + strMethod.ToLower().Trim() + "'>");
foreach (KeyValuePair<string, string> temp in dicPara)
{
sbHtml.Append("<input type='hidden' name='" + temp.Key + "' value='" + temp.Value + "'/>");
}
//submit按钮控件请不要含有name属性
sbHtml.Append("<input type='submit' value='" + strButtonValue + "' style='display:none;'></form>");
sbHtml.Append("<script>document.forms['alipaysubmit'].submit();</script>");
return sbHtml.ToString();
}
/// <summary>
/// 用于防钓鱼,调用接口query_timestamp来获取时间戳的处理函数
/// 注意:远程解析XML出错,与IIS服务器配置有关
/// </summary>
/// <returns>时间戳字符串</returns>
public static string Query_timestamp()
{
string url = GATEWAY_NEW + "service=query_timestamp&partner=" + Config.partner + "&_input_charset=" + Config.input_charset;
string encrypt_key = "";
XmlTextReader Reader = new XmlTextReader(url);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Reader);
encrypt_key = xmlDoc.SelectSingleNode("/alipay/response/timestamp/encrypt_key").InnerText;
return encrypt_key;
}
}
}

浙公网安备 33010602011771号