How are you passing to stripePost?
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
@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.
Please or to participate in this conversation.