v2board/app/Http/Controllers/User/CouponController.php

40 lines
1.3 KiB
PHP
Raw Normal View History

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'))) {
abort(500, __('user.coupon.check.coupon_not_empty'));
2020-01-01 15:59:53 +08:00
}
$coupon = Coupon::where('code', $request->input('code'))->first();
if (!$coupon) {
abort(500, __('user.coupon.check.coupon_invalid'));
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) {
abort(500, __('user.coupon.check.coupon_not_available_by_number'));
2020-01-01 23:40:14 +08:00
}
2020-01-01 15:59:53 +08:00
if (time() < $coupon->started_at) {
abort(500, __('user.coupon.check.coupon_not_available_by_time'));
2020-01-01 15:59:53 +08:00
}
if (time() > $coupon->ended_at) {
abort(500, __('user.coupon.check.coupon_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)) {
abort(500, __('user.coupon.check.coupon_limit_plan'));
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
}