PHP实现的交通银行网银在线支付接口ECSHOP插件和使用例子_php实例
最近,一个项目要求做交通银行在线支付,ecshop本身没有这方面的接口,于是通过一些时间的专研,做了一个插件出来。有好的东西,当然要分享,在此特地分享出来,希望能够帮助到跟我一样有需要的人,为大家减轻一下开发的负担,也多请大家指出一些好的方法和建议,相互的学习、进步!
在使用插件之前,请配置好交通银行在线支付的环境(具体安装方法,交行提供的demo会有,也不是很难,注意好细节就行)。安装好之后,请将把下面的插件源码和语言包源码按路径保存到相应文件,最后进入后台的支付模块安装即可。
插件源码(includes/modules/payment/bankcomm.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.php.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)
\n";
} else
{
$in = "";
$in .= "
$in .= "
$in .= "
$in .= "
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."
";
//echo "交易错误信息:" .$errMsg_value."
";
return "交易错误信息:" .$errMsg_value."
";
}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 ='
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)
\n";
return false;
}else{
$in = "";
$in .= "
$in .= "
$in .= "
$in .= "
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."
";
//echo "交易错误信息:" .$errMsg_value."
";
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):
/**
* 交通银行语言文件
* 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; //即此时已经生成了一个支付按钮

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
