From b848be65e9146e2cc21a4c4c36d16e0748f293d1 Mon Sep 17 00:00:00 2001 From: rahul shukla Date: Wed, 24 Mar 2021 17:06:14 +0530 Subject: [PATCH] Enhancement #4752 fixed --- .../Shop/TransactionController.php | 106 ++++++++++++++++++ .../Http/Resources/Sales/OrderTransaction.php | 28 +++++ packages/Webkul/API/Http/routes.php | 14 ++- 3 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 packages/Webkul/API/Http/Controllers/Shop/TransactionController.php create mode 100644 packages/Webkul/API/Http/Resources/Sales/OrderTransaction.php diff --git a/packages/Webkul/API/Http/Controllers/Shop/TransactionController.php b/packages/Webkul/API/Http/Controllers/Shop/TransactionController.php new file mode 100644 index 000000000..f64422816 --- /dev/null +++ b/packages/Webkul/API/Http/Controllers/Shop/TransactionController.php @@ -0,0 +1,106 @@ +guard = request()->has('token') ? 'api' : 'customer'; + + $this->_config = request('_config'); + + if (isset($this->_config['authorization_required']) && $this->_config['authorization_required']) { + + auth()->setDefaultDriver($this->guard); + + $this->middleware('auth:' . $this->guard); + } + + if ($this->_config) { + $this->repository = app($this->_config['repository']); + } + } + + /** + * Returns a listing of the Order Transactions. + * + * @return \Illuminate\Http\Response + */ + public function index() + { + $query = $this->repository->scopeQuery(function($query) { + $query = $query->leftJoin('orders', 'order_transactions.order_id', '=', 'orders.id')->select('order_transactions.*', 'orders.customer_id'); + + if (isset($this->_config['authorization_required']) && $this->_config['authorization_required']) { + $query = $query->where('customer_id', auth()->user()->id); + } + + foreach (request()->except(['page', 'limit', 'pagination', 'sort', 'order', 'token']) as $input => $value) { + $query = $query->whereIn($input, array_map('trim', explode(',', $value))); + } + + if ($sort = request()->input('sort')) { + $query = $query->orderBy($sort, request()->input('order') ?? 'desc'); + } else { + $query = $query->orderBy('id', 'desc'); + } + + return $query; + }); + + if (is_null(request()->input('pagination')) || request()->input('pagination')) { + $results = $query->paginate(request()->input('limit') ?? 10); + } else { + $results = $query->get(); + } + + return $this->_config['resource']::collection($results); + } + + /** + * Returns an individual invoice. + * + * @param int $id + * @return \Illuminate\Http\Response + */ + public function get($id) + { + if (isset($this->_config['authorization_required']) && $this->_config['authorization_required']) { + $query = $this->repository->leftJoin('orders', 'order_transactions.order_id', '=', 'orders.id') + ->select('order_transactions.*', 'orders.customer_id') + ->where('customer_id', auth()->user()->id) + ->findOrFail($id); + } else { + $query = $this->repository->findOrFail($id); + } + + return new $this->_config['resource']($query); + } +} diff --git a/packages/Webkul/API/Http/Resources/Sales/OrderTransaction.php b/packages/Webkul/API/Http/Resources/Sales/OrderTransaction.php new file mode 100644 index 000000000..5fa82df11 --- /dev/null +++ b/packages/Webkul/API/Http/Resources/Sales/OrderTransaction.php @@ -0,0 +1,28 @@ + $this->id, + 'transaction_id' => $this->transaction_id, + 'status' => $this->status, + 'type' => $this->type, + 'payment_method' => $this->payment_method, + 'data' => $this->data, + 'updated_at' => $this->updated_at, + 'created_at' => $this->created_at, + ]; + } +} \ No newline at end of file diff --git a/packages/Webkul/API/Http/routes.php b/packages/Webkul/API/Http/routes.php index 68f650380..90ded573b 100755 --- a/packages/Webkul/API/Http/routes.php +++ b/packages/Webkul/API/Http/routes.php @@ -215,7 +215,7 @@ Route::group(['prefix' => 'api'], function ($router) { ]); - //Invoice routes + //Shipment routes Route::get('shipments', 'ResourceController@index')->defaults('_config', [ 'repository' => 'Webkul\Sales\Repositories\ShipmentRepository', 'resource' => 'Webkul\API\Http\Resources\Sales\Shipment', @@ -228,6 +228,18 @@ Route::group(['prefix' => 'api'], function ($router) { 'authorization_required' => true ]); + //Transaction routes + Route::get('transactions', 'TransactionController@index')->defaults('_config', [ + 'repository' => 'Webkul\Sales\Repositories\OrderTransactionRepository', + 'resource' => 'Webkul\API\Http\Resources\Sales\OrderTransaction', + 'authorization_required' => true + ]); + + Route::get('transactions/{id}', 'TransactionController@get')->defaults('_config', [ + 'repository' => 'Webkul\Sales\Repositories\OrderTransactionRepository', + 'resource' => 'Webkul\API\Http\Resources\Sales\OrderTransaction', + 'authorization_required' => true + ]); //Wishlist routes Route::get('wishlist', 'ResourceController@index')->defaults('_config', [