protected void Page_Load(object sender, EventArgs e) { //商户号 string partner = "1900000109"; //密钥 string key = "8934e7d15453e97507ef794cf7b0519d"; //创建请求对象 CheckRequestHandler reqHandler = new CheckRequestHandler(Context); //通信对象 TenpayHttpClient httpClient = new TenpayHttpClient(); //----------------------------- //设置请求参数 //----------------------------- reqHandler.init(); reqHandler.setKey(key); reqHandler.setParameter("spid", partner); reqHandler.setParameter("trans_time", "2011-10-12"); reqHandler.setParameter("stamp", TenpayUtil.UnixStamp().ToString()); reqHandler.setParameter("cft_signtype", "0"); reqHandler.setParameter("mchtype", "0"); string requestUrl = reqHandler.getRequestURL(); //设置请求内容 httpClient.setReqContent(requestUrl); //设置超时 httpClient.setTimeOut(10); string rescontent = ""; //后台调用 if (httpClient.call()) { //获取结果 rescontent = httpClient.getResContent(); Response.Write("OK,内容:<br>\r\n" + httpClient.getResContent() + "<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/>"); */ }
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/>"); */ } }
protected void Page_Load(object sender, EventArgs e) { //商户号 string partner = "1900000107"; //密钥 string key = "82d2f8b9fd7695aec51415ab2900a755"; //创建请求对象 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/normalrefundquery.xml"); //reqHandler.setGateUrl("https://api.mch.tenpay.com/refundapi/gateway/normalrefundquery.xml"); reqHandler.setParameter("partner", partner); //out_trade_no和transaction_id至少一个必填,同时存在时transaction_id优先 reqHandler.setParameter("out_trade_no", "1144357708"); //reqHandler.setParameter("transaction_id", "1900000107201011300017102199"); string requestUrl = reqHandler.getRequestURL(); requestUrl = "http://localhost:8180/test/test.jsp"; //设置请求内容 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 refund_state_0 = resHandler.getParameter("refund_state_0"); Response.Write("OK,transaction_id=" + resHandler.getParameter("transaction_id") + ", refund_state_0=" + refund_state_0 + "<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/>"); }
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; }