kxh
kxh

Reputation: 59

Request doesnt get the input field name

I'm currently working on a project where I ndeed to sell user credits so users can buy merch with these credits. I have set up the stripe API to charge the users when they buy coins but when I try to pass the quantity that the user selects at the function below it doesn't work and the credits don't update. It does not show an error either, the payment goes through but the credits don't. Here is the controller code:

 public function pay(Request $request){
        Stripe::setApiKey(config('services.stripe.secret'));
        try{
        $charge = Charge::create([
            'amount' => Cart::total() * 100,
            'currency' => 'usd',
            'description' => 'test product',
            'source' => request()->stripeToken
        ]);
        Session::flash('success', 'Purchase successfull. wait for our email.');

        Cart::destroy();



        $credits = UserCredit::where('user_id', '=', Auth::user()->id)->first();
        $qty = $request->get('qty');
        $credits->amount = $credits->amount + $qty;

        $credits->save();

        Mail::to(request()->stripeEmail)->send(new \App\Mail\PurchaseSuccessful);

        } catch (Exception $exception) {
            Session::flash('failure', 'Purchase failed. please try again');
        }
        return redirect('/');
    }

And here is the input where the user chooses the number of credits he wants to buy

<td class="product-quantity">

<div class="quantity">
<a href="{{ route('cart.decr', ['id' => $pdt->rowId, 'qty' => $pdt->qty ]) }}" class="quantity-minus">-</a>

<input title="Qty" name="qty", id="qty" class="email input-text text" type="text" value="{{ $pdt->qty }}" placeholder="1" readonly>

 <a href="{{ route('cart.incr', ['id' => $pdt->rowId, 'qty' => $pdt->qty ]) }}" class="quantity-plus">+</a>
      </div>

</td>

Do I have a logic error here. Are there other ways to do this? Thanks in advance

Here is the submit

<form action="{{ route('cart.add') }}" method="post">
                            @csrf
                            <div class="quantity">
                                <a href="#" class="quantity-minus quantity-minus-d">-</a>
                                <input title="Qty" class="email input-text qty text" name="qty" id="qty" type="text" value="1">
                                <a href="#" class="quantity-plus quantity-plus-d">+</a>
                            </div>

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

                            <button class="btn btn-medium btn--primary">
                                <span class="text">Add to Cart</span>

                                <span class="semicircle"></span>
                            </button>
                        </form>

The decr and incr functions

 public function incr($id, $qty){
        Cart::update($id, $qty + 99);
        Session::flash('succes', 'Product qunatity updated.');

        return redirect()->route('cart');
    }

    public function  decr($id, $qty){
        Cart::update($id, $qty - 100);
        Session::flash('succes', 'Product qunatity updated.');

        return redirect()->back();
    }

the form that points to pay controller

 <span style="float: right;">
                                                            <form action="{{ route('cart.checkout') }}" method="POST">
                                                                  @csrf
                                                                  <script
                                                                      src="https://checkout.stripe.com/checkout.js" class="stripe-button"
                                                                      data-key="pk_test_51IxqgMAlDT4eyPaP6esPvqCU1oG6GFqtF9ZhOb0oytpk2WnYA99OcD0kqnNs8VOEm18CpV9k7wYI5VGMwRFXahp700RezbuxMS"
                                                                      data-amount="{{ Cart::total() * 100 }}"
                                                                      data-name="BetGame"
                                                                      data-description="Buy our products"
                                                                      data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
                                                                      data-locale="auto">
                                                                  </script>
                                                            </form>
                                                      </span>
                                    <div>

Upvotes: 0

Views: 85

Answers (2)

BalmainRob
BalmainRob

Reputation: 365

To retrieve an input value for any HTTP verb use:

$qty = $request->input('qty');

Upvotes: 0

kxh
kxh

Reputation: 59

Solved thanks to @nitrin0 suggestion above to use count function of the cart package

$qty = Cart::count();
            $credits->amount = $credits->amount + $qty;
            $credits->save();

Upvotes: 1

Related Questions