CouponController.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace App\Http\Controllers\User;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use App\Models\Coupon;
  6. class CouponController extends Controller
  7. {
  8. public function check(Request $request)
  9. {
  10. if (empty($request->input('code'))) {
  11. abort(500, __('Coupon cannot be empty'));
  12. }
  13. $coupon = Coupon::where('code', $request->input('code'))->first();
  14. if (!$coupon) {
  15. abort(500, __('Invalid coupon'));
  16. }
  17. if ($coupon->limit_use <= 0 && $coupon->limit_use !== NULL) {
  18. abort(500, __('This coupon is no longer available'));
  19. }
  20. if (time() < $coupon->started_at) {
  21. abort(500, __('This coupon has not yet started'));
  22. }
  23. if (time() > $coupon->ended_at) {
  24. abort(500, __('This coupon has expired'));
  25. }
  26. if ($coupon->limit_plan_ids) {
  27. $limitPlanIds = json_decode($coupon->limit_plan_ids);
  28. if (!in_array($request->input('plan_id'), $limitPlanIds)) {
  29. abort(500, __('The coupon code cannot be used for this subscription'));
  30. }
  31. }
  32. return response([
  33. 'data' => $coupon
  34. ]);
  35. }
  36. }