protected void Page_Load(object sender, EventArgs e) { //商户号 string partner = "1900000109"; //密钥 string key = "8934e7d15453e97507ef794cf7b0519d"; //创建请求对象 RequestHandler reqHandler = new RequestHandler(Context); //通信对象 TenpayHttpClient httpClient = new TenpayHttpClient(); //应答对象 ClientResponseHandler resHandler = new ClientResponseHandler(); //----------------------------- //设置请求参数 //----------------------------- reqHandler.init(); reqHandler.setKey(key); reqHandler.setGateUrl("https://api.mch.tenpay.com/refundapi/gateway/refund.xml"); reqHandler.setParameter("partner", partner); //out_trade_no和transaction_id至少一个必填,同时存在时transaction_id优先 //reqHandler.setParameter("out_trade_no", "1458268681"); reqHandler.setParameter("transaction_id", "1900000109201103020030626316"); reqHandler.setParameter("out_refund_no", "2011030201"); reqHandler.setParameter("total_fee", "1"); reqHandler.setParameter("refund_fee", "1"); reqHandler.setParameter("refund_fee", "1"); reqHandler.setParameter("op_user_id", "1900000109"); reqHandler.setParameter("op_user_passwd", MD5Util.GetMD5("111111", "GBK")); reqHandler.setParameter("service_version", "1.1"); string requestUrl = reqHandler.getRequestURL(); string certPath = "c:\\key\\1900000109.pfx"; //证书密码,正式商户号的证书密码通过短信方式发送到合同登记的手机号,系统上线前请注意修改为正确值 string certPassword = "******"; httpClient.setCertInfo(certPath, certPassword); //设置请求内容 httpClient.setReqContent(requestUrl); //设置超时 httpClient.setTimeOut(10); string rescontent = ""; //后台调用 if (httpClient.call()) { //获取结果 rescontent = httpClient.getResContent(); resHandler.setKey(key); //设置结果参数 resHandler.setContent(rescontent); //判断签名及结果 if (resHandler.isTenpaySign() && resHandler.getParameter("retcode") == "0") { //商户订单号 string out_trade_no = resHandler.getParameter("out_trade_no"); //财付通订单号 string transaction_id = resHandler.getParameter("transaction_id"); //业务处理 Response.Write("OK,transaction_id=" + resHandler.getParameter("transaction_id") + "<br>"); } else { //错误时,返回结果未签名。 //如包格式错误或未确认结果的,请使用原来订单号重新发起,确认结果,避免多次操作 Response.Write("业务错误信息或签名错误:" + resHandler.getParameter("retcode") + "," + resHandler.getParameter("retmsg") + "<br>"); } } else { //后台调用通信失败 Response.Write("call err:" + httpClient.getErrInfo() + "<br>" + httpClient.getResponseCode() + "<br>"); //有可能因为网络原因,请求已经处理,但未收到应答。 } //获取debug信息,建议把请求、应答内容、debug信息,通信返回码写入日志,方便定位问题 Response.Write("http res:" + httpClient.getResponseCode() + "," + httpClient.getErrInfo() + "<br>"); Response.Write("req url:" + requestUrl + "<br/>"); Response.Write("req debug:" + reqHandler.getDebugInfo() + "<br/>"); Response.Write("res content:" + Server.HtmlEncode(rescontent) + "<br/>"); Response.Write("res debug:" + Server.HtmlEncode(resHandler.getDebugInfo()) + "<br/>"); }
protected void Page_Load(object sender, EventArgs e) { //商户号 string partner = "1900000109"; //密钥 string key = "8934e7d15453e97507ef794cf7b0519d"; //创建请求对象 RequestHandler reqHandler = new RequestHandler(Context); //通信对象 TenpayHttpClient httpClient = new TenpayHttpClient(); //应答对象 ClientResponseHandler resHandler = new ClientResponseHandler(); //----------------------------- //设置请求参数 //----------------------------- reqHandler.init(); reqHandler.setKey(key); reqHandler.setGateUrl("https://gw.tenpay.com/gateway/normalorderquery.xml"); reqHandler.setParameter("partner", partner); //out_trade_no和transaction_id至少一个必填,同时存在时transaction_id优先 reqHandler.setParameter("out_trade_no", "0931543575"); //reqHandler.setParameter("transaction_id", "1900000109201103020030626316"); string requestUrl = reqHandler.getRequestURL(); //设置请求内容 httpClient.setReqContent(requestUrl); //设置超时 httpClient.setTimeOut(10); string rescontent = ""; //后台调用 if (httpClient.call()) { //获取结果 rescontent = httpClient.getResContent(); resHandler.setKey(key); //设置结果参数 resHandler.setContent(rescontent); //判断签名及结果 if (resHandler.isTenpaySign() && resHandler.getParameter("retcode") == "0") { //商户订单号 string out_trade_no = resHandler.getParameter("out_trade_no"); //财付通订单号 string transaction_id = resHandler.getParameter("transaction_id"); //金额,以分为单位 string total_fee = resHandler.getParameter("total_fee"); //如果有使用折扣券,discount有值,total_fee+discount=原请求的total_fee string discount = resHandler.getParameter("discount"); //支付结果 string trade_state = resHandler.getParameter("trade_state"); //支付成功 if(trade_state == "0") { //业务处理 Response.Write("OK,transaction_id=" + resHandler.getParameter("transaction_id") + "<br>"); } } else { //错误时,返回结果未签名。 //如包格式错误或未确认结果的,请使用原来订单号重新发起,确认结果,避免多次操作 Response.Write("业务错误信息或签名错误:" + resHandler.getParameter("retcode") + "," + resHandler.getParameter("retmsg") + "<br>"); } } else { //后台调用通信失败 Response.Write("call err:" + httpClient.getErrInfo() + "<br>" + httpClient.getResponseCode() + "<br>"); //有可能因为网络原因,请求已经处理,但未收到应答。 } //获取debug信息,建议把请求、应答内容、debug信息,通信返回码写入日志,方便定位问题 Response.Write("http res:" + httpClient.getResponseCode() + "," + httpClient.getErrInfo() + "<br>"); Response.Write("req url:" + requestUrl + "<br/>"); Response.Write("req debug:" + reqHandler.getDebugInfo() + "<br/>"); Response.Write("res content:" + Server.HtmlEncode(rescontent) + "<br/>"); Response.Write("res debug:" + Server.HtmlEncode(resHandler.getDebugInfo()) + "<br/>"); }
protected void Page_Load(object sender, EventArgs e) { //商户号 string partner = "1900000109"; //密钥 string key = "8934e7d15453e97507ef794cf7b0519d"; //创建ResponseHandler实例 ResponseHandler resHandler = new ResponseHandler(Context); resHandler.setKey(key); //判断签名 if (resHandler.isTenpaySign()) { ///通知id string notify_id = resHandler.getParameter("notify_id"); //通过通知ID查询,确保通知来至财付通 //创建查询请求 RequestHandler queryReq = new RequestHandler(Context); queryReq.init(); queryReq.setKey(key); queryReq.setGateUrl("https://gw.tenpay.com/gateway/verifynotifyid.xml"); queryReq.setParameter("partner", partner); queryReq.setParameter("notify_id", notify_id); //通信对象 TenpayHttpClient httpClient = new TenpayHttpClient(); httpClient.setTimeOut(5); //设置请求内容 httpClient.setReqContent(queryReq.getRequestURL()); //后台调用 if (httpClient.call()) { //设置结果参数 ClientResponseHandler queryRes = new ClientResponseHandler(); queryRes.setContent(httpClient.getResContent()); queryRes.setKey(key); //判断签名及结果 //只有签名正确,retcode为0,trade_state为0才是支付成功 if (queryRes.isTenpaySign() && queryRes.getParameter("retcode") == "0" && queryRes.getParameter("trade_state") == "0" && queryRes.getParameter("trade_mode") == "1") { //取结果参数做业务处理 string out_trade_no = queryRes.getParameter("out_trade_no"); //财付通订单号 string transaction_id = queryRes.getParameter("transaction_id"); //金额,以分为单位 string total_fee = queryRes.getParameter("total_fee"); //如果有使用折扣券,discount有值,total_fee+discount=原请求的total_fee string discount = queryRes.getParameter("discount"); //------------------------------ //处理业务开始 //------------------------------ //处理数据库逻辑 //注意交易单不要重复处理 //注意判断返回金额 //------------------------------ //处理业务完毕 //------------------------------ //通知财付通已经处理成功,无需重新通知 Response.Write("支付成功"); } else { //错误时,返回结果可能没有签名,写日志trade_state、retcode、retmsg看失败详情。 //通知财付通处理失败,需要重新通知 Response.Write("支付失败"); } //获取查询的debug信息,建议把请求、应答内容、debug信息,通信返回码写入日志,方便定位问题 /* Response.Write("http res:" + httpClient.getResponseCode() + "," + httpClient.getErrInfo() + "<br>"); Response.Write("query req url:" + queryReq.getRequestURL() + "<br/>"); Response.Write("query req debug:" + queryReq.getDebugInfo() + "<br/>"); Response.Write("query res content:" + Server.HtmlEncode(httpClient.getResContent()) + "<br/>"); Response.Write("query res debug:" + Server.HtmlEncode(queryRes.getDebugInfo()) + "<br/>"); */ } else { //通知财付通处理失败,需要重新通知 Response.Write("后台通信失败"); //写错误日志,注意实际应用中,不要返回给用户 Response.Write("call err:" + httpClient.getErrInfo() + "<br>" + httpClient.getResponseCode() + "<br>"); } //获取debug信息,建议把debug信息写入日志,方便定位问题 /* string debuginfo = resHandler.getDebugInfo(); Response.Write("<br/>debuginfo:" + debuginfo + "<br/>"); */ } }
public override bool CheckOrder(IPayHistory order,out string message) { //创建请求对象 RequestHandler reqHandler = new RequestHandler(HttpContext.Current); //通信对象 TenpayHttpClient httpClient = new TenpayHttpClient(); //应答对象 ClientResponseHandler resHandler = new ClientResponseHandler(); //----------------------------- //设置请求参数 //----------------------------- reqHandler.init(); reqHandler.setKey(key); reqHandler.setGateUrl("https://gw.tenpay.com/gateway/normalorderquery.xml"); reqHandler.setParameter("partner", bargainor_id); //out_trade_no和transaction_id至少一个必填,同时存在时transaction_id优先 reqHandler.setParameter("out_trade_no", order.OrderId); //reqHandler.setParameter("transaction_id", "1900000109201103020030626316"); string requestUrl = reqHandler.getRequestURL(); //设置请求内容 httpClient.setReqContent(requestUrl); //设置超时 httpClient.setTimeOut(10); string rescontent = ""; //后台调用 if (httpClient.call()) { //获取结果 rescontent = httpClient.getResContent(); resHandler.setKey(key); //设置结果参数 resHandler.setContent(rescontent); //判断签名及结果 if (resHandler.isTenpaySign() && resHandler.getParameter("retcode") == "0") { //商户订单号 string out_trade_no = resHandler.getParameter("out_trade_no"); //财富通订单号 string transaction_id = resHandler.getParameter("transaction_id"); //金额,以分为单位 string total_fee = resHandler.getParameter("total_fee"); //如果有使用折扣券,discount有值,total_fee+discount=原请求的total_fee string discount = resHandler.getParameter("discount"); //支付结果 string trade_state = resHandler.getParameter("trade_state"); //支付成功 if (trade_state == "0") { if (order.Status != OrderStatus.已确认) { Confirm(order, GetType(), order.Amount); } message = "成功"; return true; } } else { message = "失败"; return false; } } else { message = "服务器返回:" + httpClient.getResponseCode(); return false; } message = "失败"; return false; }
protected override string OnNotify(System.Web.HttpContext context) { var handler = new ResponseHandler(context); handler.setKey(key); if (handler.isTenpaySign()) { ///通知id string notify_id = handler.getParameter("notify_id"); //通过通知ID查询,确保通知来至财富通 //创建查询请求 RequestHandler queryReq = new RequestHandler(context); queryReq.init(); queryReq.setKey(key); queryReq.setGateUrl("https://gw.tenpay.com/gateway/verifynotifyid.xml"); queryReq.setParameter("partner", bargainor_id); queryReq.setParameter("notify_id", notify_id); //通信对象 TenpayHttpClient httpClient = new TenpayHttpClient(); httpClient.setTimeOut(5); //设置请求内容 httpClient.setReqContent(queryReq.getRequestURL()); if (httpClient.call()) { //设置结果参数 ClientResponseHandler queryRes = new ClientResponseHandler(); queryRes.setContent(httpClient.getResContent()); queryRes.setKey(key); //判断签名及结果 //只有签名正确,retcode为0,trade_state为0才是支付成功 if (queryRes.isTenpaySign() && queryRes.getParameter("retcode") == "0" && queryRes.getParameter("trade_state") == "0" && queryRes.getParameter("trade_mode") == "1") { string out_trade_no = queryRes.getParameter("out_trade_no"); //财富通订单号 string transaction_id = queryRes.getParameter("transaction_id"); //金额,以分为单位 string total_fee = queryRes.getParameter("total_fee"); IPayHistory order = OnlinePayBusiness.Instance.GetOrder(out_trade_no, ThisCompanyType); if (order == null) { CoreHelper.EventLog.Log(string.Format("在线支付确认时找不到订单{0} 订单号{1}", ThisCompanyType, out_trade_no), true); //context.Response.Write("fail"); return "fail"; } order.spBillno = transaction_id; decimal fee = Convert.ToDecimal(total_fee); lock (thisLock) { Confirm(order, GetType(), fee / 100); } //context.Response.Write("success"); return "success"; } } } //context.Response.Write("fail"); return "fail"; }
/// <summary> /// 退款接口,可重复提交得到状态 /// </summary> /// <param name="order"></param> /// <param name="message"></param> /// <returns></returns> public override bool RefundOrder(IPayHistory order, out string message) { #region 退款 RequestHandler reqHandler = new RequestHandler(HttpContext.Current); //通信对象 TenpayHttpClient httpClient = new TenpayHttpClient(); ClientResponseHandler resHandler = new ClientResponseHandler(); //----------------------------- //设置请求参数 //----------------------------- reqHandler.init(); reqHandler.setKey(key); reqHandler.setGateUrl("https://mch.tenpay.com/refundapi/gateway/refund.xml"); reqHandler.setParameter("partner", bargainor_id); //out_trade_no和transaction_id至少一个必填,同时存在时transaction_id优先 //reqHandler.setParameter("out_trade_no", order.OrderId); reqHandler.setParameter("transaction_id", order.OrderId); reqHandler.setParameter("out_refund_no", order.OrderId + "C"); reqHandler.setParameter("total_fee", (order.Amount * 100).ToString()); reqHandler.setParameter("refund_fee", (order.Amount * 100).ToString()); reqHandler.setParameter("op_user_id", bargainor_id); reqHandler.setParameter("op_user_passwd", ChargeConfig.GetConfigKey(ThisCompanyType, ChargeConfig.DataType.PayPass)); string requestUrl = reqHandler.getRequestURL(); //证书和证书密码 httpClient.setCertInfo(ChargeConfig.GetConfigKey(ThisCompanyType, ChargeConfig.DataType.CertFile), bargainor_id); //设置请求内容 httpClient.setReqContent(requestUrl); //设置超时 httpClient.setTimeOut(10); string rescontent = ""; //后台调用 if (!httpClient.call()) { message = "接口调用失败"; return false; } //获取结果 rescontent = httpClient.getResContent(); resHandler.setKey(key); //设置结果参数 resHandler.setContent(rescontent); message = resHandler.getParameter("retmsg"); //Log("退款返回结果为:" + rescontent); //判断签名及结果 if (!resHandler.isTenpaySign()) { message = "返回签名验证错误"; return false; } if (resHandler.getParameter("retcode") != "0") { return false; } //商户订单号 string out_trade_no = resHandler.getParameter("out_trade_no"); //财富通订单号 string transaction_id = resHandler.getParameter("transaction_id"); // 退款状态: //4,10:退款成功。 //3,5,6:退款失败。 //8,9,11:退款处理中。 //1,2:未确定,需要商户原退款单号重新发起。 //7:转入代发,退款到银行发现用户的卡作废或者冻结了,导致原路退款银行卡失败,资金回流到商户的现金帐号,需要商户人工干预,通过线下或者财富通转账的方式进行退款 string refund_status = resHandler.getParameter("refund_status"); if (refund_status == "4" || refund_status == "10") { string recv_user_id = resHandler.getParameter("recv_user_id"); string reccv_user_name = resHandler.getParameter("reccv_user_name"); //Log("退款接收人信息为:" + recv_user_id + " " + reccv_user_name); BaseRefundOrder(order); return true; } if (refund_status == "8" || refund_status == "9" || refund_status == "11") { //todo 自动重复查询 message = "退款处理中"; return true; } return false; #endregion }