Example #1
0
 public function attend(Request $request, Event $event, AppMailer $mailer)
 {
     \Stripe\Stripe::setApiKey(env("STRIPE_SK"));
     try {
         $token = $request->stripeToken;
         $charge = \Stripe\Charge::create(array("amount" => $event->ticket_price * 100, "currency" => "eur", "source" => $token, "description" => $event->name));
         $numTickets = isset($request->num_tickets) ? $request->num_tickets : 1;
         $event->attendees()->attach(Auth::user(), ['num_tickets' => $numTickets]);
         $event->ticket_left = $event->ticket_left - $numTickets;
         $event->save();
     } catch (Stripe_CardError $e) {
         // Since it's a decline, Stripe_CardError will be caught
         $body = $e->getJsonBody();
         $err = $body['error'];
     }
     // Send ticket email.
     $mailer->sendTicket(Auth::user(), $event);
     return Redirect::route('events.show', compact('event'));
 }