不能使用 PayPal\Api\Amount 类型的对象作为数组

问题描述

我有一个错误:“不能使用 PayPal\Api\Amount 类型的对象作为数组”,我有点困惑要解决的第一件事是什么。以下是错误

E:\laragon\www\thepaymentss\app\Http\Controllers\PaypalController.PHP

    if (empty(Input::get('PayerID')) || empty(Input::get('token'))) {
        \Session::put('error','Payment Failed');
        return Redirect::route('paywithpaypal');
    }
    $payment = Payment::get($payment_id,$this->_api_context);
    /** PaymentExecution object includes information necessary **/
    /** to execute a PayPal account payment. **/
    /** The payer_id is added to the request query parameters **/
    /** when the user is redirected from paypal back to your site **/
    $execution = new PaymentExecution();
    $execution->setPayerId(Input::get('PayerID'));
    /**Execute the payment **/
    $result = $payment->execute($execution,$this->_api_context);
    /** dd($result);exit; /** DEBUG RESULT,remove it later **/
    if ($result->getState() == 'approved') { 
        // dd($result);

        $transactions=new Transactions();
        // $transactions->id=$result->id;
        $transactions->amount=$result->transactions[0]->amount[0]->total;
        $transactions->sender=$result->payer[0]->payer_info[0]->email;
        $transactions->type=$result->payer[0]->payment_method;
        $transactions->currency=$result->transactions[0]->amount[0]->currency;
        $transactions->description=$result->transactions[0]->description;
        $transactions->fee='0';
        $transactions->client_id=1;
        $transactions->status=$result->state;
        $transactions->receiver=$result->transactions[0]->payee[0]->email;

        $transactions->Save();



        /** it's all right **/
        /** Here Write your database logic like that insert record or value in database if you want **/

        \Session::put('success','Payment success');
        return Redirect::route('paywithpaypal');
    }
        \Session::put('error','Payment Failed');

不能将类型为 PayPal\Api\Amount 的对象用作数组”的错误在行

$transactions->amount=$result->transactions[0]->amount[0]->total;

这是Paypal/Api/Amount代码

    <?PHP

namespace PayPal\Api;

use PayPal\Common\PayPalModel;
use PayPal\Converter\FormatConverter;
use PayPal\Validation\NumericValidator;

/**
 * Class Amount
 *
 * payment amount with break-ups.
 *
 * @package PayPal\Api
 *
 * @property string currency
 * @property string total
 * @property \PayPal\Api\Details details
 */
class Amount extends PayPalModel
{
    /**
     * 3-letter [currency code](https://developer.paypal.com/docs/integration/direct/rest_api_payment_country_currency_support/). PayPal does not support all currencies.
     *
     * @param string $currency
     * 
     * @return $this
     */
    public function setCurrency($currency)
    {
        $this->currency = $currency;
        return $this;
    }

    /**
     * 3-letter [currency code](https://developer.paypal.com/docs/integration/direct/rest_api_payment_country_currency_support/). PayPal does not support all currencies.
     *
     * @return string
     */
    public function getCurrency()
    {
        return $this->currency;
    }

    /**
     * Total amount charged from the payer to the payee. In case of a refund,this is the refunded amount to the original payer from the payee. 10 characters max with support for 2 decimal places.
     *
     * @param string|double $total
     * 
     * @return $this
     */
    public function setTotal($total)
    {
        NumericValidator::validate($total,"Total");
        $total = FormatConverter::formatToPrice($total,$this->getCurrency());
        $this->total = $total;
        return $this;
    }

    /**
     * Total amount charged from the payer to the payee. In case of a refund,this is the refunded amount to the original payer from the payee. 10 characters max with support for 2 decimal places.
     *
     * @return string
     */
    public function getTotal()
    {
        return $this->total;
    }

    /**
     * Additional details of the payment amount.
     *
     * @param \PayPal\Api\Details $details
     * 
     * @return $this
     */
    public function setDetails($details)
    {
        $this->details = $details;
        return $this;
    }

    /**
     * Additional details of the payment amount.
     *
     * @return \PayPal\Api\Details
     */
    public function getDetails()
    {
        return $this->details;
    }

}

有人可以解释并帮助我为什么它不起作用吗?

解决方法

$transactions->amount=$result->transactions[0]->amount[0]->total;

正如错误消息所说,您不能将该“金额”用作数组。它不是一个数组,它是一个对象。所以:

$transactions->amount=$result->transactions[0]->amount->total;