24a4b60517 refactor(gateway): 重构 v4 版本接口签名验证逻辑
- 在 AuthService 中添加对 sign 和 signature 参数的处理
- 更新 Core 控制器中的签名验证逻辑
- 新增 Goods 控制器,实现商品列表查询功能
- 修改 Handle 控制器的日志记录逻辑
- 优化 JunKa、Lingshi 等渠道服务类的实现
- 更新 MerchantService 中的回调处理逻辑
-调整 OrderService 中的订单查询和更新逻辑
- 新增 Qiling 和 Reapi 渠道服务类
2025-04-17 17:21:00 +08:00

281 lines
7.8 KiB
PHP

<?php
namespace app\gateway\controller\v4;
use app\channel\service\CardService;
/**
* 接口处理
* Class Handle
* @package app\gateway\api
*/
class Giftcard extends Core
{
# 是否检测数据
protected bool $check = true;
# 查询接口 一般用于查询数据,同步执行
# 提交接口 一般用于提交数据,异步执行
public function submit(): void
{
// $input = input();
//\dever\Log::write('gateway', 'request', $input);
$settingStatus = sysconf('settingStatus');
if ($settingStatus == 1) {
$this->no(-1000);
}
$this->checkOpenTime(sysconf('settingOpenTime'));
if (isset($this->merchant['opentime']) && $this->merchant['opentime']) {
$this->checkOpenTime($this->merchant['opentime']);
}
if(!isset($this->input['account'])){
$this->input['account'] = '卡密默认空账户';
}
$data = $this->channel($this->mid, $this->product, true);
$this->yes($data);
}
# 提交接口 一般用于提交数据,异步执行
public function dc_submit(): void
{
$this->submit();
}
# 查询订单接口
public function query(): void
{
$order = input('order_id');
$merchant_order = input('merchant_order_id');
if (!$order && !$merchant_order) {
$this->no(-100);
}
$data = $this->getOrder($order, $merchant_order);
if (!$data) {
$this->no(-101);
}
if(!$order){
$order = $data['order_id'];
}
$result['cash'] = $data['cash'];
$result['order_id'] = $data['order_id'];
$result['merchant_order_id'] = $data['merchant_order_id'];
$result['card']='';
if ($data['status'] < 0) {
$data['status'] = 1;
}
$result['status'] = $data['status'];
if(in_array($data['status'], [2,7])){
$response = json_decode($data['response'],true);
if(isset($response['kami'])) {
$cardno = $response['kami']['cardno'];
$cardpwd = $response['kami']['cardpwd'];
$expire_time= $response['kami']['expired'];
$kami = [
'cardno' =>$cardno,
'cardpwd' =>$cardpwd,
'expire_time' =>$expire_time,
];
$kami=implode(',',$kami);
$result['card']=$this->set_seacret($kami,$order);
$this->yes($result);
}elseif(isset($data['channel_callback_msg']) && str_contains($data['channel_callback_msg'], "kami")){
$msg = json_decode($data['channel_callback_msg'],true);
$cardno = $msg['kami']['cardno'];
$cardpwd = $msg['kami']['cardpwd'];
$expire_time= $msg['kami']['expired'];
$kami = [
'cardno' =>$cardno,
'cardpwd' =>$cardpwd,
'expire_time' =>$expire_time,
];
$kami=implode(',',$kami);
$result['card']=$this->set_seacret($kami,$order);
$this->yes($data);
}
}
$this->yes($result);
}
public function set_seacret($kami,$order_id){
$seacret=strtolower(md5(md5($this->key).$order_id));
// var_dump($seacret);die;
$v=substr($seacret, 0,strlen($seacret)/8);
$str=openssl_encrypt($kami, 'DES-EDE3', $seacret, OPENSSL_RAW_DATA);
// var_dump($str);die;
return base64_encode($str);
}
public function decrypt($str,$order_id){
$str = base64_decode($str);
$seacret=strtolower(md5(md5($this->key).$order_id));
$str=openssl_decrypt($str, 'DES-EDE3', $seacret, OPENSSL_RAW_DATA);
return $str;
}
# 查询账户接口
public function account(): void
{
$data = $this->merchant;
$result['name'] = $data['name'];
$result['account'] = $data['account_surplus'];
$result['credit'] = $data['credit_surplus'];
$result['appid'] = $data['appid'];
$this->yes($result);
}
# 根据日期查询账户数据
public function accountByDay(): void
{
$this->merchantLogService = \app\merchant\service\MerchantLogService::instance();
$orderService = \app\merchant\service\OrderService::instance();
$data = $this->merchant;
$day = input('day');
if (!$day) {
$day = date('Y-m-d', strtotime('-1 day'));
}
$begin = $day . ' 00:00:00';
$after = $day . ' 23:59:59';
$time = [$begin, $after];
# 获取昨天
$yday = $day . ' 00:00:00';
$yday = $this->maketime($yday);
$yday = date('Y-m-d', strtotime('-1 day', $yday));
$begin = $yday . ' 00:00:00';
$after = $yday . ' 23:59:59';
$stime = [$begin, $after];
$where['mid'] = $data['id'];
# 加款
$where['type'] = 1;
$chongzhi = $this->merchantLogService->getDb($where, $time);
$where['type'] = 2;
$jianshao = $this->merchantLogService->getDb($where, $time);
$add = round($chongzhi - $jianshao, 2);
# 冻结
$dong = $orderService->getDong($where['mid'], $time);
$result['name'] = $data['name'];
$result['account'] = $this->merchant_yue($where['mid'], $time);
$yaccount = $this->merchant_yue($where['mid'], $stime);
$result['dong'] = $dong;
$result['add'] = $add;
$where['type'] = 3;
$kou = $this->merchantLogService->getDb($where, $time);
$where['type'] = 4;
$huifu = $this->merchantLogService->getDb($where, $time);
$kou -= $huifu;
$result['zhang'] = round($yaccount + $result['add'] - $kou, 2);
$this->yes($result);
}
# 计算商户的的一天的余额
private function merchant_yue($mid, $time): float
{
$time[0] = '2020-01-01 00:00:00';
$where['mid'] = $mid;
$where['account_type'] = 1;
$where['oper'] = 1;
$shouru = $this->merchantLogService->getDb($where, $time);
$where['oper'] = 2;
$zhichu = $this->merchantLogService->getDb($where, $time);
$yue = round($shouru - $zhichu, 2);
unset($where['oper']);
$where['account_type'] = 2;
$where['type'] = 3;
$kou = $this->merchantLogService->getDb($where, $time);
$where['type'] = 4;
$huifu = $this->merchantLogService->getDb($where, $time);
$where['type'] = 1;
$zonghuikuan = $this->merchantLogService->getDb($where, $time);
$where['type'] = 2;
$zonghuifu = $this->merchantLogService->getDb($where, $time);
$zonghuikuan -= $zonghuifu;
$hui = round($kou - $huifu - $zonghuikuan, 2);
return $yue - $hui;
}
protected function maketime($v): float|false|int|string
{
if (!$v) {
return '';
}
if (is_numeric($v)) {
return $v;
}
if (is_array($v)) {
$v = $v[1];
}
if (strstr($v, ' ')) {
$t = explode(' ', $v);
$v = $t[0];
$s = explode(':', $t[1]);
} else {
$s = array(0, 0, 0);
}
if (!isset($s[1])) {
$s[1] = 0;
}
if (!isset($s[2])) {
$s[2] = 0;
}
if (strstr($v, '-')) {
$t = explode('-', $v);
} elseif (strstr($v, '/')) {
$u = explode('/', $v);
$t[0] = $u[2];
$t[1] = $u[0];
$t[2] = $u[1];
}
if (!isset($t)) {
$t = array(0, 0, 0);
}
if (!isset($t[1])) {
$t[1] = 0;
}
if (!isset($t[2])) {
$t[2] = 0;
}
$v = mktime($s[0], $s[1], $s[2], $t[1], $t[2], $t[0]);
return $v;
}
}