skip to Main Content

My project works perfectly in mode ‘sandbox’ but when I go to put it in mode ‘live’ (I did it correctly as indicated in the instructions in PayPal-PHP-SDK, my credentials are correct and I put mode ‘live’ instead of mode ‘sandbox’).
It gives me the following error:

PayPal  Exception  PayPalConnectionException
Got Http response code 401 when accessing https://api.sandbox.paypal.com/v1/oauth2/token.

Looking for this error notice that it happened to others but it was the following error:

PayPal  Exception  PayPalConnectionException
Got Http response code 401 when accessing https://api.paypal.com/v1/oauth2/token.

Why does my error say sandbox if I have mode ‘live’? So I started looking because if I had mode ‘live’ I kept getting api.sandbox.paypal.com instead of api.paypal.com error.

and get to vendor paypal rest-api-sdk-php lib PayPal Handler OauthHandler.php (Which I have never modified) has a _getEndPoint method.

 private static function _getEndpoint($config)
{
    if (isset($config['oauth.EndPoint'])) {
        $baseEndpoint = $config['oauth.EndPoint'];
    } elseif (isset($config['service.EndPoint'])) {
        $baseEndpoint = $config['service.EndPoint'];
    } elseif (isset($config['mode'])) {
        switch (strtoupper($config['mode'])) {
            case 'SANDBOX':
                $baseEndpoint = PayPalConstants::REST_SANDBOX_ENDPOINT;
                break;
            case 'LIVE':
                $baseEndpoint = PayPalConstants::REST_LIVE_ENDPOINT;
                break;
            default:
                throw new PayPalConfigurationException('The mode config parameter must be set to either sandbox/live');
        }
    } else {
        // Defaulting to Sandbox
        $baseEndpoint = PayPalConstants::REST_SANDBOX_ENDPOINT;
    }

    $baseEndpoint = rtrim(trim($baseEndpoint), '/') . "/v1/oauth2/token";

    return $baseEndpoint;
}

I noticed that the config always arrives empty in that part, when it arrives empty it goes to the case by default
which is sandbox. That’s why sandobox works for me even though nothing is coming either.
Any idea why this can happen. I really have no idea, any help is welcome.

2

Answers


  1. Chosen as BEST ANSWER

    I have a solution, it is not a very elegant one but it works for me. In vendorpaypalrest-api-sdk-phplibPayPalCorePayPalsConstants.php change the variable REST_SANDBOX_ENDPOINT from "http://api.sandbox.paypal.com" to "http://api.paypal.com"


  2. I think you need mode ‘production’ for that SDK’s config

    But you are using a deprecated v1 SDK that is no longer maintained

    You should be using the v2 Checkout-PHP-SDK, documented here: https://developer.paypal.com/docs/checkout/reference/server-integration/


    By the way, the best UI approval flow to pair it with is here: https://developer.paypal.com/demo/checkout/#/pattern/server

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search