This article mainly introduces the ECSHOP plug-in and use example for the online payment interface of Bank of Communications Network banking implemented by PHP. For more information, see Ecshop.
Recently, a project called for online payment by Bank of Communications. ecshop itself does not have this interface, so it developed a plug-in through some time research. If there are good things to share, I would like to share them here, hoping to help people who have the same needs as me and relieve the development burden for everyone, please also point out some good methods and suggestions for mutual learning and progress!
Before using the plug-in, configure the online payment environment of the Bank of Communications. (for the installation method, the demo provided by the Bank of Communications is not very difficult. just pay attention to the details ). After the installation is complete, save the following plug-in source code and language pack source code to the corresponding file in the path, and finally install the payment module in the background.
Plug-in source code (includes/modules/payment/bankcomm. php ):
The code is as follows:
/**
* Bank of Communications online payment plug-in For Ecshop
* Author: Reson
* Date: 2014/03/31
*/
If (! Defined ('in _ ECS '))
{
Die ('hacking attempt ');
}
$ Payment_lang = ROOT_PATH. 'ages/'. $ GLOBALS [' _ CFG '] ['Lang']. '/payment/bankcomm. php ';
If (file_exists ($ payment_lang ))
{
Global $ _ LANG;
Include_once ($ payment_lang );
}
/* Basic information of the module */
If (isset ($ set_modules) & $ set_modules = TRUE)
{
$ I = isset ($ modules )? Count ($ modules): 0;
/* Code */
$ Modules [$ I] ['code'] = basename (_ FILE __, '. php ');
/* Description of the corresponding language item */
$ Modules [$ I] ['desc'] = 'bankcomm _ desc ';
/* Supported Cash on Delivery */
$ Modules [$ I] ['is _ cod '] = '0 ';
/* Whether online payment is supported */
$ Modules [$ I] ['is _ online'] = '1 ';
/* Payment, determined by delivery */
$ Modules [$ I] ['pay _ period'] = '0 ';
/* Author */
$ Modules [$ I] ['autor'] = 'reson ';
/* Url */
$ Modules [$ I] ['Website'] = 'http: // www.bitsCN.com ';
/* Version */
$ Modules [$ I] ['version'] = '1. 0.0.0 ';
/* Configuration information */
$ Modules [$ I] ['config'] = array ();
Return;
}
/**
* Class
*/
Class bankcomm
{
/**
* Constructor
*
* @ Return void
*/
Function bankcomm ()
{
}
Function _ construct ()
{
$ This-> bankcomm ();
}
/**
* Submit a function
*/
Function get_code ($ order)
{
// Obtain the data transmitted from the form
$ Param ['interfaceversion'] = '1. 0.0.0 '; // message version *
$ Param ['merid'] = '000000'; // merchant ID (Test ID, which can be changed later)
$ Param ['orderid'] = $ order ['orderid']; // order number *
$ Param ['orderdate'] = local_date ("Ymd", gmtime (); // merchant Order Date * yyyyMMdd
$ Param ['ordertime'] = local_date ("His", gmtime (); // merchant order time * HHmmss
$ Param ['trantype'] = 0; // transaction type * 0: B2C
$ Param ['amount'] = $ order ['amount']; // order amount *
$ Param ['curtype'] = 'cny '; // transaction currency * default CNY
$ Param ['ordercontent'] = '';
$ Param ['ordermono'] = $ order ['ordermono']; // merchant remarks
$ Param ['phdflag'] = ''; // logistics delivery mark
$ Param ['policytype'] = 1; // notification method * 1 notification
$ Param ['merurl'] = '';
$ Param ['goodsurl'] = $ order ['goodsurl']; // pick up URL
$ Param ['jumyun conds '] = '';
$ Param ['paybatchno'] = '';
$ Param ['proxymername'] = '';
$ Param ['proxymertype'] = '';
$ Param ['proxymercredentials '] = '';
$ Param ['nettype'] = 0; // channel number * 0: html Channel
$ Param ['issbankno'] = '';
$ TranCode = "cb2200_sign ";
Htmlentities ($ param ['ordermono'], "ENT_QUOTES", "UTF-8 ");
// Connection string
$ Source = '';
Foreach ($ param as $ key => $ val ){
If ($ key! = 'Issbankno ')
$ Source. = $ val. '| ';
}
$ Source = substr ($ source, 0, strlen ($ source)-1 );
// Connection address
$ SocketUrl = "tcp: // 127.0.0.1: 8080"; // the port here depends on your configuration.
$ Fp = stream_socket_client ($ socketUrl, $ errno, $ errstr, 30 );
$ RetMsg = "";
//
If (! $ Fp ){
Echo "$ errstr ($ errno)
\ N ";
} Else
{
$ In =" ";
$ In. =" ";
$ In. =" ". $ TranCode ." ";
$ In. =" ". $ Source ." ";
$ In. =" ";
Fwrite ($ fp, $ in );
While (! Feof ($ fp )){
$ RetMsg = $ retMsg. fgets ($ fp, 1024 );
}
Fclose ($ fp );
}
// Parse and return 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 "transaction return code:". $ retCode_value ."
";
// Echo "transaction error message:". $ errMsg_value ."
";
Return "transaction error message:". $ errMsg_value ."
";
} Else {
$ Param ['signmsg _ value'] = $ signMsg_value;
$ Param ['orderurl _ value'] = $ orderUrl_value;
$ Form_code = $ this-> create_html ($ param); // Create a submission form
Return $ form_code;
}
}
/**
* Create a submission form
*/
Function create_html ($ param ){
$ Pay_html = '';
Return $ pay_html;
}
/**
* Processing functions
*/
Function respond ()
{
$ TranCode = "cb2200_verify ";
$ Policymsg = $ _ REQUEST ["policymsg"];
$ LastIndex = strripos ($ policymsg, "| ");
$ SignMsg = substr ($ policymsg, $ lastIndex + 1); // signature information
$ SrcMsg = substr ($ policymsg, 0, $ lastIndex + 1); // original
// Connection address
$ SocketUrl = "tcp: // fig: 8080 ";
$ Fp = stream_socket_client ($ socketUrl, $ errno, $ errstr, 30 );
$ RetMsg = "";
If (! $ Fp ){
// Echo "$ errstr ($ errno)
\ N ";
Return false;
} Else {
$ In =" ";
$ In. =" ";
$ In. =" ". $ TranCode ." ";
$ In. =" ". $ Policymsg ." ";
$ In. =" ";
Fwrite ($ fp, $ in );
While (! Feof ($ fp )){
$ RetMsg = $ retMsg. fgets ($ fp, 1024 );
}
Fclose ($ fp );
}
// Parse and return 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 "transaction return code:". $ retCode_value ."
";
// Echo "transaction error message:". $ errMsg_value ."
";
Return false;
} Else {
$ Arr = preg_split ("/\ | {1,}/", $ srcMsg );
$ Pay_id = $ arr [1];
$ Action_note = base64_decode ($ arr [16]);
// Complete the order.
Order_paid ($ pay_id, PS_PAYED, $ action_note );
// Notify the user that the transaction is completed
Return true;
}
/// // Respond END ///////////////
}
}
?>
Language Pack source code (ages/zh_cn/payment/bankcomm. php ):
The code is as follows:
/**
* Bank of Communications language files
* By: Reson
*
*/
Global $ _ LANG;
$ _ LANG ['bankcomm '] = 'Bank of communications online payment ';
$ _ LANG ['bankcomm _ desc'] = 'Bank of communications online payment ';
$ _ LANG ['pay _ click'] = 'Bank of communications payby ';
?>
The following is an example of a call:
The code is as follows:
Include_once (ROOT_PATH. 'includes/modules/payment/bankcomm. php ');
$ Order _ ['orderid'] = $ order ['Order _ sn '];
$ Order _ ['amount '] = $ order ['yd _ price'];
$ Order _ ['ordermono'] = 'test'; // merchant remarks
$ Order _ ['goodsurl'] = "http: //". $ _ SERVER ['http _ host']. '/respond. php? Code = bankcomm '; // pick up URL
$ Pay_code = new bankcomm;
$ Pay_button = $ pay_code-> get_code ($ order _);
$ Order ['pay _ click'] = $ pay_button; // A payment button is generated.