359 lines
9.7 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;
use http\Exception;
/**
* 智汇优享
* Class ZHIhui
* @package app\channel\service
*/
class Zhihui extends Core
{
# 这几个可以后台设置
protected $host = '';
protected $mid = '';
protected $token = '';
protected $api = '';
protected $aeskey = '';
protected $goodid = '';
private const KEY_ALGORITHM = 'AES-256-ECB';
private const CHARSET_NAME = 'UTF-8';
# 新魔力卡
public function walmart_cardbuy($param)
{
$check = array
(
'proid' => 'productCode',
// 'proid' => 'productCode',
);
$this->api = 'stock/apiMerchant/extractCardV3';
return $this->submit($param, $check);
// var_dump($param);die;
}
public function cardbuy($param)
{
#在这个方法开始
// var_dump($param);die;
$check = array
(
'proid' => 'productCode',
// 'proid' => 'productCode',
);
$this->api = 'stock/apiMerchant/extractCardV3';
return $this->submit($param, $check);
}
# 数据响应格式处理
public function response($data,$cash = '')
{
// $data = $this->charsetToUTF8($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'] == '0' || $array['code'] == '-1') {
# 正确
if(isset($array['data']['cardData'])){
$msg = 'yescard';
$kami = $this->decrypt_3des($array['data']['cardData']);
// var_dump($kami);die;
#等待
$kami = explode(',', $kami);
// $kamidata = [
// 'cardno' => $kami[0],
// 'cardpwd' => $kami[1],
// 'expired' => $array['expireTime'],
////
// ];
$array['kami'] = array
(
'cardno' => $kami[0],
'cardpwd' => $kami[1],
'expired' => $array['data']['expireTime'],
// 'value' => 1,
);
if(isset($cash)){
$array['kami']['value'] = $cash;
}
}else{
$msg = 'ok';
}
} else {
# 错误
$msg = $array['ret_msg'] ?? 'error';
}
// die;
return array
(
'msg' => $msg,
'data' => $array,
'array' => $array,
);
}
#锁卡接口
public function lockCard($order)
{
#先查卡的余额对不对
// die;
}
# 查询接口
public function query($order)
{
$orderRequest = json_decode($order['request'], true);
$request['token'] = $this->mid;
#todo订单号是哪个字段
$request['merOrderCode'] = $order['order_id'];
$request['timestamp'] =time();
$request['sign'] = $this->_sign($request, array('token', 'merOrderCode', 'timestamp'));
$url = $this->host . 'apiMerchant/queryOrder';
$response = $this->curl('post', $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($response['code']==0&&$response['status']){
if(isset($response['data']['cardData'])&&$response['data']['cardData']){
$kami = $this->decrypt_3des($response['data']['cardData']);
$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;
}
$kami=$this->decrypt_3des($response['data']['cardData']);
}else{
$result['status'] = 3;
}
// 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;
}
# 提交数据
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['token'] = $this->mid;
$request['merOrderCode'] = $param['order'];
$request['productCode'] = $this->getGid($param['cash']);
$request['expireTime'] = date('YmdHis');
// $request['bill_id'] = $param['order'];
$request['startTime'] = date('YmdHis');
$request['timestamp'] = round(microtime(true) * 1000);
$request['sign'] = $this->_sign($request, array('token', 'merOrderCode', 'productCode', 'expireTime', 'startTime','timestamp'));
$url = $this->host . $this->api;
$response = $this->curl('post', $url, $request,true);
// var_dump($response);die;
// $response = '{"status":true,"msg":"成功","code":"0","data":{"cardData":"ZEdWemRGOHdNREZmT1RrNFh6RXhORFFzZEdWemRGOHdNREZmT1RrNFh6RXhORFE9ZjYyZWNhMDg1NzJlOWMxNGRkMDEwOTU3MGQ5NTQzNzI=","productCode":"1678945347","expireTime":"2025-12-28
//12:00:00","startTime":"2023-12-27 08:13:39"},"requestId":"8ed0fa5ddf3d4a01800a7c486114c38c"}';
$response = $this->response($response,$param['cash']);
// if(isset($response['']))
// var_dump($response);die;
$channel_order_id = '';
if (isset($response['array']['requestId'])) {
$channel_order_id = $response['array']['requestId'];
}
if(isset($param['account'])){
}elseif(isset($response['kami']['cardno']) ){
$param['account'] = $response['kami']['cardno'];
}else{
$param['account'] = $request['product_code'];
}
$this->create($param['order'], $channel_order_id, $param['merchant_order'], $param['cash'], $url, $request, $response, 1, $param['account']);
return $response['msg'];
}
public function decrypt_3des($encrypted_string){
// $json = $this->channel['describe'];
// $array = $this->json_decode($json);
// if(isset($array['aeskey'])){
// $this->key = $array['aeskey'];
// }
//加密
return base64_decode(str_replace($this->token,"",base64_decode($encrypted_string)));
}
private function aes_encrypt($data) {
}
public function send_post($notify_url, $post_data,$type)
{
// $postdata = http_build_query($post_data);
$options = array(
'http' => array(
'method' => $type,
'header' => 'Content-type:application/json',
'content' => $post_data,
'timeout' => 15 * 60 // 超时时间(单位:s
)
);
$context = stream_context_create($options);
$result = file_get_contents($notify_url, false, $context);
return $result;
}
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 strtolower(md5(strtoupper(md5($signature_string))));
}
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);
}
}