Code Cookbook

Save Cards on File

Save cards on file to create a seamless purchase experience for returning customers.

Customers API
Payment Form

You can save cards on file for your customers. Customers can then make make purchases without having to re-enter their card information. Saving cards on file can make for a more seamless purchase experience and is the first step in enabling recurring payments.

Before you start

Step 1: Create customer card

  1. Send the card nonce and the customer ID to the CreateCustomerCard endpoint. It will return a customer_card_id.
//Make sure you have your card nonce and customer id
$cardNonce = "fake-card-nonce-ok"; //you can use this test nonce in sandbox
$customerId = {YOUR_CUSTOMER_ID}; //replace with an existing customer_id

// Create a CustomerCard request object.
$body = new \SquareConnect\Model\CreateCustomerCardRequest();

// Set the card nonce value for your request object.

//send the request to the CreateCustomerCard endpoint
try {
    $result = $customersApi>createCustomerCard($customerId, $body);
} catch (Exception $e) {
    echo 'Error when calling createCustomerCard: ', $e->getMessage(), PHP_EOL;

Step 2: Charge card on file

Once you have saved a card on file, you can use it with the Transactions API's Charge endpoint to process future payments by setting the customer_card_id and customer_id fields in the ChargeRequest object instead of the card_nonce field.

If you are using OAuth, you will need the PAYMENTS_WRITE permission.

// The ID of the location to associate the created transaction with.
$locationId = '{REPLACE_ME}';

// An object object from the PHP SDK with the fields to POST for the request.
$body = new \SquareConnect\Model\ChargeRequest();

// Set the customer card ID.

// Set the customer ID.

try {
    $result = $customersApi>charge($locationId, $body);
} catch (Exception $e) {
    echo 'Error when calling TransactionsApi->charge: ', $e->getMessage(), PHP_EOL;
< Browse More Recipes
Browse More Payment Recipes >

Contact Developer Support, join our Slack channel, or ask for help on Stack Overflow