Phil Phil - 1 month ago 18
PHP Question

PHP - PayPal RESTful API SDK

I'm working on allowing PayPal Direct Credit Card payments in my website. I have downloaded the PayPal PHP RESTful SDK from here https://github.com/paypal/PayPal-PHP-SDK/releases and I am following instructions on setting up the direct credit card payment from here...https://devtools-paypal.com/guide/pay_creditcard/php?interactive=ON&env=sandbox.

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

composer->subfolders...
paypal->subfolders...
psr->subfolders...
autoload.php


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
accessToken
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

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:

<?php
// 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();
$creditCard->setType("visa")
  ->setNumber("4417119669820331")
  ->setExpireMonth("11")
  ->setExpireYear("2019")
  ->setCvv2("012")
  ->setFirstName("Joe")
  ->setLastName("Shopper");

try {
  $creditCard->create($apiContext);
  echo $creditCard;
}
catch (\PayPal\Exception\PayPalConnectionException $ex) {
  // This will print the detailed information on the exception. 
  //REALLY HELPFUL FOR DEBUGGING
  echo $ex->getData();
}
Comments