protected void Page_Load(object sender, EventArgs e) { Dictionary <string, string> sPara = GetRequestPost(); if (sPara.Count > 0)//判断是否有带返回参数 { Notify aliNotify = new Notify(); string notify_id = Request.Form["notify_id"]; //获取notify_id string sign = Request.Form["sign"]; //获取sign Logger.Warn(string.Format("用户押金缴费,支付宝支付通知,结果{0}", JsonConvert.SerializeObject(sPara))); if (aliNotify.GetSignVeryfy(sPara, sign)) { ///////////////////////////////////////////////////////////////////////////////////////////////////////////// //请在这里加上商户的业务逻辑程序代码 //商户订单号 string out_trade_no = Request.Form["out_trade_no"]; //支付宝交易号 string trade_no = Request.Form["trade_no"]; //支付宝交易金额 string total_fee = Request.Form["total_amount"]; //支付宝用户号 支付宝账号对应的支付宝唯一用户号。以2088开头的纯16位数字 string buyer_id = Request.Form["buyer_id"]; //支付宝账号 string buyer_logon_id = Request.Form["buyer_logon_id"]; Logger.Warn(string.Format("用户押金缴费,支付宝支付通知,订单号{0},支付单号{1},支付金额{2},支付宝用户号{3},支付宝账号{4}", out_trade_no, trade_no, total_fee, buyer_id, buyer_logon_id)); //交易状态 string trade_status = Request.Form["trade_status"]; if (trade_status == "TRADE_SUCCESS" || trade_status == "TRADE_FINISHED") { UserInfoManager uim = new UserInfoManager(); UserFinancialManager ufm = new UserFinancialManager(); DataTable userfinancial = ufm.GetUserFinancialInfoByPayId(out_trade_no); if (userfinancial != null && userfinancial.Rows.Count == 1) { Hashtable hashufdb = DataTableHelper.DataRowToHashTable(userfinancial.Rows[0]); Hashtable hashuf = new Hashtable(); hashuf["ID"] = SiteHelper.GetHashTableValueByKey(hashufdb, "ID"); hashuf["UserID"] = SiteHelper.GetHashTableValueByKey(hashufdb, "UserID"); hashuf["State"] = UserFinancialState.Effect.GetHashCode(); hashuf["TradeNo"] = trade_no; hashuf["TotalFee"] = total_fee; hashuf["PayWay"] = UserFinancialOperatorWay.Alipay; hashuf["buyer_id"] = buyer_id; hashuf["buyer_logon_id"] = buyer_logon_id; decimal changesAmount = 0.00m; decimal.TryParse(SiteHelper.GetHashTableValueByKey(hashufdb, "ChangesAmount"), out changesAmount); bool isSuccess = false; if (Math.Abs(changesAmount) == decimal.Parse(total_fee)) { isSuccess = uim.DepositCallBack(hashuf); } if (isSuccess) { Response.Write("success"); } else { Response.Write("fail"); } } else { } } else//验证失败 { Response.Write("fail"); } } else { Response.Write("无通知参数"); } } }
/// <summary> /// 查询订单 /// </summary> /// <param name="trade_no"></param> /// <returns></returns> public OrderQueryResult QueryByTradeNO(string trade_no) { OrderQueryResult result = new OrderQueryResult(); try { StringBuilder biz_content = new StringBuilder(); biz_content.Append(@"{"); biz_content.AppendFormat("\"trade_no\":\"{0}\"", trade_no); biz_content.Append(@"}"); SortedDictionary <string, string> dicArray = new SortedDictionary <string, string>(); dicArray.Add("app_id", Config.app_id); dicArray.Add("biz_content", biz_content.ToString()); dicArray.Add("charset", charset); dicArray.Add("format", "JSON"); dicArray.Add("method", method); dicArray.Add("sign_type", sign_type); dicArray.Add("timestamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); dicArray.Add("version", "1.0"); //dicArray.Add("sign", ); string sign_content = CreateLinkString(dicArray); string sign = RSA.sign(sign_content, Config.private_key, "UTF-8", sign_type); dicArray.Add("sign", sign); string postDataStr = CreateLinkStringUrlencode(dicArray); string responseString = HttpUtil.Get(https_url, postDataStr, "utf-8"); JObject jsonObj = (JObject)JsonConvert.DeserializeObject(responseString); string alipay_trade_query_response = jsonObj["alipay_trade_query_response"].ToString(); Dictionary <string, string> dict1 = new Dictionary <string, string>(); JavaScriptSerializer jss = new JavaScriptSerializer(); dict1 = jss.Deserialize <Dictionary <string, string> >(alipay_trade_query_response); Notify aliNotify = new Notify(); if (aliNotify.GetSignVeryfy(dict1, jsonObj["sign"].ToString())) { AlipayQueryResponse query_response = Newtonsoft.Json.JsonConvert.DeserializeObject <AlipayQueryResponse>(alipay_trade_query_response); if (query_response.code == "10000") { result.out_trade_no = query_response.out_trade_no; result.total_fee = query_response.total_amount; result.transaction_id = query_response.trade_no; result.buyer_logon_id = query_response.buyer_logon_id; result.buyer_user_id = query_response.buyer_user_id; string trade_state = query_response.trade_status; result.trade_state = TradeStateEnum.OTHERS; switch (trade_state) { case "TRADE_SUCCESS": result.trade_state = TradeStateEnum.SUCCESS; break; case "WAIT_BUYER_PAY": result.trade_state = TradeStateEnum.NOTPAY; break; case "TRADE_CLOSED": result.trade_state = TradeStateEnum.CLOSED; break; default: break; } } else if (query_response.code == "40004") { if (query_response.sub_code == "ACQ.TRADE_NOT_EXIST") { result.trade_state = TradeStateEnum.NOTEXIST; } } } return(result); } catch (Exception ex) { return(null); } }