Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/V4/WalletsController.php
<?php | <?php | ||||
namespace App\Http\Controllers\API\V4; | namespace App\Http\Controllers\API\V4; | ||||
use App\Transaction; | use App\Transaction; | ||||
use App\Wallet; | use App\Wallet; | ||||
use App\Http\Controllers\Controller; | use App\Http\Controllers\Controller; | ||||
use App\Providers\PaymentProvider; | use App\Providers\PaymentProvider; | ||||
use Carbon\Carbon; | |||||
use Illuminate\Http\Request; | use Illuminate\Http\Request; | ||||
use Illuminate\Support\Facades\Auth; | use Illuminate\Support\Facades\Auth; | ||||
/** | /** | ||||
* API\WalletsController | * API\WalletsController | ||||
*/ | */ | ||||
class WalletsController extends Controller | class WalletsController extends Controller | ||||
{ | { | ||||
Show All 25 Lines | class WalletsController extends Controller | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
*/ | */ | ||||
public function store(Request $request) | public function store(Request $request) | ||||
{ | { | ||||
return $this->errorResponse(404); | return $this->errorResponse(404); | ||||
} | } | ||||
/** | /** | ||||
* Display the specified resource. | * Return data of the specified wallet. | ||||
* | * | ||||
* @param string $id | * @param string $id A wallet identifier | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse The response | ||||
*/ | */ | ||||
public function show($id) | public function show($id) | ||||
{ | { | ||||
$wallet = Wallet::find($id); | |||||
if (empty($wallet)) { | |||||
return $this->errorResponse(404); | return $this->errorResponse(404); | ||||
} | } | ||||
// Only owner (or admin) has access to the wallet | |||||
if (!Auth::guard()->user()->canRead($wallet)) { | |||||
return $this->errorResponse(403); | |||||
} | |||||
$result = $wallet->toArray(); | |||||
$provider = \App\Providers\PaymentProvider::factory($wallet); | |||||
$result['provider'] = $provider->name(); | |||||
$result['notice'] = $this->getWalletNotice($wallet); | |||||
return response()->json($result); | |||||
} | |||||
/** | /** | ||||
* Show the form for editing the specified resource. | * Show the form for editing the specified resource. | ||||
* | * | ||||
* @param int $id | * @param int $id | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
*/ | */ | ||||
public function edit($id) | public function edit($id) | ||||
▲ Show 20 Lines • Show All 183 Lines • ▼ Show 20 Lines | public function transactions($id) | ||||
return response()->json([ | return response()->json([ | ||||
'status' => 'success', | 'status' => 'success', | ||||
'list' => $result, | 'list' => $result, | ||||
'count' => count($result), | 'count' => count($result), | ||||
'hasMore' => $hasMore, | 'hasMore' => $hasMore, | ||||
'page' => $page, | 'page' => $page, | ||||
]); | ]); | ||||
} | } | ||||
/** | |||||
* Returns human readable notice about the wallet state. | |||||
* | |||||
* @param \App\Wallet $wallet The wallet | |||||
*/ | |||||
protected function getWalletNotice(Wallet $wallet): ?string | |||||
{ | |||||
if ($wallet->balance < 0) { | |||||
return \trans('app.wallet-notice-nocredit'); | |||||
} | |||||
if ($wallet->discount && $wallet->discount->discount == 100) { | |||||
return null; | |||||
} | |||||
if ($wallet->owner->created_at > Carbon::now()->subDays(14)) { | |||||
return \trans('app.wallet-notice-trial'); | |||||
} | |||||
if ($until = $wallet->balanceLastsUntil()) { | |||||
if ($until->isToday()) { | |||||
if ($wallet->owner->created_at > Carbon::now()->subDays(30)) { | |||||
return \trans('app.wallet-notice-trial-end'); | |||||
} | |||||
return \trans('app.wallet-notice-today'); | |||||
} | |||||
$params = [ | |||||
'date' => $until->toDateString(), | |||||
'days' => Carbon::now()->diffForHumans($until, Carbon::DIFF_ABSOLUTE), | |||||
]; | |||||
return \trans('app.wallet-notice-date', $params); | |||||
} | |||||
return null; | |||||
} | |||||
} | } |