添加后台代理代码

This commit is contained in:
2026-03-15 01:27:05 +08:00
parent 11f9ac4dc1
commit ea08c9366a
5254 changed files with 721042 additions and 0 deletions

View File

@@ -0,0 +1,222 @@
╭──────────────────────────────────────╮
────┤ 银联全渠道支付插件包说明 ├────
╰──────────────────────────────────────╯
  接口名称:银联全渠道支付统一接入接口
    代码版本1.1
开发语言PHP
版 权:银联全渠道
  制 作 者:银联全渠道
联系方式WZ
─────────────────────────────────
───────
代码文件结构
───────
├gbk.func┈┈┈┈┈┈┈┈┈┈工具类文件夹
│ │
│ ├encryptParams.php┈┈┈┈┈┈┈┈┈┈┈ 对卡号cvn2密码cvn2有效期处理类
│ │
│ ├PinBlock.php ┈┈┈┈┈┈┈┈┈┈密码解析类
│ │
│ ├httpClient.php┈┈┈┈┈┈┈┈┈后台交易通信处理类
│ │
│ ├SDKConfig.php ┈┈┈┈┈┈┈┈┈ 配置信息类
│ │
│ ├PublicEncrypte.php ┈┈┈┈┈┈┈┈┈┈ 密码/签名类
│ │
│ └common.php ┈┈┈┈┈┈┈┈报文方法类
│ │
│ └secureUtil.php┈┈┈┈┈┈┈┈签名/验签类
│ │
│ └log.class.php ┈┈┈┈┈┈┈┈日志打印工具类
※注意※
openssl证书需下载使用 其中的php_openssl.dll,ssleay32.dll,libeay32.dll3个文件拷到windows/system32/文件夹下在重启Apache服务
─────────
主要类文件函数说明
─────────
--------------------------------------------------------------------
SDKConfig.php
签名证书路径
const SDK_SIGN_CERT_PATH = '';
签名证书密码
const SDK_SIGN_CERT_PWD = '';
验签证书
const SDK_VERIFY_CERT_PATH = '';
密码加密证书
const SDK_ENCRYPT_CERT_PATH = '';
验签证书路径
const SDK_VERIFY_CERT_DIR = '';
前台请求地址
const SDK_FRONT_TRANS_URL = '';
后台返回结果地址
const SDK_BACK_TRANS_URL = '';
批量交易
const SDK_BATCH_TRANS_URL = '';
批量交易状态查询
const SDK_BATCH_QUERY_URL = '';
单笔查询请求地址
const SDK_SINGLE_QUERY_URL = '';
文件传输请求地址
const SDK_FILE_QUERY_URL = '';
前台通知地址
const SDK_FRONT_NOTIFY_URL = '';
后台通知地址
const SDK_BACK_NOTIFY_URL = '';
文件下载目录
const SDK_FILE_DOWN_PATH = '';
日志 目录
const SDK_LOG_FILE_PATH = '';
日志级别
const SDK_LOG_LEVEL = '';
有卡交易地址
const SDK_Card_Request_Url = '';
App交易地址
const SDK_App_Request_Url = '';
┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉
common.php
function coverParamsToString($param)
功能:数组 排序后转化为字体串
function coverStringToArray($val )
功能:字符串转换为 数组
function deal_params(&$params)
功能:处理返回报文 解码客户信息 , 如果编码为GBK 则转为utf-8
function deflate_file(&$params)
功能:处理压缩文件
function deal_file($params)
功能:处理报文文件
function create_html($params, $action)
功能:构造自动提交表单
┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉
HttpClient.php
function sendHttpRequest($params, $url)
功能建立请求以模拟远程HTTP的POST请求方式构造并获取银联的处理结果
function getRequestParamString($params)
功能:组装报文
┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉
encryptParams.php
function encrypt_params(&$params)
功能:对卡号 | cvn2 | 密码 | cvn2有效期进行处理
┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉
PinBlock.php
function Pin2PinBlock( &$sPin )
功能密码转pin 验证转换
┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉
PublicEncrypte.php
function EncryptedPin$sPin, $sCardNo ,$sPubKeyURL
功能证书Id验证密码方法
┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉
secureUtil.php
function sign(&$params)
功能:签名方法
function verify($params)
功能:验签方法
function getPulbicKeyByCertId($certId)
功能根据证书ID加载证书方法
function getCertId($cert_path)
功能取证书ID方法
function getCertIdByCerPath($cert_path)
功能:取证书类型方法
function getPublicKey($cert_path)
功能:取证书公钥 -验签
function getPrivateKey($cert_path)
功能:返回(签名)证书私钥
function encryptPan($pan)
功能:加密卡号方法
function encryptPin($pan, $pwd)
功能pin加密方法
function encryptCvn2($cvn2)
功能cvn2加密方法
function encryptDate($certDate)
功能:有效期加密方法

View File

@@ -0,0 +1,40 @@
<?php
function EncryptedPin($sPin, $sCardNo ,$sPubKeyURL)
{
global $log;
$sPubKeyURL = trim(SDK_ENCRYPT_CERT_PATH," ");
$fp = fopen($sPubKeyURL, "r");
if ($fp != NULL)
{
$sCrt = fread($fp, 8192);
fclose($fp);
}
$sPubCrt = openssl_x509_read($sCrt);
if ($sPubCrt === FALSE)
{
print("openssl_x509_read in false!");
return (-1);
}
$sPubKey = openssl_x509_parse($sPubCrt);
$sInput = Pin2PinBlockWithCardNO($sPin, $sCardNo);
if ($sInput == 1)
{
print("Pin2PinBlockWithCardNO Error ! : " . $sInput);
return (1);
}
$iRet = openssl_public_encrypt($sInput, $sOutData, $sCrt, OPENSSL_PKCS1_PADDING);
if ($iRet === TRUE)
{
$sBase64EncodeOutData = base64_encode($sOutData);
return $sBase64EncodeOutData;
}
else
{
print("openssl_public_encrypt Error !");
return (-1);
}
}
?>

View File

@@ -0,0 +1,31 @@
<?php
header ( 'Content-type:text/html;charset=utf-8' );
function sendHttpRequest($params, $url) {
$opts = getRequestParamString ( $params );
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt ( $ch, CURLOPT_SSLVERSION, 3);
curl_setopt ( $ch, CURLOPT_HTTPHEADER, array (
'Content-type:application/x-www-form-urlencoded;charset=UTF-8'
) );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $opts );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
$html = curl_exec ( $ch );
curl_close ( $ch );
return $html;
}
function getRequestParamString($params) {
$params_str = '';
foreach ( $params as $key => $value ) {
$params_str .= ($key . '=' . (!isset ( $value ) ? '' : urlencode( $value )) . '&');
}
return substr ( $params_str, 0, strlen ( $params_str ) - 1 );
}

View File

@@ -0,0 +1,228 @@
<?php
class PhpLog
{
const DEBUG = 1; const INFO = 2; const WARN = 3; const ERROR = 4; const FATAL = 5; const OFF = 6;
const LOG_OPEN = 1;
const OPEN_FAILED = 2;
const LOG_CLOSED = 3;
public $Log_Status = PhpLog::LOG_CLOSED;
public $DateFormat= "Y-m-d G:i:s";
public $MessageQueue;
private $filename;
private $log_file;
private $priority = PhpLog::INFO;
private $file_handle;
public function __construct( $filepath, $timezone, $priority )
{
if ( $priority == PhpLog::OFF ) return;
$this->filename = date('Y-m-d', time()) . '.log'; $this->log_file = $this->createPath($filepath, $this->filename);
$this->MessageQueue = array();
$this->priority = $priority;
date_default_timezone_set($timezone);
if ( !file_exists($filepath) ) {
if(!empty($filepath)) {
if(!($this->_createDir($filepath)))
{
die("创建目录失败!");
}
if ( !is_writable($this->log_file) )
{
$this->Log_Status = PhpLog::OPEN_FAILED;
$this->MessageQueue[] = "The file exists, but could not be opened for writing. Check that appropriate permissions have been set.";
return;
}
}
}
if ( $this->file_handle = fopen( $this->log_file , "a+" ) )
{
$this->Log_Status = PhpLog::LOG_OPEN;
$this->MessageQueue[] = "The log file was opened successfully.";
}
else
{
$this->Log_Status = PhpLog::OPEN_FAILED;
$this->MessageQueue[] = "The file could not be opened. Check permissions.";
}
return;
}
public function __destruct()
{
if ( $this->file_handle )
fclose( $this->file_handle );
}
private function _createDir($dir)
{
return is_dir($dir) or (self::_createDir(dirname($dir)) and mkdir($dir, 0777));
}
private function createPath($dir, $filename)
{
if (empty($dir))
{
return $filename;
}
else
{
return $dir . "/" . $filename;
}
}
public function LogInfo($line)
{
$sAarray = array();
$sAarray = debug_backtrace();
$sGetFilePath = $sAarray[0]["file"];
$sGetFileLine = $sAarray[0]["line"];
$this->Log( $line, PhpLog::INFO, $sGetFilePath, $sGetFileLine);
unset($sAarray);
unset($sGetFilePath);
unset($sGetFileLine);
}
public function LogDebug($line)
{
$sAarray = array();
$sAarray = debug_backtrace();
$sGetFilePath = $sAarray[0]["file"];
$sGetFileLine = $sAarray[0]["line"];
$this->Log( $line, PhpLog::DEBUG, $sGetFilePath, $sGetFileLine);
unset($sAarray);
unset($sGetFilePath);
unset($sGetFileLine);
}
public function LogWarn($line)
{
$sAarray = array();
$sAarray = debug_backtrace();
$sGetFilePath = $sAarray[0]["file"];
$sGetFileLine = $sAarray[0]["line"];
$this->Log( $line, PhpLog::WARN, $sGetFilePath, $sGetFileLine);
unset($sAarray);
unset($sGetFilePath);
unset($sGetFileLine);
}
public function LogError($line)
{
$sAarray = array();
$sAarray = debug_backtrace();
$sGetFilePath = $sAarray[0]["file"];
$sGetFileLine = $sAarray[0]["line"];
$this->Log( $line, PhpLog::ERROR, $sGetFilePath, $sGetFileLine);
unset($sAarray);
unset($sGetFilePath);
unset($sGetFileLine);
}
public function LogFatal($line)
{
$sAarray = array();
$sAarray = debug_backtrace();
$sGetFilePath = $sAarray[0]["file"];
$sGetFileLine = $sAarray[0]["line"];
$this->Log( $line, PhpLog::FATAL, $sGetFilePath, $sGetFileLine);
unset($sAarray);
unset($sGetFilePath);
unset($sGetFileLine);
}
public function Log($line, $priority, $sFile, $iLine)
{
if ($iLine > 0)
{
$line = iconv('GBK', 'UTF-8', $line);
if ( $this->priority <= $priority )
{
$status = $this->getTimeLine( $priority, $sFile, $iLine);
$this->WriteFreeFormLine ( "$status $line \n" );
}
}
else
{
$sAarray = array();
$sAarray = debug_backtrace();
$sGetFilePath = $sAarray[0]["file"];
$sGetFileLine = $sAarray[0]["line"];
if ( $this->priority <= $priority )
{
$status = $this->getTimeLine( $priority, $sGetFilePath, $sGetFileLine);
unset($sAarray);
unset($sGetFilePath);
unset($sGetFileLine);
$this->WriteFreeFormLine ( "$status $line \n" );
}
}
}
public function WriteFreeFormLine( $line )
{
if ( $this->Log_Status == PhpLog::LOG_OPEN && $this->priority != PhpLog::OFF )
{
if (fwrite( $this->file_handle , $line ) === false)
{
$this->MessageQueue[] = "The file could not be written to. Check that appropriate permissions have been set.";
}
}
}
private function getRemoteIP()
{
foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key)
{
if (array_key_exists($key, $_SERVER) === true)
{
foreach (explode(',', $_SERVER[$key]) as $ip)
{
$ip = trim($ip);
if (!empty($ip))
{
return $ip;
}
}
}
}
return "_NO_IP";
}
private function getTimeLine( $level, $FilePath, $FileLine)
{
$time = date( $this->DateFormat );
$ip = $this->getRemoteIP();
switch( $level )
{
case PhpLog::INFO:
return "$time, " . "INFO, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
case PhpLog::WARN:
return "$time, " . "WARN, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
case PhpLog::DEBUG:
return "$time, " . "DEBUG, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
case PhpLog::ERROR:
return "$time, " . "ERROR, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
case PhpLog::FATAL:
return "$time, " . "FATAL, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
default:
return "$time, " . "LOG, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
}
}
}
?>

View File

@@ -0,0 +1,108 @@
<?php
/**
* [WEIZAN System] Copyright (c) 2014 012WZ.COM
* WEIZAN is NOT a free software, it under the license terms, visited http://www.012wz.com/ for more details.
*/
function Pin2PinBlock( &$sPin )
{
$iTemp = 1;
$sPinLen = strlen($sPin);
$sBuf = array();
$sBuf[0]=intval($sPinLen, 10);
if($sPinLen % 2 ==0)
{
for ($i=0; $i<$sPinLen;)
{
$tBuf = substr($sPin, $i, 2);
$sBuf[$iTemp] = intval($tBuf, 16);
unset($tBuf);
if ($i == ($sPinLen - 2))
{
if ($iTemp < 7)
{
$t = 0;
for ($t=($iTemp+1); $t<8; $t++)
{
$sBuf[$t] = 0xff;
}
}
}
$iTemp++;
$i = $i + 2; }
}
else
{
for ($i=0; $i<$sPinLen;)
{
if ($i ==($sPinLen-1))
{
$mBuf = substr($sPin, $i, 1) . "f";
$sBuf[$iTemp] = intval($mBuf, 16);
unset($mBuf);
if (($iTemp)<7)
{
$t = 0;
for ($t=($iTemp+1); $t<8; $t++)
{
$sBuf[$t] = 0xff;
}
}
}
else
{
$tBuf = substr($sPin, $i, 2);
$sBuf[$iTemp] = intval($tBuf, 16);
unset($tBuf);
}
$iTemp++;
$i = $i + 2;
}
}
return $sBuf;
}
function FormatPan(&$sPan)
{
$iPanLen = strlen($sPan);
$iTemp = $iPanLen - 13;
$sBuf = array();
$sBuf[0] = 0x00;
$sBuf[1] = 0x00;
for ($i=2; $i<8; $i++)
{
$tBuf = substr($sPan, $iTemp, 2);
$sBuf[$i] = intval($tBuf, 16);
$iTemp = $iTemp + 2;
}
return $sBuf;
}
function Pin2PinBlockWithCardNO(&$sPin, &$sCardNO)
{
global $log;
$sPinBuf = Pin2PinBlock($sPin);
$iCardLen = strlen($sCardNO);
if ($iCardLen <= 10)
{
return (1);
}
elseif ($iCardLen==11){
$sCardNO = "00" . $sCardNO;
}
elseif ($iCardLen==12){
$sCardNO = "0" . $sCardNO;
}
$sPanBuf = FormatPan($sCardNO);
$sBuf = array();
for ($i=0; $i<8; $i++)
{
$sBuf[$i] = vsprintf("%c", ($sPinBuf[$i] ^ $sPanBuf[$i]));
}
unset($sPinBuf);
unset($sPanBuf);
$sOutput = implode("", $sBuf); return $sOutput;
}
?>

View File

@@ -0,0 +1,40 @@
<?php
function EncryptedPin($sPin, $sCardNo ,$sPubKeyURL)
{
global $log;
$sPubKeyURL = trim(SDK_ENCRYPT_CERT_PATH," ");
$fp = fopen($sPubKeyURL, "r");
if ($fp != NULL)
{
$sCrt = fread($fp, 8192);
fclose($fp);
}
$sPubCrt = openssl_x509_read($sCrt);
if ($sPubCrt === FALSE)
{
print("openssl_x509_read in false!");
return (-1);
}
$sPubKey = openssl_x509_parse($sPubCrt);
$sInput = Pin2PinBlockWithCardNO($sPin, $sCardNo);
if ($sInput == 1)
{
print("Pin2PinBlockWithCardNO Error ! : " . $sInput);
return (1);
}
$iRet = openssl_public_encrypt($sInput, $sOutData, $sCrt, OPENSSL_PKCS1_PADDING);
if ($iRet === TRUE)
{
$sBase64EncodeOutData = base64_encode($sOutData);
return $sBase64EncodeOutData;
}
else
{
print("openssl_public_encrypt Error !");
return (-1);
}
}
?>

View File

@@ -0,0 +1,40 @@
<?php
/**
* [WEIZAN System] Copyright (c) 2014 012WZ.COM
* WEIZAN is NOT a free software, it under the license terms, visited http://www.012wz.com/ for more details.
*/const SDK_CVN2_ENC = 0;
const SDK_DATE_ENC = 0;
const SDK_PAN_ENC = 0;
const SDK_SIGN_CERT_PATH = 'D:/certs/PM_700000000000001_acp.pfx';
const SDK_SIGN_CERT_PWD = '000000';
const SDK_ENCRYPT_CERT_PATH = 'D:/certs/verify_sign_acp.cer';
const SDK_VERIFY_CERT_DIR = 'D:/certs/';
const SDK_FRONT_TRANS_URL = 'https://101.231.204.80:5000/gateway/api/frontTransReq.do';
const SDK_BACK_TRANS_URL = 'https://101.231.204.80:5000/gateway/api/backTransReq.do';
const SDK_BATCH_TRANS_URL = 'https://101.231.204.80:5000/gateway/api/batchTrans.do';
const SDK_SINGLE_QUERY_URL = 'https://101.231.204.80:5000/gateway/api/queryTrans.do';
const SDK_FILE_QUERY_URL = 'https://101.231.204.80:9080/';
const SDK_Card_Request_Url = 'https://101.231.204.80:5000/gateway/api/cardTransReq.do';
const SDK_App_Request_Url = 'https://101.231.204.80:5000/gateway/api/appTransReq.do';
const SDK_FRONT_NOTIFY_URL = 'http://localhost:8085/upacp_sdk_php/demo/gbk/FrontReceive.php';
const SDK_BACK_NOTIFY_URL = 'http://114.82.43.123/upacp_sdk_php/demo/gbk/BackReceive.php';
const SDK_FILE_DOWN_PATH = 'd:/file/';
const SDK_LOG_FILE_PATH = 'd:/logs/';
const SDK_LOG_LEVEL = 'INFO';
?>

View File

@@ -0,0 +1,125 @@
<?php
/**
* [WEIZAN System] Copyright (c) 2014 012WZ.COM
* WEIZAN is NOT a free software, it under the license terms, visited http://www.012wz.com/ for more details.
*/$log = new PhpLog ( SDK_LOG_FILE_PATH, "PRC", SDK_LOG_LEVEL );
function coverParamsToString($params) {
$sign_str = '';
ksort ( $params );
foreach ( $params as $key => $val ) {
if ($key == 'signature') {
continue;
}
$sign_str .= sprintf ( "%s=%s&", $key, $val );
}
return substr ( $sign_str, 0, strlen ( $sign_str ) - 1 );
}
function coverStringToArray($str) {
$result = array ();
if (! empty ( $str )) {
$temp = preg_split ( '/&/', $str );
if (! empty ( $temp )) {
foreach ( $temp as $key => $val ) {
$arr = preg_split ( '/=/', $val, 2 );
if (! empty ( $arr )) {
$k = $arr ['0'];
$v = $arr ['1'];
$result [$k] = $v;
}
}
}
}
return $result;
}
function deal_params(&$params) {
if (! empty ( $params ['customerInfo'] )) {
$params ['customerInfo'] = base64_decode ( $params ['customerInfo'] );
}
if (! empty ( $params ['encoding'] ) && strtoupper ( $params ['encoding'] ) == 'utf-8') {
foreach ( $params as $key => $val ) {
$params [$key] = iconv ( 'utf-8', 'UTF-8', $val );
}
}
}
function deflate_file(&$params) {
global $log;
foreach ( $_FILES as $file ) {
$log->LogInfo ( "---------处理文件---------" );
if (file_exists ( $file ['tmp_name'] )) {
$params ['fileName'] = $file ['name'];
$file_content = file_get_contents ( $file ['tmp_name'] );
$file_content_deflate = gzcompress ( $file_content );
$params ['fileContent'] = base64_encode ( $file_content_deflate );
$log->LogInfo ( "压缩后文件内容为>" . base64_encode ( $file_content_deflate ) );
} else {
$log->LogInfo ( ">>>>文件上传失败<<<<<" );
}
}
}
function deal_file($params) {
global $log;
if (isset ( $params ['fileContent'] )) {
$log->LogInfo ( "---------处理后台报文返回的文件---------" );
$fileContent = $params ['fileContent'];
if (empty ( $fileContent )) {
$log->LogInfo ( '文件内容为空' );
} else {
$content = gzuncompress ( base64_decode ( $fileContent ) );
$root = SDK_FILE_DOWN_PATH;
$filePath = null;
if (empty ( $params ['fileName'] )) {
$log->LogInfo ( "文件名为空" );
$filePath = $root . $params ['merId'] . '_' . $params ['batchNo'] . '_' . $params ['txnTime'] . 'txt';
} else {
$filePath = $root . $params ['fileName'];
}
$handle = fopen ( $filePath, "w+" );
if (! is_writable ( $filePath )) {
$log->LogInfo ( "文件:" . $filePath . "不可写,请检查!" );
} else {
file_put_contents ( $filePath, $content );
$log->LogInfo ( "文件位置 >:" . $filePath );
}
fclose ( $handle );
}
}
}
function create_html($params, $action) {
$encodeType = isset ( $params ['encoding'] ) ? $params ['encoding'] : 'UTF-8';
$html = <<<eot
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset={$encodeType}" />
</head>
<body onload="javascript:document.pay_form.submit();">
<form id="pay_form" name="pay_form" action="{$action}" method="post">
eot;
foreach ( $params as $key => $value ) {
$html .= " <input type=\"hidden\" name=\"{$key}\" id=\"{$key}\" value=\"{$value}\" />\n";
}
$html .= <<<eot
<input type="submit" type="hidden" value="稍等,支付跳转跳..." style="border:none;">
</form>
</body>
</html>
eot;
return $html;
}
?>

View File

@@ -0,0 +1,34 @@
<?php
header ( 'Content-type:text/html;charset=utf-8' );
/**
* [WEIZAN System] Copyright (c) 2014 012WZ.COM
* WEIZAN is NOT a free software, it under the license terms, visited http://www.012wz.com/ for more details.
*/
function sendHttpRequest($params, $url) {
$opts = getRequestParamString ( $params );
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt ( $ch, CURLOPT_SSLVERSION, 3);
curl_setopt ( $ch, CURLOPT_HTTPHEADER, array (
'Content-type:application/x-www-form-urlencoded;charset=UTF-8'
) );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $opts );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
$html = curl_exec ( $ch );
curl_close ( $ch );
return $html;
}
function getRequestParamString($params) {
$params_str = '';
foreach ( $params as $key => $value ) {
$params_str .= ($key . '=' . (!isset ( $value ) ? '' : urlencode( $value )) . '&');
}
return substr ( $params_str, 0, strlen ( $params_str ) - 1 );
}

View File

@@ -0,0 +1,228 @@
<?php
class PhpLog
{
const DEBUG = 1; const INFO = 2; const WARN = 3; const ERROR = 4; const FATAL = 5; const OFF = 6;
const LOG_OPEN = 1;
const OPEN_FAILED = 2;
const LOG_CLOSED = 3;
public $Log_Status = PhpLog::LOG_CLOSED;
public $DateFormat= "Y-m-d G:i:s";
public $MessageQueue;
private $filename;
private $log_file;
private $priority = PhpLog::INFO;
private $file_handle;
public function __construct( $filepath, $timezone, $priority )
{
if ( $priority == PhpLog::OFF ) return;
$this->filename = date('Y-m-d', time()) . '.log'; $this->log_file = $this->createPath($filepath, $this->filename);
$this->MessageQueue = array();
$this->priority = $priority;
date_default_timezone_set($timezone);
if ( !file_exists($filepath) ) {
if(!empty($filepath)) {
if(!($this->_createDir($filepath)))
{
die("创建目录失败!");
}
if ( !is_writable($this->log_file) )
{
$this->Log_Status = PhpLog::OPEN_FAILED;
$this->MessageQueue[] = "The file exists, but could not be opened for writing. Check that appropriate permissions have been set.";
return;
}
}
}
if ( $this->file_handle = fopen( $this->log_file , "a+" ) )
{
$this->Log_Status = PhpLog::LOG_OPEN;
$this->MessageQueue[] = "The log file was opened successfully.";
}
else
{
$this->Log_Status = PhpLog::OPEN_FAILED;
$this->MessageQueue[] = "The file could not be opened. Check permissions.";
}
return;
}
public function __destruct()
{
if ( $this->file_handle )
fclose( $this->file_handle );
}
private function _createDir($dir)
{
return is_dir($dir) or (self::_createDir(dirname($dir)) and mkdir($dir, 0777));
}
private function createPath($dir, $filename)
{
if (empty($dir))
{
return $filename;
}
else
{
return $dir . "/" . $filename;
}
}
public function LogInfo($line)
{
$sAarray = array();
$sAarray = debug_backtrace();
$sGetFilePath = $sAarray[0]["file"];
$sGetFileLine = $sAarray[0]["line"];
$this->Log( $line, PhpLog::INFO, $sGetFilePath, $sGetFileLine);
unset($sAarray);
unset($sGetFilePath);
unset($sGetFileLine);
}
public function LogDebug($line)
{
$sAarray = array();
$sAarray = debug_backtrace();
$sGetFilePath = $sAarray[0]["file"];
$sGetFileLine = $sAarray[0]["line"];
$this->Log( $line, PhpLog::DEBUG, $sGetFilePath, $sGetFileLine);
unset($sAarray);
unset($sGetFilePath);
unset($sGetFileLine);
}
public function LogWarn($line)
{
$sAarray = array();
$sAarray = debug_backtrace();
$sGetFilePath = $sAarray[0]["file"];
$sGetFileLine = $sAarray[0]["line"];
$this->Log( $line, PhpLog::WARN, $sGetFilePath, $sGetFileLine);
unset($sAarray);
unset($sGetFilePath);
unset($sGetFileLine);
}
public function LogError($line)
{
$sAarray = array();
$sAarray = debug_backtrace();
$sGetFilePath = $sAarray[0]["file"];
$sGetFileLine = $sAarray[0]["line"];
$this->Log( $line, PhpLog::ERROR, $sGetFilePath, $sGetFileLine);
unset($sAarray);
unset($sGetFilePath);
unset($sGetFileLine);
}
public function LogFatal($line)
{
$sAarray = array();
$sAarray = debug_backtrace();
$sGetFilePath = $sAarray[0]["file"];
$sGetFileLine = $sAarray[0]["line"];
$this->Log( $line, PhpLog::FATAL, $sGetFilePath, $sGetFileLine);
unset($sAarray);
unset($sGetFilePath);
unset($sGetFileLine);
}
public function Log($line, $priority, $sFile, $iLine)
{
if ($iLine > 0)
{
$line = iconv('GBK', 'UTF-8', $line);
if ( $this->priority <= $priority )
{
$status = $this->getTimeLine( $priority, $sFile, $iLine);
$this->WriteFreeFormLine ( "$status $line \n" );
}
}
else
{
$sAarray = array();
$sAarray = debug_backtrace();
$sGetFilePath = $sAarray[0]["file"];
$sGetFileLine = $sAarray[0]["line"];
if ( $this->priority <= $priority )
{
$status = $this->getTimeLine( $priority, $sGetFilePath, $sGetFileLine);
unset($sAarray);
unset($sGetFilePath);
unset($sGetFileLine);
$this->WriteFreeFormLine ( "$status $line \n" );
}
}
}
public function WriteFreeFormLine( $line )
{
if ( $this->Log_Status == PhpLog::LOG_OPEN && $this->priority != PhpLog::OFF )
{
if (fwrite( $this->file_handle , $line ) === false)
{
$this->MessageQueue[] = "The file could not be written to. Check that appropriate permissions have been set.";
}
}
}
private function getRemoteIP()
{
foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key)
{
if (array_key_exists($key, $_SERVER) === true)
{
foreach (explode(',', $_SERVER[$key]) as $ip)
{
$ip = trim($ip);
if (!empty($ip))
{
return $ip;
}
}
}
}
return "_NO_IP";
}
private function getTimeLine( $level, $FilePath, $FileLine)
{
$time = date( $this->DateFormat );
$ip = $this->getRemoteIP();
switch( $level )
{
case PhpLog::INFO:
return "$time, " . "INFO, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
case PhpLog::WARN:
return "$time, " . "WARN, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
case PhpLog::DEBUG:
return "$time, " . "DEBUG, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
case PhpLog::ERROR:
return "$time, " . "ERROR, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
case PhpLog::FATAL:
return "$time, " . "FATAL, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
default:
return "$time, " . "LOG, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
}
}
}
?>

View File

@@ -0,0 +1,162 @@
<?php
/**
* [WEIZAN System] Copyright (c) 2014 012WZ.COM
* WEIZAN is NOT a free software, it under the license terms, visited http://www.012wz.com/ for more details.
*/$log = new PhpLog ( SDK_LOG_FILE_PATH, "PRC", SDK_LOG_LEVEL );
function sign(&$params) {
global $log;
$log->LogInfo ( '=====签名报文开始======' );
if(isset($params['transTempUrl'])){
unset($params['transTempUrl']);
}
$params_str = coverParamsToString ( $params );
$log->LogInfo ( "签名key=val&...串 >" . $params_str );
$params_sha1x16 = sha1 ( $params_str, FALSE );
$log->LogInfo ( "摘要sha1x16 >" . $params_sha1x16 );
$cert_path = SDK_SIGN_CERT_PATH;
$private_key = getPrivateKey ( $cert_path );
$sign_falg = openssl_sign ( $params_sha1x16, $signature, $private_key, OPENSSL_ALGO_SHA1 );
if ($sign_falg) {
$signature_base64 = base64_encode ( $signature );
$log->LogInfo ( "签名串为 >" . $signature_base64 );
$params ['signature'] = $signature_base64;
} else {
$log->LogInfo ( ">>>>>签名失败<<<<<<<" );
}
$log->LogInfo ( '=====签名报文结束======' );
}
function verify($params) {
global $log;
$public_key = getPulbicKeyByCertId ( $params ['certId'] );
$signature_str = $params ['signature'];
unset ( $params ['signature'] );
$params_str = coverParamsToString ( $params );
$log->LogInfo ( '报文去[signature] key=val&串>' . $params_str );
$signature = base64_decode ( $signature_str );
$params_sha1x16 = sha1 ( $params_str, FALSE );
$log->LogInfo ( '摘要shax16>' . $params_sha1x16 );
$isSuccess = openssl_verify ( $params_sha1x16, $signature,$public_key, OPENSSL_ALGO_SHA1 );
$log->LogInfo ( $isSuccess ? '验签成功' : '验签失败' );
return $isSuccess;
}
function getPulbicKeyByCertId($certId) {
global $log;
$log->LogInfo ( '报文返回的证书ID>' . $certId );
$cert_dir = SDK_VERIFY_CERT_DIR;
$log->LogInfo ( '验证签名证书目录 :>' . $cert_dir );
$handle = opendir ( $cert_dir );
if ($handle) {
while ( $file = readdir ( $handle ) ) {
clearstatcache ();
$filePath = $cert_dir . '/' . $file;
if (is_file ( $filePath )) {
if (pathinfo ( $file, PATHINFO_EXTENSION ) == 'cer') {
if (getCertIdByCerPath ( $filePath ) == $certId) {
closedir ( $handle );
$log->LogInfo ( '加载验签证书成功' );
return getPublicKey ( $filePath );
}
}
}
}
$log->LogInfo ( '没有找到证书ID为[' . $certId . ']的证书' );
} else {
$log->LogInfo ( '证书目录 ' . $cert_dir . '不正确' );
}
closedir ( $handle );
return null;
}
function getCertId($cert_path) {
$pkcs12certdata = file_get_contents ( $cert_path );
openssl_pkcs12_read ( $pkcs12certdata, $certs, SDK_SIGN_CERT_PWD );
$x509data = $certs ['cert'];
openssl_x509_read ( $x509data );
$certdata = openssl_x509_parse ( $x509data );
$cert_id = $certdata ['serialNumber'];
return $cert_id;
}
function getCertIdByCerPath($cert_path) {
$x509data = file_get_contents ( $cert_path );
openssl_x509_read ( $x509data );
$certdata = openssl_x509_parse ( $x509data );
$cert_id = $certdata ['serialNumber'];
return $cert_id;
}
function getSignCertId() {
return getCertId ( SDK_SIGN_CERT_PATH );
}
function getEncryptCertId() {
return getCertIdByCerPath ( SDK_ENCRYPT_CERT_PATH );
}
function getPublicKey($cert_path) {
return file_get_contents ( $cert_path );
}
function getPrivateKey($cert_path) {
$pkcs12 = file_get_contents ( $cert_path );
openssl_pkcs12_read ( $pkcs12, $certs, SDK_SIGN_CERT_PWD );
return $certs ['pkey'];
}
function encryptPan($pan) {
$cert_path = MPI_ENCRYPT_CERT_PATH;
$public_key = getPublicKey ( $cert_path );
openssl_public_encrypt ( $pan, $cryptPan, $public_key );
return base64_encode ( $cryptPan );
}
function encryptPin($pan, $pwd) {
$cert_path = SDK_ENCRYPT_CERT_PATH;
$public_key = getPublicKey ( $cert_path );
return EncryptedPin ( $pwd, $pan, $public_key );
}
function encryptCvn2($cvn2) {
$cert_path = SDK_ENCRYPT_CERT_PATH;
$public_key = getPublicKey ( $cert_path );
openssl_public_encrypt ( $cvn2, $crypted, $public_key );
return base64_encode ( $crypted );
}
function encryptDate($certDate) {
$cert_path = SDK_ENCRYPT_CERT_PATH;
$public_key = getPublicKey ( $cert_path );
openssl_public_encrypt ( $certDate, $crypted, $public_key );
return base64_encode ( $crypted );
}
function encryptDateType($certDataType) {
$cert_path = SDK_ENCRYPT_CERT_PATH;
$public_key = getPublicKey ( $cert_path );
openssl_public_encrypt ( $certDataType, $crypted, $public_key );
return base64_encode ( $crypted );
}
?>