public function testUnkownHttpStatusExceptionGenerates500() { $app = new Application(); $app->get('/api/test/undefined-exception', function () { throw new Exception('Exception with unknown HTTP status', 999); }); Environment::mock(array('PATH_INFO' => '/api/test/undefined-exception')); $response = $app->invoke(); $this->assertEquals(json_encode(array('status' => 500, 'statusText' => 'Internal Server Error', 'description' => 'Exception with unknown HTTP status')), $response->getBody()); $this->assertEquals(500, $response->getStatus()); }
public function testGet() { $this->assertNotEquals(0, count($this->app->config['features'])); foreach ($this->app->config['features'] as $id => $feature) { $app = new Application(); Environment::mock(array('PATH_INFO' => '/features/' . $id)); $response = $app->invoke(); $this->assertEquals(json_encode(array_merge(array('id' => $id), $feature, array('href' => './api/features/' . $id))), $response->getBody()); $this->assertEquals(200, $response->getStatus()); } }
/** * Store a newly created resource in storage. * * @return Response */ public function store() { $key = Input::get('key'); $json = Input::all(); //print_r($json); /* ->join('members as m','d.merchant_id=m.id','left') ->where('assignment_date',$indate) ->where('device_id',$dev->id) ->and_() ->group_start() ->where('status',$this->config->item('trans_status_admin_courierassigned')) ->or_() ->group_start() ->where('status',$this->config->item('trans_status_new')) ->where('pending_count >', 0) ->group_end() ->group_end() */ if (is_null($key) || $key == '') { $actor = 'no id : no name'; \Event::fire('log.api', array($this->controller_name, 'post', $actor, 'empty key')); return \Response::json(array('status' => 'ERR:EMPTYKEY', 'timestamp' => time(), 'message' => 'Empty Key')); } $app = \Application::where('key', '=', $key)->first(); if ($app) { $jsons = Input::json(); $model = new \Shipment(); $merchant_id = $app->merchant_id; $app_id = $app->id; $app_key = $app->key; $result = array(); /* foreach ($jsons as $json) { $order = $this->ordermap; print_r($json); } die(); */ foreach ($jsons as $json) { //print_r($json); $order = $this->ordermap; if (isset($json['pick_up_date'])) { if (is_array($json['pick_up_date']) && isset($json['pick_up_date']['sec'])) { $pick_up_date = date('Y-m-d H:i:s', $json['pick_up_date']['sec']); } else { $pick_up_date = $json['pick_up_date']; } } else { $pick_up_date = date('Y-m-d H:i:s', time()); } $codval = doubleval($json['cod']); //$codval = floor($codval * 100) / 100; $codval = round($codval, 0, PHP_ROUND_HALF_UP); $order['buyerdeliveryzone'] = isset($json['district']) ? $json['district'] : ''; $order['merchant_trans_id'] = $json['no_sales_order']; $order['buyerdeliverytime'] = $pick_up_date; $order['fulfillment_code'] = $json['consignee_olshop_orderid']; $order['box_count'] = $json['number_of_package']; $order['delivery_type'] = trim($json['delivery_type']); $order['total_price'] = $codval; $order['email'] = $json['email']; $order['buyer_name'] = $json['consignee_olshop_name']; $order['recipient_name'] = $json['consignee_olshop_name']; $order['shipping_address'] = $json['consignee_olshop_addr']; $order['buyerdeliverycity'] = $json['consignee_olshop_city']; $order['shipping_zip'] = $json['consignee_olshop_zip']; $order['phone'] = $json['consignee_olshop_phone']; $order['delivery_bearer'] = 'merchant'; $order['cod_bearer'] = 'merchant'; $order['actual_weight'] = strval($json['w_v']); $weight = $json['w_v']; $delivery_type = trim($json['delivery_type']); $order['weight'] = \Prefs::get_weight_tariff($weight, $delivery_type, $app_id); $order['merchant_id'] = $merchant_id; $order['application_id'] = $app_id; $order['application_key'] = $app_key; $trx_detail = array(); $trx_detail[0]['unit_description'] = $json['consignee_olshop_desc']; $trx_detail[0]['unit_price'] = $json['cod']; $trx_detail[0]['unit_quantity'] = 1; $trx_detail[0]['unit_total'] = $json['cod']; $trx_detail[0]['unit_discount'] = 0; $order['trx_detail'] = $trx_detail; $trx_id = $order['merchant_trans_id']; $trx = json_encode($order); $order['merchant_trans_id'] = $json['no_sales_order']; $order['fulfillment_code'] = $json['consignee_olshop_orderid']; $inlog = $json; $inlog['ts'] = new \MongoDate(); unset($inlog['_id']); $inlog['merchant_api_id'] = $merchant_id; \Orderapilog::insert($inlog); $check = \Shipment::where('merchant_trans_id', '=', $json['no_sales_order'])->where('fulfillment_code', '=', $json['consignee_olshop_orderid'])->first(); if ($check) { $result[] = array('order_id' => $check->merchant_trans_id, 'ff_id' => $check->fulfillment_code, 'awb' => $check->delivery_id, 'timestamp' => $check->created, 'pickup_time' => $check->pickuptime, 'delivery_time' => $check->deliverytime, 'pending' => $check->pending_count, 'status' => $check->status, 'note' => $check->delivery_note); } else { $saved = $this->order_save($trx, $app_key, $trx_id); $result[] = array('order_id' => $saved['merchant_trans_id'], 'ff_id' => $saved['fulfillment_code'], 'awb' => $saved['delivery_id'], 'timestamp' => $saved['created'], 'pickup_time' => '0000-00-00 00:00:00', 'delivery_time' => '0000-00-00 00:00:00', 'pending' => $saved['pending_count'], 'status' => $saved['status'], 'note' => $saved['delivery_note']); } //$order[] = [w_v] => 0.9 //$order[] = [awb] => //$order[] = [consignee_olshop_service] => REG //$order[] = [position] => BPDU //$order[] = [updated_at] => 2015-11-24 16:44:25 //$order[] = [created_at] => 2015-11-24 16:44:25 //$order[] = [createdDate] => stdClass Object //$order[] = [lastUpdate] => stdClass Object //$order[] = [consignee_olshop_province] => //$order[] = [trip] => 1 //$order[] = [bucket] => incoming //$order[] = [order_id] => 100282527 //$order[] = [fulfillment_code] => 249977 //$order[] = [status] => confirmed //$order[] = [logistic_status] => //$order[] = [pending_count] => 0 //$order[] = [courier_status] => at_initial_node //$order[] = [warehouse_status] => at_initial_node //$order[] = [pickup_status] => to_be_picked_up //$order[] = [device_key] => //$order[] = [device_name] => //$order[] = [device_id] => //$order[] = [courier_name] => //$order[] = [courier_id] => } /* buyerdeliverytime delivery_type buyer_name recipient_name shipping_address direction email mobile1 mobile2 phone weight package_description merchant_trans_id fulfillment_code logistic_awb total_price cod_bearer delivery_bearer buyerdeliveryzone buyerdeliverycity width height length box_count */ //$result = $json; /* ( [_id] => 56543184ccae5b6112004278 [district] => [no_sales_order] => 100282527 [pick_up_date] => stdClass Object ( [sec] => 1431104400 [usec] => 0 ) [consignee_olshop_orderid] => 249977 [number_of_package] => 1 [delivery_type] => REG [cod] => 0 [email] => bagus_sulaiman@india.com [consignee_olshop_name] => 106191 bagus sulaiman [consignee_olshop_addr] => bagus sulaiman jl.bakti rt.004 rw.008 no.10b cililitan kramatjati 13640 Jakarta Timur JK 13640 Indonesia [consignee_olshop_city] => Jakarta Timur [consignee_olshop_region] => JK [consignee_olshop_zip] => 13640 [consignee_olshop_phone] => 81317857612 [contact] => 106191 bagus sulaiman [consignee_olshop_desc] => Susu dan Perlengkapan Bayi [w_v] => 0.9 [awb] => [consignee_olshop_cust] => 7735 [consignee_olshop_service] => REG [position] => BPDU [updated_at] => 2015-11-24 16:44:25 [created_at] => 2015-11-24 16:44:25 [createdDate] => stdClass Object ( [sec] => 1448358276 [usec] => 411000 ) [lastUpdate] => stdClass Object ( [sec] => 1448358276 [usec] => 411000 ) [logistic] => JEX [logistic_type] => external [consignee_olshop_province] => [trip] => 1 [bucket] => incoming [delivery_id] => 24-112015-YAMYZ [order_id] => 100282527 [fulfillment_code] => 249977 [status] => confirmed [logistic_status] => [pending_count] => 0 [courier_status] => at_initial_node [warehouse_status] => at_initial_node [pickup_status] => to_be_picked_up [device_key] => [device_name] => [device_id] => [courier_name] => [courier_id] => ) $awbs = $model->get(); if($awbs){ foreach($awbs as $awb){ $result[] = array( 'order_id'=>$awb->merchant_trans_id, 'ff_id'=>$awb->fulfillment_code, 'awb'=>$awb->delivery_id, 'timestamp'=>date('Y-m-d H:i:s',time()), 'pending'=>$awb->pending_count, 'status'=>$awb->status, 'note'=>$awb->delivery_note ); } } */ //print_r($result); //die(); /* foreach($jsons as $sheet_id=>$rows){ $app_key = $app_entry[$sheet_id]; $app_id = get_app_id_from_key($app_key); $order = $this->ordermap; foreach ($rows['data'] as $key => $line) { if(in_array($key, $entry)){ $line['delivery_type'] = ($line['delivery_type'] == 'DO')?'Delivery Only':$line['delivery_type']; $line['actual_weight'] = $line['weight']; $line['weight'] = get_weight_tariff($line['weight'], $line['delivery_type'] ,$app_id); $trx_detail = array(); $trx_detail[0]['unit_description'] = $line['package_description']; $trx_detail[0]['unit_price'] = $line['total_price']; $trx_detail[0]['unit_quantity'] = 1; $trx_detail[0]['unit_total'] = $line['total_price'] ; $trx_detail[0]['unit_discount'] = 0; unset($line['package_description']); unset($line['no']); foreach($line as $k=>$v){ $order[$k] = $v; } $order['zip'] = '-'; $order['merchant_id'] = $merchant_id; $order['application_id'] = $app_id; $order['application_key'] = $app_key; $order['trx_detail'] = $trx_detail; $trx_id = 'TRX_'.$merchant_id.'_'.str_replace(array(' ','.'), '', microtime()); //print "order input: \r\n"; //print_r($order); $trx = json_encode($order); $result = $this->order_save($trx,$app_key,$trx_id); //print $result; } } } */ $actor = $app->key . ' : ' . $app->merchant_id; \Event::fire('log.api', array($this->controller_name, 'post', $actor, 'order create array')); return Response::json($result); } else { $actor = 'no id : no name'; \Event::fire('log.api', array($this->controller_name, 'post', $actor, 'account not found')); return \Response::json(array('status' => 'ERR:INVALIDACC', 'timestamp' => time(), 'message' => 'Invalid Account')); } }
/** * Store a newly created resource in storage. * * @return Response */ public function store() { $key = Input::get('key'); $json = Input::all(); //print_r($json); /* ->join('members as m','d.merchant_id=m.id','left') ->where('assignment_date',$indate) ->where('device_id',$dev->id) ->and_() ->group_start() ->where('status',$this->config->item('trans_status_admin_courierassigned')) ->or_() ->group_start() ->where('status',$this->config->item('trans_status_new')) ->where('pending_count >', 0) ->group_end() ->group_end() */ if (is_null($key) || $key == '') { $actor = 'no id : no name'; \Event::fire('log.api', array($this->controller_name, 'post', $actor, 'empty key')); return \Response::json(array('status' => 'ERR:EMPTYKEY', 'timestamp' => time(), 'message' => 'Empty Key')); } $app = \Application::where('key', '=', $key)->first(); if ($app) { $model = new \Shipment(); $merchant_id = $app->merchant_id; $result = array(); foreach ($json as $j) { if (is_array($j)) { if (isset($j['awb'])) { $awb = $j['awb']; $m = $model->where('delivery_id', '=', trim($awb))->first(); /* ->where(function($q){ $q->where('status','=', \Config::get('jayon.trans_status_tobeconfirmed') ) ->orWhere(function($qp){ $qp->where('status','=',\Config::get('jayon.trans_status_new')) ->where('pending_count','=',0); }); })->first(); */ if ($m) { if ($m->status == \Config::get('jayon.trans_status_tobeconfirmed') || $m->status == \Config::get('jayon.trans_status_new') && $m->pending_count == 0) { $m->status = \Config::get('jayon.trans_status_confirmed'); $m->save(); } $result[] = array('awb' => $m->delivery_id, 'status' => $m->status); } } } } if (empty($result)) { $result = array('status' => 'ERR:NOMATCH', 'message' => 'No Match Found'); } else { $result = array('status' => 'OK', 'message' => 'Confirmation Success', 'data' => $result); } //print_r($result); //die(); $actor = $app->key . ' : ' . $app->merchant_id; \Event::fire('log.api', array($this->controller_name, 'post', $actor, 'awb search with array')); return Response::json($result); } else { $actor = 'no id : no name'; \Event::fire('log.api', array($this->controller_name, 'post', $actor, 'account not found')); return \Response::json(array('status' => 'ERR:INVALIDACC', 'timestamp' => time(), 'message' => 'Invalid Account')); } }
use API\Middleware\TokenOverBasicAuth; use Flynsarmy\SlimMonolog\Log\MonologWriter; // Init application mode if (empty($_ENV['SLIM_MODE'])) { $_ENV['SLIM_MODE'] = getenv('SLIM_MODE') ? getenv('SLIM_MODE') : 'development'; } // Init and load configuration $config = array(); $configFile = dirname(__FILE__) . '/share/config/' . $_ENV['SLIM_MODE'] . '.php'; if (is_readable($configFile)) { require_once $configFile; } else { require_once dirname(__FILE__) . '/share/config/default.php'; } // Create Application $app = new Application($config['app']); // Only invoked if mode is "production" $app->configureMode('production', function () use($app) { $app->config(array('log.enable' => true, 'log.level' => \Slim\Log::WARN, 'debug' => false)); }); // Only invoked if mode is "development" $app->configureMode('development', function () use($app) { $app->config(array('log.enable' => true, 'log.level' => \Slim\Log::DEBUG, 'debug' => false)); }); // Get log writer $log = $app->getLog(); // Init database try { if (!empty($config['db'])) { \ORM::configure($config['db']['dsn']); if (!empty($config['db']['username']) && !empty($config['db']['password'])) {
/** * Store a newly created resource in storage. * * @return Response */ public function store() { $key = Input::get('key'); $json = Input::all(); //print_r($json); /* ->join('members as m','d.merchant_id=m.id','left') ->where('assignment_date',$indate) ->where('device_id',$dev->id) ->and_() ->group_start() ->where('status',$this->config->item('trans_status_admin_courierassigned')) ->or_() ->group_start() ->where('status',$this->config->item('trans_status_new')) ->where('pending_count >', 0) ->group_end() ->group_end() */ if (is_null($key) || $key == '') { $actor = 'no id : no name'; \Event::fire('log.api', array($this->controller_name, 'post', $actor, 'empty key')); return \Response::json(array('status' => 'ERR:EMPTYKEY', 'timestamp' => time(), 'message' => 'Empty Key')); } $app = \Application::where('key', '=', $key)->first(); if ($app) { $model = new \Shipment(); $merchant_id = $app->merchant_id; $result = array(); foreach ($json as $j) { if (is_array($j)) { $model = $model->orWhere(function ($q) use($j, $key) { $order_id = $j['order_id']; $ff_id = $j['ff_id']; $q->where('merchant_trans_id', '=', trim($order_id))->where('fulfillment_code', '=', trim($ff_id))->where('application_key', '=', trim($key))->whereNotNull('delivery_id'); }); } } $awbs = $model->get(); if ($awbs) { foreach ($awbs as $awb) { $result[] = array('order_id' => $awb->merchant_trans_id, 'ff_id' => $awb->fulfillment_code, 'awb' => $awb->delivery_id, 'timestamp' => date('Y-m-d H:i:s', time()), 'pending' => $awb->pending_count, 'status' => $awb->status, 'note' => $awb->delivery_note); } } //print_r($result); //die(); $actor = $app->key . ' : ' . $app->merchant_id; \Event::fire('log.api', array($this->controller_name, 'post', $actor, 'awb search with array')); return Response::json($result); } else { $actor = 'no id : no name'; \Event::fire('log.api', array($this->controller_name, 'post', $actor, 'account not found')); return \Response::json(array('status' => 'ERR:INVALIDACC', 'timestamp' => time(), 'message' => 'Invalid Account')); } }
<?php require_once __DIR__ . '/bootstrap.php'; use Api\Application; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; $app = new Application(['em' => $em]); $app->before(function (Request $request) { if ($request->getMethod() == 'OPTIONS') { return; } $tokens = (require_once __DIR__ . '/config/token.php'); if (!$tokens) { throw new \Exception("Error Processing Token file", 1); } if (!$request->headers->has('X-Token')) { return new Response('Unauthorized', 401); } if (!in_array($request->headers->get('X-Token'), array_keys($tokens))) { return new Response('Unauthorized', 401); } if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) { $data = json_decode($request->getContent(), true); $request->request->replace(is_array($data) ? $data : array()); } }); $app->after(function (Request $request, Response $response) { $response->headers->set('Access-Control-Allow-Origin', '*'); $response->headers->set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); $response->headers->set('Access-Control-Allow-Headers', 'Content-Type, X-Token'); });
/** * Return stats info API. * * @param Application $app * @return \Symfony\Component\HttpFoundation\JsonResponse */ public function info(Application $app) { return $app->json(array('name' => $app['config']['name'], 'version' => $app['config']['version'], 'source' => $app['config']['sourceVersion'], 'env' => $app['environment'], 'debug' => $app['debug'])); }
if (!empty($config['db'])) { \ORM::configure($config['db']['dsn']); if (!empty($config['db']['username']) && !empty($config['db']['password'])) { \ORM::configure('username', $config['db']['username']); \ORM::configure('password', $config['db']['password']); } } \ORM::configure('logging', true); \ORM::configure('logger', function ($log_string, $query_time) use($log) { $log->debug($log_string . '-' . $query_time); }); \ORM::configure('error_mode', \PDO::ERRMODE_WARNING); } catch (\PDOException $e) { $log->error($e->getMessage()); } $app = new Application($config['app']); #$app->validaEmail(['email'=>'ffjff@jhjhg']); // Cache Middleware (inner) #$app->add(new API\Middleware\Cache('/api/v1')); // Parses JSON body $app->add(new \Slim\Middleware\ContentTypes()); #$app->add(new \Slim\Middleware\Navigation('/api/v1',$auth)); #$app->add(new \Slim\Middleware\Navigation('/api/v1',$auth)); // Manage Rate Limit #$app->add(new API\Middleware\RateLimit('/api/v1')); // JSON Middleware #$app->add(new API\Middleware\JSON('/api/v1')); // Auth Middleware (outer) #$app->add(new API\Middleware\TokenOverBasicAuth(array('root' => '/api/v1'))); ################################################################### ###################################################################