/** * Process the donation * * @param Request $request * * @return json response */ public function donation(Request $request) { try { $charge = Stripe::charges()->create(['currency' => 'CAD', 'amount' => money_format("%i", $request->amount / 100), 'source' => $request->token]); // TODO: Do this better. } catch (\Cartalyst\Stripe\Exception\BadRequestException $e) { return response()->json(['success' => false]); } catch (\Cartalyst\Stripe\Exception\UnauthorizedException $e) { return response()->json(['success' => false]); } catch (\Cartalyst\Stripe\Exception\InvalidRequestException $e) { return response()->json(['success' => false]); } catch (\Cartalyst\Stripe\Exception\NotFoundException $e) { return response()->json(['success' => false]); } catch (\Cartalyst\Stripe\Exception\CardErrorException $e) { return response()->json(['success' => false]); } catch (\Cartalyst\Stripe\Exception\ServerErrorException $e) { return response()->json(['success' => false]); } if ($request->user()) { $request->user()->donations()->create(['stripe_token' => $charge['id'], 'amount' => $request->amount]); } else { /** * The user is not logged in, so here we are creating an anonymous donation in the database * It is not associated with a user id */ $donation = new Donation(); $donation->email = $request->email; $donation->stripe_token = $charge['id']; $donation->amount = $request->amount; $donation->save(); } return response()->json(['success' => true]); }
/** * Store a newly created resource in storage. * * @return Response */ public function store($id) { $input = Request::all(); //get userID and push to array. $user = Auth::user()->id; $input['user_id'] = $user; $donation = Donation::create($input); return redirect('/dashboard/' . $id . '/donation/view/' . $donation->id); }