$queryReq->init(); $queryReq->setKey($key); $queryReq->setGateUrl("https://gw.tenpay.com/gateway/simpleverifynotifyid.xml"); $queryReq->setParameter("partner", $partner); $queryReq->setParameter("notify_id", $notify_id); //通信对象 $httpClient = new TenpayHttpClient(); $httpClient->setTimeOut(5); //设置请求内容 $httpClient->setReqContent($queryReq->getRequestURL()); //后台调用 if ($httpClient->call()) { //设置结果参数 $queryRes = new ClientResponseHandler(); $queryRes->setContent($httpClient->getResContent()); $queryRes->setKey($key); if ($resHandler->getParameter("trade_mode") == "1") { //判断签名及结果(即时到帐) //只有签名正确,retcode为0,trade_state为0才是支付成功 if ($queryRes->isTenpaySign() && $queryRes->getParameter("retcode") == "0" && $resHandler->getParameter("trade_state") == "0") { log_result("即时到帐验签ID成功"); //取结果参数做业务处理 $out_trade_no = $resHandler->getParameter("out_trade_no"); //财付通订单号 $transaction_id = $resHandler->getParameter("transaction_id"); //金额,以分为单位 $total_fee = $resHandler->getParameter("total_fee"); //如果有使用折扣券,discount有值,total_fee+discount=原请求的total_fee $discount = $resHandler->getParameter("discount"); //------------------------------ //处理业务开始
function checkPay($PassageInfo, $OrderInfo) { /* 商户号 */ $partner = $PassageInfo['StagePartnerId']; /* 密钥 */ $key = $PassageInfo['StageSecureCode']; //创建查询请求 $queryReq = new RequestHandler(); $queryReq->init(); $queryReq->setKey($key); $queryReq->setGateUrl("https://gw.tenpay.com/gateway/normalorderquery.xml"); $queryReq->setParameter("partner", $partner); $queryReq->setParameter("out_trade_no", $OrderInfo['OrderId']); //通信对象 $httpClient = new TenpayHttpClient(); $httpClient->setTimeOut(5); //设置请求内容 $httpClient->setReqContent($queryReq->getRequestURL()); //后台调用 if ($httpClient->call()) { //设置结果参数 $queryRes = new ClientResponseHandler(); $queryRes->setContent($httpClient->getResContent()); $queryRes->setKey($key); //判断签名及结果 //只有签名正确,retcode为0,trade_state为0才是支付成功 if ($queryRes->isTenpaySign() && $queryRes->getParameter("trade_state") == "0") { return true; } else { //错误时,返回结果可能没有签名,写日志trade_state、retcode、retmsg看失败详情。 //echo "验证签名失败 或 业务错误信息:trade_state=" . $queryRes->getParameter("trade_state") . ",retcode=" . $queryRes->getParameter("retcode"). ",retmsg=" . $queryRes->getParameter("retmsg") . "<br/>" ; return false; } } else { //通信失败 return false; //后台调用通信失败,写日志,方便定位问题 //echo "<br>call err:" . $httpClient->getResponseCode() ."," . $httpClient->getErrInfo() . "<br>"; } //获取debug信息,建议把debug信息写入日志,方便定位问题 //echo $resHandler->getDebugInfo() . "<br>"; }
public function notify($request) { // $url = "http://o2o.7dit.com/tenpay.php?"; // foreach($_REQUEST as $k=>$v) // { // $url.=$k."=".$v."&"; // } // $str = file_get_contents(APP_ROOT_PATH."log.txt")."\n".print_r($_REQUEST,1)."\n".$url; // @file_put_contents(APP_ROOT_PATH."log.txt", $str); require_once APP_ROOT_PATH . "system/payment/tenpayc2c/ResponseHandler.class.php"; require_once APP_ROOT_PATH . "system/payment/tenpayc2c/RequestHandler.class.php"; require_once APP_ROOT_PATH . "system/payment/tenpayc2c/TenpayHttpClient.class.php"; require_once APP_ROOT_PATH . "system/payment/tenpayc2c/ClientResponseHandler.class.php"; $return_res = array('info' => '', 'status' => false); $payment = $GLOBALS['db']->getRow("select id,config from " . DB_PREFIX . "payment where class_name='tenpayc2c'"); $payment['config'] = unserialize($payment['config']); /* 创建支付应答对象 */ $resHandler = new ResponseHandler(); $resHandler->setKey($payment['config']['tencentpay_key']); //判断签名 if ($resHandler->isTenpaySign()) { //通知id // error_reporting(E_ALL); // ini_set("display_errors",1); $notify_id = $resHandler->getParameter("notify_id"); //通过通知ID查询,确保通知来至财付通 //创建查询请求 $queryReq = new RequestHandler(); $queryReq->init(); $queryReq->setKey($payment['config']['tencentpay_key']); $queryReq->setGateUrl("https://gw.tenpay.com/gateway/simpleverifynotifyid.xml"); $queryReq->setParameter("partner", $payment['config']['tencentpay_id']); $queryReq->setParameter("notify_id", $notify_id); //通信对象 $httpClient = new TenpayHttpClient(); $httpClient->setTimeOut(5); //设置请求内容 $httpClient->setReqContent($queryReq->getRequestURL()); //后台调用 if ($httpClient->call()) { //设置结果参数 $queryRes = new ClientResponseHandler(); $queryRes->setContent($httpClient->getResContent()); $queryRes->setKey($payment['config']['tencentpay_key']); if ($resHandler->getParameter("trade_mode") == "2") { //判断签名及结果(中介担保) //只有签名正确,retcode为0,trade_state为0才是支付成功 // print_r($queryRes); if ($queryRes->isTenpaySign() && $queryRes->getParameter("retcode") == "0") { //取结果参数做业务处理 $out_trade_no = $resHandler->getParameter("out_trade_no"); //财付通订单号 $transaction_id = $resHandler->getParameter("transaction_id"); //金额,以分为单位 $total_fee = $resHandler->getParameter("total_fee"); //如果有使用折扣券,discount有值,total_fee+discount=原请求的total_fee $discount = $resHandler->getParameter("discount"); //------------------------------ //处理业务开始 //------------------------------ //处理数据库逻辑 //注意交易单不要重复处理 //注意判断返回金额 if ($resHandler->getParameter("trade_state") == "0" || $resHandler->getParameter("trade_state") == '5' || $resHandler->getParameter("status") == "3") { $payment_notice = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "payment_notice where notice_sn = '" . $out_trade_no . "'"); $order_info = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "deal_order where id = " . $payment_notice['order_id']); require_once APP_ROOT_PATH . "system/libs/cart.php"; $GLOBALS['db']->query("update " . DB_PREFIX . "payment_notice set outer_notice_sn = '" . $transaction_id . "' where id = " . $payment_notice['id']); $rs = payment_paid($payment_notice['id']); if ($rs) { order_paid($payment_notice['order_id']); echo "success"; } else { echo 'fail'; } } else { echo "success"; } } else { //错误时,返回结果可能没有签名,写日志trade_state、retcode、retmsg看失败详情。 //echo "验证签名失败 或 业务错误信息:trade_state=" . $resHandler->getParameter("trade_state") . ",retcode=" . $queryRes-> getParameter("retcode"). ",retmsg=" . $queryRes->getParameter("retmsg") . "<br/>" ; echo "fail"; } } //获取查询的debug信息,建议把请求、应答内容、debug信息,通信返回码写入日志,方便定位问题 /* echo "<br>------------------------------------------------------<br>"; echo "http res:" . $httpClient->getResponseCode() . "," . $httpClient->getErrInfo() . "<br>"; echo "query req:" . htmlentities($queryReq->getRequestURL(), ENT_NOQUOTES, "GB2312") . "<br><br>"; echo "query res:" . htmlentities($queryRes->getContent(), ENT_NOQUOTES, "GB2312") . "<br><br>"; echo "query reqdebug:" . $queryReq->getDebugInfo() . "<br><br>" ; echo "query resdebug:" . $queryRes->getDebugInfo() . "<br><br>"; */ } else { //通信失败 echo "fail"; //后台调用通信失败,写日志,方便定位问题 echo "<br>call err:" . $httpClient->getResponseCode() . "," . $httpClient->getErrInfo() . "<br>"; } } else { header("Content-Type:text/html; charset=utf-8"); echo "<br/>" . "认证签名失败" . "<br/>"; echo $resHandler->getDebugInfo() . "<br>"; } }
public function mobile_refund() { import("@.ORG.pay.Tenpay.RequestHandler"); import("@.ORG.pay.Tenpay.client.ClientResponseHandler"); import("@.ORG.pay.Tenpay.client.TenpayHttpClient"); $reqHandler = new RequestHandler(); $httpClient = new TenpayHttpClient(); $resHandler = new ClientResponseHandler(); $reqHandler = new RequestHandler(); $reqHandler->init(); $reqHandler->setKey($this->pay_config["pay_tenpay_partnerkey"]); $reqHandler->setGateUrl("https://gw.tenpay.com/gateway/normalrefundquery.xml"); $httpClient = new TenpayHttpClient(); $resHandler = new ClientResponseHandler(); $reqHandler->setParameter("partner", $this->pay_config["pay_tenpay_partnerid"]); $reqHandler->setParameter("out_trade_no", $this->order_info["order_type"] . "_" . $this->order_info["order_id"]); $reqHandler->setParameter("input_charset", "utf-8"); $httpClient->setTimeOut(30); $httpClient->setMethod("POST"); $httpClient->setReqContent($reqHandler->getRequestURL()); if ($httpClient->call()) { $resHandler->setContent($httpClient->getResContent()); $resHandler->setKey($this->pay_config["pay_tenpay_partnerkey"]); if ($resHandler->isTenpaySign() && $resHandler->getParameter("retcode") == 0) { $refund_param["refund_id"] = $resHandler->getParameter("out_refund_no_0"); $refund_param["refund_time"] = $refundResult["timestamp"]; return array("error" => 0, "type" => "ok", "msg" => "退款申请成功!5到10个工作日款项会自动流入您支付时使用的银行卡内。", "refund_param" => $refund_param); } else { $refund_param["err_msg"] = "验证签名失败 或 业务错误信息:retcode= " . $resHandler->getParameter("retcode") . ",retmsg= " . $resHandler->getParameter("retmsg"); $refund_param["refund_time"] = time(); return array("error" => 1, "type" => "fail", "msg" => "退款申请失败!如果重试多次还是失败请联系系统管理员。", "refund_param" => $refund_param); } } else { $refund_param["err_msg"] = "call err:" . $httpClient->getResponseCode() . "," . $httpClient->getErrInfo(); $refund_param["refund_time"] = time(); return array("error" => 1, "type" => "fail", "msg" => "退款申请失败!如果重试多次还是失败请联系系统管理员。", "refund_param" => $refund_param); } }
public function houtai() { $pay_type = $this->db->GetOne("SELECT * from `@#_pay` where `pay_class` = 'tenpay' and `pay_start` = '1'"); $pay_type_key = unserialize($pay_type['pay_key']); $key = $pay_type_key['key']['val']; //支付KEY $partner = $pay_type_key['id']['val']; //支付商号ID /* 创建支付应答对象 */ $resHandler = new ResponseHandler(); $resHandler->setKey($key); //判断签名 if (!$resHandler->isTenpaySign()) { echo "认证签名失败"; exit; } //通知ID $notify_id = $resHandler->getParameter("notify_id"); //通过通知ID查询,确保通知来至财付通 //创建查询请求 $queryReq = new RequestHandler(); $queryReq->init(); $queryReq->setKey($key); $queryReq->setGateUrl("https://gw.tenpay.com/gateway/simpleverifynotifyid.xml"); $queryReq->setParameter("partner", $partner); $queryReq->setParameter("notify_id", $notify_id); //通信对象 $httpClient = new TenpayHttpClient(); $httpClient->setTimeOut(5); //设置请求内容 $httpClient->setReqContent($queryReq->getRequestURL()); //后台调用 if ($httpClient->call()) { //设置结果参数 $queryRes = new ClientResponseHandler(); $queryRes->setContent($httpClient->getResContent()); $queryRes->setKey($key); } else { echo "通信失败"; exit; } //及时到账 if ($resHandler->getParameter("trade_mode") == "1") { //只有签名正确,retcode为0,trade_state为0才是支付成功 if ($queryRes->isTenpaySign() && $queryRes->getParameter("retcode") == "0" && $resHandler->getParameter("trade_state") == "0") { //log_result("即时到帐验签ID成功"); //取结果参数做业务处理 $out_trade_no = $resHandler->getParameter("out_trade_no"); //财付通订单号 $transaction_id = $resHandler->getParameter("transaction_id"); //金额,以分为单位 $total_fee = $resHandler->getParameter("total_fee"); //如果有使用折扣券,discount有值,total_fee+discount=原请求的total_fee $discount = $resHandler->getParameter("discount"); //------------------------------ //处理业务开始 //------------------------------ //处理数据库逻辑 //注意交易单不要重复处理 //注意判断返回金额 $total_fee_t = $total_fee / 100; $this->db->Autocommit_start(); $dingdaninfo = $this->db->GetOne("select * from `@#_member_addmoney_record` where `code` = '{$out_trade_no}' and `money` = '{$total_fee_t}' and `status` = '未付款' for update"); if (!$dingdaninfo) { echo "fail"; exit; } $time = time(); $up_q1 = $this->db->Query("UPDATE `@#_member_addmoney_record` SET `pay_type` = '财付通', `status` = '已付款' where `id` = '{$dingdaninfo['id']}' and `code` = '{$dingdaninfo['code']}'"); $up_q2 = $this->db->Query("UPDATE `@#_member` SET `money` = `money` + {$total_fee_t} where (`uid` = '{$dingdaninfo['uid']}')"); $up_q3 = $this->db->Query("INSERT INTO `@#_member_account` (`uid`, `type`, `pay`, `content`, `money`, `time`) VALUES ('{$dingdaninfo['uid']}', '1', '账户', '充值', '{$total_fee_t}', '{$time}')"); if ($up_q1 && $up_q2 && $up_q3) { $this->db->Autocommit_commit(); } else { $this->db->Autocommit_rollback(); echo "fail"; exit; } if (empty($dingdaninfo['scookies'])) { echo "success"; exit; } $uid = $dingdaninfo['uid']; $scookies = unserialize($dingdaninfo['scookies']); $pay = System::load_app_class('pay', 'pay'); $pay->scookie = $scookies; $ok = $pay->init($uid, $pay_type['pay_id'], 'go_record'); //闪购商品 if ($ok != 'ok') { _setcookie('Cartlist', NULL); echo "fail"; exit; //商品购买失败 } $check = $pay->go_pay(1); if ($check) { $this->db->Query("UPDATE `@#_member_addmoney_record` SET `scookies` = '1' where `code` = '{$out_trade_no}' and `status` = '已付款'"); _setcookie('Cartlist', NULL); echo "success"; exit; } else { echo "fail"; exit; } //------------------------------ //处理业务完毕 //------------------------------ //log_result("即时到帐后台回调成功"); } else { echo "fail"; } } else { //通信失败 //echo "fail"; //后台调用通信失败,写日志,方便定位问题 //echo "<br>call err:" . $httpClient->getResponseCode() ."," . $httpClient->getErrInfo() . "<br>"; } }
public function notify_url() { $partner = CS_Tenpay_ID; $key = CS_Tenpay_Key; require CSCMSPATH . "pay/tenpay/ResponseHandler.class.php"; require CSCMSPATH . "pay/tenpay/RequestHandler.class.php"; require CSCMSPATH . "pay/tenpay/client/ClientResponseHandler.class.php"; require CSCMSPATH . "pay/tenpay/client/TenpayHttpClient.class.php"; $resHandler = new ResponseHandler(); $resHandler->setKey($key); if ($resHandler->isTenpaySign()) { $notify_id = $resHandler->getParameter("notify_id"); $queryReq = new RequestHandler(); $queryReq->init(); $queryReq->setKey($key); $queryReq->setGateUrl("https://gw.tenpay.com/gateway/simpleverifynotifyid.xml"); $queryReq->setParameter("partner", $partner); $queryReq->setParameter("notify_id", $notify_id); $httpClient = new TenpayHttpClient(); $httpClient->setTimeOut(5); $httpClient->setReqContent($queryReq->getRequestURL()); if ($httpClient->call()) { $queryRes = new ClientResponseHandler(); $queryRes->setContent($httpClient->getResContent()); $queryRes->setKey($key); if ($resHandler->getParameter("trade_mode") == "1") { //判断签名及结果(即时到帐) if ($queryRes->isTenpaySign() && $queryRes->getParameter("retcode") == "0" && $resHandler->getParameter("trade_state") == "0") { $out_trade_no = $resHandler->getParameter("out_trade_no"); $transaction_id = $resHandler->getParameter("transaction_id"); $total_fee = $resHandler->getParameter("total_fee"); $discount = $resHandler->getParameter("discount"); //获取订单记录 $row = $this->CsdjDB->get_row('pay', '*', $out_trade_no, 'dingdan'); if ($row && $row->pid != 1) { //增加金钱 $this->db->query("update " . CS_SqlPrefix . "user set rmb=rmb+" . $row->rmb . " where id=" . $row->uid . ""); //改变状态 $this->db->query("update " . CS_SqlPrefix . "pay set pid=1 where id=" . $row->id . ""); //发送通知 $add['uida'] = $row->uid; $add['uidb'] = 0; $add['name'] = L('pay_11'); $add['neir'] = L('pay_17', array($row->rmb, $out_trade_no)); $add['addtime'] = time(); $this->CsdjDB->get_insert('msg', $add); } echo "success"; } else { echo "fail"; } } elseif ($resHandler->getParameter("trade_mode") == "2") { //判断签名及结果(中介担保) if ($queryRes->isTenpaySign() && $queryRes->getParameter("retcode") == "0") { $out_trade_no = $resHandler->getParameter("out_trade_no"); $transaction_id = $resHandler->getParameter("transaction_id"); $total_fee = $resHandler->getParameter("total_fee"); $discount = $resHandler->getParameter("discount"); $row = $this->CsdjDB->get_row('pay', '*', $out_trade_no, 'dingdan'); switch ($resHandler->getParameter("trade_state")) { case "0": //付款成功 if ($row) { $this->db->query("update " . CS_SqlPrefix . "pay set pid=2 where id=" . $row->id . ""); //发送通知 $add['uida'] = $row->uid; $add['uidb'] = 0; $add['name'] = L('pay_13'); $add['neir'] = L('pay_14', array($row->rmb, $out_trade_no)); $add['addtime'] = time(); $this->CsdjDB->get_insert('msg', $add); } break; case "1": //交易创建 break; case "2": //收获地址填写完毕 break; case "4": //卖家发货成功 if ($row) { $this->db->query("update " . CS_SqlPrefix . "pay set pid=3 where id=" . $row->id . ""); //发送通知 $add['uida'] = $row->uid; $add['uidb'] = 0; $add['name'] = L('pay_15'); $add['neir'] = L('pay_16', array($out_trade_no)); $add['addtime'] = time(); $this->CsdjDB->get_insert('msg', $add); } break; case "5": //买家收货确认,交易成功 if ($row && $row->pid != 1) { //增加金钱 $this->db->query("update " . CS_SqlPrefix . "user set rmb=rmb+" . $row->rmb . " where id=" . $row->uid . ""); //改变状态 $this->db->query("update " . CS_SqlPrefix . "pay set pid=1 where id=" . $row->id . ""); //发送通知 $add['uida'] = $row->uid; $add['uidb'] = 0; $add['name'] = L('pay_11'); $add['neir'] = L('pay_17', array($row->rmb, $out_trade_no)); $add['addtime'] = time(); $this->CsdjDB->get_insert('msg', $add); } break; case "6": //交易关闭,未完成超时关闭 break; case "7": //修改交易价格成功 break; case "8": //买家发起退款 break; case "9": //退款成功 break; case "10": //退款关闭 break; default: break; } echo "success"; } else { echo "fail"; } } } else { //通信失败 echo "fail"; } } else { echo "fail"; } }
function checkPay($PassageInfo, $OrderInfo) { /* 商户号 */ $partner = $PassageInfo['StagePartnerId']; /* 密钥 */ $key = $PassageInfo['StageSecureCode']; //创建查询请求 $queryReq = new RequestHandler(); $queryReq->init(); $queryReq->setKey($key); $queryReq->setGateUrl("https://gw.tenpay.com/gateway/normalorderquery.xml"); $queryReq->setParameter("partner", $partner); $queryReq->setParameter("out_trade_no", $OrderInfo['OrderId']); //通信对象 $httpClient = new TenpayHttpClient(); $httpClient->setTimeOut(5); //设置请求内容 $httpClient->setReqContent($queryReq->getRequestURL()); //后台调用 if ($httpClient->call()) { //设置结果参数 $queryRes = new ClientResponseHandler(); $queryRes->setContent($httpClient->getResContent()); $queryRes->setKey($key); //判断签名及结果 //只有签名正确,retcode为0,trade_state为0才是支付成功 if ($queryRes->isTenpaySign()) { echo "here"; // //取结果参数做业务处理 // $out_trade_no = $queryRes->getParameter("out_trade_no"); // //财付通订单号 // $transaction_id = $queryRes->getParameter("transaction_id"); // //金额,以分为单位 // $total_fee = $queryRes->getParameter("total_fee"); // //如果有使用折扣券,discount有值,total_fee+discount=原请求的total_fee // $discount = $queryRes->getParameter("discount"); // // if($OrderInfo['OrderId']==$out_trade_no)&&($total_fee==$Pay['Coin']) // { // $Pay['OrderId'] = $OrderInfo['OrderId']; // $Pay['PayUserId'] = $OrderInfo['PayUserId']; // $Pay['AcceptUserId'] = $OrderInfo['AcceptUserId']; // $Pay['PassageId'] = $OrderInfo['PassageId']; // $Pay['Amount'] = $OrderInfo['Amount']; // $Pay['Coin'] = $OrderInfo['Coin']; // $Pay['Credit'] = $OrderInfo['Credit']; // $Pay['PayIP'] = $OrderInfo['PayIp']; // $Pay['PayTime'] = $OrderInfo['PayTime']; // $Pay['PayedTime'] = strtotime($queryRes->getParameter("time_end")); // $Pay['StageOrder'] = $transaction_id; // $Pay['comment'] = json_encode(array('bank_info'=>'bank_type'!="DEFAULT"?$queryRes->getParameter("bank_type")."|".$queryRes->getParameter("bank_billno"):"DEFAULT")); // return $Pay; // } // else // { // return false; // } // //------------------------------ // //处理业务开始 // //------------------------------ // // //处理数据库逻辑 // //注意交易单不要重复处理 // //注意判断返回金额 // // //------------------------------ // //处理业务完毕 // //------------------------------ // echo "success"; } else { //错误时,返回结果可能没有签名,写日志trade_state、retcode、retmsg看失败详情。 //echo "验证签名失败 或 业务错误信息:trade_state=" . $queryRes->getParameter("trade_state") . ",retcode=" . $queryRes->getParameter("retcode"). ",retmsg=" . $queryRes->getParameter("retmsg") . "<br/>" ; echo "1fail"; } //获取查询的debug信息,建议把请求、应答内容、debug信息,通信返回码写入日志,方便定位问题 /* echo "<br>------------------------------------------------------<br>"; echo "http res:" . $httpClient->getResponseCode() . "," . $httpClient->getErrInfo() . "<br>"; echo "query req:" . htmlentities($queryReq->getRequestURL(), ENT_NOQUOTES, "GB2312") . "<br><br>"; echo "query res:" . htmlentities($queryRes->getContent(), ENT_NOQUOTES, "GB2312") . "<br><br>"; echo "query reqdebug:" . $queryReq->getDebugInfo() . "<br><br>" ; echo "query resdebug:" . $queryRes->getDebugInfo() . "<br><br>"; */ } else { //通信失败 echo "2fail"; //后台调用通信失败,写日志,方便定位问题 //echo "<br>call err:" . $httpClient->getResponseCode() ."," . $httpClient->getErrInfo() . "<br>"; } //获取debug信息,建议把debug信息写入日志,方便定位问题 //echo $resHandler->getDebugInfo() . "<br>"; }
public function tenpaynotice() { require_once C('APP_ROOT') . "Lib/Pay/Tenpay/ResponseHandler.class.php"; require_once C('APP_ROOT') . "Lib/Pay/Tenpay/RequestHandler.class.php"; require_once C('APP_ROOT') . "Lib/Pay/Tenpay/client/ClientResponseHandler.class.php"; require_once C('APP_ROOT') . "Lib/Pay/Tenpay/client/TenpayHttpClient.class.php"; /* 创建支付应答对象 */ $resHandler = new ResponseHandler(); $resHandler->setKey($this->payConfig['tenpay']['mkey']); //Log::write('tenpay notice:'.print_r($resHandler->getAllParameters(), true), Log::DEBUG); //判断签名 if ($resHandler->isTenpaySign()) { //通知id $notify_id = $resHandler->getParameter("notify_id"); $tenpay_params = C('TENPAY_PARAMS'); //通过通知ID查询,确保通知来至财付通 //创建查询请求 $queryReq = new RequestHandler(); $queryReq->init(); $queryReq->setKey($this->payConfig['tenpay']['mkey']); $queryReq->setGateUrl($tenpay_params["VERIFY_URL"]); $queryReq->setParameter("partner", $this->payConfig['tenpay']['MerCode']); $queryReq->setParameter("notify_id", $notify_id); $queryReq->setParameter("input_charset", "UTF-8"); //通信对象 $httpClient = new TenpayHttpClient(); $httpClient->setTimeOut(5); //设置请求内容 $httpClient->setReqContent($queryReq->getRequestURL()); //后台调用 if ($httpClient->call()) { //设置结果参数 $queryRes = new ClientResponseHandler(); $queryRes->setContent($httpClient->getResContent()); $queryRes->setKey($this->payConfig['tenpay']['mkey']); //判断签名及结果 //只有签名正确,retcode为0,trade_state为0才是支付成功 if ($queryRes->isTenpaySign() && $queryRes->getParameter("retcode") == "0" && $queryRes->getParameter("trade_state") == "0" && $queryRes->getParameter("trade_mode") == "1") { //取结果参数做业务处理 $out_trade_no = $queryRes->getParameter("out_trade_no"); //财付通订单号 $transaction_id = $queryRes->getParameter("transaction_id"); //金额,以分为单位 $total_fee = $queryRes->getParameter("total_fee"); //如果有使用折扣券,discount有值,total_fee+discount=原请求的total_fee $discount = $queryRes->getParameter("discount"); //------------------------------ //处理业务开始 //------------------------------ //处理数据库逻辑 //注意交易单不要重复处理 //注意判断返回金额 $nid = $this->createnid("tenpay", $out_trade_no); $done = $this->payDone(1, $nid, $transaction_id, "total_fee:" . $total_fee); //------------------------------ //处理业务完毕 //------------------------------ echo "success"; } else { //错误时,返回结果可能没有签名,写日志trade_state、retcode、retmsg看失败详情。 //echo "验证签名失败 或 业务错误信息:trade_state=" . $queryRes->getParameter("trade_state") . ",retcode=" . $queryRes->getParameter("retcode"). ",retmsg=" . $queryRes->getParameter("retmsg") . "<br/>" ; $out_trade_no = $resHandler->getParameter("out_trade_no"); $nid = $this->createnid("tenpay", $out_trade_no); $done = $this->payDone(3, $nid, "", print_r($queryRes->getAllParameters(), true)); echo "fail"; } } else { // $out_trade_no = $resHandler->getParameter("out_trade_no"); // $nid = $this->createnid( "tenpay", $out_trade_no); // $done = $this->payDone(3,$nid, "", print_r($resHandler->getAllParameters(), true)); //通信失败 echo "fail"; //后台调用通信失败,写日志,方便定位问题 //echo "<br>call err:" . $httpClient->getResponseCode() ."," . $httpClient->getErrInfo() . "<br>"; Log::write("tenpay notice:<br>call err:" . $httpClient->getResponseCode() . "," . $httpClient->getErrInfo(), Log::DEBUG); } } else { $out_trade_no = $resHandler->getParameter("out_trade_no"); $transaction_id = $resHandler->getParameter("transaction_id"); $nid = $this->createnid("tenpay", $out_trade_no); $done = $this->payDone(2, $nid, $transaction_id, print_r($resHandler->getAllParameters(), true)); //回调签名错误 echo "fail"; } }
/** * POST接收数据 * 状态码说明 (0 交易完成 1 交易失败 2 交易超时 3 交易处理中 4 交易未支付 5交易取消 6交易发生错误) */ public function notify() { /* 创建支付应答对象 */ $resHandler = new ResponseHandler(); $resHandler->setKey($this->config['key']); //判断签名 if ($resHandler->isTenpaySign()) { //通知id $notify_id = $resHandler->getParameter("notify_id"); //通过通知ID查询,确保通知来至财付通 //创建查询请求 $queryReq = new RequestHandler(); $queryReq->init(); $queryReq->setKey($this->config['key']); $queryReq->setGateUrl("https://gw.tenpay.com/gateway/simpleverifynotifyid.xml"); $queryReq->setParameter("partner", $this->config['partner']); $queryReq->setParameter("notify_id", $notify_id); //通信对象 $httpClient = new TenpayHttpClient(); $httpClient->setTimeOut(5); //设置请求内容 $httpClient->setReqContent($queryReq->getRequestURL()); //后台调用 if ($httpClient->call()) { //设置结果参数 $queryRes = new ClientResponseHandler(); $queryRes->setContent($httpClient->getResContent()); $queryRes->setKey($this->config['key']); if ($resHandler->getParameter("trade_mode") == "1") { //判断签名及结果(即时到帐) //只有签名正确,retcode为0,trade_state为0才是支付成功 if ($queryRes->isTenpaySign() && $queryRes->getParameter("retcode") == "0" && $resHandler->getParameter("trade_state") == "0") { $return_data['order_id'] = $resHandler->getParameter("out_trade_no"); $return_data['price'] = dround($resHandler->getParameter("total_fee") / 100); $return_data['account'] = $resHandler->getParameter("partner"); $return_data['order_status'] = 0; return $return_data; } else { //错误时,返回结果可能没有签名,写日志trade_state、retcode、retmsg看失败详情。 //echo "验证签名失败 或 业务错误信息:trade_state=" . $resHandler->getParameter("trade_state") . ",retcode=" . $queryRes-> getParameter("retcode"). ",retmsg=" . $queryRes->getParameter("retmsg") . "<br/>" ; error_log(date('m-d H:i:s') . '| POST: ' . $queryRes->getParameter("retmsg") . ' |' . "\r\n", 3, LOG_PATH . 'tenpay_error_log.php'); return false; } } elseif ($resHandler->getParameter("trade_mode") == "2") { //判断签名及结果(中介担保) //只有签名正确,retcode为0,trade_state为0才是支付成功 if ($queryRes->isTenpaySign() && $queryRes->getParameter("retcode") == "0") { $return_data['order_id'] = $resHandler->getParameter("out_trade_no"); $return_data['price'] = dround($resHandler->getParameter("total_fee") / 100); $return_data['account'] = $resHandler->getParameter("partner"); switch ($resHandler->getParameter("trade_state")) { case 0: $return_data['order_status'] = 3; break; case 1: $return_data['order_status'] = 4; break; case 2: $return_data['order_status'] = 4; break; case 4: $return_data['order_status'] = 3; break; case 5: $return_data['order_status'] = 0; break; case 6: $return_data['order_status'] = 5; break; case 7: $return_data['order_status'] = 3; break; case 8: $return_data['order_status'] = 3; break; case 9: $return_data['order_status'] = 5; break; case 10: $return_data['order_status'] = 5; break; default: $return_data['order_status'] = 5; } return $return_data; } else { //错误时,返回结果可能没有签名,写日志trade_state、retcode、retmsg看失败详情。 //echo "验证签名失败 或 业务错误信息:trade_state=" . $resHandler->getParameter("trade_state") . ",retcode=" . $queryRes-> getParameter("retcode"). ",retmsg=" . $queryRes->getParameter("retmsg") . "<br/>" ; error_log(date('m-d H:i:s') . '| POST: ' . $queryRes->getParameter("retmsg") . ' |' . "\r\n", 3, LOG_PATH . 'tenpay_error_log.php'); return false; } } //获取查询的debug信息,建议把请求、应答内容、debug信息,通信返回码写入日志,方便定位问题 /* echo "<br>------------------------------------------------------<br>"; echo "http res:" . $httpClient->getResponseCode() . "," . $httpClient->getErrInfo() . "<br>"; echo "query req:" . htmlentities($queryReq->getRequestURL(), ENT_NOQUOTES, "GB2312") . "<br><br>"; echo "query res:" . htmlentities($queryRes->getContent(), ENT_NOQUOTES, "GB2312") . "<br><br>"; echo "query reqdebug:" . $queryReq->getDebugInfo() . "<br><br>" ; echo "query resdebug:" . $queryRes->getDebugInfo() . "<br><br>"; */ } else { error_log(date('m-d H:i:s') . '| POST: ' . $httpClient->getErrInfo() . ' |' . "\r\n", 3, LOG_PATH . 'tenpay_error_log.php'); return false; } } else { error_log(date('m-d H:i:s') . '| POST: ' . $resHandler->getDebugInfo() . ' |' . "\r\n", 3, LOG_PATH . 'tenpay_error_log.php'); return false; } }
$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", "201009151621261820"); //$reqHandler->setParameter("transaction_id", "2000000501201004300000000442"); //----------------------------- //设置通信参数 //----------------------------- $httpClient->setTimeOut(5); //设置请求内容 $httpClient->setReqContent($reqHandler->getRequestURL()); //后台调用 if ($httpClient->call()) { //设置结果参数 $resHandler->setContent($httpClient->getResContent()); $resHandler->setKey($key); //判断签名及结果 //只有签名正确并且retcode为0才是请求成功 if ($resHandler->isTenpaySign() && $resHandler->getParameter("retcode") == "0") { //取结果参数做业务处理 //商户订单号 $out_trade_no = $resHandler->getParameter("out_trade_no"); //财付通订单号 $transaction_id = $resHandler->getParameter("transaction_id"); //金额,以分为单位 $total_fee = $resHandler->getParameter("total_fee"); //支付结果 $trade_state = $resHandler->getParameter("trade_state"); //支付成功 if ($trade_state == "0") { }