0

I have a function a stripe function that slashes the price of a product, and I am trying to charge the newly slashed price after 5 minutes.

in my checkoutController I have

public function secondHalf($id){

    // fetch the price
    $productprice = Product::where('id', $id)
                    ->pluck('price')
                    ->firstOrFail();
    // slash price into half
    $slashPrice = intdiv($productprice,2); // divide by 2
         
    // commence payment 

    \Stripe\Stripe::setApiKey(config('stripe.sk'));

    $session = \Stripe\Checkout\Session::create([
             'line_items'  => [
                 [
                     'price_data' => [
                         'currency'     => 'gbp',
                         'product_data' => [
                             'name' => 'tax online payment',
                         ],
                         'unit_amount'  => $slashPrice*100,
                     ],
                     'quantity'   => 1,
                 ],
             ],
             'mode'        => 'payment',
             'success_url' => route('success'),
             'cancel_url'  => url('/single-product/'.$id),
           
         ]);

     $details = ['email' => 'thomsontochi@gmail.com'];
     $emailJob = (new AfterPayment($details))
                        ->delay(Carbon::now()
                        ->addMinutes(5));
    dispatch($emailJob);

    return redirect()->away($session->url);
}

it basically find the product, slash the price and I then charge the slashed price.

I have tried making a job , called LatePaymentCharge and I took the function that handles the charge to the job like so

public function handle()
{
    $slashPrice = $this->slashPrice;
    //dd('stipe checkout');

    \Stripe\Stripe::setApiKey(config('stripe.sk'));

    $session = \Stripe\Checkout\Session::create([
        'line_items'  => [
             [
                 'price_data' => [
                     'currency'     => 'gbp',
                     'product_data' => [
                         'name' => 'staxo online payment',
                     ],
                     'unit_amount'  => $slashPrice*100,
                 ],
                 'quantity'   => 1,
             ],
         ],
         'mode'        => 'payment',
         'success_url' => route('success'),
        //  'cancel_url'  => url('/single-product/'.$id),
       
     ]);

     return redirect()->away($session->url);
}

I am currently having issues on how to pass the slash price value from my checkoutController to my job and im not sure if this is the right way to do it . please help

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
dev opia
  • 1
  • 1
  • Within the job you can't redirect to stripe. The job is handled by an Laravel worker and isn't in the web context. What you can do is send the user an [email](https://laravel.com/docs/9.x/mail#delayed-message-queueing) with the payment link. You can pass information to a job dispatch method `LatePaymentCharge::dispatch($price)`, these parameters are injected in the job construct. – Thijs Jan 30 '23 at 17:27
  • @Thijs thanks for the response, that explains why the function doesn't run in my job, what do you mean by send users email with the payment link , you mean when they get the email they click the link and then proceed to another payment? – dev opia Jan 31 '23 at 09:09
  • You need a way to notify the user after 5 minutes. Easiest way is by sending an email. I don't completely understand why you want to slice the payment? Why not pay the full amount the first time? – Thijs Jan 31 '23 at 17:20
  • @Thijs for slicing the payment its just a requirement, and I don't just want to notify the user after 5 minutes , I want to function to tun after 5 minutes – dev opia Feb 01 '23 at 07:19

0 Answers0