431 lines
11 KiB
PHP

<?php
namespace app\channel\service\system;
use app\channel\service\KamiService;
use app\kami\service\LockCardService;
use app\merchant\service\MerchantService;
use app\merchant\service\OrderService;
use parallel\Events\Event\Type;
/**
* 勋脉
* Class Xunmai
* @package app\channel\service
*/
class Xunmai extends Core
{
# 这几个可以后台设置
protected $host = '';
protected $mid = '';
protected $token = '';
protected $api = '';
protected $card_kind = '';
protected $aeskey = '';
# 购买卡密
public function cardbuy($param)
{
$check = array
(
'num' => 'number',
'cash' => 'money',
);
$this->api = 'cards/api/sellCard';
return $this->submit($param, $check);
}
# 数汇一卡通
public function shykt_cardbuy($param)
{
$check = array
(
'num' => 'number',
'cash' => 'money',
);
$this->api = 'cards/api/sellCard';
return $this->submit($param, $check);
}
public function lockCard($order)
{
$response =$this->lock_send($order);
$log['type'] = 'LockCard_response';
$log['data'] = $response;
$log['config'] = $this->data;
$this->log($log);
$array = $this->json_decode($response);
if (!$array) {
return 'error';
} elseif (isset($array['code']) && $array['code'] == '1') {
# 正确
return 'ok';
} else {
# 错误
return 'error';
}
}
#解锁接口
public function unlockCard($order)
{
$response =$this->lock_send($order,2);
$log['type'] = 'UnLockCard_response';
$log['data'] = $response;
$log['config'] = $this->data;
$this->log($log);
$array = $this->json_decode($response);
if (!$array) {
return 'error';
} elseif (isset($array['code']) && $array['code'] == '1') {
# 正确
return 'ok';
} else {
# 错误
return 'error';
}
}
#锁卡发送
public function lock_send($order,$type = 1){
$request=[];
$request['customerId'] = $this->mid;
// $request['money'] = $order['cash'];
if($type==1){
$response = $this->json_decode($order['response']);
if(empty($response['kami'])){
return '未找到卡密';
}
$request['cardNo'] = $response['kami']['cardno'];
}else{
$request['cardNo'] = $order['cardno']; #解锁默认order卡号
}
$request['lockType'] = $type;
$request['timestamp'] =time();
$request['sign'] = $this->_sign($request, array('cardNo', 'customerId', 'lockType', 'timestamp'));
$url = $this->host . 'cards/api/orderLocked';
$data = $this->curl('post', $url, $request,true);
return $data;
}
# 通知处理 主要返回状态 2是成功 3是失败
public function notify($data)
{
$result = array();
$result['cash'] = 1;
$result['status'] = $data['status'] ;
if (isset($data['kami']) && $data['kami']['cardno']) {
# 流水号
$data['s_nubmer'] = $result['s_nubmer'] = $data['kami']['cardno'];
}
$result['yes'] = 'ok';
$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);
// 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[]
$cardData = $this->aes_decrypt($cardlist);
$cardArray = $this->json_decode($cardData);
$logs['type'] = 'test';
$logs['data'] = $cardArray;
$this->log($logs);
// var_dump($cardArray[0]);die;
$cardno = $cardArray[0]['cardno'];
$cardpwd = $cardArray[0]['cardkey'];
$array['kami'] = array
(
'cardno' => $cardno,
'cardpwd' => $cardpwd,
'expired' => $cardArray[0]['expirtime'] ?? '',
'value' => $array['data']['count_money'],
);
// var_dump($array);die;
}
} else {
# 错误
$msg = $array['msg'] ?? 'error';
}
return array
(
'msg' => $msg,
'data' => $array,
'array' => $array,
);
}
//AES-128-ECB解密 data 字符串
public function query($order)
{
$request['customerId'] = $this->mid;
$request['orderId'] = $order['order_id'];
$request['timestamp'] = time();
$request['sign'] = $this->_sign($request, array('customerId', 'orderId','timestamp'));
$url = $this->host . 'cards/api/orderSearch';
$response = $this->curl('post', $url, $request,true);
$log['type'] = 'query_response';
$log['data'] = $response;
$log['config'] = $this->data;
$this->log($log);
$array = $this->json_decode($response);
// if (isset($array['data']['cards'])) {
$result = array();
// $result['status'] = 4;
if (isset($array['code']) && $array['code'] == '1') {
if (isset($array['data']['cards'])) {
$cardList = $array['data']['cards'];
$cardData = $this->aes_decrypt($cardList);
$cardArray = $this->json_decode($cardData);
$cardno = $cardArray[0]['cardno'];
$cardpwd = $cardArray[0]['cardkey'];
$kamidata = array
(
'cardno' => $cardno,
'cardpwd' => $cardpwd,
'expired' => $cardArray[0]['expirtime'] ?? '',
'value' => $array['data']['count_money'],
);
// var_dump($kamidata);die;
$this->upOrderResponse($kamidata, $order['merchant_order_id'], $order['response']);
$result['status'] = 7;
$result['kami'] = $kamidata;
} else {
$result['status'] = 4;
}
} else {
$result['status'] = 3;
}
// }
return $result;
}
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['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($request['productCode']== 'SHYKT02'){
#数汇02
$randomKami = $KamiService->getData($param['cash'],1, 1, true, false, 1);
}elseif($request['productCode']== 'SHYKT04'){
#数汇04
$randomKami = $KamiService->getData($param['cash'],1, 1, true, false, 2);
}elseif($request['productCode']== 'TDK01'){
#通兑01
$randomKami = $KamiService->getData($param['cash'],1, 1, true, false, 3);
}else{
$randomKami = null;
}
if ($randomKami) {
$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
];
$unlockcard = $this->unlockCard($randomKami_data['kami']);
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 {
$response = $this->curl('post', $url, $request, true);
$response = $this->response($response);
// var_dump($response);die;
}
//
if (isset($response['array']['data']['billno'])) {
$channel_order_id = $response['array']['data']['billno'];
}
if(!isset($param['account']) || !$param['account'] ){
$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;
}
public function upOrderResponse($kami,$merchant_order_id,$orderResponse)
{
$orderService = OrderService::instance();
if(isset($orderResponse)){
$response = json_decode($orderResponse,true);
}else{
$response = [];
}
$response['kami'] = $kami;
$data = ['response' => json_encode($response)];
$orderService->db()->where(array('merchant_order_id' => $merchant_order_id))->update($data);
}
}