123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?php namespace Api\Api;
- use Dever;
- use Api\Lib\Api;
- class Notify extends Api
- {
- public function common()
- {
- $file = file_get_contents("php://input");
- if ($file) {
- $file = json_decode($file, true);
- if ($file) {
- $input = array_merge($file, $input);
- }
- } else {
- $input = Dever::input();
- }
- Dever::log($input, 'notify');
- echo 11;die;
-
-
- $input = Dever::input();
- 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) {
- $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');
- }
- $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');
- }
- }
- }
- if (!$input) {
- $this->error('error');
- }
- $this->load($input, $connect_id, $api_id, $order_id);
- }
- private function load($input, $connect_id, $api_id, $order_id)
- {
-
- 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');
- }
- $this->connect['appsecret'] = $channel['appsecret'];
- $code = Dever::db('api_notify_code', 'api')->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::issets($input, $this->connect['sign_name'])) {
- unset($input[$this->connect['sign_name']]);
- unset($input['l']);
-
- }
- 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();
- 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);
- }
- } else {
- Dever::load('order', 'seller')->notify($order, $msg, $update);
- }
- echo $this->info['notify_success'];die;
- }
- $this->error('error');
- }
- private function error($msg)
- {
- 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;
- }
- }
- echo $msg;die;
- }
-
- public function callback()
- {
-
-
- $input = Dever::input();
- if (!isset($input['t'])) {
- $this->error('error');
- }
- $file = file_get_contents("php://input");
- if ($file) {
- $file = json_decode($file, true);
- if ($file) {
- $input = array_merge($file, $input);
- }
- }
- $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');
- }
- $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');
- }
- $this->load($input, $connect_id, $api_id, $order);
- }
- }
|