Probably a bit like this:
// controller
public function pay(Request $request)
{
// if the request has a stripeEmail & stripeToken, they can be used to create
// the stripe customer, if the user was already a stripCustomer,
// they wont be used even if supplied
$charge = auth()->user()->charge( 2500, 'usd', $request->stripeEmail, $request->stripeToken);
return redirect('users/'. auth()->id());
}
// user model:
public function charge($amount, $currency, $stripeEmail = null, $stripeToken = null)
{
// If not stripe customer, create customer in stripe
// and update stripe_customer_id field in the database
if(!$this->stripe_customer_id) {
\Stripe\Stripe::setApiKey(config('services.stripe.secret'));
$customer = \Stripe\Customer::create([
'email' => $stripeEmail,
'source'=> $stripeToken
]);
$stripe_customer_id = $customer->id;
$this->stripe_customer_id = $stripe_customer_id;
$this->save();
}
// now that we know for sure that the user is a strip customer, charge the account
return \Stripe\Charge::create([
'amount'=> $amount,
'customer' => $this->stripe_customer_id,
'currency' => $currency
]);
}