PayPal Rest API - End Point Used

538 views Asked by At

In my Dev environment, I can correctly use the Rest API to the sandbox site:

    $sdkConfig = array(
         "mode" => 'sandbox'
    );

    $cred = new OAuthTokenCredential(
        $SANDBOX_clientId,
        $SANDBOX_clientSecret
    );

    $access_token = $cred->getAccessToken($sdkConfig);

When using the same code with Live Keys and a verified Live account:

    $sdkConfig = array(
         "mode" => 'live'
    );

    $cred = new OAuthTokenCredential(
        $LIVE_clientId,
        $LIVE_clientSecret
    );

    $access_token = $cred->getAccessToken($sdkConfig);

I get this error: Http response code 401 when accessing https://api.sandbox.paypal.com/v1/oauth2/token

How does the PayPal REST API know which endpoint to access?

I am not specifying the endpoint in the sandbox or live calls and am not using a bootstrap or ini file. The account is verified and approved.

1

There are 1 answers

5
Jay Patel - PayPal On BEST ANSWER

The best way I would recommend is to create an ApiContext object similar to shown at https://gist.github.com/jaypatel512/a2b037ab5ddc51fa7280

<?php

// 1. Autoload the SDK Package. This will include all the files and classes to your autoloader
require __DIR__  . '/PayPal-PHP-SDK/autoload.php';

// 2. Provide your Secret Key. Replace the given one with your app clientId, and Secret
// https://developer.paypal.com/webapps/developer/applications/myapps
$apiContext = new \PayPal\Rest\ApiContext(
    new \PayPal\Auth\OAuthTokenCredential(
        'AYSq3RDGsmBLJE-otTkBtM-jBRd1TCQwFf9RGfwddNXWz0uFU9ztymylOhRS',     // ClientID
        'EGnHDxD_qRPdaLdZz8iCr8N7_MzF-YHPTkjs6NKYQvQSBngp4PTTVWkPZRbL'      // ClientSecret
    )
);

// Step 2.1 : Between Step 2 and Step 3
$apiContext->setConfig(
  array(
    'mode' => 'live',
    'log.LogEnabled' => true,
    'log.FileName' => 'PayPal.log',
    'log.LogLevel' => 'FINE'
  )
);


// 3. Lets try to save a credit card to Vault using Vault API mentioned here
// https://developer.paypal.com/webapps/developer/docs/api/#store-a-credit-card
$creditCard = new \PayPal\Api\CreditCard();
$creditCard->setType("visa")
    ->setNumber("4417119669820331")
    ->setExpireMonth("11")
    ->setExpireYear("2019")
    ->setCvv2("012")
    ->setFirstName("Joe")
    ->setLastName("Shopper");

// 4. Make a Create Call and Print the Card
try {
    $creditCard->create($apiContext);
    echo $creditCard;
}
catch (\PayPal\Exception\PayPalConnectionException $ex) {
    echo $ex;
}