首页 > 解决方案 > Omnipay Paypal Express 结帐错误:安全标头无效

问题描述

每当使用 setTestMode() 方法时,都会弹出错误“Security header is not valid”。

但是,如果我删除 setTestMode() 并只保留 setUsername()、setPassword() 和 setSignature() 方法,它会通过并直接重定向到 PayPal(Live Paypal)。

因此,AFAIK 问题应该在于我如何使用 setTestMode 而不是错误的 API 凭据,因为大多数“安全标头无效”错误都是关于。

我目前正在使用 Laravel 5.8 和 Omnipay/PayPal 使用 PayPal Express Checkout

以下是使用的文件

贝宝.php

public function gateway()
{
    $gateway = Omnipay::create('PayPal_Express');

    $gateway->setUsername(config('services.paypal.username'));
    $gateway->setPassword(config('services.paypal.password'));
    $gateway->setSignature(config('services.paypal.signature'));
    $gateway->setTestMode(config('services.paypal.sandbox'));
    // $gateway->setTestMode(true);

    return $gateway;
}

public function purchase(array $parameters)
{
    $response = $this->gateway()
        ->purchase($parameters)
        ->send();

    return $response;
}

贝宝控制器.php

public function checkout($order_id)
{
    $order = Order::findOrFail(decrypt($order_id));

    $paypal = new PayPal;

    $response = $paypal->purchase([
        'amount' => $paypal->formatAmount($order->amount),
        'transactionId' => $order->transaction_id,
        'currency' => 'PHP',
        'cancelUrl' => $paypal->getCancelUrl($order),
        'returnUrl' => $paypal->getReturnUrl($order),
        'notifyUrl' => $paypal->getNotifyUrl($order),
    ]);

    if ($response->isRedirect()) {
        $response->redirect();
    }

    return redirect()->back()->with([
            'message' => $response->getMessage(),                
    ]);
}

这是 $response 的内容

ExpressAuthorizeResponse {#1098 ▼
  #liveCheckoutEndpoint: "https://www.paypal.com/cgi-bin/webscr"
  #testCheckoutEndpoint: "https://www.sandbox.paypal.com/cgi-bin/webscr"
  #request: ExpressAuthorizeRequest {#1095 ▼
    #liveEndpoint: "https://api-3t.paypal.com/nvp"
    #testEndpoint: "https://api-3t.sandbox.paypal.com/nvp"
    #negativeAmountAllowed: true
    #parameters: ParameterBag {#1097 ▶}
    #httpClient: Client {#1063 ▶}
    #httpRequest: Request {#1086 ▶}
    #response: ExpressAuthorizeResponse {#1098}
    #currencies: ISOCurrencies {#1096}
    #zeroAmountAllowed: true
  }
  #data: array:9 [▼
    "TIMESTAMP" => "2020-02-03T11:04:45Z"
    "CORRELATIONID" => "c8d066c9b5ccd"
    "ACK" => "Failure"
    "VERSION" => "119.0"
    "BUILD" => "54118205"
    "L_ERRORCODE0" => "10002"
    "L_SHORTMESSAGE0" => "Security error"
    "L_LONGMESSAGE0" => "Security header is not valid"
    "L_SEVERITYCODE0" => "Error"
  ]
}

- -编辑 - -

刚刚创建了一个新的沙盒业务帐户并使用了该帐户的 NVP API 凭据,然后它就可以工作了!旧帐户的问题可能是我编辑的电子邮件,因为这是新旧帐户之间的唯一区别。

标签: laravellaravel-5paypalpaypal-sandboxomnipay

解决方案


实时和测试/沙盒环境是完全独立的,因此需要单独的凭据。

对于 PayPal 沙盒(测试模式),您需要来自沙盒PayPal 企业帐户的 API 用户名、密码和签名,通过https://www.paypal.com/signin?intent=developer&returnUri=https%3A%2F%2Fdeveloper .paypal.com%2F开发者%2Faccounts%2F


10002 "Security Header is not Valid" 总是表示您的凭据无效,即用户名/密码/签名有问题,或者您在沙盒模式下使用实时的,反之亦然


推荐阅读