【问题标题】:Paypal integration with Laravel 5.1Paypal 与 Laravel 5.1 的集成
【发布时间】:2016-01-06 05:05:43
【问题描述】:

我正在尝试将支付网关 (paypal) 与 laravel 5.1 集成。我找到了一些适用于 laravel 4 的解决方案,但没有找到适用于 laravel 5.0 及更高版本的解决方案。

我一直在遵循此链接中提到的所有步骤。 xroot/laravel-paypalpayment

但我在 PaypalPaymentController.php 第 10 行收到 FatalErrorException:

我附上了我的 Paypal 集成代码,以及错误的屏幕截图。

Router.php

resource('payment','PaymentController');

app.php

Anouar\Paypalpayment\PaypalpaymentServiceProvider::class,
'Paypalpayment'   => Anouar\Paypalpayment\Facades\PaypalPayment::class,

PaypalPaymentController.php

    <?php
namespace my_app\Http\Controllers;
use Illuminate\Support\Facades\Cache;
use Illuminate\Http\Request;
use my_app\Http\Requests;
use my_app\Http\Controllers\Controller;
use Paypalpayment;
    class PaypalPaymentController extends Facades {
    private $_apiContext;
    private $_ClientId=''/* ... */;
    private $_ClientSecret=''/* ... */;

    public function __construct(){
        // ### Api Context
        // Pass in a `ApiContext` object to authenticate 
        // the call. You can also send a unique request id 
        // (that ensures idempotency). The SDK generates
        // a request id if you do not pass one explicitly. 
        $this->_apiContext = Paypalpayment:: ApiContext(
                Paypalpayment::OAuthTokenCredential(
                    $this->_ClientId,
                    $this->_ClientSecret
                )
        );
        // dynamic configuration instead of using sdk_config.ini
        $this->_apiContext->setConfig(array(
            'mode' => 'sandbox',
            'http.ConnectionTimeOut' => 30,
            'log.LogEnabled' => true,
            'log.FileName' => __DIR__.'/../PayPal.log',
            'log.LogLevel' => 'FINE'
        ));
    }
    /*
     * Create payment using credit card
     * url:payment/create
    */
    public function create(){
        // ### Address
        // Base Address object used as shipping or billing
        // address in a payment. [Optional]
        $addr= Paypalpayment::Address();
        $addr->setLine1(/* ... */);
        $addr->setLine2(/* ... */);
        $addr->setCity(/* ... */);
        $addr->setState(/* ... */);
        $addr->setPostal_code(/* ... */);
        $addr->setCountry_code(/* ... */);
        $addr->setPhone(/* ... */);
        // ### CreditCard
        // A resource representing a credit card that can be
        // used to fund a payment.
        $card = Paypalpayment::CreditCard();
        $card->setType(/* ... */);
        $card->setNumber(/* ... */);
        $card->setExpire_month(/* ... */);
        $card->setExpire_year(/* ... */);
        $card->setCvv2(/* ... */);
        $card->setFirst_name(/* ... */);
        $card->setLast_name(/* ... */);
        $card->setBilling_address($addr);
        // ### FundingInstrument
        // A resource representing a Payer's funding instrument.
        // Use a Payer ID (A unique identifier of the payer generated
        // and provided by the facilitator. This is required when
        // creating or using a tokenized funding instrument)
        // and the `CreditCardDetails`
        $fi = Paypalpayment::FundingInstrument();
        $fi->setCredit_card($card);
        // ### Payer
        // A resource representing a Payer that funds a payment
        // Use the List of `FundingInstrument` and the Payment Method
        // as 'credit_card'
        $payer = Paypalpayment::Payer();
        $payer->setPayment_method("credit_card");
        $payer->setFunding_instruments(array($fi));
        // ### Amount
        // Let's you specify a payment amount.
        $amount = Paypalpayment:: Amount();
        $amount->setCurrency("USD");
        $amount->setTotal("1.00");
        // ### Transaction
        // A transaction defines the contract of a
        // payment - what is the payment for and who
        // is fulfilling it. Transaction is created with
        // a `Payee` and `Amount` types
        $transaction = Paypalpayment:: Transaction();
        $transaction->setAmount($amount);
        $transaction->setDescription("This is the payment description.");
        // ### Payment
        // A Payment Resource; create one using
        // the above types and intent as 'sale'
        $payment = Paypalpayment:: Payment();
        $payment->setIntent("sale");
        $payment->setPayer($payer);
        $payment->setTransactions(array($transaction));
        // ### Create Payment
        // Create a payment by posting to the APIService
        // using a valid ApiContext
        // The return object contains the status;
        try {
            $payment->create($this->_apiContext);
        } catch (\PPConnectionException $ex) {
            return "Exception: " . $ex->getMessage() . PHP_EOL;
            var_dump($ex->getData());
            exit(1);
        }
        $response=$payment->toArray();

        echo"<pre>";
        print_r($response);
        //var_dump($payment->getId());
        //print_r($payment->toArray());//$payment->toJson();
    }  
    /*
        Use this call to get a list of payments. 
        url:payment/
    */
    public function index(){
        echo "<pre>";
        $payments = Paypalpayment::all(array('count' => 1, 'start_index' => 0),$this->_apiContext);
         print_r($payments);
    }
}

我得到的错误:

【问题讨论】:

  • 问题是这样的:PaypalPaymentController extends Facades - 你正在扩展一个名为Facades 的类,它似乎不在my_app\Http\Controllers 命名空间下。如果是,则运行 composer dump-auto,否则正确命名它(然后运行 ​​composer dump-auto 以确保自动加载器能够拾取它)。
  • @Quasdunk:我做了 composer dump-auto,它给出了 Generating autoload files 作为输出,但我仍然遇到同样的错误。跨度>
  • 嗯,错误信息非常简单。它找不到Facades 类,这意味着:您的Facedes-class 不在my_app\Http\Controllers 下命名空间。您能否仅显示 Facades 类的头部(仅显示类声明和导入,而不是类主体)?
  • @Quasdunk:这是 vendor 文件夹内的 PaypalPayment.php 文件 &lt;?php namespace Anouar\Paypalpayment\Facades; use Illuminate\Support\Facades\Facade; class PaypalPayment extends Facade { /** * Get the registered name of the component. * * return string */ protected static function getFacadeAccessor() { return 'paypalpayment'; } }
  • 对不起,但这对我来说真的没有意义。那你为什么要扩展你的 Controller 类呢?对于您收到的“400 Bad Request”响应:您可能缺少一些必填字段或者您发送的格式错误。

标签: php paypal payment-gateway laravel-5.1


【解决方案1】:

我不知道你的代码库,但如果你的类是控制器,你为什么要扩展 Facades 类?问题是您的代码正在您的 my_app\Http\Controllers 命名空间中寻找 Facades 类。

删除该行或正确导入类将解决您的问题。但是,我认为您可能需要在这里重新考虑您的设计。

【讨论】:

  • 如果我扩展了 Controller 类而不是 Facades,我会收到类似这样的新错误:Class 'Anouar\Paypalpayment\Facades\PaypalPayment' not found
猜你喜欢
  • 2018-01-25
  • 2016-12-26
  • 2020-10-19
  • 2016-03-02
  • 2017-05-11
  • 2021-01-06
  • 2017-11-22
  • 2015-11-02
  • 2015-12-20
相关资源
最近更新 更多