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

acoustic85's avatar

Handle stripe payment with Laravel queue

Hi guys,

I am trying to charge the customer 2 times and I want to proceed the 2nd payment with Laravel queue but I am not sure what needs to be done in the Job that i created.

This is my controller:

public function StripeCharge(Request $request) {

    $product_name = $request->product_name;
    $product_price = $request->product_price;

Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
    $customer = \Stripe\Customer::create(array(
        "email" => $email = Auth::user()->email,
        "source" => $request->stripeToken
    ));

    $mailInfo = \Stripe\Charge::create(array(
         "amount" => $product_price * 100,
         "currency" => "gbp",
         "customer" => $customer->id,
         "description" => "Deposit" . "-" . $product_name 
    ));

    $newcustomer = $customer->id;

  

    $email = Auth::user()->email;
        
      Mail::to($email)->send(new WelcomeMail($mailInfo));

    
      $fullamount = New ChargeCustomer($mailInfo); 


      dispatch($fullamount)->delay(now()->addMinutes(5));
    

    
      
    return view('confirmation');

}

This is my ChargeCustomer job


<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ChargeCustomer implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $fullamount;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($fullamount)
    {
        $fullamount = $this->fullamount;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        
    }
}




Any help will be much appreciated. Thank you very much in advance.

0 likes
6 replies
martinbean's avatar

@doncho85 What do you mean, you don’t know what needs to be done in the job you created? If you want to charge a customer an amount, then that’s what you do:

class ChargeCustomer implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $customerId;
    public $amount;

    public function __construct(string $customerId, int $amount)
    {
        $this->customerId = $customerId;
        $this->amount = $amount;
    }

    public function handle()
    {
        \Stripe\Charge::create([
            'amount' => $this->amount,
            'currency' => 'gbp',
            'customer' => $this->customer,
        ]);
    }
}
ChargeCustomer::dispatch('cus_test', 1000);
acoustic85's avatar

Hi Martin thanks for your help.

Then I need to pass only this variables from the controller $customerId and $amount into the dispatch right?

 dispatch($customerId,$amount)->delay(now()->addMinutes(5)); 

Thanks again.

acoustic85's avatar

This is what i did on the controller side

 $fullamount = New ChargeCustomer($customerId,$amount); 


          dispatch($fullamount)->delay(now()->addMinutes(5));

ChargeCustomer Job



<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Stripe;

class ChargeCustomer implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public $customerId;
    public $amount;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(string $customerId, int $amount)
    {
       $this->customerId = $customerId;
       $this->amount = $amount;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        \Stripe\Charge::create([
            'amount' => $this->amount,
            'currency'=> 'gbp',
            'customer' => $this->customerId

        ]);

        ChargeCustomer::dispatch('cus_test',1000);
    }
}

But for some reason it's not charging again.

acoustic85's avatar

this is what i have in the exception column

No API key provided. (HINT: set your API key using "Stripe::setApiKey()". You can generate API keys from the Stripe web interface. See https://stripe.com/api for details, or email [email protected] if you have any questions.

martinbean's avatar
Level 80

@doncho85 Right. So set the API key in a service provider. Otherwise you’re going to have to do it in every place you use the Stripe SDK, which isn’t ideal:

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Stripe\Stripe;

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

You’ll need to actually add an entry for stripe in your config/services.php file if you haven’t already:

// config/services.php

return [
    'stripe' => [
        'key' => env('STRIPE_KEY'),
        'secret' => env('STRIPE_SECRET'),
    ],
];

Please or to participate in this conversation.