phppaypalpaypal-sandboxcredit-cardpaypal-vault

PayPal-PHP-SDK vault store and retrieve card information


I am using PayPal-PHP-SDK and I want to store and retrieve card information in PayPal vault. Whenever I store using following code I get a response. But whenever I want to retrieve the card details I get nothing and it doesn't show error.

 $apiContext = new \PayPal\Rest\ApiContext(
    new \PayPal\Auth\OAuthTokenCredential(
        'XXXXXXXXXXXxXX',     // ClientID
        'XXXXXXXXXXXXXX'      // ClientSecret
        )
    );
    $apiContext->setConfig(
      array(
          'log.LogEnabled' => true,
          'log.FileName' => 'PayPal.log',
          'log.LogLevel' => 'DEBUG'
              )
        );
    $card = new \PayPal\Api\CreditCard();
    $card->setNumber('4032032476376683');
    $card->setType('visa');
    $card->setExpireMonth(2);
    $card->setExpireYear(2023);
    $card->setCvv2('123');
    $card->setFirstName('Farhan');
    $card->setLastName('Khan');

     try {
        $card->create($apiContext);
        echo "<pre>"; print_r($card);

    }
    catch (\PayPal\Exception\PayPalConnectionException $ex) {
        echo "error";
        echo $ex->getData();

 }

This is the second code where I want to get the added card using CardId. I get no error and an empty array []

$card1 = new \PayPal\Api\CreditCard();

try {
     $card1->get("CARD-42P155089V838232ULJU73VA",$apiContext);
     echo $card1->getNumber();
     echo "<pre>"; print_r($card1);
    }
catch (\PayPal\Exception\PayPalConnectionException $ex) {
      echo "error";
      echo $ex->getData();
    }

Solution

  • I made some changes and it is working now. Someone may see the same error, So I am answering to my own question.

     $card1->get("CARD-42P155089V838232ULJU73VA",$apiContext);
    

    get function returns value so I have changed it to this

    $response = $card1->get("CARD-42P155089V838232ULJU73VA",$apiContext);
     echo "<pre>"; print_r($response);
    

    And it worked