|  | @@ -1,219 +1,155 @@
 | 
	
		
			
				|  |  |  <?php namespace Api\Api;
 | 
	
		
			
				|  |  |  use Dever;
 | 
	
		
			
				|  |  |  use Api\Lib\Api;
 | 
	
		
			
				|  |  | +use Api\Lib\Platform\Sign;
 | 
	
		
			
				|  |  | +use Api\Lib\Platform\Value;
 | 
	
		
			
				|  |  |  class Notify extends Api
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  |      public function common()
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  | +        $input = Dever::input();
 | 
	
		
			
				|  |  |          $file = file_get_contents("php://input");
 | 
	
		
			
				|  |  |          if ($file) {
 | 
	
		
			
				|  |  | -            $file = json_decode($file, true);
 | 
	
		
			
				|  |  | +            $file = Dever::json_decode($file);
 | 
	
		
			
				|  |  |              if ($file) {
 | 
	
		
			
				|  |  |                  $input = array_merge($file, $input);
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  | -        } else {
 | 
	
		
			
				|  |  | -            $input = Dever::input();
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        Dever::log($input, 'notify');
 | 
	
		
			
				|  |  | -        echo 11;die;
 | 
	
		
			
				|  |  | -        //$param = '{"s":"132dcszvteLAx3r5eKrjarbGyJilBFECGu2F7mCX-H6K2_w","appid":"app483393213726","order_id":"Q202401040798155761553480","merchant_order_id":"yk18f2401040253000002","status":2,"cash":1000,"msg":"充值成功","actual_cash":1004.9999999999999,"time":1704308034101,"nonce":"fa0912fcfeb3ddfec4e821c26","signature":"d58295706e069e61a4a4ef9428ae0d155b36fae6"}';
 | 
	
		
			
				|  |  | -        //$input = Dever::json_decode($param);
 | 
	
		
			
				|  |  | -        $input = Dever::input();
 | 
	
		
			
				|  |  | +        Dever::log($input, 'api_notify');
 | 
	
		
			
				|  |  |          if (!isset($input['s'])) {
 | 
	
		
			
				|  |  |              $this->error('error');
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          $s = \Dever\Helper\Str::decode($input['s']);
 | 
	
		
			
				|  |  |          if ($s) {
 | 
	
		
			
				|  |  | -            list($connect_id, $api_id, $order_id) = explode('|', $s);
 | 
	
		
			
				|  |  | -        } elseif (isset($input['order_num'])) {
 | 
	
		
			
				|  |  | -            $order_id = Dever::db('order', 'seller')->find(array('order_num' => $input['order_num']));
 | 
	
		
			
				|  |  | -            if (!$order_id) {
 | 
	
		
			
				|  |  | +            $data = explode('|', $s);
 | 
	
		
			
				|  |  | +            $api_id = $data[0];
 | 
	
		
			
				|  |  | +            if (!$api_id) {
 | 
	
		
			
				|  |  |                  $this->error('error');
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  | -            $channel = Dever::db('info', 'channel')->find($order_id['channel_id']);
 | 
	
		
			
				|  |  | -            if (!$channel) {
 | 
	
		
			
				|  |  | -                $this->error('order error');
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | -            $connect_id = $channel['connect_id'];
 | 
	
		
			
				|  |  | -            $api = Dever::db('api', 'api')->find(array('connect_id' => $connect_id, 'type' => 1));
 | 
	
		
			
				|  |  | -            if (!$api) {
 | 
	
		
			
				|  |  | -                $this->error('order error');
 | 
	
		
			
				|  |  | +            if (isset($data[1])) {
 | 
	
		
			
				|  |  | +                $method = $data[1];
 | 
	
		
			
				|  |  | +                unset($data[0]);
 | 
	
		
			
				|  |  | +                unset($data[1]);
 | 
	
		
			
				|  |  | +                $param = array_values($data);
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  | -            $api_id = $api['id'];
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |          } else {
 | 
	
		
			
				|  |  |              $this->error('error');
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        $this->connect = Dever::db('info', 'api')->find($connect_id);
 | 
	
		
			
				|  |  | -        $this->info = Dever::db('api', 'api')->find($api_id);
 | 
	
		
			
				|  |  | -        
 | 
	
		
			
				|  |  |          unset($input['s']);
 | 
	
		
			
				|  |  |          unset($input['l']);
 | 
	
		
			
				|  |  | -        if (!$input) {
 | 
	
		
			
				|  |  | -            $input = file_get_contents("php://input");
 | 
	
		
			
				|  |  | -            if ($input) {
 | 
	
		
			
				|  |  | -                $input = json_decode($input, true);
 | 
	
		
			
				|  |  | -                if (is_array($input)) {
 | 
	
		
			
				|  |  | -                    if (isset($input['s'])) {
 | 
	
		
			
				|  |  | -                        unset($input['s']);
 | 
	
		
			
				|  |  | -                    }
 | 
	
		
			
				|  |  | -                    if (isset($input['l'])) {
 | 
	
		
			
				|  |  | -                        unset($input['l']);
 | 
	
		
			
				|  |  | -                    }
 | 
	
		
			
				|  |  | -                } else {
 | 
	
		
			
				|  |  | -                    $this->error('error');
 | 
	
		
			
				|  |  | -                }
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | +        $state = $this->setting($api_id);
 | 
	
		
			
				|  |  | +        if (!$state) {
 | 
	
		
			
				|  |  | +            $this->error('error');
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          if (!$input) {
 | 
	
		
			
				|  |  |              $this->error('error');
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        $this->load($input, $connect_id, $api_id, $order_id);
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    private function load($input, $connect_id, $api_id, $order_id)
 | 
	
		
			
				|  |  | -    {
 | 
	
		
			
				|  |  | -        //Dever::log($input, 'notify');
 | 
	
		
			
				|  |  | -        if (is_array($order_id)) {
 | 
	
		
			
				|  |  | -            $order = $order_id;
 | 
	
		
			
				|  |  | -        } else {
 | 
	
		
			
				|  |  | -            $order = Dever::db('order', 'seller')->find($order_id);
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        if (!$order || $order['status'] >= 10) {
 | 
	
		
			
				|  |  | -            $this->error('order error');
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        $channel = Dever::db('info', 'channel')->find($order['channel_id']);
 | 
	
		
			
				|  |  | -        if (!$channel) {
 | 
	
		
			
				|  |  | -            $this->error('channel error');
 | 
	
		
			
				|  |  | +        if ($this->info['notify_type'] == 2) {
 | 
	
		
			
				|  |  | +            $this->error('error');
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        $this->connect['appsecret'] = $channel['appsecret'];
 | 
	
		
			
				|  |  | +        $body = $this->body($input);
 | 
	
		
			
				|  |  | +        $this->header();
 | 
	
		
			
				|  |  | +        $this->verify();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        $code = Dever::db('api_notify_code', 'api')->select(array('api_id' => $api_id));
 | 
	
		
			
				|  |  | +        # 判断是否成功
 | 
	
		
			
				|  |  | +        $status = $this->status($body);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        $status = 0;
 | 
	
		
			
				|  |  | -        if ($code) {
 | 
	
		
			
				|  |  | -            foreach ($code as $k => $v) {
 | 
	
		
			
				|  |  | -                if (isset($input[$v['key']]) && $input[$v['key']] == $v['value']) {
 | 
	
		
			
				|  |  | -                    $status = $v['type'];
 | 
	
		
			
				|  |  | -                }
 | 
	
		
			
				|  |  | +        if ($status < 3 && isset($method)) {
 | 
	
		
			
				|  |  | +            $param[] = $status;
 | 
	
		
			
				|  |  | +            $param[] = $body;
 | 
	
		
			
				|  |  | +            $msg = Dever::call($method, $param);
 | 
	
		
			
				|  |  | +            if ($msg) {
 | 
	
		
			
				|  |  | +                $this->error($msg);
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        $data = $input;
 | 
	
		
			
				|  |  | -        $data['status'] = $status;
 | 
	
		
			
				|  |  | -        $data = $this->service($data);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        $msg = '';
 | 
	
		
			
				|  |  | +        # 返回给上游信息
 | 
	
		
			
				|  |  |          if ($status == 1) {
 | 
	
		
			
				|  |  | -            $msg = 'ok';
 | 
	
		
			
				|  |  | +            echo $this->info['notify_success'];die;
 | 
	
		
			
				|  |  |          } elseif ($status == 2) {
 | 
	
		
			
				|  |  | -            $msg = 'error';
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        if ($this->info['notify_sign_col']) {
 | 
	
		
			
				|  |  | -            $this->info['sign_col'] = $this->info['notify_sign_col'];
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        if ($sign = Dever::issets($input, $this->connect['sign_name'])) {
 | 
	
		
			
				|  |  | -            unset($input[$this->connect['sign_name']]);
 | 
	
		
			
				|  |  | -            unset($input['l']);
 | 
	
		
			
				|  |  | -            /*
 | 
	
		
			
				|  |  | -            $string = '';
 | 
	
		
			
				|  |  | -            if ($sign != $this->sign($input, $string)) {
 | 
	
		
			
				|  |  | -                $this->error('sign error');
 | 
	
		
			
				|  |  | -            }*/
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        if ($msg) {
 | 
	
		
			
				|  |  | -            $update = array();
 | 
	
		
			
				|  |  | -            $update['official_msg'] = $data['official_msg'] ?? '';
 | 
	
		
			
				|  |  | -            $update['official_order_num'] = $data['official_order_num'] ?? '';
 | 
	
		
			
				|  |  | -            $update['channel_callback'] = json_encode($input, JSON_UNESCAPED_UNICODE);
 | 
	
		
			
				|  |  | -            $update['channel_callback_date'] = time();
 | 
	
		
			
				|  |  | +            $this->error('error');
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            if ($msg != 'ok') {
 | 
	
		
			
				|  |  | -                $channel_num = Dever::db('channel', 'seller')->count(array('seller_id' => $order['seller_id'], 'goods_id' => $order['goods_id'], 'status' => 1));
 | 
	
		
			
				|  |  | -                if ($channel_num > 1) {
 | 
	
		
			
				|  |  | -                    # 记录渠道错误信息
 | 
	
		
			
				|  |  | -                    $selected = array();
 | 
	
		
			
				|  |  | -                    $selected[$order['channel_id']] = true;
 | 
	
		
			
				|  |  | -                    $order_error_data = Dever::db('order_error', 'seller')->select(array('order_num' => $order['order_num']));
 | 
	
		
			
				|  |  | -                    if ($order_error_data) {
 | 
	
		
			
				|  |  | -                        foreach ($order_error_data as $k => $v) {
 | 
	
		
			
				|  |  | -                            $selected[$v['channel_id']] = true;
 | 
	
		
			
				|  |  | -                        }
 | 
	
		
			
				|  |  | -                    }
 | 
	
		
			
				|  |  | -                    $num = count($selected);
 | 
	
		
			
				|  |  | -                    $channel_num = $channel_num - $num;
 | 
	
		
			
				|  |  | -                    if ($channel_num > 0) {
 | 
	
		
			
				|  |  | -                        $order_error = array();
 | 
	
		
			
				|  |  | -                        $order_error['order_num'] = $order['order_num'];
 | 
	
		
			
				|  |  | -                        $order_error['buy_price'] = $order['buy_price'];
 | 
	
		
			
				|  |  | -                        $order_error['channel_id'] = $order['channel_id'];
 | 
	
		
			
				|  |  | -                        $order_error['channel_goods_id'] = $order['channel_goods_id'];
 | 
	
		
			
				|  |  | -                        $order_error['channel_goods_discount'] = $order['channel_goods_discount'];
 | 
	
		
			
				|  |  | -                        $order_error['channel_order_date'] = $order['channel_order_date'];
 | 
	
		
			
				|  |  | -                        $order_error['channel_order_num'] = $order['channel_order_num'];
 | 
	
		
			
				|  |  | -                        $order_error['channel_request'] = $order['channel_request'];
 | 
	
		
			
				|  |  | -                        $order_error['channel_response'] = $order['channel_response'];
 | 
	
		
			
				|  |  | -                        $order_error['channel_callback'] = $update['channel_callback'];
 | 
	
		
			
				|  |  | -                        $order_error['channel_callback_date'] = $update['channel_callback_date'];
 | 
	
		
			
				|  |  | -                        Dever::db('order_error', 'seller')->insert($order_error);
 | 
	
		
			
				|  |  | -                        Dever::load('order', 'seller')->handleAct($order, $selected);
 | 
	
		
			
				|  |  | -                    } else {
 | 
	
		
			
				|  |  | -                        Dever::load('order', 'seller')->notify($order, $msg, $update);
 | 
	
		
			
				|  |  | -                    }
 | 
	
		
			
				|  |  | -                } else {
 | 
	
		
			
				|  |  | -                    Dever::load('order', 'seller')->notify($order, $msg, $update);
 | 
	
		
			
				|  |  | +    protected function body($body)
 | 
	
		
			
				|  |  | +    {
 | 
	
		
			
				|  |  | +        $config = Dever::db('api_notify_body', 'api')->select(array('api_id' => $this->info['id']));
 | 
	
		
			
				|  |  | +        $value = new Value($this->field, $this->sign);
 | 
	
		
			
				|  |  | +        $result = $value->get($config, $body);
 | 
	
		
			
				|  |  | +        if ($result) {
 | 
	
		
			
				|  |  | +            foreach ($config as $k => $v) {
 | 
	
		
			
				|  |  | +                if (isset($body[$v['value']])) {
 | 
	
		
			
				|  |  | +                    $value = $this->field->value($body[$v['value']], $v['type'], false);
 | 
	
		
			
				|  |  | +                    $this->field->set($v['key'], $value);
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  | -            } else {
 | 
	
		
			
				|  |  | -                Dever::load('order', 'seller')->notify($order, $msg, $update);
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  | -            echo $this->info['notify_success'];die;
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        $this->error('error');
 | 
	
		
			
				|  |  | +        return $result;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    private function error($msg)
 | 
	
		
			
				|  |  | +    protected function header()
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  | -        if ($this->info && $this->info['notify_error']) {
 | 
	
		
			
				|  |  | -            if ($this->info['notify_error'] == 500) {
 | 
	
		
			
				|  |  | -                header("HTTP/1.1 500 Internal Server Error");
 | 
	
		
			
				|  |  | -                header("Status: 500 Internal Server Error");
 | 
	
		
			
				|  |  | -            } else {
 | 
	
		
			
				|  |  | -                echo $this->info['notify_error'];die;
 | 
	
		
			
				|  |  | +        $header = getallheaders();
 | 
	
		
			
				|  |  | +        $config = Dever::db('platform_response_header', 'api')->select(array('platform_id' => $this->platform['id']));
 | 
	
		
			
				|  |  | +        if ($config) {
 | 
	
		
			
				|  |  | +            foreach ($config as $k => $v) {
 | 
	
		
			
				|  |  | +                if (isset($header[$v['value']])) {
 | 
	
		
			
				|  |  | +                    $value = $this->field->value($header[$v['value']], $v['type'], false);
 | 
	
		
			
				|  |  | +                    $this->field->set($v['key'], $value);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        echo $msg;die;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    # 生成回调
 | 
	
		
			
				|  |  | -    public function callback()
 | 
	
		
			
				|  |  | +    protected function verify()
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  | -        //$param = '{"l":"notify.callback","t":"4-5-user_order","userid":"41438","order_id":"24020213181441438abbf5b39f","account":"15810090811","amount":"100","price":"104.1","state":"failed","user_order":"C2024020251094338265393","sign":"0447CC15F7D0218A1C5C09160A12A19A","voucher":""}';
 | 
	
		
			
				|  |  | -        //$input = Dever::json_decode($param);
 | 
	
		
			
				|  |  | -        $input = Dever::input();
 | 
	
		
			
				|  |  | -        if (!isset($input['t'])) {
 | 
	
		
			
				|  |  | -            $this->error('error');
 | 
	
		
			
				|  |  | +        $this->sign['verify_set'] = explode(',', $this->sign['verify_set']);
 | 
	
		
			
				|  |  | +        if (!in_array(1, $this->sign['verify_set'])) {
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        $file = file_get_contents("php://input");
 | 
	
		
			
				|  |  | -        if ($file) {
 | 
	
		
			
				|  |  | -            $file = json_decode($file, true);
 | 
	
		
			
				|  |  | -            if ($file) {
 | 
	
		
			
				|  |  | -                $input = array_merge($file, $input);
 | 
	
		
			
				|  |  | +        if (!$this->field->sign) {
 | 
	
		
			
				|  |  | +            Dever::error('签名验证失败');
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if ($this->sign['verify_col']) {
 | 
	
		
			
				|  |  | +            $this->sign['col'] = $this->sign['verify_col'];
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        $sign = new Sign($this->field, $this->sign);
 | 
	
		
			
				|  |  | +        $sign->check($this->field->sign);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    protected function status($body)
 | 
	
		
			
				|  |  | +    {
 | 
	
		
			
				|  |  | +        # 1成功 2失败 3不做任何操作
 | 
	
		
			
				|  |  | +        $status = 3;
 | 
	
		
			
				|  |  | +        $config = Dever::db('api_notify_code', 'api')->select(array('api_id' => $this->info['id']));
 | 
	
		
			
				|  |  | +        if ($config) {
 | 
	
		
			
				|  |  | +            foreach ($config as $k => $v) {
 | 
	
		
			
				|  |  | +                if (isset($body[$v['key']]) && $body[$v['key']] == $v['value']) {
 | 
	
		
			
				|  |  | +                    $status = $v['type'];
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        $temp = explode('-', $input['t']);
 | 
	
		
			
				|  |  | -        $connect_id = $temp[0];
 | 
	
		
			
				|  |  | -        $api_id = $temp[1];
 | 
	
		
			
				|  |  | -        $order_key = $temp[2];
 | 
	
		
			
				|  |  | -        $this->connect = Dever::db('info', 'api')->find($connect_id);
 | 
	
		
			
				|  |  | -        $this->info = Dever::db('api', 'api')->find($api_id);
 | 
	
		
			
				|  |  | -        if (!isset($input[$order_key])) {
 | 
	
		
			
				|  |  | -            $this->error('error');
 | 
	
		
			
				|  |  | +        return $status;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    protected function error($msg)
 | 
	
		
			
				|  |  | +    {
 | 
	
		
			
				|  |  | +        if ($this->info && $this->info['notify_error']) {
 | 
	
		
			
				|  |  | +            $temp = explode("\n", $this->info['notify_error']);
 | 
	
		
			
				|  |  | +            if (!isset($temp[1])) {
 | 
	
		
			
				|  |  | +                $temp[1] = 500;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            $this->code($temp[1]);
 | 
	
		
			
				|  |  | +            echo $this->info['notify_error'];die;
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        $order_id = $input[$order_key];
 | 
	
		
			
				|  |  | -        unset($input['t']);
 | 
	
		
			
				|  |  | -        $order = Dever::db('order', 'seller')->find(array('order_num' => $order_id));
 | 
	
		
			
				|  |  | -        if (!$order || $order['status'] >= 10) {
 | 
	
		
			
				|  |  | -            $this->error('order error');
 | 
	
		
			
				|  |  | +        echo $msg;die;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    protected function code($code)
 | 
	
		
			
				|  |  | +    {
 | 
	
		
			
				|  |  | +        if ($code == 500) {
 | 
	
		
			
				|  |  | +            header("HTTP/1.1 500 Internal Server Error");
 | 
	
		
			
				|  |  | +            header("Status: 500 Internal Server Error");
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        $this->load($input, $connect_id, $api_id, $order);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  }
 |