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 that represents a buyer's payment card using the Web Payments SDKWeb Payments SDK or In-App Payments SDKIn-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 CreateCardCreateCard request.

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

Link to section

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

Use the CreateCustomerCreateCustomer 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

Copy

The Customers API returns the following response:

Copy
Expand
{ "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": "Amelia.Earhart@example.com", "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 CreateCardCreateCard 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

Copy

The Cards API returns the following response:

Copy
Expand

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 CreatePaymentCreatePayment 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

Copy
Link to section

See also