1,引入支付宝的sdk(AopSdk) 支付宝接口文档网站可下载,注意下载C#版本;
2,代码写的比较简单
public static string RSASign(string OrderNo,decimal Money,string bodyp,string subjectp)
{
string publicKeyPem = ;//支付宝公钥
string privateKeyPem = ;//商户私钥
string out_trade_no = OrderNo;//订单号
double total_fee = (double)Money;//交易金额
string app_id = ;//app支付,支付宝中该应用的ID
string charset = "utf-8";//utf-8
string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
string notify_url = ;//回调地址
string body = bodyp;
string subject = subjectp;
Dictionary<string, string> dic = new Dictionary<string, string>();
Dictionary<string, object> dic2 = new Dictionary<string, object>();
dic.Add("app_id", app_id);
dic.Add("method", "alipay.trade.app.pay");
dic.Add("version", "1.0");
dic.Add("charset", charset);
dic.Add("notify_url", notify_url);
dic.Add("sign_type", "RSA2");
dic.Add("timestamp", timestamp);
dic2.Add("out_trade_no", out_trade_no);//商户订单号
dic2.Add("total_amount", total_fee);//支付金额
dic2.Add("product_code", "QUICK_MSECURITY_PAY");
dic2.Add("body", bodyp);
dic2.Add("subject", subject);
dic.Add("biz_content", JsonConvert.SerializeObject(dic2));
//string sign = AlipaySignature.RSASign(dic, privateKeyPem, null, "RSA2");
//AlipaySignature.RSASign方法是支付宝提供的签名方法,使用这个方法需要注意参数的传入,第一个参数为需要签名的参数,第二个参数为商户私钥,第4个参数如果传入fase,表示支付宝私钥以字符串的形式传入,true表示第二个参数要传存储私钥的文件地址,并且这个文件内容开头和结尾都要加“-----BEGIN RSA PRIVATE KEY----- 私钥 -----END RSA PRIVATE KEY-----”
string sign = AlipaySignature.RSASign(dic, privateKeyPem, "utf-8", false, "RSA2");
sign =HttpUtility.UrlEncode(sign, Encoding.GetEncoding(charset));
string Parms = string.Empty;
var testparms = string.Empty;
//Parms = Aop.Api.Util.AlipaySignature.GetSignContent(dic);
foreach (KeyValuePair<String, string> k in dic)
{
testparms += k.Key + "=" + k.Value + "&";
if (Parms == string.Empty)
{
Parms = k.Key + "=" + HttpUtility.UrlEncode(k.Value, Encoding.GetEncoding(charset));
}
else
{
Parms += "&" + k.Key + "=" + HttpUtility.UrlEncode(k.Value, Encoding.GetEncoding(charset));
}
}
Parms = Parms + "&sign=" + sign;
return Parms;
}
支付宝签名和验签都有提供相应的源码大家可以参考,如果问题还请大家提出