コード例 #1
0
 /**
  * Add a row to the rsvp table corresponding to the event id and the user id.
  * Called from a post from the Stripe payment javascript.
  *
  * @param  \Illuminate\Http\Request  $req
  * @return \Illuminate\Http\Response
  */
 public function attendEvent(Request $req)
 {
     //requests the event id passed in form
     $evid = $req->input('evid');
     //Counts the users attending the event
     $count = Rsvp::where('eventid', $evid)->count();
     //Gets the details of the event
     $ev = Event::where('id', $evid)->first();
     //Checks the count isn't >= capacity
     if ($count < $ev->capacity) {
         do {
             $code = str_random(10);
         } while (Rsvp::where("code", $code)->where('eventid', $evid)->first() instanceof Rsvp);
         // Inserts the user into the RSVP table for the current event
         Rsvp::insert(['userid' => Auth::user()->id, 'eventid' => $evid, 'code' => $code]);
         //Charges the user via Stripe for the current event
         \Stripe\Stripe::setApiKey(env('STRIPE_PRI'));
         $token = $req->input('stripeToken');
         $charge = \Stripe\Charge::create(array('source' => $token, 'amount' => $ev->price . '00', 'currency' => 'eur', 'description' => Auth::user()->email, "metadata" => array("Event" => $ev->name, "Customer Name" => Auth::user()->name . ' ' . Auth::user()->surname, "Address" => Auth::user()->direction)));
     } else {
         echo "Event full";
     }
 }