C#调用支付宝接口案例
public ActionResult PayResult()
{
SortedDictionary<string, string> sPara = GetRequestGet();
T_Trade objTrade = null;
if (sPara.Count > 0)
{
Notify aliNotify = new Notify();
bool verifyResult = aliNotify.Verify(sPara, Request.QueryString["notify_id"], Request.QueryString["sign"]);
if (verifyResult)
{
string out_trade_no = Request.QueryString["out_trade_no"];
string trade_no = Request.QueryString["trade_no"];
string trade_status = Request.QueryString["trade_status"];
VerifyPay(out_trade_no, trade_no, trade_status);
var db = DBHelper.QueryDB();
string strSql = string.Format(@"select * from T_Trade where ID='{0}'", out_trade_no);
objTrade = db.Sql(strSql).QuerySingle();
strSql = string.Format(@"select UserName from T_User where ID={0}", objTrade.UserID);
ViewBag.UserName = db.Sql(strSql).QuerySingle<string>();
}
}
return View(objTrade);
}
[AllowAnonymous]
public ActionResult PayNotify()
{
SortedDictionary<string, string> sPara = GetRequestPost();
if (sPara.Count > 0)
{
Notify aliNotify = new Notify();
bool verifyResult = aliNotify.Verify(sPara, Request.Form["notify_id"], Request.Form["sign"]);
if (verifyResult)
{
string out_trade_no = Request.Form["out_trade_no"];
string trade_no = Request.Form["trade_no"];
string trade_status = Request.Form["trade_status"];
VerifyPay(out_trade_no, trade_no, trade_status);
Response.Write("success");
}
else
{
Response.Write("fail");
}
}
else
{
Response.Write("无通知参数");
}
return View();
}
public bool VerifyPay(string strOutTradeNo, string strTradeNo, string strTradeStatus)
{
bool bRes = false;
string strSql = string.Format(@"select * from T_Trade where ID='{0}'", strOutTradeNo);
T_Trade objTrade = DBHelper.QueryDB().Sql(strSql).QuerySingle();
if (strTradeStatus == "TRADE_FINISHED" || strTradeStatus == "TRADE_SUCCESS")
{
if (objTrade.State != 2)
{
objTrade.TradeNo = strTradeNo;
objTrade.FinishTime = DateTime.Now;
objTrade.State = 2;
doPay(objTrade);
}
bRes = true;
}
else if (strTradeStatus == "TRADE_CLOSED")
{
if (objTrade.State != 1)
{
objTrade.TradeNo = strTradeNo;
objTrade.FinishTime = DateTime.Now;
objTrade.State = 1;
doPay(objTrade);
}
bRes = false;
}
return bRes;
}
public SortedDictionary<string, string> GetRequestGet()
{
int i = 0;
SortedDictionary<string, string> sArray = new SortedDictionary<string, string>();
NameValueCollection coll;
coll = Request.QueryString;
String[] requestItem = coll.AllKeys;
for (i = 0; i < requestItem.Length; i++)
{
sArray.Add(requestItem[i], Request.QueryString[requestItem[i]]);
}
return sArray;
}
public SortedDictionary<string, string> GetRequestPost()
{
int i = 0;
SortedDictionary<string, string> sArray = new SortedDictionary<string, string>();
NameValueCollection coll;
coll = Request.Form;
String[] requestItem = coll.AllKeys;
for (i = 0; i < requestItem.Length; i++)
{
sArray.Add(requestItem[i], Request.Form[requestItem[i]]);
}
return sArray;
}
public ActionResult PostToAlipay(int id)
{
int uid = id;
var db = DBHelper.QueryDB();
string strSql = string.Format(@"select count(ID) from T_Trade where UserID={0} and State=2", uid);
if (db.Sql(strSql).QuerySingle<int>() > 0)
{
return Redirect("/home");
}
strSql = string.Format(@"select * from T_Trade where UserID={0} and State=0", uid);
var objTrade = db.Sql(strSql).QuerySingle();
string payment_type = "1";
string notify_url = "http://zc.jcxt99.com/pay/paynotify";
string return_url = "http://zc.jcxt99.com/pay/payresult";
string out_trade_no = objTrade == null ? Guid.NewGuid().ToString("N").Trim() : objTrade.ID;
string subject = "会员资格";
string total_fee = "200.00";
string it_b_pay = "30m";
SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();
sParaTemp.Add("partner", Config.Partner);
sParaTemp.Add("seller_email", Config.Seller_email);
sParaTemp.Add("_input_charset", Config.Input_charset.ToLower());
sParaTemp.Add("service", "create_direct_pay_by_user");
sParaTemp.Add("payment_type", payment_type);
sParaTemp.Add("notify_url", notify_url);
sParaTemp.Add("return_url", return_url);
sParaTemp.Add("out_trade_no", out_trade_no);
sParaTemp.Add("subject", subject);
sParaTemp.Add("total_fee", total_fee);
sParaTemp.Add("it_b_pay", it_b_pay);
string sHtmlText = Submit.BuildRequest(sParaTemp, "get", "确认");
if (objTrade == null)
{
objTrade = new T_Trade();
objTrade.ID = out_trade_no;
objTrade.UserID = uid;
objTrade.TradeTime = DateTime.Now;
objTrade.Value = total_fee;
objTrade.TradeNo = "";
objTrade.FinishTime = DateTime.Now;
objTrade.State = 0;
if (db.Insert("T_Trade", objTrade).AutoMap().Execute() > 0)
{
Response.Write(sHtmlText);
}
else
{
return Content("生成订单失败,请刷新页面重试!");
}
}
else
{
Response.Write(sHtmlText);
}
return null;
}