Created
December 20, 2018 15:23
-
-
Save tshafer/078ec6f3508ffef0afb478127adf5046 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Requests; | |
use Illuminate\Foundation\Http\FormRequest; | |
class ValidateEntryRequest extends FormRequest | |
{ | |
/** | |
* Determine if the user is authorized to make this request. | |
* | |
* @return bool | |
*/ | |
public function authorize() | |
{ | |
return true; | |
} | |
/** | |
* @return array | |
*/ | |
public function messages() | |
{ | |
return [ | |
// | |
]; | |
} | |
/** | |
* Get the validation rules that apply to the request. | |
* | |
* @return array | |
*/ | |
public function rules() | |
{ | |
return [ | |
'first_name' => 'required', | |
'last_name' => 'required', | |
'email' => 'required|email', | |
// 'address' => 'required', | |
// 'city' => 'required', | |
// 'state' => 'required', | |
// 'zipcode' => 'required', | |
// 'stripe_token' => 'required', | |
'coupon_code' => '', | |
]; | |
} | |
protected function withValidator($validator) | |
{ | |
$validator->after(function ($validator) { | |
$event = $this->route()->parameter('event'); | |
// Reject if there are no tickets available | |
// This checks max tickets per ticket type, and if we are still selling tickets | |
if ($event->available_tickets->count() === 0) { | |
$validator->getMessageBag()->add('sold_out', 'Sorry, this event is sold out.'); | |
return false; | |
} | |
// Reject if we are sold out from max tickets event | |
if ($event->max_tickets <= $event->tickets->count()) { | |
$validator->getMessageBag()->add('sold_out', 'Sorry, this event is sold out.'); | |
return false; | |
} | |
$tickets = collect($this->input('selectedTickets')); | |
$ticketCount = $tickets->sum('count'); | |
if ($ticketCount === 0) { | |
$validator->getMessageBag()->add('general_error', 'Please choose an amount of tickets to buy.'); | |
return false; | |
} | |
if ($this->get('coupon_code') !== null) { | |
$requestedCoupon = trim(strtolower($this->get('coupon_code'))); | |
// get the coupon code we want to check for the event | |
$coupon = $event->coupons->where('code', $requestedCoupon)->first(); | |
// We dont have a coupon so lets reject | |
if (!$coupon) { | |
$validator->getMessageBag()->add('coupon_code', 'This is not an active coupon code.'); | |
return; | |
} | |
// Get attendees by coupon code, reject if max_redemptions is met | |
$attendees = $event->attendees()->where('coupon_code', $requestedCoupon); | |
if ($coupon->max_redemptions <= $attendees->count()) { | |
$validator->getMessageBag()->add('coupon_code', 'This coupon is no longer valid.'); | |
return; | |
} | |
if ($coupon->min_qty && $ticketCount < $coupon->min_qty) { | |
$validator->getMessageBag()->add('coupon_code', 'This coupon only applies to orders of {$coupon->min_qty} or more tickets'); | |
return; | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment