Create a Card on File from a Payment ID
Learn how to create a new card on file in a Square account using a Payment id
as a source.
Before saving a card on file, you need to get a valid one-time-use payment token, which represents a buyer's payment card. Use the Web Payments SDK or In-App Payments SDK to generate the payment token. You do not need to charge the buyer's card in order to save it. The payment token can be used to save a card on file, create a payment, or both.
The two Cards API examples in this topic require their own unique token.
Important
The postal code entered in the payment card form must match the postal code used in each CreateCard call in this topic.
When you enter card information from Sandbox Test Values, be sure to enter the postal code 10003 to match the values used in this topic.
Call the CreatePayments endpoint to request a new Payment object.
If the payment succeeds, your application gets a response from the Payments API like the following example:
The Payment
id
field value is used in step 3.
Create a new customer to associate with the card you create in step 3.
The required OAuth scope is CUSTOMERS_WRITE
.
The Customers API returns the following response:
The id
field in the response is used in the following step.
Call the CreateCard endpoint, setting the value of the source_id
field to the payment ID in the step 1 Payment response object and the customer_id
value from the step 2 Customer response object.
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.
If you need more assistance, contact Developer Support or ask for help in the Developer Forums.