<?php namespace Connect\Api;
use Dever;
use Connect\Lib\Func\Api;
class Notify extends Api
{
    public function common()
    {
        //$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, 'notify');
        if (!isset($input['s'])) {
            $this->error('error');
        }
        $s = \Dever\Helper\Str::decode($input['s']);
        list($connect_id, $api_id, $order_id) = explode('|', $s);
        unset($input['s']);
        $this->connect = Dever::db('info', 'connect')->find($connect_id);
        $this->info = Dever::db('api', 'connect')->find($api_id);
        $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');
        }
        $this->connect['appsecret'] = $channel['appsecret'];

        $code = Dever::db('api_notify_code', 'connect')->select(array('api_id' => $api_id));

        $status = 0;
        if ($code) {
            foreach ($code as $k => $v) {
                if (isset($input[$v['key']]) && $input[$v['key']] == $v['value']) {
                    $status = $v['type'];
                }
            }
        }
        $data = $input;
        $data['status'] = $status;
        $data = $this->service($data);

        $msg = '';
        if ($status == 1) {
            $msg = 'ok';
        } elseif ($status == 2) {
            $msg = 'error';
        }
        if ($this->info['notify_sign_col']) {
            $this->info['sign_col'] = $this->info['notify_sign_col'];
        }
        if ($sign = Dever::isset($input, $this->connect['sign_name'])) {
            unset($input[$this->connect['sign_name']]);
            unset($input['l']);
            if ($sign != $this->sign($input)) {
                $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'] = Dever::json_encode($input);
            $update['channel_callback_date'] = time();
            Dever::load('order', 'seller')->notify($order, $msg, $update);
        }
        echo $this->info['notify_success'];die;
    }

    private function error($msg)
    {
        if ($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;
            }
        }
        echo $msg;die;
    }
}