733 lines
20 KiB
PHP
Raw Normal View History

2024-09-29 15:43:18 +08:00
<?php
namespace app\openapi\controller;
use app\channel\service\ChannelService;
use app\gateway\service\CurlService;
use app\gateway\service\RedisService as redisObj;
use app\merchant\service\MerchantService;
use app\merchant\service\OrderHistoryService;
use app\merchant\service\OrderService;
use app\order\service\Kami91OrderService;
use think\admin\Controller;
use app\core\Service as CoreService;
use think\facade\Db;
use think\facade\Log;
class Kami91 extends Controller
{
# 是否检测数据
// protected bool $check = false;
protected $merchant_id;
# code码定义
protected $code = array
(
# 成功
1 => 'ok',
# 小于0为失败
0 => '暂时未处理',
-1 => 'appid为空',
-2 => '产品错误',
-3 => 'appid无效',
-4 => '余额不足或者没有传入价格',
-5 => 'signature不能为空',
-6 => 'nonce不能为空',
-7 => 'time不能为空',
-8 => 'signature已失效',
-9 => 'signature验证失败',
-100 => '请求错误',
-101 => '订单不存在',
-102 => '订单号重复',
-103 => '正在处理中',
-104 => '锁卡失败',
-1000 => '系统维护中',
);
# 查询接口 一般用于查询数据,同步执行
# 提交接口 一般用于提交数据,异步执行
public function query()
{
$data = input();
if(empty($data['api_product']) || $data['api_product'] != 'query'){
$this->no(-2);
}
if (empty($data['appid'])) {
$this->no(-1);
}
if (empty($data['merchant_order_id'])) {
$this->no(-100);
}
// http_post($url, $data)
$getMerchantInfo = MerchantService::instance()->get($data['appid']);
// $mid = $getMerchantInfo['id'];
$info = Kami91OrderService::instance()->db()->where(['merchant_order_id' => $data['merchant_order_id']])->find();
if(!$info){
$this->no(-100);
}
$order = $this->getOrder('', $data['merchant_order_id'] ,$getMerchantInfo['id']);
if(!$order){
if(isset($info['create_at'] )){
$timestamp = strtotime($info['create_at']);
$now = time();
$time = $timestamp + 60*10 ;
if($now < $time){
$this->no();
}
}
$this->no(-101);
}
if($order['status'] == 2){
if(empty($info['cardno'])){
$response = json_decode($order['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,
];
$where = [
'order_id' => $order['order_id']
];
$result = Kami91OrderService::instance()->up($where,$kami);
$data = [
'CardNum' => $cardno,
'CardPassword' => $cardpwd,
'expireDate' => $expire_time
];
return $this->yes($data);
}elseif(isset($order['channel_callback_msg']) && str_contains($order['channel_callback_msg'], "kami")){
$msg = json_decode($order['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,
];
$where = [
'order_id' => $order['order_id']
];
$result = Kami91OrderService::instance()->up($where,$kami);
$data = [
'CardNum' => $cardno,
'CardPassword' => $cardpwd,
'expireDate' => $expire_time
];
return $this->yes($data);
}else{
if(isset($info['create_at'] )){
$timestamp = strtotime($info['create_at']);
$now = time();
$time = $timestamp + 60*10 ;
if($now < $time){
$this->no();
}
}
$this->no(-103);
}
}
$data = [
'CardNum' => $info['cardno'],
'CardPassword' => $info['cardpwd'],
'expireDate' => $info['expire_time']
];
}elseif ($order['status'] == '7'){
if($info['status'] == '1' || $info['status'] == '4'){
$data =array();
$data['order_id'] = $order['order_id'];
$data['status'] = 6;
Kami91OrderService::instance()->db()->where(array('merchant_order_id' => $order['merchant_order_id']))->update($data);
}
// $this->huidiao($order['order_id']);
if(empty($info['cardno'])){
$response = json_decode($order['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,
];
$where = [
'order_id' => $order['order_id']
];
$result = Kami91OrderService::instance()->up($where,$kami);
}elseif(isset($order['channel_callback_msg']) && str_contains($order['channel_callback_msg'], "kami")) {
$msg = json_decode($order['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,
];
$where = [
'order_id' => $order['order_id']
];
$result = Kami91OrderService::instance()->up($where, $kami);
}else{
if(isset($info['create_at'] )){
$timestamp = strtotime($info['create_at']);
$now = time();
$time = $timestamp + 60*10 ;
if($now < $time){
$this->no();
}
}
$this->no(-103);
}
}else{
$cardno = $info['cardno'];
$cardpwd = $info['cardpwd'];
$expire_time= $info['expire_time'];
}
$data = [
'CardNum' => $cardno,
'CardPassword' => $cardpwd,
'expireDate' => $expire_time
];
}else {
if(isset($info['create_at'] )){
$timestamp = strtotime($info['create_at']);
$now = time();
$time = $timestamp + 60*10 ;
if($now < $time){
$this->no();
}
}
$this->no(-100);
}
return $this->yes($data);
}
/**
* 返回失败的消息
* @param mixed $info
* @param string $data
* @param integer $code
*/
protected function no($code = 0, $info = '', $data = '{-null-}')
{
$msg = $this->code[$code] ?? 'error';
if ($info) {
$msg .= ':' . $info;
}
$data = '{-null-}';
$this->error($msg, $data, $code);
}
protected function getOrder($order_id, $merchant_order_id = false ,$mid = false)
{
$orderService = OrderService::instance();
$order = $orderService->get($order_id, $merchant_order_id ,$mid);
// if (!$order) {
// $orderHistoryService = OrderHistoryService::instance();
// $order = $orderHistoryService->get($order_id, $merchant_order_id ,$mid);
// }
2024-09-29 15:43:18 +08:00
return $order;
}
/**
* 返回成功的消息
* @param mixed $info
* @param string $data
* @param integer $code
*/
protected function yes($data = '{-null-}', $info = 'ok', $code = 1)
{
if (is_string($data) && $data != 'ok' && $data != 'success') {
if ($data == '订单号重复') {
return $this->no(-102);
}
return $this->no(-100, $data);
}
$this->success($info, $data, $code);
}
public function cancel()
{
$data = input();
$getMerchantInfo = MerchantService::instance()->get($data['appid']);
$channelService = ChannelService::instance();
$order = $this->getOrder('', $data['merchant_order_id'], $getMerchantInfo['mid']);
$channelInfo = $channelService->get($order['cid']);
$response = json_decode($order['response'], true);
if($channelInfo['suoka_status'] == 1) {
if (isset($response['kami'])) {
$suoka = $channelService->call('lockCard', $order['cid'], $order);
if($suoka=='ok' || $suoka=='old_ok'){
$tkorder = [
'mid' => $getMerchantInfo['mid'],
'order_id' => $order['order_id'],
'merchant_order_id' => $data['merchant_order_id'],
'status' => 1
];
Db::name('order_tk')->insert($tkorder);
$this->yes($suoka);
}
}
}
$this->no(-104);
}
public function uphuidiao($order_id)
{
// var_dump(123);die;
$order = $this->getOrder($order_id);
if(isset($order['channel_callback_msg'])){
$msg = json_decode($order['channel_callback_msg'],true);
if(isset($msg['kami'])){
return 'ok';
// OrderService::instance()->upStatus($order_id);
}else{
return 'error';
}
}else{
$response = json_decode($order['response'],true);
//
if(isset($response['kami'])){
$expired = $response['kami']['expired']??null;
if($expired == ''){
$expired = null;
}
$expired = null;
2024-09-29 15:43:18 +08:00
// var_dump($response['kami']);die;
$kami = [
'cardno' =>$response['kami']['cardno'],
'cardpwd' =>$response['kami']['cardpwd'],
'expire_time' =>$expired,
2024-09-29 15:43:18 +08:00
];
$where = [
'order_id' => $order_id
];
$result = Kami91OrderService::instance()->up($where,$kami);
// OrderService::instance()->upChannelMsg($order_id,'2',['kami' => $response['kami'],'s_nubmer'=>$response['kami']['cardno']]);
}else{
return 'error';
}
}
return 'ok';
}
2024-09-29 15:43:18 +08:00
/**
* @return string
*/
// public function queryFaka1()
// {
//
// $url='https://s.52jisu.com/open/intf/SendStatus.jsp';
//
// $param = [
// 'tid'=>input('orderid'),
// 'sellerNick'=>'宝斓网游专营店'
// ];
//
//
// $response = $this->send_post( $url, $param, 'POST');
// var_dump($response);die;
// }
public function huidiao($order_id)
{
// var_dump(123);die;
$order = $this->getOrder($order_id);
if(isset($order['channel_callback_msg'])){
$msg = json_decode($order['channel_callback_msg'],true);
if(isset($msg['kami'])){
OrderService::instance()->upStatus($order_id);
}else{
return 'error';
}
}else{
$response = json_decode($order['response'],true);
//
if(isset($response['kami'])){
// var_dump($response['kami']);die;
$expired = $response['kami']['expired']??null;
if($expired == ''){
$expired = null;
}
$expired = null;
2024-09-29 15:43:18 +08:00
$kami = [
'cardno' =>$response['kami']['cardno'],
'cardpwd' =>$response['kami']['cardpwd'],
'expire_time' =>$expired,
2024-09-29 15:43:18 +08:00
];
$where = [
'order_id' => $order_id
];
$result = Kami91OrderService::instance()->up($where,$kami);
OrderService::instance()->upChannelMsg($order_id,'2',['kami' => $response['kami'],'s_nubmer'=>$response['kami']['cardno']]);
}else{
return 'error';
}
}
if ( $order['merchant_callback_error'] != 1) {
// var_dump(111);die;
$data['merchant_callback_error'] = 2;
$where = [
'order_id' => $order_id
];
OrderService::instance()->db()->where($where)->update($data);
}
return 'ok';
}
2024-09-29 15:43:18 +08:00
public function dc_charge(): void
{
$this->submit();
}
# 提交接口 一般用于提交数据,异步执行
public function submit()
{
#注意修改id
// $this->mid = '2';
$input = input();
$this->log($input,'submit');
$data = [];
if (empty($input)) {
$this->no(-100);
}
if (empty($input['appid'])) {
$this->no(-1);
}
if (empty($input['order'])) {
$this->no(-100);
}
$order = '';
$merchant_order = $input['order'];
if (isset($input['proid'])) {
$input['proid'] = $this->mapping($input['proid'] );
# 根据产品id 获取信息
$pinfo = \app\channel\service\ProductBaseService::instance()->getOne($input['proid']);
if (!$pinfo) {
$this->no(-2);
}
$data['product_key'] = $pinfo['key'];
$data['cash'] = $pinfo['value'] ;
$pid = \app\channel\service\ProductBaseService::instance()->getProductId($pinfo['service_id']);
$data['pid'] = $pid['id'];
} else {
$this->no(-2);
}
$data['status'] = '1';
$data['param'] = json_encode($input);
// if ($data['request'] && $data['status'] == '-7') {
// $data['request'] = json_decode($data['request'], true);
//// \app\gateway\controller\api\Task::run($data['request'], true);
//
// }
$getMerchantInfo = MerchantService::instance()->get($input['appid']);
$data['mid'] = $getMerchantInfo['id'];
$orderdata = $this->getOrder($order, $merchant_order ,$getMerchantInfo['id']);
if(!$orderdata){
}else{
$data['status'] = '4';
$data['order_id'] = $orderdata['order_id'];
}
$where = [
'mid' => $data['mid'],
'merchant_order_id' => $merchant_order
];
$data['merchant_order_id'] = $merchant_order;
$data['notifyurl'] = $input['notify'];
$result = Kami91OrderService::instance()->up($where,$data);
if(!is_numeric($result)){
$this->no(-1000);
}
// var_dump($orderdata);die;
// if(isset($orderdata['status']) && $orderdata['status'] == '7'){
// $this->huidiao($orderdata['order_id']);
// }
$this->yes('ok');
}
// protected function getMerchant(): void
// {
// $this->merchant = MerchantService::instance()->get('PT_'.$this->agentId);
// $this->mid = $this->merchant['id'];
//
//// $this->agentId = $this->merchant['agentId'];
//
// $this->agentkey = $this->merchant['agentkey'];
//
// $this->callnotify = $this->merchant['notify_url'];
//// $this->signkey = $this->merchant['other_key'];
//
//
// }
protected static function log($data, $type = 'request'):void
{
\dever\Log::write('kami91', $type, $data);
// \dever\Log::write('jingdong', $type, $data);
}
public function mapping($proid)
{
if($proid == '5438110079611'){
return '10091';
}else{
return $proid;
}
}
public function kaminotify( $order_id ,$status)
{
$kami91Service = Kami91OrderService::instance();
$getkami91info = $kami91Service->getOrder(1, $order_id);
$url = $getkami91info['notifyurl'];
$param = array();
$param['status'] = $status;
if ($status == 2 ){
if($getkami91info['status'] == '6'){
$check = $this->queryFaka($getkami91info['merchant_order_id']);
if($check == 'ok'){
$msg = 'success';
// $datas = ['status'=> '2'] ;
$kami91Service->upStatus($order_id, 2 ,$msg);
return 'ok';
}else{
$this->log($check,'errormsg');#写入到日志里
return 'no';
}
}elseif ($getkami91info['status'] == '2'){
return 'ok';
}else{
return 'no';
}
}
return 'ok';
if($status == '3'){
return 'ok';
}
//// var_dump($param);die;
//// $msg = http_post( $url, $param);
//
//// $msg = $this->send_post( $url, $param, 'POST');
// $msg = 'success';
//// var_dump($msg);die;
// $this->log($msg,'msg');#写入到日志里
//
//
// if($msg != 'success'){
// $this->log($msg,'errormsg');#写入到日志里
// $callbalck_status = '3';
// }else{
// $callbalck_status = '2';
// }
//
//
// if ($callbalck_status == 3){
// die;
// }
//
//
//
//
// return $upstatus;
}
public function queryFaka($merchant_order){
$url='https://s.52jisu.com/open/intf/SendStatus.jsp';
$param = [
'tid'=>$merchant_order,
'sellerNick'=>'宝斓网游专营店'
];
// $response = $this->send_post( $url, $param, 'POST');
//
// $msg = json_decode($response,true);
$msg['code'] = '1';
if($msg['code'] == '1'){
return 'ok';
}else{
return 'no';
}
}
public function send_post($notify_url, $post_data, $type): mixed
{
$postdate = http_build_query($post_data);
$options = array(
'http' => array(
'method' => $type,
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $postdate,
'timeout' => 15 * 60 // 超时时间(单位:s
)
);
$context = stream_context_create($options);
return file_get_contents($notify_url, false, $context);
}
protected function curl($method, $url, $param = array(), $json = false, $header = false):mixed
{
if ($param) {
$log['type'] = 'request';
$log['url'] = $url;
$log['param'] = $param;
$this->log($log);
}
$curl = CurlService::getInstance($url, $param, $method, $json, $header);
$curl->setTimeOut(3600);
return $curl->result();
}
}