2024-09-29 15:43:18 +08:00

265 lines
7.1 KiB
PHP

<?php
namespace app\channel\service\system;
use app\channel\service\KamiService;
use app\kami\service\LockCardService;
use app\merchant\service\MerchantService;
/**
* 中兆接口
* Class Bodingcheng
* @package app\channel\service
*/
class Zhongzhao extends Core
{
# 这几个可以后台设置
protected $host = 'http://101.200.165.85/';
protected $mid = '24';
protected $token = 'GfEVXoIs6VgZZfaQDCbqrf2t1Wvr04';
protected $api = '';
protected $card_kind = '';
protected $aeskey = '383147446d6e4e6c';
# 购买卡密
public function cardbuy($param)
{
// var_dump($param);die;
$check = array
(
// 'proid' => 'productCode',
'num' => 'number',
'cash' => 'money',
);
$this->api = 'api.php/sellCard.html';
return $this->submit($param, $check);
}
# 骏卡智选购买卡密
public function jkzx_cardbuy($param)
{
// var_dump($param);die;
$check = array
(
// 'proid' => 'productCode',
'num' => 'number',
'cash' => 'money',
);
$this->api = 'api.php/sellCard.html';
// $this->aeskey = '434339776a344c36';
$this->card_kind = 'jkzx';
return $this->submit($param, $check);
}
public function jkjl_cardbuy($param)
{
// var_dump($param);die;
$check = array
(
// 'proid' => 'productCode',
'num' => 'number',
'cash' => 'money',
);
$this->api = 'api.php/sellCard.html';
// $this->aeskey = '4e3455324d526657';
$this->card_kind = 'jkjl';
return $this->submit($param, $check);
}
# 数据响应格式处理
public function response($data)
{
$log['type'] = 'response';
$log['data'] = $data;
$log['config'] = $this->data;
$this->log($log);
$array = $this->json_decode($data);
// var_dump($array);die;
if (!$array) {
$msg = 'error';
} elseif (isset($array['code']) && $array['code'] == '1' ) {
# 正确
$msg = 'yescard';
if (isset($array['data'])) {
$cardlist = $array['data']['cards'];
// $merchant = MerchantService::instance()->get($this->data['merchant']);
// $cardlist[]
$cardno = $this->aes_decrypt($cardlist[0]['cardno']);
// var_dump($cardno);die;
$cardpwd = $this->aes_decrypt($cardlist[0]['cardkey']);
$array['kami'] = array
(
'cardno' => $cardno,
'cardpwd' => $cardpwd,
'expired' => $cardlist[0]['expirtime'],
'value' => $array['data']['count_money'],
);
}
} else {
# 错误
$msg = $array['msg'] ?? 'error';
}
return array
(
'msg' => $msg,
'data' => $array,
'array' => $array,
);
}
//AES-128-ECB解密 data 字符串
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 = $param['detail'];
$request['customerId'] = $this->mid;
$request['productCode'] = $this->getGid($param['cash']);
$request['timestamp'] = time();
$request['orderId'] = $param['merchant_order'];
// $request['money'] = '0';
$request['sign'] = $this->_sign($request, array('customerId', 'money', 'number', 'orderId', 'productCode', 'timestamp'));
$url = $this->host . $this->api;
$KamiService = KamiService::instance();
if($this->card_kind == 'jkzx'){
$randomKami = $KamiService->getData($param['cash'],1, 1, true, false, 1);
}elseif($this->card_kind == 'jkjl'){
$randomKami = $KamiService->getData($param['cash'],1, 1, true, false, 2);
} else{
$randomKami = null;
}
// $randomKami = '';
if ($randomKami) {
$LockCardService = LockCardService::instance();
$unlockcard = $LockCardService->call('UnlockCard', $this->channel['id'],$randomKami['cnum'],$randomKami['cpwd'],1,$randomKami['value']);
if($unlockcard != 'ok'){
$response = $this->curl('post', $url, $request, true);
$response = $this->response($response);
$KamiService->db()->where(array('cnum' => $randomKami['cnum']))->update(array('status' => '7'));
}else{
$randomKami_data = [
'is_kami' =>1,
];
$msg = 'yescard';
$randomKami_data['kami'] = array
(
'cardno'=>$randomKami['cnum'],
'cardpwd'=>$randomKami['cpwd'],
'expired'=>$randomKami['expire_time'],
'value'=>$randomKami['value'],
);
$response = [
'msg' => $msg,
'data' => $randomKami_data,
'array' => $randomKami_data
];
}
} else {
$response = $this->curl('post', $url, $request, true);
$response = $this->response($response);
// var_dump($response);die;
}
// var_dump($response);die;
// var_dump($response);
//$response['data'] = '';
//$response['msg'] = 'ok';
$channel_order_id = '';
if (isset($response['array']['data']['billno'])) {
$channel_order_id = $response['array']['data']['billno'];
}
$param['account'] = $request['productCode'];
$this->create($param['order'], $channel_order_id, $param['merchant_order'], $param['cash'], $url, $request, $response, 1, $param['account']);
return $response['array']['card'] ?? $response['msg'];
}
private function _sign($request, $param)
{
// ksort($request);
$signature_string = '';
foreach ($param as $k => $v) {
// if (isset($request[$v]) && $request[$v]) {
$signature_string .= $v . '=' . $request[$v] . '&';
}
$signature_string = substr($signature_string, 0, -1);
$signature_string .= '&key='.$this->token;
// var_dump($signature_string);die;
// $signature_string = iconv("UTF-8", "gbk", $signature_string);
return md5($signature_string);
}
//AES-128-ECB解密 data 字符串
protected function aes_decrypt($data)
{
$encrypted = hex2bin($data);
$json = $this->channel['describe'];
$array = $this->json_decode($json);
if(isset($array['aeskey'])){
$this->aeskey = $array['aeskey'];
}
// var_dump($encrypted);die;
// $key = '';
$data = openssl_decrypt($encrypted, 'AES-128-ECB', $this->aeskey, OPENSSL_RAW_DATA);
return $data;
}
}