PHP Version Traffic Banking Network Silver Payment Interface Development Example

Source: Internet
Author: User
Tags foreach php and

Overview: The network Silver payment interface and Alipay interface is roughly the same, the main difference is that the bank of communications to provide the core of the interface, such as encryption, such as through the Java implementation. So, to find a way to make PHP and Java normal communication, for this. The authorities also provide two implementations, one for communication via sockets and the other for Java bridging. The socket method is shown below:

1. Configure the operating environment
1.1 Install Java, go to Oracle website to download Java, and then install, and configure the correct environment variables.
1.2 Import the test certificate into the Java Virtual machine;
Keytool "-import-keystore" The address of the Java Virtual Machine Placement Certificate-storepass changeit-alias test_bocommca-file "certificate Path" completes the import.
Example: Keytool "-import-keystore" C:\Program files\java\jre1.5\lib\security\cacerts "-storepass changeit-alias test_ Bocommca-file "C:\socket\cert\test_root.cer"

1.3 Modify configuration file (In/b2cmerchantsocket.xml):
When testing with an official test firm, no configuration is required or configured. See the XML file description specifically.

1.4 Starting the Socket service
window: Start Start.bat and can;
Linux: Start ohup sh.start,sh&//Leave the current script out of the terminal and run in the background.

2. Integration of net silver into existing systems: (described in the structure of MVC):

2.1 Write unchanged parameter configuration to configuration file:

The code is as follows Copy Code

$config [' interfaceversion '] = "1.0.0.0"; #接口版本
$config [' trantype '] = 0; #交易类别 0:B:C
$config [' curtype '] = ' CNY '; # Currency of Exchange
$config [' notifytype '] = 1; #0 = Do not notify 1= notification 2= crawl
$config [' merurl '] = "http://www.111cn.net/pay/notify"; # Unsolicited Notification URL
$config [' goodsurl ' = ' http://www.111cn.net/goods/1.html '; #取货url
$config [' jumpseconds '] = 3; #跳转时间
$config [' paybatchno '] = '; #商户批次号
$config [' proxymername '] = '; #代理商家名字
$config [' proxymertype '] = '; #代理商类型
$config [' proxymercredentials ']= '; #代理商家批次号
$config [' nettype '] = 0; #渠道编号

The following are the parameters required for the new interface
$config [' socketurl '] = "tcp://127.0.0.1:8891"; #socket URL
$config [' merid '] = ' 301310063009501 '; #商户id 3013100630095012
2.2 Model


/**
* Bank of communications Payment category
*/
Class Bocom extends Ci_model {
Private $arrReturn =array ();
Private $socket;
Public Function __construct () {
Parent::__construct ();
Load Traffic Bank profile
$this->config->load (' bocom ');
$this->socket= $this->config->item (' Socketurl ');
}
/**
* Payment Method
*
* @param unknown $arr _data=array (
* ' Bill_no ' =>
* )
*/
Public function Pay ($arr _data) {
Get the data from the table conveys.
$this->arrreturn[' interfaceversion '] = $this->config->item (' interfaceversion ');
$this->arrreturn[' merid '] = $this->config->item (' Merid '); Merchant number is fixed
$this->arrreturn[' orderid '] = $arr _data[' bill_no '];
$this->arrreturn[' orderDate '] = $arr _data[' bill_date '];
$this->arrreturn[' ordertime '] = $arr _data[' bill_time '];
$this->arrreturn[' trantype '] = $this->config->item (' Trantype ');
$this->arrreturn[' amount '] = $arr _data[' Bill_fee '];
$this->arrreturn[' curtype '] = $this->config->item (' Curtype ');
$this->arrreturn[' ordercontent '] = isset ($arr _data[' Bill_title ')) iconv (' Utf-8 ', ' gb2312 ', $arr _data["Bill_   Title "]): '; #订单内容
$this->arrreturn[' ordermono '] = isset ($arr _data[' Bill_mono '))?     Iconv (' Utf-8 ', ' gb2312 ', $arr _data[' Bill_mono ']): '; #商家备注
$this->arrreturn[' phdflag '] = isset ($arr _data[' Phpflag '))? $arr _data[' Phpflag ']: ';
$this->arrreturn[' notifytype '] = $this->config->item (' Notifytype ');
$this->arrreturn[' merurl '] = $this->config->item (' Merurl ');
$this->arrreturn[' goodsurl '] = $this->config->item (' Goodsurl ');
$this->arrreturn[' jumpseconds '] = $this->config->item (' jumpseconds ');
$this->arrreturn[' paybatchno '] = $this->config->item (' Paybatchno ');
$this->arrreturn[' proxymername '] = $this->config->item (' proxymername ');
$this->arrreturn[' proxymertype '] = $this->config->item (' Proxymertype ');
$this->arrreturn[' proxymercredentials ']= $this->config->item (' proxymercredentials ');
$this->arrreturn[' nettype '] = $this->config->item (' NetType ');
The following parameters do not participate in the signature
$this->arrreturn[' Issbankno '] =isset ($arr _data[' code_id '))? Trim ($arr _data[' code_id ']): ';
$tranCode = "Cb2200_sign";
$source = ';
$len = count ($this->arrreturn)-1; $j = 1;
foreach ($this->arrreturn as $v) {
if ($j <= $len) {
$source. = $v. "|";
}
$j + +;
}
$source = substr ($source, 0,strlen ($source)-1);
$fp = Stream_socket_client ($this->socket, $errno, $errstr, 30);
$RETMSG = "";
if (! $fp) {
Log_message ("info", "Socket Connection failed");
return false;
} else
{
$in = "<?xml version= ' 1.0 ' encoding= ' gbk2312 '?> '";
$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);
}
if (false!== $xml _arr= $this->xmlparse ($RETMSG)) {
if (Is_array ($xml _arr)) {
foreach ($xml _arr as $k => $v) {
$this->arrreturn[$k]= $v;
}
}else{
return false;
}
}else{
return false;
}
return $this->arrreturn;
}

/**
* Parsing xml
*/
Public Function Xmlparse ($RETMSG) {
$arr =array ();
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;
if ($retCode _value!= "0") {
Log_message ("Info", "Transaction return code:". $retCode _value);
Log_message ("Info", "Transaction error message:". $ERRMSG _value);
return false;
}
$arr [' mersignmsg '] = $SIGNMSG _value;
$arr [' merid '] = $merID;
$arr [' orderurl '] = $ORDERURL _value;
return $arr;
}
/**
* Bank of communications Payment notice
* @return Boolean|unknown
*/
Public Function notify () {
$tranCode = "Cb2200_verify";
if (!isset ($_request[' notifymsg ')) {
Log_message ("error", "NET silver payment notice • Illegal request");
return false;
}
$NOTIFYMSG = $_request["Notifymsg"];
Log_message ("Error", $NOTIFYMSG. " Callback ... ");

$lastIndex = Strripos ($NOTIFYMSG, "|");
$SIGNMSG = substr ($NOTIFYMSG, $lastIndex + 1); Signature information
$SRCMSG = substr ($notifyMsg, 0, $lastIndex + 1);/original
$merID = $this->config->item (' Merid ');
$fp = Stream_socket_client ($this->socket, $errno, $errstr, 30);
$RETMSG = "";
//
if (! $fp) {
echo "$errstr ($errno) <br/>\n";
Log_message ("Error", "$errstr ($errno) <br/>\n");
} else{
$in = "<?xml version= ' 1.0 ' encoding= ' gb2312 '?> '";
$in. = "<Message>";
$in. = "<TranCode>". $tranCode. " </TranCode> ";
$in. = "<merchantID>". $merID. " </merchantID> ";
$in. = "<MsgContent>". $notifyMsg. " </MsgContent> ";
$in. = "</Message>";
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;
if ($retCode _value!= ' 0 ')
{
Log_message (' Error ', ' Transaction error message: '. $ERRMSG _value. " <br> ");
return false;
}else{
$arr = Preg_split ("/\|{ 1,}/", $SRCMSG);
if ($arr [9]== "1") {
return $this->updatebill ($arr [1]);
}
Log_message ("Error", "Transaction failed:". $arr [13]. " <br/> ");
return false;
}
}
Private Function Updatebill ($billNo) {
Update order Status
}
End Class
}
2.3 Controller
$this->load->model ("bocom");
Payment Method:
$this->arrdata = $this->bocom->pay ($this->data);
Notice:
$this->arrdata = $this->bocom->notify ();

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.