769 lines
23 KiB
PHP
Raw Normal View History

2024-09-29 15:43:18 +08:00
<?php
namespace app\channel\service\system;
use app\channel\service\KamiService;
use app\merchant\service\MerchantService;
use app\merchant\service\OrderService;
/**
* 骏网接口
* Class Junka
* @package app\channel\service
*/
class JunKa extends Core
{
# 这几个可以后台设置
protected $host = 'http://service.800j.com/';
protected $mid = 'dongfakeji';
protected $token = 'dfkj6688';
protected $api = '';
protected $aeskey = '383147446d6e4e6c';
protected $goodid = '';
# 新魔力卡
public function jkxml_cardbuy($param)
{
$check = array
(
// 'proid' => 'productCode',
'num' => 'product_num',
'card_kind'=>'card_kind'
);
$param['card_kind'] = '335';#新魔力卡种
$this->api = 'UCard/UCardFetchSubmit.aspx';
return $this->submit($param, $check);
// var_dump($param);die;
}
# 美橙卡
public function jkmc_cardbuy($param)
{
$check = array
(
// 'proid' => 'productCode',
'num' => 'product_num',
'card_kind'=>'card_kind'
);
$param['card_kind'] = '357';#新魔力卡种
$this->api = 'UCard/UCardFetchSubmit.aspx';
return $this->submit($param, $check);
// var_dump($param);die;
}
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['agent_id'] = $this->mid;
$request['bill_id'] = $param['order'];
$request['bill_time'] = date('YmdHis');
// $request['bill_id'] = $param['order'];
$request['product_code'] = $this->getGid($param['cash']);
$request['time_stamp'] = date('YmdHis');
$request['sign'] = $this->_sign($request, array('agent_id', 'bill_id', 'bill_time', 'product_code', 'product_num', 'card_kind','time_stamp'));
$url = $this->host . $this->api;
$KamiService = KamiService::instance();
2024-10-08 15:30:39 +08:00
if($request['card_kind']== '335'){
$randomKami = $KamiService->getData($param['cash'],1, 1, true, false, 1);
}elseif($request['card_kind']== '357'){
$randomKami = $KamiService->getData($param['cash'],1, 1, true, false, 2);
}else{
$randomKami = null;
}
// $randomKami = null;
2024-09-29 15:43:18 +08:00
// $randomKami = '';
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,
];
} else {
$response = $this->curl('get', $url, $request);
$response = $this->response($response);
// var_dump($response);die;
}
$channel_order_id = '';
if (isset($response['array']['jnet_bill_no'])) {
$channel_order_id = $response['array']['jnet_bill_no'];
}
if(isset($param['account'])){
}elseif(isset($response['kami']['cardno']) ){
$param['account'] = $response['kami']['cardno'];
}else{
$param['account'] = $request['card_kind'];
}
$this->create($param['order'], $channel_order_id, $param['merchant_order'], $param['cash'], $url, $request, $response, 1, $param['account']);
return $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 .= '|||'.$this->token;
// var_dump($signature_string);die;
// $signature_string = iconv("UTF-8", "gbk", $signature_string);
return md5($signature_string);
}
public function response($data)
{
$data = iconv('GBK', 'UTF-8', $data);
// $data = $this->charsetToUTF8($data);
$log['type'] = 'response';
$log['data'] = $data;
$log['config'] = $this->data;
$this->log($log);
// var_dump($expression);
// ret_code=0&ret_msg=提卡成功&agent_id=2206275&bill_id=B202403299393536297372079&jnet_bill_no=P240329495813913&par_price=10.000&purchase_amt=10.000&card_no_data=AEDC1230E76096DD903BDDE60D36411582E2827F86346921C85570A7F20BA040949EB10C395E1F2F247BFCBC16C75604BFB366F235934C0B&ext_param=&sign=803dd91bf19dbfda0a30671a8740e181
//$array = $this->json_decode($data);
// $str = "key1=value1&key2=value2";
// 按照 & 分割字符串
$pairs = explode('&', $data);
// 将每个键值对分割并创建数组
$array = [];
foreach ($pairs as $pair) {
// 分割键和值
list($key, $value) = explode('=', $pair);
// 解码键和值
$array[urldecode($key)] = urldecode($value);
}
if (!$array) {
$msg = 'error';
} elseif (isset($array['ret_code']) && $array['ret_code'] == '0' || $array['ret_code'] == '999') {
# 正确
if(isset($array['card_no_data'])){
$msg = 'yescard';
$kami = $this->decrypt_3des($array['card_no_data']);
$kami = explode(',', $kami);
// $kamidata = [
// 'cardno' => $kami[0],
// 'cardpwd' => $kami[1],
// 'expired' => $kami[2],
//
// ];
$array['kami'] = array
(
'cardno' => $kami[0],
'cardpwd' => $kami[1],
'expired' => $kami[2],
'value' => $array['par_price'],
);
}else{
$msg = 'ok';
}
} else {
# 错误
$msg = $array['ret_msg'] ?? 'error';
}
return array
(
'msg' => $msg,
'data' => $array,
'array' => $array,
);
}
public function decrypt_3des($encrypted_string,$aeskey = ''){
$cipher = "des-ede3"; // 3des-ebc 使用的是des-ede3算法
$mode = "ebc"; // 工作模式为ebc
$iv = ""; // EBC模式不需要IV留空即可
$json = $this->channel['describe'];
$array = $this->json_decode($json);
if(!$aeskey){
if(isset($array['aeskey'])){
$this->aeskey = $array['aeskey'];
}
}else{
$this->aeskey = $aeskey;
}
// if(isset($array['aeskey'])){
// $this->aeskey = $array['aeskey'];
// }
// $this->aeskey =
return openssl_decrypt(hex2bin($encrypted_string), $cipher, $this->aeskey, $options=OPENSSL_RAW_DATA, $iv); // 输出解密后的字符串
}
#换卡查询接口
public function cardbuy($param)
{
// var_dump($param);die;
$check = array
(
// 'proid' => 'productCode',
'num' => 'product_num',
);
$this->api = 'UCard/UCardFetchSubmit.aspx';
return $this->submit($param, $check);
}
# 查询接口
public function lockCard($order)
{
#先查卡的余额对不对
// die;
$replaceCard = $this->replaceCard($order);
// var_dump($replaceCard);die;
if($replaceCard['msg'] == 'ok'){
$KamiService = KamiService::instance();
$requestData = $this->json_decode($order['request']);
// $request['card_kind'] = ;
if($requestData['card_kind'] == '335'){
$upkami = $KamiService->upKami($replaceCard['array']['kami']['cardno'], $replaceCard['array']['kami']['cardpwd'], $replaceCard['array']['kami']['value'], $replaceCard['array']['kami']['expired'],1,$order['mid']);
}elseif ($requestData['card_kind'] == '357'){
$upkami = $KamiService->upKami($replaceCard['array']['kami']['cardno'], $replaceCard['array']['kami']['cardpwd'], $replaceCard['array']['kami']['value'], $replaceCard['array']['kami']['expired'],2,$order['mid']);
}
if($upkami){
return 'ok';
}else{
return 'old_ok';
}
}else{
return 'error';
}
// $test = $url.'?'.http_build_query($request_queryUcard);
// var_dump($test);die;
//
// $data = http_post($url,$request_queryUcard);
#===========================================需要人工介入确定解决,先行注释===========================
// $param = [];
// $param['lock_type'] = 2;
//
//
// $url = $this->host . 'UCard/UCardLock.aspx';
// $request=$param;
// $request['agent_id']=$this->mid;
// $request['account_type']='2';
//
//
// $request['card_no']=$cardno;
// $request['card_par_amt']=$cash;
// $request['card_style']='1';
// $request['time_stamp'] =date('YmdHis');
//
//
//
// $request['sign'] = $this->_sign($request, array('agent_id', 'account_type', 'card_no', 'card_par_amt', 'card_style', 'lock_type', 'time_stamp'));
// $response = http_get($url,$request);
// $response = iconv('GBK', 'UTF-8', $response);
// var_dump($response);die;
// ======================锁卡
// var_dump($response);die;
// ret_code=***&ret_msg=***&agent_id=***&card_no=***&card_par_amt=***&sign=***
// $pairs = explode('&', $response);
//
//// 将每个键值对分割并创建数组
// $array = [];
//
// foreach ($pairs as $pair) {
// // 分割键和值
// list($key, $value) = explode('=', $pair);
// // 解码键和值
// $array[urldecode($key)] = urldecode($value);
// }
// var_dump($array);die;
// if (!$array) {
//
// $msg = 'error';
// } elseif (isset($array['ret_code']) && $array['ret_code'] == '0' || $array['ret_code'] == '999') {
// # 正确
// $msg = 'ok';
//
//
// } elseif(isset($array['ret_msg']) && $array['ret_code'] == '1' && $array['ret_msg'] == '该卡已被锁定') {
// $msg = 'old_ok';
//
// }else{
// # 错误
// $msg = $array['ret_msg'] ?? 'error';
//
// }
// return $msg;
//
// if($msg=='ok' || $msg=='old_ok'){
//
//
// return $msg;
// }else{
//
// return 'error';
// }
// $replaceCard = $this->replaceCard($order);
// var_dump($replaceCard);die;
//
//
// #先换卡
// $replaceCard_query = $this->replaceCard_query($order['order_id']);
// var_dump($replaceCard_query);die;
2024-12-04 12:33:47 +08:00
}
public function UnlockCard($order,$desc = '')
{
$request = [
'agent_id' => $this->mid,
'account_type' => 2,
'lock_type' => 2,
];
$response = $this->json_decode($order['response']);
if(empty($response['kami'])){
return '未找到卡密';
}
$request['card_no'] = $response['kami']['cardno'];
$request['card_par_amt'] = $order['cash'].'.00';
$request['card_style'] = 1;
if($desc){
$request['desc'] = $desc;
}
$request['time_stamp'] = date('YmdHis');
$request['sign'] = $this->_sign($request, array('agent_id', 'account_type', 'card_no', 'card_par_amt', 'card_style', 'lock_type','time_stamp'));
// $url =
// $response = http_get($url,$request);
// $response = iconv('GBK', 'UTF-8', $response);
// var_dump($response);die;
// ======================锁卡
// var_dump($response);die;
// ret_code=***&ret_msg=***&agent_id=***&card_no=***&card_par_amt=***&sign=***
// $pairs = explode('&', $response);
//
//// 将每个键值对分割并创建数组
// $array = [];
//
// foreach ($pairs as $pair) {
// // 分割键和值
// list($key, $value) = explode('=', $pair);
// // 解码键和值
// $array[urldecode($key)] = urldecode($value);
// }
// var_dump($array);die;
// if (!$array) {
//
// $msg = 'error';
// } elseif (isset($array['ret_code']) && $array['ret_code'] == '0' || $array['ret_code'] == '999') {
// # 正确
// $msg = 'ok';
//
//
// } elseif(isset($array['ret_msg']) && $array['ret_code'] == '1' && $array['ret_msg'] == '该卡已被锁定') {
// $msg = 'old_ok';
//
// }else{
// # 错误
// $msg = $array['ret_msg'] ?? 'error';
//
// }
// return $msg;
//
// if($msg=='ok' || $msg=='old_ok'){
//
//
// return $msg;
// }else{
//
// return 'error';
// }
//
//
// $url = $this->host . 'UCard/UCardLock.aspx';
// $request=$param;
// $request['agent_id']=$this->mid;
// $request['account_type']='2';
2024-09-29 15:43:18 +08:00
}
# 提交数据
public function replaceCard($order){
$request=[];
$request['agent_id'] = $this->mid;
$request['version'] = 1;
$request['bill_id'] = $order['order_id'].'_R';
$request['time_stamp'] = $request['bill_time'] = date('YmdHis');
$requestData = $this->json_decode($order['request']);
$request['card_kind'] = $requestData['card_kind'];
$request['card_style'] = 1;
$request['total_amt'] = $request['card_price'] = $order['cash'].'.00';
$response = $this->json_decode($order['response']);
if(empty($response['kami'])){
return '未找到卡密';
}
$kami_data = $response['kami']['cardno'].','. $response['kami']['cardpwd'].','.$request['card_price'];
$aeskey = '';
$json = $this->channel['other_data'];
$array = $this->json_decode($json);
if(is_array($array) && isset($array['replaceCard_aeskey'])){
if(isset($aeskey)){
$aeskey = $array['replaceCard_aeskey'];
}
}
if(is_array($array) && isset($array['replaceCard_token'])){
$this->token = $array['replaceCard_token'];
}
$request['card_data'] = $this->aes_encrypt($kami_data,$aeskey);
// var_dump($request);die;
$request['sign'] = $this->_sign($request, array('agent_id', 'bill_id', 'bill_time', 'card_data', 'card_kind', 'card_price','card_style','time_stamp','version'));
$url = $this->host . 'UCard/UCardExchargeSubmit.aspx';
// $test =$url.'?'.http_build_query($request);
// $test=$url.'?' ;
// var_dump($test);die;
$data = http_post($url,$request);
$data = iconv('GBK', 'UTF-8', $data);
$log['type'] = 'replaceCard_response';
$log['data'] = $data;
$log['config'] = $this->data;
$this->log($log);
// #"ret_code=0&ret_msg=成功&agent_id=2206307&bill_id=B202404050125748573224716&jnet_bill_no=&fee_amt=&total_amt=&card_data=&ext_param=&sign=82edf84b09b9455d8dae5f886dde91f6"
// $data= 'ret_code=0&ret_msg=成功&agent_id=2206307&bill_id=B202404050125748573224716_R&jnet_bill_no=144&fee_amt=0.00&total_amt=20.00&card_data=03623CFF34E2F24358299BD5464C49EDD8290D278101DC2CF1260FCEE6B2F87425AAB1E411619CBBCF5B523D6395619D5F14F084993FF15B&ext_param=&sign=95999c013384c7c3fc52c5c4f2eb3138';
// var_dump($data);die;
$pairs = explode('&', $data);
$array = [];
// 创建一个DateTime对象初始化为2000年1月1日
foreach ($pairs as $pair) {
// 分割键和值
list($key, $value) = explode('=', $pair);
// 解码键和值
$array[urldecode($key)] = urldecode($value);
}
if (!$array) {
$msg = 'error';
} elseif (isset($array['ret_code']) && $array['ret_code'] == '0' || $array['ret_code'] == '999') {
# 正确
if(isset($array['card_data'])){
$msg = 'ok';
$kami = $this->decrypt_3des($array['card_data'],$aeskey);
$kami = explode(',', $kami);
$tempdate=substr($kami[3], 0, 4).'-'.substr($kami[3], 4, 2).'-'.substr($kami[3], 6, 2).' '.substr($kami[3], 8, 2).':'.substr($kami[3], 10, 2).':'.substr($kami[3], 12, 2);
$array['kami'] = array
(
'cardno' => $kami[0],
'cardpwd' => $kami[1],
'expired' => $tempdate,
'value' => $kami[2],
);
}else{
$msg = 'no_data';
}
} else {
# 错误
$msg = $array['ret_msg'] ?? 'error';
}
return array
(
'msg' => $msg,
'data' => $array,
'array' => $array,
);
}
private function aes_encrypt($data,$aeskey = '') {
$data = iconv('UTF-8', 'GBK', $data);
$json = $this->channel['describe'];
$array = $this->json_decode($json);
if(!$aeskey){
if(isset($array['aeskey'])){
$this->aeskey = $array['aeskey'];
}
}else{
$this->aeskey = $aeskey;
}
$encrypted = openssl_encrypt($data, 'des-ede3', $this->aeskey , OPENSSL_RAW_DATA);
return bin2hex($encrypted);
}
public function replaceCard_query($order_id)
{
$request['agent_id'] = $this->mid;
$request['bill_id'] = $order_id;
$request['time_stamp'] =date('YmdHis');
$aeskey = '';
$json = $this->channel['other_data'];
$array = $this->json_decode($json);
if(is_array($array) && isset($array['replaceCard_aeskey'])){
if(isset($aeskey)){
$aeskey = $array['replaceCard_aeskey'];
}
}
if(is_array($array) && isset($array['replaceCard_token'])){
$this->token = $array['replaceCard_token'];
}
$request['sign'] = $this->_sign($request, array('agent_id', 'bill_id', 'time_stamp'));
$url = $this->host . 'UCard/UCardExchargeQuery.aspx';
// var_dump($url);die;
$response = http_get($url, $request);
$response = iconv('GBK', 'UTF-8', $response);
// var_dump($response);die;
// ret_code=0&ret_msg=成功&agent_id=2206307&bill_id=B202404050125748573224716_R&jnet_bill_no=144&fee_amt=0.00&total_amt=20.00&card_data=03623CFF34E2F24358299BD5464C49EDD8290D278101DC2CF1260FCEE6B2F87425AAB1E411619CBBCF5B523D6395619D5F14F084993FF15B&ext_param=&sign=95999c013384c7c3fc52c5c4f2eb3138
// var_dump($response);die;
$log['type'] = 'replaceCard_query_response';
$log['data'] = $response;
$log['config'] = $this->data;
$this->log($log);
$pairs = explode('&', $response);
$array = [];
foreach ($pairs as $pair) {
// 分割键和值
list($key, $value) = explode('=', $pair);
// 解码键和值
$array[urldecode($key)] = urldecode($value);
}
// $result = array();
// $result['status'] = 4;
if (isset($array['ret_code']) && $array['ret_code'] == '0') {
if(isset($array['card_data'])){
$msg = 'yescard';
$kami = $this->decrypt_3des($array['card_data'],$aeskey);
$kami = explode(',', $kami);
$kamidata = [
'cardno' => $kami[0],
'cardpwd' => $kami[1],
'value'=>$kami[2],
'expired' => $kami[3],
];
$array['kami'] = $kamidata;
}else{
$msg = 'ok';
}
} else{
$msg = $array['ret_msg'] ?? 'error';
}
if (isset($array['jnet_bill_no']) && $array['jnet_bill_no']) {
# 流水号
// $result['status'] = 3;
$array['s_nubmer'] = $array['jnet_bill_no'] ;
}
return array
(
'msg' => $msg,
'data' => $array,
'array' => $array,
);
}
public function query($order)
{
$orderRequest = json_decode($order['request'], true);
$request['agent_id'] = $this->mid;
$request['bill_id'] = $order['order_id'];
$request['time_stamp'] =date('YmdHis');
$request['sign'] = $this->_sign($request, array('agent_id', 'bill_id', 'time_stamp'));
$url = $this->host . 'UCard/UCardFetchQuery.aspx';
$response = $this->curl('get', $url, $request);
$response = iconv('GBK', 'UTF-8', $response);
$log['type'] = 'query_response';
$log['data'] = $response;
$log['config'] = $this->data;
$this->log($log);
$pairs = explode('&', $response);
$array = [];
foreach ($pairs as $pair) {
// 分割键和值
list($key, $value) = explode('=', $pair);
// 解码键和值
$array[urldecode($key)] = urldecode($value);
}
$result = array();
// $result['status'] = 4;
if (isset($array['ret_code']) && $array['ret_code'] == '0') {
if(isset($array['card_no_data'])){
$kami = $this->decrypt_3des($array['card_no_data']);
$kami = explode(',', $kami);
$kamidata = [
'cardno' => $kami[0],
'cardpwd' => $kami[1],
'expired' => $kami[2],
];
$this->upOrderResponse($kamidata,$order['merchant_order_id'],$order['response']);
$result['status'] = 7;
$result['kami'] = $kamidata;
}else{
$result['status'] = 4;
}
} else{
$result['status'] = 3;
}
if (isset($array['jnet_bill_no']) && $array['jnet_bill_no']) {
# 流水号
// $result['status'] = 3;
//$data['s_nubmer'] = $result['s_nubmer'] = $data['result']['code'];
}
return $result;
}
public function upOrderResponse($kami,$merchant_order_id,$orderResponse)
{
$orderService = OrderService::instance();
$response = json_decode($orderResponse,true);
$response['kami'] = $kami;
$data = ['response' => json_encode($response)];
$orderService->db()->where(array('merchant_order_id' => $merchant_order_id))->update($data);
}
}