b38d9b8e40 test(kami): 测试解锁 JC 卡密
- 在解锁卡密的过程中添加日志记录,以便调试和追踪
- 将响应数据从 GBK 编码转换为 UTF-8 编码,确保兼容性
2025-06-06 14:04:36 +08:00

573 lines
16 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\channel\service\system;
use app\channel\service\KamiService;
use app\merchant\service\MerchantService;
use app\merchant\service\OrderService;
use dever\Log;
use think\facade\Db;
/**
* 骏网接口--汇元重构版本--无换卡逻辑
* Class HyJk
*
* @package app\channel\service
*/
class HyJk2 extends Core
{
# 这几个可以后台设置
protected $host = '';
protected $mid = '';
protected $token = '';
protected $api = '';
protected $aeskey = '';
# 通用卡,只需后台设置卡种
public function cardbuy($param)
{
$check = array
(
'num' => 'product_num',
);
$this->api = 'UCard/UCardFetchSubmit.aspx';
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['agent_id'] = $this->mid;
$request['bill_id'] = $param['order'];
$request['bill_time'] = date('YmdHis');
$request['card_kind']= $this->getGid($param['cash']);
$request['product_code'] = $this->GoodId($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();
$kami_group_id = $this->product['cid'].'00'.$this->product['service_id'];
#通过后台其他参数确定参与换卡
# Secondary_sales_type二次销售类型1为全部支持二销2为部分支持二销
$channel_other_data = $this->channel['other_data'];
$channel_other_data_array = json_decode($channel_other_data, true);
$randomKami = null;
if(is_array($channel_other_data_array) && isset($channel_other_data_array['Secondary_sales_type'])){
$m_id = $this->data['merchant'];
if($channel_other_data_array['Secondary_sales_type'] == 1) {
// var_dump($m_id);die;
$randomKami = $KamiService->getData($param['cash'],$m_id, 1, true, false, $kami_group_id);
if(!$randomKami){
$randomKami = $KamiService->getData($param['cash'],$m_id, 1, true, false, '100'.$this->product['service_id']);
}
}elseif($channel_other_data_array['Secondary_sales_type'] == 2){
$Secondary_sales= $channel_other_data_array['Secondary_sales'] ?? false;
if($Secondary_sales && in_array($this->product['service_id'], $Secondary_sales)){
$randomKami = $KamiService->getData($param['cash'],$m_id, 1, true, false, $kami_group_id);
if(!$randomKami){
$randomKami = $KamiService->getData($param['cash'],$m_id, 1, true, false, '100'.$this->product['service_id']);
}
}
}
}
if ($randomKami) {
#解锁卡密
$cards = [
'cardno' => $randomKami['cnum'],
'cardpwd' => $randomKami['cpwd'],
];
$unlocks = $this->UnLockCards($cards, $param['cash']);
if($unlocks == 'ok'){
$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{
#解锁失败
$data = array();
$data['cnum'] = $randomKami['cnum'];
$data['cpwd'] = $randomKami['cpwd'];
$data['value'] = $randomKami['value'];
$KamiService->db()->where($data)->update(['status'=>0,'use_info'=>'解锁失败,请核验']);
// var_dump(223);die;
$response = $this->curl('get', $url, $request);
$response = $this->response($response);
}
} else {
// var_dump(223);die;
$response = $this->curl('get', $url, $request);
$response = $this->response($response);
}
$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 GoodId($cash)
{
$goodId = [
'10' => 'AAJWVVXKYK010FV',
'15' => 'AAJWVVXKYK015FV',
'20' => 'AAJWVVXKYK020FV',
'30' => 'AAJWVVXKYK030FV',
'50' => 'AAJWVVXKYK050FV',
'100' => 'AAJWVVXKYK100FV',
'120' => 'AAJWVVXKYK120FV',
'200' => 'AAJWVVXKYK200FV',
'500' => 'AAJWVVXKYK500FV',
'1000' => 'AAJWVVXKYK1K0FV'
];
return $goodId[$cash] ?? '';
}
# 数据响应格式处理
private function _sign($request, $param)
{
$signature_string = '';
foreach ($param as $k => $v) {
$signature_string .= $v . '=' . $request[$v] . '&';
}
$signature_string = substr($signature_string, 0, -1);
$signature_string .= '|||'.$this->token;
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);
$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);
$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;
}
return openssl_decrypt(hex2bin($encrypted_string), $cipher, $this->aeskey, $options=OPENSSL_RAW_DATA, $iv); // 输出解密后的字符串
}
#换卡查询接口
# 查询接口
public function lockCard($order)
{
$lockCard = $this->lockCards($order,'首次锁定');
// var_dump($lockCard);die;
if($lockCard == 'ok'){
$KamiService = KamiService::instance();
$response = $this->json_decode($order['response']);
$cardno = $response['kami']['cardno'];
$cardpwd = $response['kami']['cardpwd'];
$expired = $response['kami']['expired'];
$value = $response['kami']['value'];
$where['id'] = $order['pid'];
$where['status'] = 1;
$channel_product = Db::name('channel_product')->where($where)->find();
$service_id = $channel_product['service_id'];
$kami_group_id = $order['cid'].'00'.$service_id;
$upkami = $KamiService->upKami($cardno, $cardpwd, $value, $expired,$kami_group_id,$order['mid']);
if($upkami){
return 'ok';
}else{
$KamiService->db()->where(['cnum'=>$cardno,'cpwd'=>$cardpwd,'value'=>$value])->update(['use'=>1,'use_mid'=>$order['mid']]);
$logss['type'] = 'Restorage';
$logss['data'] = [
'cardno'=>$cardno,
'cpwd'=>$cardpwd,
'value'=>$value,
'order_id'=>$order['order_id'],
];
Log::write('kami', 'Secondary_sales', $logss);
return 'old_ok';
}
}else{
return 'error';
}
}
public function UnlockCard($order,$desc = '')
{
return $this->lockCards($order,'解锁',2);
}
public function lockCards($order,$desc = '',$lock_type = 1)
{
$request = [
'agent_id' => $this->mid,
'card_from_type' => 0,
'lock_type' => $lock_type,
'lock_amt_fen'=>$order['cash']*100,
];
$response = $this->json_decode($order['response']);
if(empty($response['kami'])){
return '未找到卡密';
}
$kami_data = $response['kami']['cardno'].'='. $response['kami']['cardpwd'];
$request['card_data'] = $this->aes_encrypt($kami_data);
$request['card_style'] = 1;
if($desc){
$request['desc'] = $desc;
}
$request['time_stamp'] = date('YmdHis');
$request['sign'] = $this->_sign($request, array('agent_id', 'card_data', 'card_style', 'lock_amt_fen', 'lock_type', 'time_stamp'));
$url = $this->host.'UCard/UCardJPointLock.aspx';
$response = http_get($url,$request);
// var_dump($request);die;
$response = iconv('GBK', 'UTF-8', $response);
// var_dump($response);die;
$pairs = explode('&', $response);
$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' ) {
# 正确
if ((isset($array['total_amt_fen']) && ($array['total_amt_fen'] == $array['current_lock_amt_fen']) && $array['total_amt_fen'] == $request['lock_amt_fen']))$msg = 'ok';
else $msg = '金额错误';
} elseif(isset($array['ret_msg']) && $array['ret_code'] == '1' && $array['ret_msg'] == '该卡已被锁定' && ($array['total_amt_fen'] == $array['current_lock_amt_fen']) ) {
$msg = 'old_ok';
}else{
# 错误
$msg = $array['ret_msg'] ?? 'error';
}
return $msg;
}
# 提交数据
public function UnLockCards($cards,$cash){
$request = [
'agent_id' => $this->mid,
'card_from_type' => 0,
'lock_type' => 2,
'lock_amt_fen'=>$cash*100,
];
$kami_data = $cards['cardno'].'='. $cards['cardpwd'];
$request['card_data'] = $this->aes_encrypt($kami_data);
$request['card_style'] = 1;
$request['desc'] = '二销解锁';
$request['time_stamp'] = date('YmdHis');
$request['sign'] = $this->_sign($request, array('agent_id', 'card_data', 'card_style', 'lock_amt_fen', 'lock_type', 'time_stamp'));
$url = $this->host.'UCard/UCardJPointLock.aspx';
$response = http_get($url,$request);
$response = iconv('GBK', 'UTF-8', $response);
Log::write('kami', 'test_unlock_jc', $request);
Log::write('kami', 'test_unlock_jc', $response);
$pairs = explode('&', $response);
$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' ) {
if ((isset($array['total_amt_fen']) && ($array['total_amt_fen'] == $array['current_lock_amt_fen']) && $array['total_amt_fen'] == $request['lock_amt_fen']))$msg = 'ok';
else $msg = '金额错误';
} else{
# 错误
$msg = $array['ret_msg'] ?? 'error';
}
return $msg;
}
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 base64_encode($encrypted);
}
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);
}
}