You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Omnipay对接PayPal支付时出现身份验证失败错误的排查求助

Omnipay对接PayPal支付时出现身份验证失败错误的排查求助

我现在用Omnipay集成PayPal支付功能,但是一直遇到下面这个错误:

Payment failed: Authentication failed due to invalid authentication credentials or a missing Authorization header.

我已经换了好几个账号测试,结果都报这个错,所以实在不觉得是凭证本身的问题。下面是我写的支付相关代码,麻烦各位帮忙排查下哪里可能出问题了?

生成PayPal支付的代码

public function generatePaypalPaymentByTransaction(Transaction $transaction) {
    try {
        $gateway = Omnipay::create('PayPal_Rest');
        $gateway->setClientId(config('paypal.client_id'));
        $gateway->setSecret(config('paypal.secret'));
        $gateway->setTestMode(config('paypal.test_mode'));
        
        
        $urlHost = rtrim(config('paypal.url_host'), '/') . '/';

        if($transaction->type != Transaction::DEPOSIT_TYPE) {
            $username = User::find($transaction->recipient_user_id)->username;
            $username = rawurlencode($username); // Codificar el username para la URL
        }
        $returnUrl = $urlHost . 'pay/execute-payment' . '?transaction_id=' . $transaction->id;
        
        if($transaction->type == Transaction::ROULETTE) {
            $cancel_url = $urlHost . 'roulette/' . $username . '/' . $transaction->roulette_id;
        } else {
            $cancel_url = $urlHost . 'feed';
        }
        
        if($transaction->type == 'deposit') {
            // Incrementa el amount en un 10%
            $transaction->amount = $transaction->amount * 1.10;
        }

        $response = $gateway->purchase([
            'amount' => $transaction->amount,
            'currency' => config('paypal.currency'),
            'returnUrl' => $returnUrl,
            'cancelUrl' => $cancel_url,
        ])->send();

        if($response->isRedirect()) {
            $response->redirect();
        } else {
            return $response->getMessage();
        }
    } catch (Throwable $e){
        return $e->getMessage();
    }
}

确认支付的代码

public function completePurchase($PayerID, $paymentId, $amount)
{
    try {
        Log::info("Pre Complte Purchase");
        $response = $this->gateway->completePurchase([
            'payer_id' => $PayerID,
            'transactionReference' => $paymentId,
            'amount' => $amount,
            'currency' => env('PAYPAL_CURRENCY'),
        ])->send();
        Log::info("Post Complte Purchase");
        if ($response->isSuccessful()) {
            Log::info('Payment completed successfully: ' . json_encode($response->getData()));
        } else {
            Log::error('Payment failed: ' . $response->getMessage());
        }

        return $response;
    } catch (Throwable $e){
        return $e->getMessage();
    }
}

支付成功回调的处理代码

public function success (Request $request)
{
    $transaction = Transaction::find($request->transaction_id);

    Log::info("Request All");
    Log::info($request->all());
    $response = $this->completePurchase($request->PayerID, $request->paymentId, $request->amount);

    if ($response instanceof \Omnipay\Common\Message\ResponseInterface && $response->isSuccessful()) {
        // El pago se completó con éxito
        Log::info('Payment completed successfully.');
       
    } else {
        // El pago falló
        Log::error('Payment failed or an error occurred.');
        // Actualizar el estado de la transacción a 'declined'
        $transaction->status = Transaction::DECLINED_STATUS;
        $transaction->save();

        if($transaction->type === Transaction::ROULETTE) {
            // Redirigir al usuario a la página de error
            return redirect()->route('roulette.show', ['username' => rawurlencode(User::find($transaction->recipient_user_id)->username), 'roulette_id' => $transaction->roulette_id])
                             ->with('error', __('Payment failed, please try again.'));
        } else if ($transaction->type === Transaction::TIP_TYPE) {
            // Redirigir al usuario a la página de error
            return redirect()->route('profile', ['username' => rawurlencode(User::find($transaction->recipient_user_id)->username)])
                             ->with('error', __('Payment failed, please try again.'));
        } else {
            // Redirigir al usuario a la página de error
            return redirect()->route('feed')->with('error', __('Payment failed, please try again.'));
        }
    }
}

备注:内容来源于stack exchange,提问作者José Vicente López Delgadillo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 15:18:04