error('error'); } $s = \Dever\Helper\Str::decode($input['s']); if ($s) { $data = explode('|', $s); $api_id = $data[0]; if (!$api_id) { $this->error('error'); } if (isset($data[1])) { $method = $data[1]; unset($data[0]); unset($data[1]); $param = array_values($data); } } else { $this->error('error'); } unset($input['s']); unset($input['l']); $state = $this->setting($api_id); if (!$state) { $this->error('error'); } if (!$input) { $this->error('error'); } if ($this->info['notify'] == 2) { $this->error('error'); } $this->notify = Dever::db('api/api_notify')->find(['api_id' => $api_id]); if (!$this->notify) { $this->error('error'); } $body = $this->body($input); $this->header(); $this->verify(); # 判断是否成功 $status = $this->status($body); if ($status < 3 && isset($method)) { $param[] = $status; $param[] = $body; $msg = Dever::call($method, $param); if ($msg) { $this->error($msg); } } # 返回给上游信息 if ($status == 1) { echo $this->notify['success'];die; } elseif ($status == 2) { $this->error('error'); } } protected function body($body) { $config = Dever::db('api/api_notify_body')->select(['api_id' => $this->info['id']]); $result = Value::load($this->field)->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); } } } return $result; } protected function header() { $header = getallheaders(); $config = Dever::db('api/platform_response_header')->select(['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); } } } $config = Dever::db('api/api_response_header')->select(['api_id' => $this->info['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); } } } } protected function verify() { if (!$this->notify['sign_id']) { Dever::error('签名验证失败'); } $info = Dever::db('api/platform_sign')->find($this->notify['sign_id']); Sign::load($this->field, $info)->check($this->notify['sign_arg']); } protected function status($body) { # 1成功 2失败 3不做任何操作 $status = 3; $config = Dever::db('api/api_notify_code')->select(['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']; } } } return $status; } protected function error($msg) { if ($this->notify && $this->notify['error']) { $temp = explode("\n", $this->notify['error']); if (!isset($temp[1])) { $temp[1] = 500; } $this->code($temp[1]); echo $this->notify['error'];die; } echo $msg;die; } protected function code($code) { if ($code == 500) { header("HTTP/1.1 500 Internal Server Error"); header("Status: 500 Internal Server Error"); } } }