CouponController.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. if (!in_array($request->input('plan_id'), $coupon->limit_plan_ids)) {
  28. abort(500, __('The coupon code cannot be used for this subscription'));
  29. }
  30. }
  31. return response([
  32. 'data' => $coupon
  33. ]);
  34. }
  35. }