PHP implementation of the Bank of communications online payment Interface Ecshop plug-in and use examples _php tutorial

Source: Internet
Author: User
Recently, a project required to do Bank of communications online payment, Ecshop itself does not have this interface, so through some time the research, made a plug-in out. There are good things, of course, to share, in this specially shared out, hope to help the same people as I need, for everyone to alleviate the burden of development, but also please point out some good methods and suggestions, mutual learning, progress!


Before using the plug-in, please configure the Traffic Bank online payment environment (specific installation method, the delivery of the demo will have, it is not difficult, pay attention to the details on the line). After installation, please put the following plug-in source code and language package source code by path to the corresponding file, and finally into the background of the payment module installation can be.


Plugin source code (includes/modules/payment/bankcomm.php):
Copy the Code code as follows:

/**
* Bank of Communications online payment plugin 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);
}

/* Basic information for module */
if (Isset ($set _modules) && $set _modules = = TRUE)
{
$i = Isset ($modules)? Count ($modules): 0;

/* Code */
$modules [$i] [' code '] = basename (__file__, '. php ');

/* Describe the corresponding language item */
$modules [$i] [' desc '] = ' bankcomm_desc ';

/* Whether to support cash on delivery */
$modules [$i] [' is_cod '] = ' 0 ';

/* Whether online payment is supported */
$modules [$i] [' is_online '] = ' 1 ';

/* payment fee, determined by delivery */
$modules [$i] [' pay_fee '] = ' 0 ';

/* Author */
$modules [$i] [' author '] = ' reson ';

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

/* Version number */
$modules [$i] [' version '] = ' 1.0.0.0 ';

/* Configuration information */
$modules [$i] [' config '] = array ();

Return
}

/**
* Class
*/
Class Bankcomm
{
/**
* Constructor function
*
* @return void
*/
function Bankcomm ()
{
}

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

/**
* Submit function
*/
function Get_code ($order)
{
Get the data from the table only son.
$param [' interfaceversion '] = ' 1.0.0.0 '; Message Version number *
$param [' merid '] = ' 301310063009501 '; Merchant Number (test number, later can be changed by itself)
$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 '; Currency of transaction * Default CNY
$param [' ordercontent '] = ';
$param [' ordermono '] = $order [' Ordermono ']; Merchant notes
$param [' phdflag '] = '; Logistics and distribution Mark
$param [' notifytype '] = 1; Notification method * 1 Notice
$param [' merurl '] = ';
$param [' goodsurl '] = $order [' Goodsurl ']; Pickup URL
$param [' jumpseconds '] = ';
$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 ports here are configured according to their own situation
$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);
}
Parsing returns 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 "Trading error message:" $ERRMSG _value. "
";
Return "Trading 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 function
*/
function respond ()
{
$tranCode = "Cb2200_verify";
$NOTIFYMSG = $_request["Notifymsg"];
$lastIndex = Strripos ($NOTIFYMSG, "|");
$SIGNMSG = substr ($NOTIFYMSG, $lastIndex + 1); Signature information
$SRCMSG = substr ($notifyMsg, 0, $lastIndex + 1);//Original

Connection Address
$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. = " ". $tranCode. "";
$in. = " ". $notifyMsg. "";
$in. = " ";
Fwrite ($fp, $in);
while (!feof ($fp)) {
$RETMSG = $retMsg. Fgets ($FP, 1024);
}
Fclose ($FP);
}

Parsing returns 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 "Trading 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);
Tell the user that the transaction is complete
return true;
}

Respond END///////////////
}

}

?>

Language Pack source code (languages/zh_cn/payment/bankcomm.php):
Copy the Code code as follows:

/**
* Bank of communications language documents
* By:reson
* 2014/03/31
*/

Global $_lang;

$_lang[' bankcomm ' = ' Bank of communications online payment ';
$_lang[' bankcomm_desc ' = ' Bank of communications online payment ';
$_lang[' Pay_button ' = ' Bank of communications payment ';

?>

The following is an example of a call:

Copy the Code code 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 notes
$order _[' goodsurl '] = "http://". $_server[' Http_host ']. ' /respond.php?code=bankcomm '; Pickup URL
$pay _code = new Bankcomm;
$pay _button = $pay _code->get_code ($order _);
$order [' pay_button '] = $pay _button; That is, a payment button has been generated at this time

http://www.bkjia.com/PHPjc/768140.html www.bkjia.com true http://www.bkjia.com/PHPjc/768140.html techarticle recently, a project required to do Bank of communications online payment, Ecshop itself does not have this interface, so through some time the research, made a plug-in out. There are good things, of course ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.