333 lines
8.7 KiB
PHP
Raw Normal View History

2024-09-29 15:43:18 +08:00
<?php
namespace app\channel\service\system;
/**
* 泽横服务
* Class Tiancheng
* @package app\channel\service
*/
class Zeheng extends Core
{
# 这几个可以后台设置
protected $host = 'http://home.ogrnkbh.cn/';
protected $mid = '10101';
protected $token = '625d3c437eb4be7ad99755ece0ae222f';
protected $t = 1;
protected $iv = '';
# 通用的充值
public function common($param)
{
$check = array
(
'account' => 'mobile',
'cash' => 'cash',
);
$this->t = 2;
return $this->submit($param, $check);
}
# 中石油油卡充值 拼音来吧
public function zsycz($param)
{
$check = array
(
'card' => 'mobile',
'cash' => 'cash',
'mobile' => 'phone',
'idCard' => 'id_no',
'realName' => 'name',
);
return $this->submit($param, $check);
}
# 中石油油卡充值 拼音来吧
public function zshcz($param)
{
$check = array
(
'card' => 'mobile',
'cash' => 'cash',
);
return $this->submit($param, $check);
}
# 通知处理 主要返回状态 2是成功 3是失败
public function notify($data)
{
$result = array();
$result['cash'] = 1;
if ($data['retcode'] == 1) {
$result['status'] = 2;
} elseif ($data['retcode'] == 0) {
$result['status'] = 3;
} else {
$result['status'] = 4;
}
if (isset($data['errcode']) && $data['errcode'] && $data['errcode'] != 'null') {
# 流水号
$data['s_nubmer'] = $result['s_nubmer'] = $data['errcode'];
}
$result['data'] = $data;
return $result;
}
# 数据响应格式处理
public function response($data)
{
$log['type'] = 'response';
$log['data'] = $data;
$log['config'] = $this->data;
$this->log($log);
$array = $this->json_decode($data);
if (!$array) {
$msg = 'error';
} elseif (isset($array['retcode']) && $array['retcode'] == 200) {
# 正确
$msg = 'ok';
} else {
# 错误
$msg = isset($array['retmsg']) ? $array['retmsg'] : 'error';
}
return array
(
'msg' => $msg,
'data' => $data,
'array' => $array,
);
}
# 查询接口
public function query($param)
{
}
# 提交数据
private function submit($param, $check)
{
$param = $this->param($param, $check);
if (is_string($param) || (is_array($param) && $param['status'] == 1)) {
return $param;
}
$request['api_userid'] = $this->mid;
$request['api_data'] = $this->getApiData($param);
//$request['demons'] = $this->getDemons($param);
$url = $this->host . 'api/recharge.jsp';
$response = $this->curl('post', $url, http_build_query($request), false, array('Content-Type' => 'application/x-www-form-urlencoded'));
$response = $this->response($response);
//$response['data'] = '';
//$response['msg'] = 'ok';
$channel_order_id = '';
$this->create($param['order'], $channel_order_id, $param['merchant_order'], $param['cash'], $url, $request, $response, 1, $param['account']);
return $response['msg'];
}
private function getApiData($param)
{
$data['api_userid'] = $this->mid;
$data['product_id'] = $this->getGid($param['cash']);
$data['mobile'] = isset($param['card']) ? $param['card'] : $param['account'];
$data['order_no'] = $param['order'];
$data = json_encode($data);
$aes = new CryptAES();
$aes->set_key($this->token);
$aes->require_pkcs5();
$result = $aes->encrypt($data);
return $result;
}
private function getProduct($cash)
{
return '';
}
private function getDemons($param)
{
return '';
$data['id_no'] = $param['idCard'];
$data['name'] = $param['realName'];
$data['phone'] = $param['mobile'];
return json_encode($data);
}
protected function aes_encrypt($data)
{
return '59CFB5035F5E8A9EF421D4CE801237925215F1B76543FC5B4C8D5CDB284CFF12DAE0EA2EE9C5239E01FE2F8550F74913064010E347B06A620770621C51B2B181D0813CB0AF766AD9BA2902DE5065CD7C2EDB04776B96D6E13D7CF6C56CDE011E4DFB6D21001899C14CF5238AFB9149E5';
if (is_array($data)) {
//ksort($data);
$data = json_encode($data);
}
//echo $data;die;
$data = $this->pkcs5Pad($data);
//把key值截取成16位的
$secretAccessKey = substr($this->token, 0, 16);
//进行AES加密
$crypted = openssl_encrypt($data, 'AES-128-ECB', $secretAccessKey, OPENSSL_RAW_DATA);
//把字符串转换为16进制
return base64_encode($crypted);
return $data;
}
function pkcs5Pad($adminPass)
{
$pad = 16 - (strlen($adminPass) % 16);
return $adminPass . str_repeat(chr($pad), $pad);
}
//AES-128-ECB解密 data 字符串
protected function aes_decrypt($data)
{
$encrypted = base64_decode($data);
$data = openssl_decrypt($encrypted, 'AES-128-ECB', $this->token, OPENSSL_RAW_DATA, $this->iv);
$data = str_replace("\0", '', $data);
$data = json_decode($data, true);
return $data;
}
}
class CryptAES
{
protected $cipher = MCRYPT_RIJNDAEL_128;
protected $mode = MCRYPT_MODE_ECB;
protected $pad_method = NULL;
protected $secret_key = '';
protected $iv = '';
public function set_cipher($cipher)
{
$this->cipher = $cipher;
}
public function set_mode($mode)
{
$this->mode = $mode;
}
public function set_iv($iv)
{
$this->iv = $iv;
}
public function set_key($key)
{
$this->secret_key = $key;
}
public function require_pkcs5()
{
$this->pad_method = 'pkcs5';
}
protected function pad_or_unpad($str, $ext)
{
if ( is_null($this->pad_method) )
{
return $str;
}
else
{
$func_name = __CLASS__ . '::' . $this->pad_method . '_' . $ext . 'pad';
if ( is_callable($func_name) )
{
@$size = mcrypt_get_block_size($this->cipher, $this->mode);
return call_user_func($func_name, $str, $size);
}
}
return $str;
}
protected function pad($str)
{
return $this->pad_or_unpad($str, '');
}
protected function unpad($str)
{
return $this->pad_or_unpad($str, 'un');
}
public function encrypt($str)
{
$str = $this->pad($str);
@$td = mcrypt_module_open($this->cipher, '', $this->mode, '');
if ( empty($this->iv) )
{
@$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
}
else
{
$iv = $this->iv;
}
@mcrypt_generic_init($td, hex2bin($this->secret_key), $iv);
@$cyper_text = mcrypt_generic($td, $str);
$rt = strtoupper(bin2hex($cyper_text));
@mcrypt_generic_deinit($td);
@mcrypt_module_close($td);
return $rt;
}
public function decrypt($str){
$str = base64_decode($str);
$td = @mcrypt_module_open($this->cipher, '', $this->mode, '');
if ( empty($this->iv) )
{
$iv = @mcrypt_create_iv(@mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
}
else
{
$iv = $this->iv;
}
@mcrypt_generic_init($td, $this->secret_key, $iv);
$decrypted_text = mdecrypt_generic($td, self::hex2bin($str));
//$decrypted_text = @mdecrypt_generic($td, base64_decode($str));
$rt = $decrypted_text;
@mcrypt_generic_deinit($td);
@mcrypt_module_close($td);
return $this->unpad($rt);
}
public static function hex2bin($hexdata) {
$bindata = '';
$length = strlen($hexdata);
for ($i=0; $i< $length; $i += 2)
{
$bindata .= chr(hexdec(substr($hexdata, $i, 2)));
}
return $bindata;
}
public static function pkcs5_pad($text, $blocksize)
{
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
public static function pkcs5_unpad($text)
{
$pad = ord($text{strlen($text) - 1});
if ($pad > strlen($text)) return false;
if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
return substr($text, 0, -1 * $pad);
}
}