Retrieve cards on file for a customer

I’m trying to figure out how to get all cards on file for a customer. I’m looking at the Customers API. I see how to add/remove cards but not how to query them. What am I missing?

When retrieving a customer (RetrieveCustomer), a customer will have a cards array in the response. Let me know if that helps!

Cool! I haven’t uploaded any yet, so that wasn’t obvious…

1 Like

I am able to get a dump of a customer’s card information but I am not seeing how I can display the “private” information for the customer, so that they can see what card they have saved on file…
It just keeps telling me,

PHP Fatal error:  Uncaught Error: Cannot access private property Square\Models\ListCardsResponse

What am I missing?

Do you have the correct permission scopes to read data from the Cards API? :slightly_smiling_face:

I’m not exactly sure what that is.
I am using this type of code, from the explorer…

$api_response = $client->getCardsApi()->listCards(
    $cursor,              //cursor(pagination)
    $customerId,          //cutomerID
    $includeDisabled,
    $referenceId,          //referenceID
    $sortOrder            //sort order ASC or DESC
 );
if ($api_response->isSuccess()) {
    $result = $api_response->getResult();
} else {
    $errors = $api_response->getErrors();
}
$results = $api_response->getResult();
var_export($results);
print $results->{"cards"}->{"cardholderName"}->{"value"};

This is the error: PHP Fatal error: Uncaught Error: Cannot access private property

It looks like you’re trying to access a private property directly, which is not allowed in PHP. Private properties can only be accessed within the class that defines them. You’ll need to use a getter method to return the values. :slightly_smiling_face:

Can you point me in the direction of some documentation for accessing those properties?

Since I am apparently new, it only allows 3 replies, so I have to edit this to be able to give a response…

Here is what shows up when attempting your code…

PHP Fatal error:  Cannot use isset() on the result of an expression (you can use "null !== expression" instead)

After I believe that I fixed that error, then I get this error…

PHP Fatal error:  Uncaught Error: Call to undefined method Square\Apis\CustomersApi::listCards()

I am just testing with your almost exact code, to see if I can get it to work the way I want, then I will adjust as needed…

Here’s an example code snippet that demonstrates how to do this:

require 'vendor/autoload.php'; // Make sure to include the autoload file for Composer packages

use Square\SquareClient;
use Square\Environment;
use Square\Exceptions\ApiException;

// Initialize the Square client
$client = new SquareClient([
    'accessToken' => 'YOUR_ACCESS_TOKEN', // Replace with your access token
    'environment' => Environment::SANDBOX, // Use Environment::PRODUCTION when in production
]);

// Replace with your customer ID
$customerId = 'CUSTOMER_ID';

try {
    // Retrieve the list of cards for the customer
    $customersApi = $client->getCustomersApi();
    $response = $customersApi->listCards($customerId);

    // Check if the response is successful
    if ($response->isSuccess()) {
        $result = $response->getResult();

        // Check if the cards property exists and is an array
        if (isset($result->cards) && is_array($result->cards)) {
            // Loop through each card in the cards array
            foreach ($result->cards as $card) {
                // Check if the cardholderName property exists
                if (isset($card->getCardholderName())) {
                    // Print the cardholderName value
                    echo $card->getCardholderName() . "\n";
                }
            }
        }
    } else {
        // Handle error response
        $errors = $response->getErrors();
        // Print the error details (you may want to log this instead)
        echo "API response errors: " . print_r($errors, true);
    }
} catch (ApiException $e) {
    // Handle exceptions from the Square API
    echo "Caught ApiException: " . $e->getMessage();
}

Please note the following:

  1. Replace 'YOUR_ACCESS_TOKEN' with your actual Square access token.
  2. Replace 'CUSTOMER_ID' with the actual customer ID whose cards you want to list.
  3. The code uses Environment::SANDBOX for testing purposes. When you’re ready to use this code in production, change it to Environment::PRODUCTION.
  4. The listCards method is used to retrieve the list of cards for a specific customer. Make sure the customer has cards associated with their account.
  5. The getCardholderName method is used to access the cardholder’s name from the card object.
  6. Error handling is included to manage any exceptions or errors returned by the API.

Before running the code, make sure you have installed the Square PHP SDK using Composer with the following command:

composer require square/square

Adjust the code as needed based on your application’s structure and error-handling preferences. :slightly_smiling_face:

Here is what shows up when attempting your code…

PHP Fatal error:  Cannot use isset() on the result of an expression (you can use "null !== expression" instead)

After I believe that I fixed that error, then I get this error…

PHP Fatal error:  Uncaught Error: Call to undefined method Square\Apis\CustomersApi::listCards()

I am just testing with your almost exact code, to see if I can get it to work the way I want, then I will adjust as needed…