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

WAL30's avatar
Level 1

Can't take price in stripe laravel

Hi,

I have an issue that I don't know how to solve, I have created a function that takes my product by id and I want to send the price of that product to the second function stripePost().

public function index($id) {

     $products = Product::where('id', $id)->get();
     
    return view('frontend.checkout', compact('products'));
}


public function stripePost(Request $request)
{
    Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
    Charge::create ([
            
            "amount" =>  100,  (here I want to send price value)
            "currency" => "ron",
            "source" => $request->stripeToken,
            "description" => "This payment is tested purpose "
    ]);
     return back();

}

routes Route::get('checkout/{id}', [CheckoutController::class, 'index']); Route::post('checkout', [CheckoutController::class, 'stripePost'])->name('stripe.post');

Any advice, please. Thanks

0 likes
14 replies
jlrdw's avatar

How are you passing to stripePost?

1 like
WAL30's avatar
Level 1

@jlrdw I tried many ways, today all day :| I tried even with cookies, but when I want to get the cookie, it shows me the code, not the value

{{ $item->price }}

in checkout.blade.php I have this and I get it:

@foreach ($products as $item) {{ $item->price }} @endforeach

I am not sure how to send it in stripe function I do not have a cart, I want to skip the cart and go to checkout

tykus's avatar

I have created a function that takes my product by id

Do you mean this function?

public function index($id) {

     $products = Product::where('id', $id)->get();
     
    return view('frontend.checkout', compact('products'));
}

You are getting a Collection - not an individual Product which you would have if you used

$product = Product::findOrFail($id);

Now, how are you passing the $id to the stripePost method - I assume you have a form in the checkout view? Is there a hidden input?

1 like
WAL30's avatar
Level 1

@tykus Yes, that is the function that I take the product by id but I don't know how to send it in stripe function this is my checkout file

@foreach ($products as $item)
        <div class="col-md-6 col-md-offset-3">
           <div class="panel panel-default credit-card-box">
              <div class="panel-heading display-table" >
                 <div class="row display-tr" >
                    <h3 class="panel-title display-td" >Payment Details</h3>
                    {{ $item->title }}
                    <span name="price" id="price" value="{{ $item->price }}" >{{ $item->price }}</span>
                   
                    <div class="display-td" >                            
                       
                    </div>
                 </div>
              </div><br><br>
              <div class="panel-body">
                 @if (Session::has('success'))
                 <div class="alert alert-success text-center">
                    <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
                    <p>{{ Session::get('success') }}</p>
                 </div>
                 @endif
                 <form
                    role="form"
                    action="{{ route('stripe.post') }}"
                    method="post"
                    class="require-validation"
                    data-cc-on-file="false"
                    data-stripe-publishable-key="{{ env('STRIPE_KEY') }}"
                    id="payment-form">
                    @csrf
                    <div class='form-row row'>
                       <div class='col-xs-12 form-group required'>
                          <label class='control-label'>Name on Card</label> <input
                             class='form-control' value="Valentin" size='4' type='text'>
                       </div>
                    </div>
                    <div class='form-row row'>
                       <div class='col-xs-12 form-group card required'>
                           <p>4242 4242 4242 4242</p>
                          <label class='control-label'>Card Number</label> <input
                             autocomplete='off' value="4242 4242 4242 4242" class='form-control card-number' size='20'
                             type='text'>
                       </div>
                    </div>
                    <div class='form-row row'>
                       <div class='col-xs-12 col-md-4 form-group cvc required'>
                          <label class='control-label'>CVC</label> <input autocomplete='off'
                             class='form-control card-cvc' value="123" placeholder='ex. 311' size='4'
                             type='text'>
                       </div>
                       <div class='col-xs-12 col-md-4 form-group expiration required'>
                          <label class='control-label'>Expiration Month</label> <input
                             class='form-control card-expiry-month' value="06" placeholder='MM' size='2'
                             type='text'>
                       </div>
                       <div class='col-xs-12 col-md-4 form-group expiration required'>
                          <label class='control-label'>Expiration Year</label> <input
                             class='form-control card-expiry-year' value="2023" placeholder='YYYY' size='4'
                             type='text'>
                       </div>
                    </div>
                    <div class='form-row row'>
                       <div class='col-md-12 error form-group hide'>
                          <div class='alert-danger alert'>Please correct the errors and try
                             again.
                          </div>
                       </div>
                    </div>
                    <div class="row">
                       <div class="col-xs-12">
                          <button class="btn btn-primary btn-lg btn-block" type="submit">Pay ({{ $item->price }} lei)</button>
                       </div>
                    </div>
                 </form>
              </div>
           </div>
        </div>
     </div>
tykus's avatar

@wallentin30 do you really expect there should be more than one Product on the checkout page.

WAL30's avatar
Level 1

@tykus no, I only want a product to be on the checkout page, I am not using a cart.

I don't know how to send the price of the corresponding id to the post function stripePost();

Can I store it somehow in a cookie and call it from there?

tykus's avatar
tykus
Best Answer
Level 104

@wallentin30 Ok, my reason for the question was to know what you needed.

public function index($id)
{
    $product = Product::findOrFail($id);
     
    return view('frontend.checkout', compact('product'));
}

Now, there is exactly one product passed to the view template, so there is no need for the foreach loop.

 <div class="col-md-6 col-md-offset-3">
    <div class="panel panel-default credit-card-box">
        <div class="panel-heading display-table">
            <div class="row display-tr">
                <h3 class="panel-title display-td">Payment Details</h3>
                {{ $product->title }}
                <span name="price" id="price" value="{{ $product->price }}">{{ $product->price }}</span>
                <div class="display-td"></div>
            </div>
        </div><br><br>
        <div class="panel-body">
            @if (Session::has('success'))
            <div class="alert alert-success text-center">
                <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
                <p>{{ Session::get('success') }}</p>
            </div>
            @endif
            <form role="form" action="{{ route('stripe.post') }}" method="post" class="require-validation"
                data-cc-on-file="false" data-stripe-publishable-key="{{ env('STRIPE_KEY') }}" id="payment-form">
                @csrf

                <input type="hidden" name="product_id" value="{{ $product->id }}" />

                <div class='form-row row'>
                    <div class='col-xs-12 form-group required'>
                        <label class='control-label'>Name on Card</label> <input class='form-control' value="Valentin"
                            size='4' type='text'>
                    </div>
                </div>
                <div class='form-row row'>
                    <div class='col-xs-12 form-group card required'>
                        <p>4242 4242 4242 4242</p>
                        <label class='control-label'>Card Number</label> <input autocomplete='off'
                            value="4242 4242 4242 4242" class='form-control card-number' size='20' type='text'>
                    </div>
                </div>
                <div class='form-row row'>
                    <div class='col-xs-12 col-md-4 form-group cvc required'>
                        <label class='control-label'>CVC</label> <input autocomplete='off' class='form-control card-cvc'
                            value="123" placeholder='ex. 311' size='4' type='text'>
                    </div>
                    <div class='col-xs-12 col-md-4 form-group expiration required'>
                        <label class='control-label'>Expiration Month</label> <input
                            class='form-control card-expiry-month' value="06" placeholder='MM' size='2' type='text'>
                    </div>
                    <div class='col-xs-12 col-md-4 form-group expiration required'>
                        <label class='control-label'>Expiration Year</label> <input
                            class='form-control card-expiry-year' value="2023" placeholder='YYYY' size='4' type='text'>
                    </div>
                </div>
                <div class='form-row row'>
                    <div class='col-md-12 error form-group hide'>
                        <div class='alert-danger alert'>Please correct the errors and try again.</div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-xs-12">
                        <button class="btn btn-primary btn-lg btn-block" type="submit">
                            Pay ({{ $product->price }} lei)
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
</div>

Notice, I also have added a hidden input to the form: <input type="hidden" name="product_id" value="{{ $product->id }}" /> , so now you will have the Product id in the Request, which you can use to locate the product again:

public function stripePost(Request $request)
{
    $product = Product::findOrFail($request->input('product_id'));
    Stripe\Stripe::setApiKey(config('services.stripe.secret'));
    Charge::create ([
            
            "amount" =>  $product->price,
            "currency" => "ron",
            "source" => $request->stripeToken,
            "description" => "This payment is tested purpose "
    ]);
     return back();
}

Also, don't use env in the Controller, see I have replaced with config('services.stripe.secret') - if you don't know how this should work; come back again.

1 like
WAL30's avatar
Level 1

@tykus Thank you very much for your help.

I tried findOrFail before and gives me this error ErrorException Trying to get property 'title' of non-object (View: C:\xampp\htdocs\app\resources\views\frontend\checkout.blade.php)

it refers to checkout.blade.php ( {{ $product->title }})

Payment Details

{{ $product->title }} {{ $product->price }}
jlrdw's avatar

@wallentin30 did you change $products = to $product = because that findOrFail should be working.

WAL30's avatar
Level 1

@jlrdw Yes I did, also I have solved the problem with that error, but now it shows me the checkout form 4 times on the checkout page :|

Edit: Solved :D

I only have an issue, it did not send the correct number to stripe, I have 100, 400, and 500, and only get in stripe 1,4,5 Do you know why?

martinbean's avatar

@wallentin30 I don’t really understand your checkout flow. If it’s a one-page checkout for a single product, then use route-model binding.

Have a URI something like /products/{product}/purchase. A GET request shows the checkout form, a POST request handles the purchase request. You know which product the user is trying to purchase because of the parameter, so you can fetch the price from product that way.

WAL30's avatar
Level 1

@martinbean Thanks, solved

I only have an issue, it did not send the correct number to stripe, I have 100, 400, and 500, and only get in stripe 1,4,5 Do you know why?

WAL30's avatar
Level 1

I found the problem, now works as I want. Thank you very much @tykus , you're amazing!!

Please or to participate in this conversation.