1
votes

Hello I've been trying to integrate paypal with the shopping cart for my site using Omnipay paypal with Laravel 4. I've mainly used THIS tutorial so far.

I'm still in the initial stages but I've hit a snag. When I try to checkout I get an error that says "The amount parameter is required".

I am a bit of a noob so I might be doing something stupid but if I hardcode in the amount (ie: 'price' => 25.00, )then it works as it should. The decription and currency are also both being pulled from the database and sent to the paypal page fine. The questions I've found on here don't seem to have people pulling data dynamically to their controller so maybe I'm doing something wrong?

Here's the relevant part of my controller:

<?php 
use Omnipay\Omnipay; 

class PaymentController extends Controller { 

     public function postPayment() { 

        $params = array( 
            'cancelUrl' => 'http://localhost/cancel_order', 
            'returnUrl' => 'http://localhost/payment_success', 
            'name'  => Input::get('name'), 
            'description' => Input::get('description'), 
            'price' => Input::get('price'), 
            'currency' => Input::get('currency') ); 

            Session::put('params', $params); 

            Session::save(); 

            $gateway = Omnipay::create('PayPal_Express'); 

            $gateway->setUsername('my username'); 

            $gateway->setPassword('my pass'); 

            $gateway->setSignature('my signature'); 

            $gateway->setTestMode(true); 



            $response = $gateway->purchase($params)->send(); 

Here's my cart checkout button:

          {{ Form::open([ 'url' => 'pay_via_paypal', 'method' => 'post'  ]) }}
            {{Form::hidden('product',Product::find($productID)->name)}}
            {{Form::hidden('description',Product::find($productID)->description)}}
            {{Form::hidden('amount',Product::find($productID)->price)}}  
            {{Form::hidden('currency',Product::find($productID)->currency)}}
            {{Form::submit('CHECKOUT')}}
          {{Form::close()}}

The form might look a bit confusing but the values are all showing up fine on the form before I submit.

Thanks for any help.

1

1 Answers

3
votes

If you check the tutorial carefully, you will see there is a index() function is there which is responsible for generating the form. and the postPayment() function which handles the form submit.

In the index() function (in the tutorial)

in the hello.blade.php there is a parameter called price

<input type="hidden" value="{{ $price }}" name="price" />

in your case

{{ Form::hidden('amount',Product::find($productID)->price) }}  

should replace with

{{ Form::hidden('price',Product::find($productID)->price) }}  

then when you submitting the form it will route to the postPayment() function, in here, so Route::post('pay_via_paypal', 'PaymentController@postPayment'); this route should be in your route file

in postPayment() function,

$params = array( 
        'cancelUrl' => 'http://localhost/cancel_order', 
        'returnUrl' => 'http://localhost/payment_success', 
        'name'  => Input::get('name'), 
        'description' => Input::get('description'), 
        // you dont need this price parameter ('price' => Input::get('price'),) 
        'amount' => Input::get('price'), // add amount parameter which is required in paypal.
        'currency' => Input::get('currency') ); 

just for note,

you are repeatedly use Product::find($productID) which is not a good practice, If you get that product into a Object variable then, You can use that object without repeating Product::find($productID).

to do that you can pass the object to the blade view, from the controller,

like,

$product = Product::find($productId);
return View::make('hello')->with(Array("product" => $product));

in the blade view,

....

{{ Form::hidden('product',$product->name) }}
{{ Form::hidden('description',$product->description) }}

....

.. so on