1

您好,我一直在尝试使用 Omnipay paypal 和 Laravel 4 将 paypal 与我的网站的购物车集成。到目前为止,我主要使用这个教程。

我仍处于初始阶段,但我遇到了障碍。当我尝试结帐时,我收到一条错误消息,提示“需要金额参数”。

我有点菜鸟,所以我可能会做一些愚蠢的事情,但如果我硬编码数量(即:'price' => 25.00,),那么它应该可以正常工作。解密和货币也都从数据库中提取并发送到贝宝页面罚款。我在这里发现的问题似乎并没有人们将数据动态地拉到他们的控制器上,所以也许我做错了什么?

这是我的控制器的相关部分:

<?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(); 

这是我的购物车结帐按钮:

          {{ 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()}}

表格可能看起来有点混乱,但在我提交之前,表格上的值都显示得很好。

谢谢你的帮助。

4

1 回答 1

3

如果你仔细查看教程,你会看到有一个index()函数负责生成表单。以及postPayment()处理表单提交的函数。

在 index() 函数中(在教程中)

hello.blade.php里面有一个参数叫做price

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

在你的情况下

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

应该替换为

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

那么当你提交表单时,它会路由到postPayment()函数,在这里,所以Route::post('pay_via_paypal', 'PaymentController@postPayment');这个路由应该在你的route文件中

postPayment()功能上,

$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') ); 

只是为了说明,

您重复使用Product::find($productID)这不是一个好习惯,如果您将该产品放入一个 Object 变量中,您可以使用该对象而无需重复Product::find($productID)

为此,您可以object从控制器传递到刀片视图,

喜欢,

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

在刀片视图中,

....

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

....

.. 很快

于 2015-02-01T04:48:22.783 回答