385 lines
12 KiB
PHP
Raw Normal View History

2024-09-29 15:43:18 +08:00
<?php
namespace app\gateway\controller\api;
set_time_limit(0);
ini_set('memory_limit','512M');
ini_set('default_socket_timeout', -1); //不超时
use app\merchant\service\MerchantService;
use app\merchant\service\MerchantLogService;
use app\channel\service\ChannelService;
use app\merchant\service\OrderService;
use app\channel\service\CardService;
/**
* 测试接口 上线取消
* Class Test
* @package app\gateway\api
*/
class Test extends Core
{
# 是否检测数据
protected $check = false;
public function card()
{
CardService::instance()->upStatus();
}
# 提交卡密
public function addCard()
{
$csign = md5('shouka5devcn168shemic168');
$sign = input('sign');
$sid = input('sid');
$cnum = input('cnum');
$cpwd = input('cpwd');
$value = input('value');
if ($csign == $sign && $sid && $cnum && $cpwd && $value) {
$cardService = CardService::instance();
$cnum = explode(',', $cnum);
$cpwd = explode(',', $cpwd);
$value = explode(',', $value);
foreach ($cnum as $k => $v) {
$cardService->up(false, $v, $cpwd[$k], $value[$k], -1, 1, $sid);
}
echo 'ok';
} else {
echo 'error';
}
die;
}
# 测试 获取加密信息 上线注释
public function getSign()
{
$request = $this->getSignature();
// $this->yes($request);
ksort($this->input);
if(isset($this->input['controller'])){
unset($this->input['controller']);
}
if(isset($this->input['function'])){
unset($this->input['function']);
}
if ($this->proid && !str_contains($this->product, '_cash')) {
unset($this->input['cash']);
}
// var_dump($request);die;
$signature_string = '';
$empty =true;
$suffix = '';
foreach ($this->input as $k => $v) {
if (strstr($v, 'http') && !strstr($v, '%')) {
$v = urlencode($v);
}
// if()
if (!$empty) {
$signature_string .= $k . '=' . $v . '&';
} elseif (strlen($v) && $v != 'empty') {
$signature_string .= $k . '=' . $v . '&';
}
}
if ($suffix != '&') {
$signature_string = substr($signature_string, 0, -1) . $suffix;
}
// $string = http_build_query($request);
$request['string'] = $signature_string;
$this->yes($request);
// $request = $this->getSignature();
// $string = http_build_query($request);
// $request['string'] = $string;
// $this->yes($request);
}
# 把当前order表里的卡密倒出来
public function dao()
{
//{"uid":"1000113200033148512","itemId":"DC500","cardNo":"2110640046764755","cardPwd":"11479097403897704242","userId":"147","serialno":"Q202106285276756004600827","dtCreate":"20210628115927","sign":"762e4a8c5ceaa6e2285edeffab316395"}
$data = OrderService::instance()->db()->whereRaw('card_id > 0')->order('id asc')->select();
$card = \app\channel\service\CardService::instance();
if ($data) {
foreach ($data as $k => $v) {
$request = json_decode($v['request'], true);
if ($request && isset($request['cardNo']) && isset($request['cardPwd'])) {
$card_info = $card->getOne($request['cardNo'], $request['cardPwd']);
$msg = json_decode($v['channel_callback_msg'], true);
$msg = isset($msg['msg']) ? $msg['msg'] : 'error';
if ($v['status'] == 2) {
$use = 5;
$info = '充值成功';
} else {
$use = 7;
$info = '疑似:卡密充值失败';
}
if (!$card_info) {
$id = $card->up($request['cardNo'], $request['cardPwd'], $v['cash'], -1, $use);
} else {
$id = $card_info['id'];
}
if ($id > 0) {
OrderService::instance()->db()->where(array('id' => $v['id']))->update(array('card_id' => $id));
$order['card_id'] = $id;
$order['mid'] = $v['mid'];
$order['order_id'] = $v['order_id'];
$order['account'] = $v['account'];
$order['cash'] = $v['cash'];
$card->log($order, $use, $info, $msg);
}
}
}
}
}
public function query_card(): void
{
$this->input();
$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);
// $result['key'] =$this->key;
$result['token']=strtolower(md5(md5($this->key).$order));
$result['decrypt_card'] = $this->decrypt($result['card'], $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);
$result['token']=strtolower(md5(md5($this->key).$order));
// $result['key'] =$this->key;
$result['decrypt_card'] = $this->decrypt($result['card'], $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 submit()
{
$this->input();
$this->input['order'] = $this->createOrderId();
$request = \app\gateway\service\AuthService::get($this->input, $this->key);
$string = http_build_query($request);
$request['string'] = $string;
$host = $_SERVER['SERVER_NAME'];
$host = 'http://'.$host.'/gateway/api.handle/submit?' . $string;
echo $host;die;
file_get_contents($host);
echo '已提交';die;
}
# 测试,命令行提交
public function command()
{
if (isset($_SERVER["argv"][2]) && $_SERVER["argv"][2]) {
$order = $_SERVER["argv"][2];
} else {
$order = false;
}
if (!$order) {
exit('error');
}
$param = array();
# 1是mid2是产品key, 3是卡号4是金额5是短信代充充值卡卡号6是密码
$config = explode('_', $order);
if ($config[2] == 'dhcz') {
$param['mobile'] = $config[3];
} else {
$param['card'] = $config[3];
}
$param['cash'] = $config[4];
if (isset($config[5])) {
$param['cnum'] = $config[5];
}
if (isset($config[6])) {
$param['cpwd'] = $config[6];
}
$param['order'] = $this->createOrderId();
$result = \app\channel\service\ChannelService::instance()->use($config[1], $config[2], $param, true);
$this->yes($result);
}
protected function createOrderId()
{
if (function_exists('session_create_id')) {
return strtoupper(session_create_id());
} else {
$charid = strtoupper(md5(uniqid(mt_rand(), true)));
return substr($charid, 0, 8) . substr($charid, 8, 4) . substr($charid, 12, 4) . substr($charid, 16, 4) . substr($charid, 20, 12);
}
}
# 测试的商家回调接口
public function call()
{
$log['type'] = 'merchant_notify';
$log['request'] = input();
$this->log($log);
echo 'success';die;
echo 'fail';die;
}
# 测试给商户发通知
public function send()
{
$msg = '{"data":"{\"order_no\":\"1783a02c264bcc01af35d3885a5806a80b4982f5\",\"account_number\":\"1000111100021483339\",\"amount\":200,\"official_serial_number\":\"empty\",\"supplier_store\":\"empty\",\"sign\":\"2c76d1c2e1cda0053ece706a4d247811\",\"serial_number\":194541670597918720,\"merchant_id\":11505,\"notify_url\":\"http:\\\/\\\/api.qizhixiong.com\\\/gateway\\\/api.notify\\\/call?s_order=1783a02c264bcc01af35d3885a5806a80b4982f5&s_type=2\",\"status\":4}","cash":200,"status":3,"order_id":"1783a02c264bcc01af35d3885a5806a80b4982f5","notify_num":0}';
$msg = json_decode($msg, true);
$merchant = MerchantService::instance()->getInfo(1);
MerchantService::instance()->notify(1, $merchant, $msg);
print_r($msg);die;
}
# 同步order_id里的pid
public function getPidByOrderId()
{
MerchantLogService::instance()->getPidByOrderId();
echo 'success';die;
}
# 测试回调
public function testCall()
{
$cid = input('cid', 10025);
$request = '{"cpid":"83027","order_no":"21032915140683027554733742","mobile":"1000111100021483339512","amount":"200","status":"failed","sz_order_no":"","ret_para":"Q202103290204622510221821","sign":"4bd8330937797b074b834e8cd43e919c"}';
$request = json_decode($request, true);
$channelService = ChannelService::instance();
$channelService->call('notify', $cid, $request);
}
# 测试查找上游余额
public function testAccount()
{
$cid = input('cid', 10040);
$day = input('day');
$channelService = ChannelService::instance();
$account_data = $channelService->call('account', $cid, $day);
print_r($account_data);die;
}
# 计算当前订单表中的实际充值金额
public function act()
{
OrderService::instance()->actual();
}
# 计算当前订单表中的实际扣费
public function kou()
{
$day = input('day');
\app\merchant\service\OrderService::instance()->kou($day);
}
# 计算历史订单表中的实际扣费
public function kou_history()
{
$day = input('day');
\app\merchant\service\OrderHistoryService::instance()->kou($day);
}
# 删除重复数据
public function delcf()
{
$day = input('day');
\app\merchant\service\OrderHistoryService::instance()->delcf($day);
}
# 校正商户的余额
public function init()
{
MerchantService::instance()->init(date('Y-m-d'));
}
}