Phil Phil - 1 year ago 175
PHP Question


I'm working on allowing PayPal Direct Credit Card payments in my website. I have downloaded the PayPal PHP RESTful SDK from here and I am following instructions on setting up the direct credit card payment from here...

I'm stumped. The SDK has a folder structure like the following:


So I include the autoload.php file in my PHP script, and when I try to perform the first step in the tutorial which is the following bit of code

$paypal = new OAuthTokenCredential($clientId, $clientSecret, $sdkConfig);

I get
Fatal error: Class 'OAuthTokenCredential' not found

However, If I instead use the following code...

$paypal = new \PayPal\Auth\OAuthTokenCredential($clientId, $clientSecret, $sdkConfig);

I get a an object back which looks like the following:

PayPal\Auth\OAuthTokenCredential Object
[clientId:PayPal\Auth\OAuthTokenCredential:private] => CLIENT_ID
[clientSecret:PayPal\Auth\OAuthTokenCredential:private] => CLIENT_SECRET
[accessToken:PayPal\Auth\OAuthTokenCredential:private] =>
[tokenExpiresIn:PayPal\Auth\OAuthTokenCredential:private] =>
[tokenCreateTime:PayPal\Auth\OAuthTokenCredential:private] =>
[cipher:PayPal\Auth\OAuthTokenCredential:private] => PayPal\Security\Cipher Object
[secretKey:PayPal\Security\Cipher:private] => CLIENT_SECRET

[_propMap:PayPal\Common\PayPalModel:private] => Array


But the
which is what I need for Step 2 in the tutorial is empty.
What the heck am i doing wrong? Can anyone give a straight forward step by step guide for this? Is there one available that I'm not finding?

Thank you!

Answer Source

The tutorial you linked to says it is for PHP, but it really isn't. This one on PayPal's PHP SDK github is more directly for PHP. Here is the code from that page in case it goes away:

// 1. Autoload the SDK Package. This will include all the files
// and classes to your autoloader
// Used for composer based installation
require __DIR__  . '/vendor/autoload.php';
// Use below for direct download installation
// require __DIR__  . '/PayPal-PHP-SDK/autoload.php';

$apiContext = new \PayPal\Rest\ApiContext(
  new \PayPal\Auth\OAuthTokenCredential(
    'AYSq3RDGsmBLJE-otTkBtM-jBRd1TCQwFf9RGfwddNXWz0uFU9ztymylOhRS',     // ClientID
    'EGnHDxD_qRPdaLdZz8iCr8N7_MzF-YHPTkjs6NKYQvQSBngp4PTTVWkPZRbL'      // ClientSecret

// Save Credit Card to vault and then read it back
$creditCard = new \PayPal\Api\CreditCard();

try {
  echo $creditCard;
catch (\PayPal\Exception\PayPalConnectionException $ex) {
  // This will print the detailed information on the exception. 
  echo $ex->getData();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download