Home > php教程 > php手册 > ThinkPHP3.2中集成财付通支付

ThinkPHP3.2中集成财付通支付

WBOY
Release: 2016-06-07 11:41:50
Original
1164 people have browsed it

ThinkPHP3.2中集成财付通支付
在项目过程中用到了支付宝和财付通的支付接口问题,发现在TP还未有人写财付通的实例,我仿照前辈们写的一个案例http://www.thinkphp.cn/code/240.html写了一个

一、把接口集成开发包中文件提出来,用的php(utf8)版
ThinkPHP3.2中集成财付通支付
把classes文件夹中的文件复制到TP目录下的Library\Think 新建一个Tenpay目录。
二,加入命名空间,方便调用
ThinkPHP3.2中集成财付通支付ThinkPHP3.2中集成财付通支付ThinkPHP3.2中集成财付通支付ThinkPHP3.2中集成财付通支付

三、在项目配置文件中加入接口配置参数
  //财付通配置参数<br>     'tenpay_config' => array(<br>         'partner' => '**********', //这里是你在成功申请财付通接口后获取到的商户号;<br>         'key' => '*******************', //这里是你在成功申请财付通接口后获取到的密钥<br>         'return_url' => '********/tenreturnurl',<br>         'notify_url' => '********/tennotifyurl',<br>     ),三、在支付操作的控制器中加入代码如下
    public function tenpay($data) {<br>         $tenpay_config = C('tenpay_config');<br>         /* 获取提交的订单号 */<br>         $out_trade_no = $data["orderid"];<br>         /* 获取提交的商品名称 */<br>         $product_name = $data["title"];<br>         /* 获取提交的商品价格 */<br>         $order_price = $data["money"];<br>         /* 获取提交的备注信息 */<br>         $remarkexplain = $data["remark"];<br>         /* 支付方式 */<br>         $trade_mode = 1; <br> <br>         $strDate = date("Ymd");<br>         $strTime = date("His");<br> <br>         /* 商品价格(包含运费),以分为单位 */<br>         $total_fee = $order_price * 100;<br> <br>         /* 商品名称 */<br>         $desc = "商品:" . $product_name . ",备注:" . $remarkexplain;<br> <br>         /* 创建支付请求对象 */<br>         $reqHandler = new \Think\Tenpay\RequestHandler();<br>         $reqHandler->init();<br>         $reqHandler->setKey(trim($tenpay_config['key']));<br>         $reqHandler->setGateUrl("https://gw.tenpay.com/gateway/pay.htm");<br> <br> //----------------------------------------<br> //设置支付参数 <br> //----------------------------------------<br>         $reqHandler->setParameter("partner", trim($tenpay_config['partner']));<br>         $reqHandler->setParameter("out_trade_no", $out_trade_no);<br>         $reqHandler->setParameter("total_fee", $total_fee);  //总金额<br>         $reqHandler->setParameter("return_url", $tenpay_config['return_url']);<br>         $reqHandler->setParameter("notify_url", $tenpay_config['notify_url']);<br>         $reqHandler->setParameter("body", $desc);<br>         $reqHandler->setParameter("bank_type", "DEFAULT");     //银行类型,默认为财付通<br> //用户ip<br>         $reqHandler->setParameter("spbill_create_ip", $_SERVER['REMOTE_ADDR']); //客户端IP<br>         $reqHandler->setParameter("fee_type", "1");               //币种<br>         $reqHandler->setParameter("subject", $desc);          //商品名称,(中介交易时必填)<br> //系统可选参数<br>         $reqHandler->setParameter("sign_type", "MD5");       //签名方式,默认为MD5,可选RSA<br>         $reqHandler->setParameter("service_version", "1.0");    //接口版本号<br>         $reqHandler->setParameter("input_charset", "utf-8");      //字符集<br>         $reqHandler->setParameter("sign_key_index", "1");       //密钥序号<br> //业务可选参数<br>         $reqHandler->setParameter("attach", "");                //附件数据,原样返回就可以了<br>         $reqHandler->setParameter("product_fee", "");           //商品费用<br>         $reqHandler->setParameter("transport_fee", "0");         //物流费用<br>         $reqHandler->setParameter("time_start", date("YmdHis"));  //订单生成时间<br>         $reqHandler->setParameter("time_expire", "");             //订单失效时间<br>         $reqHandler->setParameter("buyer_id", "");                //买方财付通帐号<br>         $reqHandler->setParameter("goods_tag", "");               //商品标记<br>         $reqHandler->setParameter("trade_mode", $trade_mode);              //交易模式(1.即时到帐模式,2.中介担保模式,3.后台选择(卖家进入支付中心列表选择))<br>         $reqHandler->setParameter("transport_desc", "");              //物流说明<br>         $reqHandler->setParameter("trans_type", "1");              //交易类型<br>         $reqHandler->setParameter("agentid", "");                  //平台ID<br>         $reqHandler->setParameter("agent_type", "");               //代理模式(0.无代理,1.表示卡易售模式,2.表示网店模式)<br>         $reqHandler->setParameter("seller_id", "");                //卖家的商户号<br> //请求的URL<br>         $reqUrl = $reqHandler->getRequestURL();<br> //获取debug信息,建议把请求和debug信息写入日志,方便定位问题<br>         /**/<br>         $reqHandler->doSend2($reqUrl);<br>         $debugInfo = $reqHandler->getDebugInfo();<br>     } 声明一下,$data是商品的参数,里面必须包含订单号,商品名称,订单描述,商品价格。上述代码中的doSend2函数是一个跳转函数,修改的是ResponseHandler.class.php中的dosend函数。function doSend2($url) {<br>         header("Location:" . $url);<br>         exit;<br>     }四、支付成功跳转处理
/*<br>       财付通页面跳转处理方法;<br> <br>      */<br> <br>     function tenreturnurl() {<br>         /* 创建支付应答对象 */<br>         $tenpay_config = C('tenpay_config');<br>         $resHandler = new \Think\Tenpay\ResponseHandler();<br>         $resHandler->setKey(trim($tenpay_config['key']));<br> <br> //判断签名<br>         if ($resHandler->isTenpaySign()) {<br>             //通知id<br>             $notify_id = $resHandler->getParameter("notify_id");<br>             //商户订单号<br>             $out_trade_no = $resHandler->getParameter("out_trade_no");<br>             //财付通订单号<br>             $transaction_id = $resHandler->getParameter("transaction_id");<br>             //金额,以分为单位<br>             $total_fee = $resHandler->getParameter("total_fee");<br>             //如果有使用折扣券,discount有值,total_fee+discount=原请求的total_fee<br>             $discount = $resHandler->getParameter("discount");<br>             //支付结果<br>             $trade_state = $resHandler->getParameter("trade_state");<br>             //交易模式,1即时到账<br>             $trade_mode = $resHandler->getParameter("trade_mode");<br>             //支付完成时间<br>             $time_end = $resHandler->getParameter("time_end");<br>             $parameter = array(<br>                 "out_trade_no" => $out_trade_no, //商户订单编号;<br>                 "trade_no" => $transaction_id, //财付通订单号;<br>                 "total_fee" => $total_fee, //交易金额;<br>                 "trade_status" => $trade_state, //交易状态<br>                 "notify_id" => $notify_id, //通知校验ID。<br>                 "notify_time" => $time_end, //通知的发送时间。<br>             );<br>             if ("1" == $trade_mode) {<br>                 if ("0" == $trade_state) {<br>                     $status = CommonController::checkorderstatus($out_trade_no);<br>                     if (!$status) {<br>                         CommonController::orderhandle($parameter);<br>                         //进行订单处理,并传送从支付宝返回的参数;<br>                     }<br>                     $this->success("支付成功", U('Home/Order/Payscuess', array('id' => $out_trade_no)));<br>                 } else {<br>                     //当做不成功处理<br>                     $this->error("支付失败");<br>                 }<br>             }<br>         } else {<br>             $this->error("认证签名失败," . $resHandler->getDebugInfo());<br>         }<br>     }<br> <br>     /*<br>       财付通异步通知处理方法;<br> <br>      */<br> <br>     function tennotifyurl() {<br>         $tenpay_config = C('tenpay_config');<br>         /* 创建支付应答对象 */<br>         $resHandler = new \Think\Tenpay\ResponseHandler();<br>         $resHandler->setKey(trim($tenpay_config['key']));<br>         //判断签名<br>         if ($resHandler->isTenpaySign()) {<br> <br>             //通知id<br>             $notify_id = $resHandler->getParameter("notify_id");<br> <br>             //通过通知ID查询,确保通知来至财付通<br>             //创建查询请求<br>             $queryReq = new \Think\Tenpay\RequestHandler();<br>             $queryReq->init();<br>             $queryReq->setKey(trim($tenpay_config['key']));<br>             $queryReq->setGateUrl("https://gw.tenpay.com/gateway/simpleverifynotifyid.xml");<br>             $queryReq->setParameter("partner", trim($tenpay_config['partner']));<br>             $queryReq->setParameter("notify_id", $notify_id);<br> <br>             //通信对象<br>             $httpClient = new \Think\Tenpay\client\TenpayHttpClient();<br>             $httpClient->setTimeOut(5);<br>             //设置请求内容<br>             $httpClient->setReqContent($queryReq->getRequestURL());<br> <br>             //后台调用<br>             if ($httpClient->call()) {<br>                 //设置结果参数<br>                 $queryRes = new \Think\Tenpay\client\ClientResponseHandler();<br>                 $queryRes->setContent($httpClient->getResContent());<br>                 $queryRes->setKey(trim($tenpay_config['key']));<br> <br>                 if ($resHandler->getParameter("trade_mode") == "1") {<br>                     //判断签名及结果(即时到帐)<br>                     //只有签名正确,retcode为0,trade_state为0才是支付成功<br>                     if ($queryRes->isTenpaySign() && $queryRes->getParameter("retcode") == "0" && $resHandler->getParameter("trade_state") == "0") {<br> <br>                         //通知id<br>                         $notify_id = $resHandler->getParameter("notify_id");<br>                         //商户订单号<br>                         $out_trade_no = $resHandler->getParameter("out_trade_no");<br>                         //财付通订单号<br>                         $transaction_id = $resHandler->getParameter("transaction_id");<br>                         //金额,以分为单位<br>                         $total_fee = $resHandler->getParameter("total_fee");<br>                         //如果有使用折扣券,discount有值,total_fee+discount=原请求的total_fee<br>                         $discount = $resHandler->getParameter("discount");<br>                         //支付结果<br>                         $trade_state = $resHandler->getParameter("trade_state");<br>                         //交易模式,1即时到账<br>                         $trade_mode = $resHandler->getParameter("trade_mode");<br>                         //支付完成时间<br>                         $time_end = $resHandler->getParameter("time_end");<br>                         $parameter = array(<br>                             "out_trade_no" => $out_trade_no, //商户订单编号;<br>                             "trade_no" => $transaction_id, //财付通订单号;<br>                             "total_fee" => $total_fee, //交易金额;<br>                             "trade_status" => $trade_state, //交易状态<br>                             "notify_id" => $notify_id, //通知校验ID。<br>                             "notify_time" => $time_end, //通知的发送时间。<br>                         );<br>                         $status = CommonController::checkorderstatus($out_trade_no);<br>                         if (!$status) {<br>                             CommonController::orderhandle($parameter);<br>                             //进行订单处理,并传送从支付宝返回的参数;<br>                         }<br>                         echo "success";<br>                     } else {<br> <br>                         echo "fail";<br>                     }<br>                 }<br>             } else {<br>                 //通信失败<br>                 echo "fail";<br>                 //后台调用通信失败,写日志,方便定位问题<br>                 echo "<br>call err:" . $httpClient->getResponseCode() . "," . $httpClient->getErrInfo() . "<br>";<br>             }<br>         } else {<br>             echo "<br>" . "认证签名失败" . "<br>";<br>             echo $resHandler->getDebugInfo() . "<br>";<br>         }<br>     }   其中的checkorderstatus和orderhandle用的是上述支付宝即时交易案例中的订单处理函数  //处理订单函数<br>     //更新订单状态,写入订单支付后返回的数据<br>     function orderhandle($parameter) {<br>         $ordid = $parameter['out_trade_no'];<br>         $data['trade_no'] = $parameter['trade_no'];<br>         $data['trade_status'] = $parameter['trade_status'];<br>         $data['notify_id'] = $parameter['notify_id'];<br>         $data['notify_time'] = $parameter['notify_time'];<br>         $data['buyer_email'] = $parameter['buyer_email'];<br>         $data['paystatus'] = 1;<br>         $Ord = M('order');<br>         $Ord->where('orderid=' . $ordid)->save($data);<br>     }<br>     //在线交易订单支付处理函数<br>     //函数功能:根据支付接口传回的数据判断该订单是否已经支付成功;<br>     //返回值:如果订单已经成功支付,返回true,否则返回false;<br>     function checkorderstatus($ordid) {<br>         $Ord = M('order');<br>         $ordstatus = $Ord->where('orderid=' . $ordid)->getField('paystatus');<br>         if ($ordstatus == 1) {<br>             return true;<br>         } else {<br>             return false;<br>         }<br>     }其他请参看http://www.thinkphp.cn/code/240.html中的案例,支付接口的方法大致都是一样的,下面提供财付通集成开发包

附件 php(utf8).zip ( 60.83 KB 下载:330 次 )

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template