805 lines
23 KiB
PHP
Raw Normal View History

<?php
namespace app\openapi\controller\agisoAcprSupplierApi;
use app\channel\service\ChannelService;
use app\gateway\service\CurlService;
use app\gateway\service\RedisService;
use app\merchant\service\MerchantService;
use app\merchant\service\OrderLastweekHistoryService;
use app\merchant\service\OrderService;
use app\order\service\Kami91OrderService;
use think\admin\Controller;
use think\exception\HttpResponseException;
use think\facade\Db;
use think\facade\Log;
class Order extends Controller
{
# 是否检测数据
// protected bool $check = false;
protected $merchant_id;
# code码定义
protected $code = array
(
# 成功
1 => 'ok',
# 小于0为失败
0 => '暂时未处理',
1100 => '商品不存在',
);
# 查询接口 一般用于查询数据,同步执行
# 提交接口 一般用于提交数据,异步执行
public function get()
{
$input = input();
if (empty($input['userId'])) {
$this->no(-1);
}
if (empty($input['orderNo'])) {
$this->no(-100);
}
// http_post($url, $data)
$getMerchantInfo = MerchantService::instance()->get($input['userId']);
if(!$getMerchantInfo){
$this->no(-3);
}
$other_param = json_encode($getMerchantInfo['other_param'],true);
if(!is_array($other_param) || !isset($other_param['agiso_AppSecret'])){
$secretKey = '';
}else{
$secretKey = $other_param['agiso_AppSecret'];
}
// $mid = $getMerchantInfo['id'];
$info = Kami91OrderService::instance()->db()->where(['merchant_order_id' => $input['orderNo']])->find();
if(!$info){
$this->no(-100);
}
$order = $this->getOrder('', $input['orderNo'] ,$getMerchantInfo['id']);
$response_data = [];
$response_data['orderNo'] = $input['orderNo'];
if(!$order){
$response_data['outTradeNo'] = 'E'.$input['orderNo'];
if(isset($info['create_at'] )){
$timestamp = strtotime($info['create_at']);
$now = time();
$time = $timestamp + 60*10 ;
if($now < $time){
$response_data['orderStatus'] = 10;
$this->yes($response_data);
}
}
$response_data['orderStatus'] = 30;
$response_data['failCode'] = 9999;
$response_data['failReason'] = '未找到对应订单';
$this->yes($response_data);
}
$response_data['outTradeNo'] = $order['order_id'];
$response_data['orderStatus'] = $order['status'] == 2 ? 20 : ($order['status'] == 3 ? 30 : 10);
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);
$card[] = [
'cardNo' => $cardno,
'cardPwd' => $cardpwd,
'expireTime' => $expire_time
];
$cards = json_encode($card);
$card_aes =$this->encryptWithAES($cards, $secretKey);
$response_data['orderCost'] = sprintf("%.4f", $order['actual_cash']) ?? sprintf("%.4f", $order['cash']);
$response_data['cards'] = $card_aes;
return $this->yes($response_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);
$card[] = [
'cardNo' => $cardno,
'cardPwd' => $cardpwd,
'expireTime' => $expire_time
];
$cards = json_encode($card);
$card_aes =$this->encryptWithAES($cards, $secretKey);
$response_data['orderCost'] = sprintf("%.4f", $order['actual_cash']) ?? sprintf("%.4f", $order['cash']);
$response_data['cards'] = $card_aes;
return $this->yes($response_data);
}else{
if(isset($info['create_at'] )){
$timestamp = strtotime($info['create_at']);
$now = time();
$time = $timestamp + 60*10 ;
if($now < $time){
$response_data['orderStatus'] = 10;
$this->yes($response_data);
}
}
$response_data['orderStatus'] = 30;
$response_data['failCode'] = 9999;
$response_data['failReason'] = '未找到卡密超时';
$this->yes($response_data);
}
}
$card[] = [
'cardNo' => $info['cardno'],
'cardPwd' => $info['cardpwd'],
'expireTime' => $info['expire_time']
];
$cards = json_encode($card);
$card_aes =$this->encryptWithAES($cards, $secretKey);
$response_data['orderCost'] = sprintf("%.4f", $order['actual_cash']) ?? sprintf("%.4f", $order['cash']);
$response_data['cards'] = $card_aes;
// return $this->yes($response_data);
}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);
}
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){
$response_data['orderStatus'] = 10;
$this->yes($response_data);
}
}
$response_data['orderStatus'] = 30;
$response_data['failCode'] = 9999;
$response_data['failReason'] = '未找到卡密超时';
$this->yes($response_data);
}
}else{
$cardno = $info['cardno'];
$cardpwd = $info['cardpwd'];
$expire_time= $info['expire_time'];
}
$card[] = [
'cardNo' => $cardno,
'cardPwd' =>$cardpwd,
'expireTime' => $expire_time
];
$cards = json_encode($card);
$card_aes =$this->encryptWithAES($cards, $secretKey);
$response_data['orderStatus'] = 20;
$response_data['orderCost'] = sprintf("%.4f", $order['actual_cash']) ?? sprintf("%.4f", $order['cash']);
$response_data['cards'] = $card_aes;
}else {
if(isset($info['create_at'] )){
$timestamp = strtotime($info['create_at']);
$now = time();
$time = $timestamp + 60*10 ;
if($now < $time){
$response_data['orderStatus'] = 10;
$this->yes($response_data);
}
}
$response_data['orderStatus'] = 30;
$response_data['failCode'] = 9999;
$response_data['failReason'] = '未找到卡密超时';
$this->yes($response_data);
}
return $this->yes($response_data);
}
protected function getOrder($order_id, $merchant_order_id = false ,$mid = false)
{
if(str_ends_with($merchant_order_id, '-00')){
$merchant_order_id = substr($merchant_order_id, 0, -3);
}
$orderService = OrderService::instance();
$order = $orderService->get($order_id, $merchant_order_id ,$mid);
if (!$order) {
$orderHistoryService = OrderLastweekHistoryService::instance();
$order = $orderHistoryService->get($order_id, $merchant_order_id ,$mid);
}
return $order;
}
public function encryptWithAES($plainText, $secretKey) {
$secretKey = 'ez8bfsrgefga5nkx5zgccegrb25mct7w';
// $secretKey =
// 使用 AES-256-ECB 加密模式,并启用 PKCS7 填充
$cipher = 'aes-256-ecb';
// 确保编码一致
$plainText = mb_convert_encoding($plainText, 'UTF-8');
// 执行加密并返回 Base64 编码结果
$encryptedBytes = openssl_encrypt($plainText, $cipher, $secretKey, OPENSSL_RAW_DATA);
return base64_encode($encryptedBytes);
}
public function cancelOrder()
{
$input = input();
$this->no(null);
}
public function uphuidiao($order_id)
{
$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';
}else{
return 'error';
}
}else{
$response = json_decode($order['response'],true);
//
if(isset($response['kami'])){
// var_dump($response['kami']);die;
$kami = [
'cardno' =>$response['kami']['cardno'],
'cardpwd' =>$response['kami']['cardpwd'],
'expire_time' =>$response['kami']['expired'],
];
$where = [
'order_id' => $order_id
];
$result = Kami91OrderService::instance()->up($where,$kami);
}else{
return 'error';
}
}
return 'ok';
}
public function test_notify()
{
$input = input();
$getMerchantInfo = MerchantService::instance()->get('app285941979594');
return $this->notify('https://mai.91kami.com/AldsSupplierTest/2025011935557114401/CreateRechargeCallback', $input,$getMerchantInfo);
}
# 通知处理
public function notify($url,$param,$merchant = [])
{
$log['type'] = 'agiso_notify';
$log['request'] =$param;
if (!isset($log['request']['merchant_order_id'])) {
return 'error';
}
$merchant_order_id = $log['request']['merchant_order_id'];
$param_data = [
'orderNo' =>$merchant_order_id,
'outTradeNo' => $param['order_id'],
'failCode'=>0,
'failReason'=>'',
];
if (isset($log['request']['status']) && ($log['request']['status'] == 2 || $log['request']['status'] == 3)) {
$other_param = json_encode($merchant['other_param'],true);
if(!is_array($other_param) || !isset($other_param['agiso_AppSecret'])){
$secretKey = 'ez8bfsrgefga5nkx5zgccegrb25mct7w';
}else{
$secretKey = $other_param['agiso_AppSecret'];
}
if ($log['request']['status'] == 2) {
$param_data['orderStatus']=20;
$param_data['orderCost']=$log['request']['actual_cash'];
} else {
$param_data['orderStatus']=30;
$param_data['failCode']=9999;
$param_data['failReason']='订单失败';
}
if(isset($param['kami'])){
$cards=[];
$cards[]=[
'cardNo' => $param['kami']['cardno'],
'cardPwd' => $param['kami']['cardpwd'],
'expireTime' => $param['kami']['expired'],
];
$param_data['cards'] = $this->encryptWithAES(json_encode($cards), $secretKey);
}
$param_data['timestamp'] = time();
$param_data['sign'] = $this->_sign($param_data, $merchant,$secretKey);
$response = $this->send_post($url, $param_data, 'POST');
if ($response) {
$response = json_decode($response, true);
if (isset($response['code']) && $response['code'] == '200') {
return 'success';
}
}
}
return 'error';
}
public function _sign($request, $merchant,$secretKey)
{
ksort($request);
$str = '';
foreach ($request as $k => $v){
$str .= $k . '=' . $v . '&';
}
$str = substr($str, 0,-1) ;
$str = $secretKey.$merchant['appsecret'].$str.$secretKey.$merchant['appsecret'];
// var_dump($str);die;
return MD5($str);
}
# 提交接口 一般用于提交数据,异步执行
public function createPurchase()
{
#注意修改id
// $this->mid = '2';
$input = input();
$this->log($input,'submit');
$data = [];
if (empty($input)) {
$this->no(-100);
}
if (empty($input['userId'])) {
$this->no(-1);
}
if (empty($input['orderNo'])) {
$this->no(-100);
}
$order = '';
$merchant_order = $input['orderNo'];
if (isset($input['productNo'])) {
$input['productNo'] = $this->mapping($input['productNo'] );
# 根据产品id 获取信息
$pinfo = \app\channel\service\ProductBaseService::instance()->getOne($input['productNo']);
if (!$pinfo) {
$this->no(1100);
}
$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(1100);
}
$data['status'] = '1';
$data['param'] = json_encode($input);
$getMerchantInfo = MerchantService::instance()->get($input['userId']);
$data['mid'] = $getMerchantInfo['id'];
$orderData = $this->getOrder($order, $merchant_order ,$getMerchantInfo['id']);
if(!$orderData){
}else{
$data['status'] = '4';
$data['order_id'] = $orderData['order_id'];
try{
$redis = RedisService::getInstance();
$redis_key = 'Kami_wait_'.$orderData['order_id'];
$getRedisData = $redis->get($redis_key);
if($getRedisData){
$redis->delete($redis_key);
$redis->push('submit', $getRedisData);
}
}catch (\Exception $e){
}
}
$where = [
'mid' => $data['mid'],
'merchant_order_id' => $merchant_order
];
$data['merchant_order_id'] = $merchant_order;
$data['notifyurl'] = $input['callbackUrl'];
$data['api_key'] = 'agiso';
$result = Kami91OrderService::instance()->up($where,$data);
if(!is_numeric($result)){
$this->no(-1000);
}
$response = [
'orderNo' => $merchant_order,
'orderStatus' => 10,
'outTradeNo'=>$orderData['order_id']??'D'.$merchant_order,
'OrderCost'=> sprintf("%.4f", $pinfo['value']),
];
$this->yes($response);
}
public function createRecharge()
{
#注意修改id
// $this->mid = '2';
$input = input();
$this->log($input,'submit');
$data = [];
if (empty($input)) {
$this->no(-100);
}
if (empty($input['userId'])) {
$this->no(-1);
}
if (empty($input['orderNo'])) {
$this->no(-100);
}
$order = '';
$merchant_order = $input['orderNo'];
$response = [
'orderNo' => $merchant_order,
'orderStatus' => 10,
'outTradeNo'=>'D'.$merchant_order,
'OrderCost'=> 10.00,
];
$this->yes($response);
}
protected static function log($data, $type = 'request'):void
{
\dever\Log::write('agiso', $type, $data);
// \dever\Log::write('jingdong', $type, $data);
}
public function mapping($proid)
{
if($proid == '5438110079611'){
return '10091';
}else{
return $proid;
}
}
public function queryFaka($merchant_order){
$url='https://s.52jisu.com/open/intf/SendStatus.jsp';
$param = [
'tid'=>$merchant_order,
'sellerNick'=>'宝斓网游专营店'
];
$msg['code'] = '1';
if($msg['code'] == '1'){
return 'ok';
}else{
return 'no';
}
}
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'])){
$kami = [
'cardno' =>$response['kami']['cardno'],
'cardpwd' =>$response['kami']['cardpwd'],
'expire_time' =>$response['kami']['expired'],
];
$where = [
'order_id' => $order_id
];
OrderService::instance()->upChannelMsg($order_id,'2',['kami' => $response['kami'],'s_nubmer'=>$response['kami']['cardno']]);
}else{
return 'error';
}
}
if ( $order['merchant_callback_error'] != 1) {
$data['merchant_callback_error'] = 2;
$where = [
'order_id' => $order_id
];
OrderService::instance()->db()->where($where)->update($data);
}
return 'ok';
}
public function send_post($notify_url, $post_data, $type): mixed
{
$postdate = json_encode($post_data);
$options = array(
'http' => array(
'method' => $type,
'header' => 'Content-type:application/json',
'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();
}
/**
* 返回成功的消息
* @param mixed $info
* @param string $data
* @param integer $code
*/
protected function yes($data = '{-null-}', $info = 'ok', $code = 200)
{
if (is_string($data) && $data != 'ok' && $data != 'success') {
if ($data == '订单号重复') {
return $this->no(-102);
}
return $this->no(-100, $data);
}
$response = [
'code' => $code,
'message' => $info,
'data' => $data
];
throw new HttpResponseException(json($response));
}
/**
* 返回失败的消息
* @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;
}
$response = [
'code' => $code,
'message' => $msg,
'data' => $data
];
throw new HttpResponseException(json($response));
}
}