Populating Customers credit card info

If customer has their credit card saved in their profile, is there a way to pre populate that card in to the HTML square inputs?

If the card is saved on a customer profile, then you do not need to use the Square Payment Form at all, you can pass the card_id (as well as the customer_id) into the CreatePayment request. Note that source_id == card_id instead of a nonce in this case.

1 Like

Ok sounds pretty simple. I have all the codes for that thanks to you :slight_smile:
I can’t however find a code in PHP where it actually saves the customers credit card info in their account dynamically.
I can add the checkbox in HTML form of course where it says “Save CC on file”, but when creating a customer in PHP, I don’t see any fields for adding a credit card :S

It’s a completely separate API endpoint; you’ll need to pass the nonce over to the CreateCustomerCard endpoint which will save it to their profile. You can retrieve their profile (either by RetrieveCustomer or ListCustomers for all customers), and there will be a cards array in the customer’s profile if they’ve previously saved a card on file.

1 Like

Ok this I can manage.
Can I just ask you one thing? One of the biggest struggles I’ve had is to get an ID from a response.
When getting a customer ID after creating it I use:

$customer = $api_response->getResult()->getCustomer();
$square_customer_id = $customer->getId();

When getting Order ID I use:

$order = $api_response->getResult()->getOrder();
$square_order_id = $order->getId();

When getting payment ID I use:

$payment = $api_response->getResult()->getPayment();
$payment_id = $payment->getId();

But when I’m trying to search for a customer:
($api_response = $client->getCustomersApi()->searchCustomers($body);

It just seems so hard to get the ID of customer. In the response I get (trying to get the ID):

[errors:Square\Models\SearchCustomersResponse:private] => 
[customers:Square\Models\SearchCustomersResponse:private] => Array
    (
        [0] => Square\Models\Customer Object
            (
                [id:Square\Models\Customer:private] => D6P8E7JJSGZP7FPNJW4QQJT8PR

But to get that ID just seems impossible!! I tried this:

$customer = $api_response->getResult()->getCustomer();
$square_customer_id = $customer->getId();

But I get:
Uncaught Error: Call to undefined method Square\Models\SearchCustomersResponse::getCustomer()

The SearchCustomersResponse is an array of customers, so you’ll need to specify which customer you want to get the id of. For example:

$customers = $api_response->getResult()->getCustomers(); // get all the customers returned
$first_customer_id = $customers[0]->getId();

You may want to implement a loop or something depending on what you’re trying to accomplish, as this will only get the id of the first customer in the response.

1 Like

getCustomers() gives error:
HP Fatal error: Uncaught Error: Call to undefined method Square\Models\RetrieveCustomerResponse::getCustomers()

So I changed it to this (changed from Customers to Customer), and it does not give an error message anymore:
$customers = $api_response->getResult()->getCustomer(); // get all the customers returned

Now there’s another problem. The second row:

$first_customer_id = $customers[0]->getId();

Gives error message:
PHP Fatal error: Uncaught Error: Cannot use object of type Square\Models\Customer as array in …

If I do a print_r($customers), I get:

Square\Models\Customer Object
(
[id:Square\Models\Customer:private] => TMRANYKFW___________
[createdAt:Square\Models\Customer:private] => 2021-03-07T06:40:49.317Z
[updatedAt:Square\Models\Customer:private] => 2021-03-07T06:40:49Z
[cards:Square\Models\Customer:private] => Array
(
[0] => Square\Models\Card Object
(
[id:Square\Models\Card:private] => ccof:______

Ugh there’s always a struggle when it comes to extracting information from a response :confused:

That error was due to you changing to RetrieveCustomer instead of SearchCustomers (like your previous message). RetrieveCustomer returns one single customer, while Search returns multiple (possibly).

$first_customer_id = $customers[0]->getId();

This will not work with RetrieveCustomer; apologies for the confusion but it seems we’re talking about two different endpoints at this time. If you’re using RetrieveCustomer you should be able to use your initial code:

$customer = $api_response->getResult()->getCustomer();
$square_customer_id = $customer->getId();
1 Like

I understand. So the GetId is its own feature and gets the ID from a result. But what about other variables?
I’m trying to get the LAST4 variable from:

Square\Models\Customer Object
(
    [id:Square\Models\Customer:private] => TMRANYKFW8Z77_________
    [createdAt:Square\Models\Customer:private] => 2021-03-07T06:40:49.317Z
    [updatedAt:Square\Models\Customer:private] => 2021-03-07T06:40:49Z
    [cards:Square\Models\Customer:private] => Array
        (
            [0] => Square\Models\Card Object
                (
                    [id:Square\Models\Card:private] => ccof:GFxo___________
                    [cardBrand:Square\Models\Card:private] => VISA
                    [last4:Square\Models\Card:private] => 0382

If you can help me figure out the most safest via square function to or example get the last4 from this, I can use this in so many other functions. So far I can use your code which is:
$customers = $api_response->getResult()->getCustomer();

I’m not sure if the row above even needs to be run as it seems to show same content as what I have in $api_response.
Anyway, can I bother you for few seconds with just (I’m guessing) 2 lines of code that will be able to get the last4 content ?

Thank you sooooooo much!!!

So just by the way, all the functions are documented in the PHP SDK documentation, so for example here’s the Customer object details. For the last 4, you can see it lives in the cards field (which according to the documentation is an array of Card objects). So you should be able to get it like so:

$customer = $api_response->getResult()->getCustomer();
$last_four = $customer->getCards()[0]->getLast4();

Again, since this is an array, there may be more than one card and the above only shows the first card (array index 0), so you may want to loop through to provide a dropdown list for the customer to choose from, or something similar. As for if the row is needed, technically speaking you can do the above in one line of code, it depends on how you want to break it out. For example this would also work:

$last_four =$api_response->getResult()->getCustomer()->getCards()[0]->getLast4();
1 Like

This link is perfect!!! Thank you!
I was wondering for so long if there was a place you could find all these function names.

Are there any links to troubleshooting them as well? When it comes to to retrieving them that is?
I’m trying to run a loop and delete ALL credit cards customer has in their profile (besides the first 1) by first checking if they have any:

for ($i = 0; $i <= 10; $i++) {
	if ( ! ( ISSET  $customer->getCards()[$i]->getid()   )  ){  break;  }
	if ( $i > 0){

		// ok DELETE card

		$card_id	= $customer->getCards()[$i]->getid();
		echo "delete";
		//$api_response = $client->getCustomersApi()->deleteCustomerCard($square_customer_id, $card_id);
	}
}

The problem is, the ISSET line gives an “Uncaught Error: Call to a member function getid() on null” when there’s no credit card etc. I tried “Empty” instead of “Isset” . doesn’t work.

If there was a way to find out HOW many credit cards customer has stored it would help.
Is there a way to run “$customer->getCards()[ $i ]->getid()” without getting a Call to a member function getid() on null error?
Thank you :slight_smile:

getCards() has the potential to be null (if there are no cards), so you would probably want to check for that:

if ($customer->getCards() != null) {
  foreach ($customer->getCards() as $card) {
    $card_id = $card->getId();
...

also note that the function is case-sensitive, and should be getId() and not getid().

1 Like