/** * Checkout an item. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\RedirectResponse */ public function postCheckout(Request $request) { $item = Item::findOrFail($request->input('item_id')); if ($item->seller->userId == Auth::user()->userId) { return redirect($item->url)->withStatus('You cannot purchase your own items.'); } // Check for unpaid item if ($unpaid = $item->purchases()->where('user_id', Auth::user()->userId)->where('paid', 0)->first()) { return redirect('/pay/' . $unpaid->purchaseId); } if (!$item->auction && $item->isEnded()) { return redirect()->back()->withErrors(['You can no longer can no longer continue with this purchase because the item has ended.']); } if (!$item->auction && $request->input('quantity') > $item->quantity) { return redirect()->back()->withErrors(['You cannot purchase more than the available stock.']); } if ($item->auction && !$item->isEnded()) { return redirect()->back()->withErrors(['You must win an auction before you can pay for it.']); } if ($item->auction && !$item->winningBid) { return redirect()->back()->withErrors(['Please wait while this auction is processed.']); } if ($item->auction && $item->winningBid->bidder != Auth::user()) { return redirect()->back()->withErrors(["You can only pay for an auction you've won."]); } if ($item->auction && $item->isEnded() && $item->purchases->count()) { return redirect('/pay/' . $item->purchases->first()->purchaseId); } $validator = \Validator::make(\Input::all(), ['delivery_option' => 'required', 'quantity' => 'integer', 'postal_address' => 'required_if:delivery_option,collection', 'name' => 'required_if:postal_address,add', 'street1' => 'required_if:postal_address,add', 'city' => 'required_if:postal_address,add', 'county' => 'required_if:postal_address,add', 'postcode' => 'required_if:postal_address,add', 'country' => 'required_if:postal_address,add']); if ($validator->fails()) { return redirect()->back()->withErrors($validator); } $purchase = new Purchase(); $purchase->created = time(); $purchase->item()->associate($item); $purchase->buyer()->associate(Auth::user()); if ($request->input('delivery_option') != 'collection') { if (!($delivery_option = DeliveryOption::find($request->input('delivery_option')))) { return redirect()->back()->withErrors(['delivery_option' => 'Please select a valid delivery option.']); } $purchase->deliveryOption()->associate($delivery_option); if ($request->input('postal_address') == 'add') { $postal_address = new PostalAddress(); $postal_address->name = $request->input('name'); $postal_address->street1 = $request->input('street1'); $postal_address->street2 = $request->input('street2'); $postal_address->city = $request->input('city'); $postal_address->county = $request->input('county'); $postal_address->country = $request->input('country'); $postal_address->postcode = $request->input('postcode'); $postal_address->user()->associate(Auth::user()); if (!Iso3166::exists($postal_address->country)) { return redirect()->back()->withErrors(['country' => 'Please select a valid country.']); } if ($request->input('remember')) { $postal_address->save(); } } elseif (!($postal_address = PostalAddress::find($request->input('postal_address')))) { return redirect()->back()->withErrors(['postal_address' => 'Please select a valid postal address.']); } elseif ($postal_address->user != Auth::user()) { return redirect()->back()->withErrors(['postal_address' => 'You can only select your own postal addresses.']); } $purchase->useAddress($postal_address); } if ($item->auction && !$item->isActive()) { $purchase->unitPrice = $purchase->total = $item->biddingPrice; $purchase->quantity = 1; } else { $purchase->unitPrice = $item->fixedPrice; $purchase->quantity = $request->input('quantity') ?: 1; $purchase->total = round($purchase->unitPrice * $purchase->quantity, 2); $item->quantity -= $purchase->quantity; $item->save(); } $purchase->save(); return redirect("/pay/{$purchase->purchaseId}"); }