2019-12-31 17:49:24 +08:00
|
|
|
<?php
|
|
|
|
|
2020-01-29 16:08:50 +08:00
|
|
|
namespace App\Http\Controllers\User;
|
2019-12-31 17:49:24 +08:00
|
|
|
|
2020-01-29 16:08:50 +08:00
|
|
|
use App\Http\Controllers\Controller;
|
2019-12-31 17:49:24 +08:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Models\Coupon;
|
|
|
|
|
|
|
|
class CouponController extends Controller
|
|
|
|
{
|
2020-01-11 13:36:52 +08:00
|
|
|
public function check(Request $request)
|
|
|
|
{
|
2020-01-01 15:59:53 +08:00
|
|
|
if (empty($request->input('code'))) {
|
2021-06-12 00:56:39 +08:00
|
|
|
abort(500, __('Coupon cannot be empty'));
|
2020-01-01 15:59:53 +08:00
|
|
|
}
|
|
|
|
$coupon = Coupon::where('code', $request->input('code'))->first();
|
|
|
|
if (!$coupon) {
|
2021-06-12 00:56:39 +08:00
|
|
|
abort(500, __('Invalid coupon'));
|
2020-01-01 15:59:53 +08:00
|
|
|
}
|
2020-01-01 23:42:25 +08:00
|
|
|
if ($coupon->limit_use <= 0 && $coupon->limit_use !== NULL) {
|
2021-06-12 00:56:39 +08:00
|
|
|
abort(500, __('This coupon is no longer available'));
|
2020-01-01 23:40:14 +08:00
|
|
|
}
|
2020-01-01 15:59:53 +08:00
|
|
|
if (time() < $coupon->started_at) {
|
2021-06-12 00:56:39 +08:00
|
|
|
abort(500, __('This coupon has not yet started'));
|
2020-01-01 15:59:53 +08:00
|
|
|
}
|
|
|
|
if (time() > $coupon->ended_at) {
|
2021-06-12 00:56:39 +08:00
|
|
|
abort(500, __('This coupon has expired'));
|
2020-01-01 15:59:53 +08:00
|
|
|
}
|
2020-06-13 23:27:43 +08:00
|
|
|
if ($coupon->limit_plan_ids) {
|
|
|
|
$limitPlanIds = json_decode($coupon->limit_plan_ids);
|
|
|
|
if (!in_array($request->input('plan_id'), $limitPlanIds)) {
|
2021-06-12 00:56:39 +08:00
|
|
|
abort(500, __('The coupon code cannot be used for this subscription'));
|
2020-06-13 23:27:43 +08:00
|
|
|
}
|
|
|
|
}
|
2020-01-01 15:59:53 +08:00
|
|
|
return response([
|
|
|
|
'data' => $coupon
|
|
|
|
]);
|
2019-12-31 17:49:24 +08:00
|
|
|
}
|
2020-01-11 13:36:52 +08:00
|
|
|
}
|