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

236 lines
6.8 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 Yiqida extends Core
{
# 这几个可以后台设置
protected $host = '';
protected $mid = '';
protected $token = '';
protected $api = '';
protected $card_kind = '';
protected $aeskey = '';
# 通用的充值
public function common($param)
{
$check = array
(
'merchant_order' => 'external_orderno',
'num' => 'buyCount',
);
$this->api = 'api/UserCommdity/GetCommodityInfo';
$this->method = 'post';
return $this->submit($param, $check);
}
# 骏卡智选购买卡密
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['timestamp'] = time();
// $request['remark'] = $param['merchant_order'];
$request['callbackUrl'] = $this->getNotify($param['order'], 1);;
$request['externalSellPrice'] = $param['cash'];
$request['template'] = $param['template'];
$request['sign'] = $this->_sign($request, array('commodityId', 'external_orderno', 'buyCount', 'remark', 'callbackUrl', 'externalSellPrice','template'));
$url = $this->host . $this->api.'?timestamp='.$request['timestamp'].'&username='.$this->mid.'&sign='.$request['sign'];
unset($request['sign']);
unset($request['timestamp']);
$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;
}
$channel_order_id = '';
if (isset($response['array']['data']['orderNo'])) {
$channel_order_id = $response['array']['data']['orderNo'];
}
$param['account'] = $request['branchId'];
$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)
{
$signature_string = array();
foreach ($param as $k => $v) {
if(!isset($signature_string[$v])){
$signature_string[$v]=$request[$v];
}
}
$signature_string = json_encode($signature_string);
return md5($request['timestamp'].$signature_string.$this->token);
}
# 数据响应格式处理
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,
);
}
# 查询接口
protected function aes_decrypt($data)
{
$encrypted = hex2bin($data);
// var_dump($encrypted);die;
// $key = '';
$data = openssl_decrypt($encrypted, 'AES-128-ECB', $this->aeskey, OPENSSL_RAW_DATA);
return $data;
}
# 提交数据
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 = '383147446d6e4e6c';
$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);
}
//AES-128-ECB解密 data 字符串
public function query($param)
{
}
}