1

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!

1 Answer 1

1

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();
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for this code. I have updated to use it, however I still get back no value in the $apiContext object for accessToken, tokenExpiresIn and tokenCreateTime. Also, the $creditCard->create($apiContext) call is failing with an error stating error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure
It looks like the error I'm getting is due to the TLS 1.2 requirement. I'm on PHP 5.4.7 - upgrading to 5.6 to see if that fixes the issue. Thank you!
Ah yeah that sounds like the issue. Glad to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.