Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

behnampmdg3's avatar

How would you clean this up?

It's basic stuff. Please show me how you clean this up.

Thanks

 <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Stripe\{Stripe, Charge, Customer};

class PaymentController extends Controller
{
    public function index()
        {
            return view('payment');
        }
    public function pay(Request $request)
        {
        
      //If not stripe customer, create customer in stripe and update stripe_customer_id field in the database
      if(!auth()->user()->stripe_customer_id)
        {
          \Stripe\Stripe::setApiKey(config('services.stripe.secret'));
            $customer = Customer::create(
               ['email' => $request['stripeEmail']
               , 'source'=>$request['stripeToken']]
            );
            $stripe_customer_id = $customer->id;
            $user = auth()->user();
            $user->stripe_customer_id = $stripe_customer_id;
            $user->save();
        }  
        else 
        {
            $stripe_customer_id = auth()->user()->stripe_customer_id;
        }
        Charge::create([
            'amount'=>2500,
            'customer' => $stripe_customer_id,
            'currency' => 'usd'
        ]);
        
            return redirect('users/'.\Auth::id());
        }   
}

0 likes
2 replies
lostdreamer_nl's avatar

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
    ]);
}

martinbean's avatar
Level 80

@behnampmdg3 First, I’d create a StripeServiceProvider class and set the secret key there so you don’t have to do it before every interaction with Stripe:

use Stripe\Stripe;

class StripeServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Stripe::setApiKey($this->app['config']['services.stripe.secret']);
    }
}

Next, I’d use Cashier to do the actual charging of the user:

class PaymentController extends Controller
{
    public function pay(Request $request)
    {
        $request->user()->charge([
            'currency' => 'usd', // Although Cashier assumes USD by default
            'source' => $request->input('stripeToken'),
        ]);

        // Redirect to where you need to here
    }
}

It’s also worth creating a form request class (i.e. CreatePaymentRequest) to validate the request input actually contains a stripeToken, and use the auth middleware to check a user is currently authenticated.

As an aside, I take care in formatting your code. It’s much easier to reason about if it’s formatted consistently. PSR-2 is the de facto standard when it comes to formatting PHP source code.

1 like

Please or to participate in this conversation.