【问题标题】:Error Cannot use object of type Omnipay\Stripe\Message\Response as array错误不能使用 Omnipay\Stripe\Message\Response 类型的对象作为数组
【发布时间】:2020-07-12 03:41:36
【问题描述】:

当买家付款时,我在 laravel 上出现条纹问题,它在条纹仪表板上显示成功,但我收到此错误是否有任何解决方案 (itError 不能使用 Omnipay\Stripe\Message\Response 类型的对象作为数组)

这是日志

不能使用 Omnipay\Stripe\Message\Response 类型的对象作为数组 {"userId":6,"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): 不能使用Omnipay\Stripe\Message\Response 类型的对象作为数组位于 /home/tameriu/example.com/app/Http/Controllers/OfferController.php:1349)

这是行:1349

$check_payment = Payment::where('transaction_id', $response['transactions']['0']['related_resources']['0']['sale']['id'])->first();

            // Check if a payment with this transaction is already in the database
            if ($check_payment == null) {

                // Create new payment
                $payment = new Payment;

                // Offer details
                $payment->item_id = $offer->id;
                $payment->item_type = Offer::class;

                // Page User
                $payment->user_id = Auth::user()->id;

                // Transaction details from gateway
                $payment->transaction_id = $data['id'];
                $payment->payment_method = 'stripe';
                $payment->payer_info = json_encode($data['source']);

                // Money
                $payment->total = number_format($balance_data['amount']/100, 2);
                $payment->transaction_fee = number_format($balance_data['fee']/100, 2);
                $payment->currency = strtoupper($balance_data['currency']);

                // Save payment
                $payment->save();
            }

            // Send notification to seller
            $offer->listing->user->notify(new PaymentNew($offer, $payment));

            \Alert::success('<i class="fa fa-check m-r-5"></i> ' . trans('payment.alert.successful'))->flash();
        } else {
            \Alert::error('<i class="fa fa-times m-r-5"></i> ' . trans('payment.alert.canceled'))->flash();
            Session::forget('params');
        }

        return $this->show($id);
    }

【问题讨论】:

  • 可以分享一下代码吗?
  • 我已经分享了代码
  • 你从var_dump($response);得到什么?
  • 你能用这个$check_payment = Payment::where('transaction_id', $response-&gt;transactions['0']['related_resources']['0']['sale']['id'])-&gt;first();替换这个声明$check_payment = Payment::where('transaction_id', $response['transactions']['0']['related_resources']['0']['sale']['id'])-&gt;first();吗?还有var_dump($response); 在此声明之前向我们展示response 的样子。

标签: php laravel stripe-payments


【解决方案1】:

用这个替换了控制器中的条带代码


    public function payStripe($id, $token)
        {
            // Check if user is logged in
            if (!(Auth::check())) {
                return Redirect::to('/login');
            }
    
            $offer =  Offer::withTrashed()->find($id);
            // check if offer exist or is deleted
            if (!$offer) {
                return abort('404');
            }
    
            $listing = Listing::with('game', 'user', 'game.giantbomb', 'game.platform')->withTrashed()->find($offer->listing_id);
    
            // check if listing exist
            if (!$listing) {
                return abort('404');
            }
    
            // check if user is offer user
            if (Auth::user()->id != $offer->user_id) {
                \Alert::error('<i class="fa fa-times m-r-5"></i> ' . trans('payment.alert.canceled'))->flash();
                return $this->show($id);
            }
    
            // check if offer already paid
            if ($offer->payment && $offer->payment->status) {
                \Alert::error('<i class="fa fa-times m-r-5"></i> ' . trans('payment.alert.already_paid'))->flash();
                return $this->show($id);
            }
    
            $gateway = Omnipay::create('Stripe');
    
            // Initialise the gateway
            $gateway->initialize(array(
                'apiKey' => config('settings.stripe_client_secret'),
            ));
    
            $response = $gateway->purchase([
                'amount' => str_replace(',', '.', money($offer->price_offer + $listing->delivery_price, Config::get('settings.currency'))->format(false)),
                'currency' => Config::get('settings.currency'),
                'token' => $token,
                'expand' => array('balance_transaction'),
            ])->send();
    
            // check if payment is approved
            if ($response->isSuccessful()) {
    
                $data = $response->getData();
    
                // Fetch the balance to get information about the payment.
                $balance = $gateway->fetchBalanceTransaction();
                $balance->setBalanceTransactionReference($response->getBalanceTransactionReference());
                $response_balance = $balance->send();
                $balance_data = $response_balance->getData();
    
                // Create new payment
                $payment = new Payment;
    
                // Offer details
                $payment->item_id = $offer->id;
                $payment->item_type = Offer::class;
    
                // Page User
                $payment->user_id = Auth::user()->id;
    
                // Transaction details from gateway
                $payment->transaction_id = $data['id'];
                $payment->payment_method = 'stripe';
                $payment->payer_info = json_encode($data['source']);
    
                // Money
                $payment->total = number_format($balance_data['amount']/100, 2);
                $payment->transaction_fee = number_format($balance_data['fee']/100, 2);
                $payment->currency = strtoupper($balance_data['currency']);
    
                // Save payment
                $payment->save();
    
                // Send notification to seller
                $offer->listing->user->notify(new PaymentNew($offer, $payment));
    
                \Alert::success('<i class="fa fa-check m-r-5"></i> ' . trans('payment.alert.successful'))->flash();
            } else {
                return print_r($response);
                \Alert::error('<i class="fa fa-times m-r-5"></i> ' . trans('payment.alert.canceled'))->flash();
                Session::forget('params');
            }
    
            return $this->show($id);
        }

【讨论】:

    猜你喜欢
    • 2019-12-09
    • 2018-04-12
    • 2019-12-11
    • 2016-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多