Deprecated
This API is deprecated, see below for guidance on what to use instead.
Transactions API: Code Cookbook

Charge Cards on File

Note

Transactions API is deprecated. The API functionality is split into the new Payments API and a more robust Orders API. For more information, Payments and Refunds Overview and Orders: What it Does .

Customers API
Transactions API
PHP (5.x) (SDK)

Before you start Permalink Get a link to this section

  • You will need an access token . If you are using OAuth, you will need CUSTOMERS_WRITE permission to save a card on file and PAYMENTS_WRITE permission to process payments with the saved card. Cards on file are automatically updated on a monthly basis to confirm they are still valid and can be charged.

  • You need to have created a Customer object using the Customers API. You can follow the Customers API build guide to create your first Customer object.

  • You need to have saved a card on file for the Customer. You can follow the Save a Card on File to save a card on file.

Charge a saved card Permalink Get a link to this section

Once you have saved a card on file, you can use the Transactions API and call the Charge endpoint to process the saved card. If you are using OAuth, you will need the PAYMENTS_WRITE permission.

To charge a saved card and set the customer_card_id and customer_id fields in the ChargeRequest object instead of the card_nonce field.


$customerId = "{YOUR_CUSTOMER_ID}"; // Replace with an existing customer_id
$customerCardId = "{SAVED_CARD_ID}";  // Replace with an existing customer_id

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

// 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.
$body->setCustomerCardId($customerCardId);

// Set the customer ID.
$body->setCustomerId($customerId);

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