前陣子作了幾個第三方支付接口,寫下來跟你們分享下,我用的是yii1框架作的。javascript
1:from表單,提交商戶給銀行的數據,經過$this->createUrl('Payment/tenpay')?>" 跳轉php
<!-- 財付通支付頁面 --> <form name=tenpayment action="<?php echo $this->createUrl('Payment/tenpay')?>" method=post target="_blank"> <div style="display: none;"><input size="30" name="WIDseller_email" value="xxxx.com" /> <input size="30" name="order_no" value="<?php echo @$OrderInfo['PayNumber']?>" /> <input size="30" name="product_name" value="<?php echo @XUtils::cutstr($OrderInfo['OrderName'],30)?>" /> <input size="30" name="order_price" value="<?php echo @$OrderInfo['TotalMoney']?>" /> <input size="30" name="trade_mode" value="1"/> <input size="30" name="remarkexplain" /></div> <li> <div class="pay_boxin pay_curr"> <span class="left"><img src="<?php echo Yii::app()->request->baseUrl ?>/assets/default/images/tenpay.png" height="30px"></span> <span class="pay_money right"> <p>支付<strong><?php echo @sprintf('%0.2f',$OrderInfo['TotalMoney'])?></strong>元</p> </span> </div> <input type="submit" style="width:220px; height:50px; border:0px; margin:30px 0 0 500px; background: url(<?php echo Yii::app()->request->baseUrl ?>/assets/default/images/pay_quite.png) center no-repeat; cursor:pointer;" value="" > <div class="pay_disno" style="margin:30px 0 0 500px;"><img src="<?php echo Yii::app()->request->baseUrl ?>/assets/default/images/pay_quite.png"></div> </li> </form>
2:跳轉頁面 經過控制器跳轉html
3:tenpay.php頁面 驗證商戶提交的數據 並進行加密處理java
<?php //--------------------------------------------------------- //財付通即時到賬支付請求示例,商戶按照此文檔進行開發便可 //--------------------------------------------------------- require_once ("classes/RequestHandler.class.php"); require_once ("tenpay_config.php"); /* 獲取提交的訂單號 */ $out_trade_no = $_REQUEST["order_no"]; /* 獲取提交的商品名稱 */ $product_name = $_REQUEST["product_name"]; /* 獲取提交的商品價格 */ $order_price = $_REQUEST["order_price"]; /* 獲取提交的備註信息 */ $remarkexplain = $_REQUEST["remarkexplain"]; /* 支付方式 */ $trade_mode=$_REQUEST["trade_mode"]; $strDate = date("Ymd"); $strTime = date("His"); /* 商品價格(包含運費),以分爲單位 */ $total_fee = $order_price*100; /* 商品名稱 */ $desc = "商品:".$product_name.",備註:".$remarkexplain; /* 建立支付請求對象 */ $reqHandler = new RequestHandler(); $reqHandler->init(); $reqHandler->setKey($key); $reqHandler->setGateUrl("https://gw.tenpay.com/gateway/pay.htm"); //---------------------------------------- //設置支付參數 //---------------------------------------- $reqHandler->setParameter("partner", $partner); $reqHandler->setParameter("out_trade_no", $out_trade_no); $reqHandler->setParameter("total_fee", $total_fee); //總金額 $reqHandler->setParameter("return_url", $return_url); $reqHandler->setParameter("notify_url", $notify_url); $reqHandler->setParameter("body", $desc); $reqHandler->setParameter("bank_type", "DEFAULT"); //銀行類型,默認爲財付通 //用戶ip $reqHandler->setParameter("spbill_create_ip", $_SERVER['REMOTE_ADDR']);//客戶端IP $reqHandler->setParameter("fee_type", "1"); //幣種 $reqHandler->setParameter("subject",$desc); //商品名稱,(中介交易時必填) //系統可選參數 $reqHandler->setParameter("sign_type", "MD5"); //簽名方式,默認爲MD5,可選RSA $reqHandler->setParameter("service_version", "1.0"); //接口版本號 $reqHandler->setParameter("input_charset", "utf-8"); //字符集 $reqHandler->setParameter("sign_key_index", "1"); //密鑰序號 //業務可選參數 $reqHandler->setParameter("attach", ""); //附件數據,原樣返回就能夠了 $reqHandler->setParameter("product_fee", ""); //商品費用 $reqHandler->setParameter("transport_fee", "0"); //物流費用 $reqHandler->setParameter("time_start", date("YmdHis")); //訂單生成時間 $reqHandler->setParameter("time_expire", ""); //訂單失效時間 $reqHandler->setParameter("buyer_id", ""); //買方財付通賬號 $reqHandler->setParameter("goods_tag", ""); //商品標記 $reqHandler->setParameter("trade_mode",$trade_mode); //交易模式(1.即時到賬模式,2.中介擔保模式,3.後臺選擇(賣家進入支付中心列表選擇)) $reqHandler->setParameter("transport_desc",""); //物流說明 $reqHandler->setParameter("trans_type","1"); //交易類型 $reqHandler->setParameter("agentid",""); //平臺ID $reqHandler->setParameter("agent_type",""); //代理模式(0.無代理,1.表示卡易售模式,2.表示網店模式) $reqHandler->setParameter("seller_id",""); //賣家的商戶號 //請求的URL $reqUrl = $reqHandler->getRequestURL(); //獲取debug信息,建議把請求和debug信息寫入日誌,方便定位問題 /**/ //$debugInfo = $reqHandler->getDebugInfo(); //echo "<br/>" . $reqUrl . "<br/>"; //var_dump($reqUrl);die; ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>財付通即時到賬交易接口</title> </head> <body> <br/><!--a href="<?php echo $reqUrl ?>" target="_blank">財付通支付</a--> <form action="<?php echo $reqHandler->getGateUrl() ?>" method="post"> <?php $params = $reqHandler->getAllParameters(); foreach($params as $k => $v) { echo "<input type=\"hidden\" name=\"{$k}\" value=\"{$v}\" />\n"; } ?> <input type="submit" id="submitPay" value="財付通支付" style="display:none"> <script type="text/javascript"> document.getElementById("submitPay").click(); </script> </form> </body> </html>
4:payNotifyUrl.php 後臺回調頁面 顯示用戶支付的結果信息 銀行經過祕鑰與商戶進行簽名 確認商戶信息數據庫
<META http-equiv=Content-Type content="text/html; charset=utf-8"> <?php //--------------------------------------------------------- //財付通即時到賬支付後臺回調示例,商戶按照此文檔進行開發便可 //--------------------------------------------------------- require ("classes/ResponseHandler.class.php"); require ("classes/RequestHandler.class.php"); require ("classes/client/ClientResponseHandler.class.php"); require ("classes/client/TenpayHttpClient.class.php"); require ("./classes/function.php"); require_once ("./tenpay_config.php"); log_result("進入後臺回調頁面"); /* 建立支付應答對象 */ $resHandler = new ResponseHandler(); $resHandler->setKey($key); //判斷簽名 if($resHandler->isTenpaySign()) { //通知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); 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"); //------------------------------ //處理業務開始 //------------------------------ //處理數據庫邏輯 //注意交易單不要重複處理 //注意判斷返回金額 //------------------------------ //處理業務完畢 //------------------------------ log_result("即時到賬後臺回調成功"); echo "success"; } else { //錯誤時,返回結果可能沒有簽名,寫日誌trade_state、retcode、retmsg看失敗詳情。 //echo "驗證簽名失敗 或 業務錯誤信息:trade_state=" . $resHandler->getParameter("trade_state") . ",retcode=" . $queryRes-> getParameter("retcode"). ",retmsg=" . $queryRes->getParameter("retmsg") . "<br/>" ; log_result("即時到賬後臺回調失敗"); echo "fail"; } }elseif ($resHandler->getParameter("trade_mode") == "2") { //判斷簽名及結果(中介擔保) //只有簽名正確,retcode爲0,trade_state爲0纔是支付成功 if($queryRes->isTenpaySign() && $queryRes->getParameter("retcode") == "0" ) { log_result("中介擔保驗籤ID成功"); //取結果參數作業務處理 $out_trade_no = $resHandler->getParameter("out_trade_no"); //財付通訂單號 $transaction_id = $resHandler->getParameter("transaction_id"); //------------------------------ //處理業務開始 //------------------------------ //處理數據庫邏輯 //注意交易單不要重複處理 //注意判斷返回金額 log_result("中介擔保後臺回調,trade_state=".$resHandler->getParameter("trade_state")); switch ($resHandler->getParameter("trade_state")) { case "0": //付款成功 break; case "1": //交易建立 break; case "2": //收穫地址填寫完畢 break; case "4": //賣家發貨成功 break; case "5": //買家收貨確認,交易成功 break; case "6": //交易關閉,未完成超時關閉 break; case "7": //修改交易價格成功 break; case "8": //買家發起退款 break; case "9": //退款成功 break; case "10": //退款關閉 break; default: //nothing to do break; } //------------------------------ //處理業務完畢 //------------------------------ echo "success"; } else { //錯誤時,返回結果可能沒有簽名,寫日誌trade_state、retcode、retmsg看失敗詳情。 //echo "驗證簽名失敗 或 業務錯誤信息:trade_state=" . $resHandler->getParameter("trade_state") . ",retcode=" . $queryRes-> getParameter("retcode"). ",retmsg=" . $queryRes->getParameter("retmsg") . "<br/>" ; log_result("中介擔保後臺回調失敗"); 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 { echo "<br/>" . "認證簽名失敗" . "<br/>"; echo $resHandler->getDebugInfo() . "<br>"; } ?>
5:payReturnUrl.php 前臺回調頁面 銀行回調信息給商戶app
<META http-equiv=Content-Type content="text/html; charset=utf-8"> <?php //--------------------------------------------------------- //財付通即時到賬支付頁面回調示例,商戶按照此文檔進行開發便可 //--------------------------------------------------------- require_once ("classes/ResponseHandler.class.php"); require_once ("classes/function.php"); require_once ("tenpay_config.php"); log_result("進入前臺回調頁面"); /* 建立支付應答對象 */ $resHandler = new ResponseHandler(); $resHandler->setKey($key); //判斷簽名 if($resHandler->isTenpaySign()) { //通知id $notify_id = $resHandler->getParameter("notify_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"); //支付結果 $trade_state = $resHandler->getParameter("trade_state"); //交易模式,1即時到帳 $trade_mode = $resHandler->getParameter("trade_mode"); if($out_trade_no){ $this->actionSetPayN($out_trade_no,2); $this->redirect(array('/UserCenter')); } if("1" == $trade_mode ) { if( "0" == $trade_state){ echo "<br/>" . "即時到賬支付成功" . "<br/>"; } else { //當作不成功處理 echo "<br/>" . "即時到賬支付失敗" . "<br/>"; } }elseif( "2" == $trade_mode ) { if( "0" == $trade_state) { echo "<br/>" . "中介擔保支付成功" . "<br/>"; } else { //當作不成功處理 echo "<br/>" . "中介擔保支付失敗" . "<br/>"; } } } else { echo "<br/>" . "認證簽名失敗" . "<br/>"; echo $resHandler->getDebugInfo() . "<br>"; } ?>
6:設置頁面 設置商戶信息,以及商戶祕鑰和商戶帳號框架
<?php $spname="財付通雙接口測試"; $partner = ""; //財付通商戶號 $key = ""; //財付通密鑰 $return_url = "http://www.xxx.com".$this->createUrl('Payment/Returns'); //顯示支付結果頁面,*替換成payReturnUrl.php所在路徑 $notify_url = "http://www.xxx.com".$this->createUrl('Payment/payNotifyUrl'); //支付完成後的回調處理頁面,*替換成payNotifyUrl.php所在路徑 ?>