Create a Card on File and a Payment

Link to section

Get a payment token

Before saving a card on file, you need to get a valid single-use payment token (which represents a buyer's payment card) using the Web Payments SDK or In-App Payments SDK. You don't need to charge the buyer's card to save it. The payment token can be used to save a card on file, create a payment, or both.

Important

The postal code entered in the SDK's payment card form must match the postal code used in the CreateCard request.

You can use Sandbox test card information to generate a test payment token for this topic. Be sure to enter the postal code 10003 in the payment card form so it matches the example values used in this topic.

Link to section

Step 1: Create a customer record in the target seller account

Use the CreateCustomer endpoint to create a new customer in the seller Square account using an OAuth token generated for the seller.

The following example CreateCustomer request is made with an access token from the seller Square account:

Create customer

The Customers API returns the following response:

{ "customer": { "id": "Q6VKKKGW8GWQNEYMDRMV01QMK8", "created_at": "2021-03-31T18:27:07.803Z", "updated_at": "2021-03-31T18:27:07Z", "given_name": "Amelia", "family_name": "Earhart", "email_address": "[email protected]", "preferences": { "email_unsubscribed": false }, "creation_source": "THIRD_PARTY" } }

The id field in the response is used in the following step.

Link to section

Step 2: Save a card on file in the seller Square account

Call the CreateCard endpoint with a card token and idempotency key using an OAuth token from the seller Square account.

The following example uses the customer_id value of Q6VKKKGW8GWQNEYMDRMV01QMK8 returned in the response in the previous step:

Create card

The Cards API returns the following response:

Important

Always ask customers for permission before saving their card information. For example, include a checkbox in your purchase flow that customers can select to specify that they want to save their card information for future purchases.

Linking cards on file without obtaining customer permission can result in your application being disabled without notice.

Link to section

Step 3: Create a payment using the card on file

Use CreatePayment for a new payment in the seller account.

Did you know?

The access token that you use in any Square API call determines which Square account the call applies to.

This is an optional step that you do only if you want to take a payment with the card that was just stored on file. If you don't want to take a payment, skip this step. The customer payment card is already stored on file.

Create a new payment in the seller account where the source_id is the ID of the newly saved card on file from step 2 and the customer_id from step 1.

Create payment

Link to section

See also