程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> PHP實現的交通銀行網銀在線支付接口ECSHOP插件和使用例子

PHP實現的交通銀行網銀在線支付接口ECSHOP插件和使用例子

編輯:PHP綜合

最近,一個項目要求做交通銀行在線支付,ecshop本身沒有這方面的接口,於是通過一些時間的專研,做了一個插件出來。有好的東西,當然要分享,在此特地分享出來,希望能夠幫助到跟我一樣有需要的人,為大家減輕一下開發的負擔,也多請大家指出一些好的方法和建議,相互的學習、進步!


在使用插件之前,請配置好交通銀行在線支付的環境(具體安裝方法,交行提供的demo會有,也不是很難,注意好細節就行)。安裝好之後,請將把下面的插件源碼和語言包源碼按路徑保存到相應文件,最後進入後台的支付模塊安裝即可。


插件源碼(includes/modules/payment/bankcomm.php):
復制代碼 代碼如下:
<?php

/**
 * 交通銀行在線支付插件 For Ecshop
 * Author: Reson
 * Date: 2014/03/31
 */

if (!defined('IN_ECS'))
{
 die('Hacking attempt');
}

$payment_lang = ROOT_PATH . 'languages/' .$GLOBALS['_CFG']['lang']. '/payment/bankcomm.php';

if (file_exists($payment_lang))
{
 global $_LANG;
 include_once($payment_lang);
}

/* 模塊的基本信息 */
if (isset($set_modules) && $set_modules == TRUE)
{
 $i = isset($modules) ? count($modules) : 0;

 /* 代碼 */
 $modules[$i]['code']    = basename(__FILE__, '.php');

 /* 描述對應的語言項 */
 $modules[$i]['desc']    = 'bankcomm_desc';

 /* 是否支持貨到付款 */
 $modules[$i]['is_cod']  = '0';

 /* 是否支持在線支付 */
 $modules[$i]['is_online']  = '1';

 /* 支付費用,由配送決定 */
 $modules[$i]['pay_fee'] = '0';

 /* 作者 */
 $modules[$i]['author']  = 'Reson';

 /* 網址 */
 $modules[$i]['website'] = 'http://www.jb51.net';

 /* 版本號 */
 $modules[$i]['version'] = '1.0.0.0';

 /* 配置信息 */
 $modules[$i]['config']  = array();

 return;
}

/**
 * 類
 */
class bankcomm
{
 /**
  * 構造函數
  *
  * @return void
  */
 function bankcomm()
 {
 }

 function __construct()
 {
  $this->bankcomm();
 }

 /**
  * 提交函數
  */
 function get_code($order)
 {
  //獲得表單傳過來的數據
  $param['interfaceVersion'] = '1.0.0.0'; //消息版本號*
  $param['merID'] = '301310063009501'; //商戶號 (測試號,後期可自行更改)
  $param['orderid'] = $order['orderid']; //訂單號*
  $param['orderDate'] = local_date("Ymd",gmtime()); //商戶訂單日期* yyyyMMdd
  $param['orderTime'] = local_date("His",gmtime()); //商戶訂單時間* HHmmss
  $param['tranType'] = 0; //交易類別* 0:B2C
  $param['amount'] = $order['amount']; //訂單金額*
  $param['curType'] = 'CNY'; //交易幣種* 默認CNY
  $param['orderContent'] = '';
  $param['orderMono'] = $order['orderMono']; //商家備注
  $param['phdFlag'] = ''; //物流配送標志
  $param['notifyType'] = 1; //通知方式* 1 通知
  $param['merURL'] = ''; 
  $param['goodsURL'] = $order['goodsURL']; //取貨URL
  $param['jumpSeconds'] = '';
  $param['payBatchNo'] = '';
  $param['proxyMerName'] = '';
  $param['proxyMerType'] = '';
  $param['proxyMerCredentials'] = '';
  $param['netType'] = 0; //渠道編號* 0:html渠道
  $param['issBankNo'] = '';

  $tranCode = "cb2200_sign";

  htmlentities($param['orderMono'],"ENT_QUOTES","utf-8");
  //連接字符串
  $source = '';
  foreach($param as $key=>$val){
   if($key != 'issBankNo')
   $source .= $val.'|';
  }
  $source = substr($source,0,strlen($source)-1);

  //連接地址
  $socketUrl = "tcp://127.0.0.1:8080"; //這裡的端口根據自己配置的情況
  $fp = stream_socket_client($socketUrl, $errno, $errstr, 30);
  $retMsg="";
  //
  if (!$fp) {
   echo "$errstr ($errno)<br />\n";
  } else
  {
   $in  = "<?xml version='1.0' encoding='UTF-8'?>";
   $in .= "<Message>";
   $in .= "<TranCode>".$tranCode."</TranCode>";
   $in .= "<MsgContent>".$source."</MsgContent>";
   $in .= "</Message>";
   fwrite($fp, $in);
   while (!feof($fp)) {
    $retMsg =$retMsg.fgets($fp, 1024); 
   }
   fclose($fp);
  } 
  //解析返回xml
  $dom = new DOMDocument;
  $dom->loadXML($retMsg);

  $retCode = $dom->getElementsByTagName('retCode');
  $retCode_value = $retCode->item(0)->nodeValue;

  $errMsg = $dom->getElementsByTagName('errMsg');
  $errMsg_value = $errMsg->item(0)->nodeValue;

  $signMsg = $dom->getElementsByTagName('signMsg');
  $signMsg_value = $signMsg->item(0)->nodeValue;

  $orderUrl = $dom->getElementsByTagName('orderUrl');
  $orderUrl_value = $orderUrl->item(0)->nodeValue;

  $MerchID = $dom->getElementsByTagName('MerchID');
  $merID = $MerchID->item(0)->nodeValue;
  //echo "retMsg=".$retMsg;
  //echo $retCode_value." ".$errMsg_value." ".$signMsg_value." ".$orderUrl_value;
  if($retCode_value != "0"){
   //echo "交易返回碼:".$retCode_value."<br>";
   //echo "交易錯誤信息:" .$errMsg_value."<br>";
   return "交易錯誤信息:" .$errMsg_value."<br>";
  }else{
   $param['signMsg_value'] = $signMsg_value;
   $param['orderUrl_value'] = $orderUrl_value;
   $form_code = $this->create_html($param); //創建提交表單
   return $form_code;
  }

 }

 /**
  * 創建提交表單
  */
 function create_html($param){
  $pay_html ='<form name = "form1" method = "post" action = "'.$param['orderUrl_value'].'">
  <input type = "hidden" name = "interfaceVersion" value = "'.$param['interfaceVersion'].'">
  <input type = "hidden" name = "merID" value = "'.$param['merID'].'">
  <input type = "hidden" name = "orderid" value = "'.$param['orderid'].'">
  <input type = "hidden" name = "orderDate" value = "'.$param['orderDate'].'">
  <input type = "hidden" name = "orderTime" value = "'.$param['orderTime'].'">
  <input type = "hidden" name = "tranType" value = "'.$param['tranType'].'">
  <input type = "hidden" name = "amount" value = "'.$param['amount'].'">
  <input type = "hidden" name = "curType" value = "'.$param['curType'].'">
  <input type = "hidden" name = "orderContent" value = "'.$param['orderContent'].'">
  <input type = "hidden" name = "orderMono" value = "'.$param['orderMono'].'">
  <input type = "hidden" name = "phdFlag" value = "'.$param['phdFlag'].'">
  <input type = "hidden" name = "notifyType" value = "'.$param['notifyType'].'">
  <input type = "hidden" name = "merURL" value = "'.$param['merURL'].'">
  <input type = "hidden" name = "goodsURL" value = "'.$param['goodsURL'].'">
  <input type = "hidden" name = "jumpSeconds" value = "'.$param['jumpSeconds'].'">
  <input type = "hidden" name = "payBatchNo" value = "'.$param['payBatchNo'].'">
  <input type = "hidden" name = "proxyMerName" value = "'.$param['proxyMerName'].'">
  <input type = "hidden" name = "proxyMerType" value = "'.$param['proxyMerType'].'">
  <input type = "hidden" name = "proxyMerCredentials" value = "'.$param['proxyMerCredentials'].'">
  <input type = "hidden" name = "netType" value = "'.$param['netType'].'">
  <input type = "hidden" name = "merSignMsg" value = "'.$param['signMsg_value'].'">
  <input type = "hidden" name = "issBankNo" value = "'.$param['issBankNo'].'">

  <input type="submit" value=" " class="pay_button" />
  </form>';
  return $pay_html;
 }

 /**
  * 處理函數
  */
 function respond()
 {  
  $tranCode = "cb2200_verify";
  $notifyMsg = $_REQUEST["notifyMsg"];  
  $lastIndex = strripos($notifyMsg,"|");
  $signMsg = substr($notifyMsg,$lastIndex+1); //簽名信息
  $srcMsg = substr($notifyMsg,0,$lastIndex+1);//原文

  //連接地址
  $socketUrl = "tcp://127.0.0.1:8080";
  $fp = stream_socket_client($socketUrl, $errno, $errstr, 30);
  $retMsg="";
  if (!$fp) {
   //echo "$errstr ($errno)<br />\n";
   return false;
  }else{
   $in  = "<?xml version='1.0' encoding='UTF-8'?>";
   $in .= "<Message>";
   $in .= "<TranCode>".$tranCode."</TranCode>";
   $in .= "<MsgContent>".$notifyMsg."</MsgContent>";
   $in .= "</Message>";
   fwrite($fp, $in);
   while (!feof($fp)) {
    $retMsg =$retMsg.fgets($fp, 1024); 
   }
   fclose($fp);
  } 

  //解析返回xml
  $dom = new DOMDocument;
  $dom->loadXML($retMsg);

  $retCode = $dom->getElementsByTagName('retCode');
  $retCode_value = $retCode->item(0)->nodeValue;

  $errMsg = $dom->getElementsByTagName('errMsg');
  $errMsg_value = $errMsg->item(0)->nodeValue;

  $signMsg = $dom->getElementsByTagName('signMsg');
  $signMsg_value = $signMsg->item(0)->nodeValue;

  if($retCode_value != ''){
   //echo "交易返回碼:".$retCode_value."<br>";
   //echo "交易錯誤信息:" .$errMsg_value."<br>";
   return false;
  }else{
   $arr = preg_split("/\|{1,}/",$srcMsg); 
   $pay_id = $arr[1];
   $action_note = base64_decode($arr[16]);
   // 完成訂單。
   order_paid($pay_id, PS_PAYED, $action_note);
   //告訴用戶交易完成
   return true; 
  }

  ///////////////// respond END ///////////////
 }

}

?>

語言包源碼(languages/zh_cn/payment/bankcomm.php):
復制代碼 代碼如下:
<?php

/**
 * 交通銀行語言文件
 * by: Reson
 * 2014/03/31
 */

global $_LANG;

$_LANG['bankcomm'] = '交通銀行在線支付';
$_LANG['bankcomm_desc'] = '交通銀行在線支付';
$_LANG['pay_button'] = '交通銀行支付';

?>

以下是調用的范例:

復制代碼 代碼如下:
include_once(ROOT_PATH . 'includes/modules/payment/bankcomm.php');
$order_['orderid'] = $order['order_sn'];
$order_['amount'] = $order['yd_price'];
$order_['orderMono'] = '測試'; //商家備注
$order_['goodsURL'] = "http://".$_SERVER['HTTP_HOST'].'/respond.php?code=bankcomm'; //取貨URL
$pay_code = new bankcomm;
$pay_button = $pay_code->get_code($order_);
$order['pay_button'] = $pay_button; //即此時已經生成了一個支付按鈕

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved