在我的 Laravel 网站上付款成功后,首先调用 null 的成员函数

问题描述 投票:0回答:1

在 null 上调用成员函数 first() 54 错误 …/platform/plugins/stripe/src/Http/Controllers/StripeController.php38

这是我的条带控制器的代码:付款成功,但之后显示此错误而不是确认付款:

class StripeController extends Controller
{
    /**
     * @param StripePaymentCallbackRequest $request
     * @param StripePaymentService $stripePaymentService
     * @param BaseHttpResponse $response
     * @return BaseHttpResponse
     */
    public function success(StripePaymentCallbackRequest $request, StripePaymentService $stripePaymentService, BaseHttpResponse $response)
    {
        try {
            $stripePaymentService->setClient();

            $session = Session::retrieve($request->input('session_id'));

            if ($session->payment_status == 'paid') {
                $metadata = $session->metadata->toArray();

                $orderIds = json_decode($metadata['order_id'], true);

                $charge = PaymentIntent::retrieve($session->payment_intent);

                $chargeId = $charge->charges->first()->id;

                do_action(PAYMENT_ACTION_PAYMENT_PROCESSED, [
                    'amount' => $metadata['amount'],
                    'currency' => strtoupper($session->currency),
                    'charge_id' => $chargeId,
                    'order_id' => $orderIds,
                    'customer_id' => Arr::get($metadata, 'customer_id'),
                    'customer_type' => Arr::get($metadata, 'customer_type'),
                    'payment_channel' => STRIPE_PAYMENT_METHOD_NAME,
                    'status' => PaymentStatusEnum::COMPLETED,
                ]);

                return $response
                    ->setNextUrl(PaymentHelper::getRedirectURL() . '?charge_id=' . $chargeId)
                    ->setMessage(__('Checkout successfully!'));
            }

            return $response
                ->setError()
                ->setNextUrl(PaymentHelper::getCancelURL())
                ->setMessage(__('Payment failed!'));
        } catch (Exception $exception) {
            return $response
                ->setError()
                ->setNextUrl(PaymentHelper::getCancelURL())
                ->withInput()
                ->setMessage($exception->getMessage() ?: __('Payment failed!'));
        }
    }

    /**
     * @param BaseHttpResponse $response
     * @return BaseHttpResponse
     */
    public function error(BaseHttpResponse $response)
    {
        return $response
            ->setError()
            ->setNextUrl(PaymentHelper::getCancelURL())
            ->withInput()
            ->setMessage(__('Payment failed!'));
    }
}

我已经尝试了 StackOverflow 中的所有解决方案,但都不起作用,请帮我解决这个问题。

php laravel stripe-payments payment-gateway checkout
1个回答
0
投票
$charge = PaymentIntent::retrieve($session->payment_intent);
$chargeId = $charge->charges->first()->id;

如果我正确理解这段代码,您正在寻找付款意图的

id
数组的第一个元素的
charges
first()
函数可能会返回错误,因为没有
charges
数组。

根据您的帐户/请求中设置的 API 版本,您的付款意向中可能没有

charges
数组 - 自“2022-11-15”起,它已被
latest_charge
属性替换:
https://stripe.com/docs/upgrades#2022-11-15

如果我是正确的,如果您将 API 版本硬设置为“2022-08-01”等旧版本,您的代码应该可以正确运行:
https://stripe.com/docs/libraries/set-version

也就是说,我建议更改您的代码以适应新的更改:

$charge = PaymentIntent::retrieve(
   $session->payment_intent,
   ['expand' => ['latest_charge']]
);
$chargeId = $charge->charges->first()->id;

(您需要

expand
latest_charge
才能将其退回)

仅供参考,我不确定上面的代码是否有效,因为您似乎使用的是 stripe-php 的旧格式,它可以追溯到 7.33.0 之前的版本,这些被认为是遗留的。在这里了解更多:
https://github.com/stripe/stripe-php/wiki/Migration-to-StripeClient-and-services-in-7.33.0

© www.soinside.com 2019 - 2024. All rights reserved.