Learn how to create a new card on file in a Square account using a Payment id
as a source.
Create a Card on File from a Payment ID
Before saving a card on file, you need to get a valid single-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 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.
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.
Create payment
If the payment succeeds, your application gets a response from the Payments API like the following example:
{
"payment": {
"id": "Dv9xlBgSgVB8i6eT0imRYFjcrOaZY",
"created_at": "2021-03-31T20:56:13.220Z",
"updated_at": "2021-03-31T20:56:13.411Z",
"amount_money": {
"amount": 100,
"currency": "USD"
},
"status": "COMPLETED",
"delay_duration": "PT168H",
"source_type": "CARD",
"card_details": {
"status": "CAPTURED",
"card": {
"card_brand": "AMERICAN_EXPRESS",
"last_4": "6550",
"exp_month": 3,
"exp_year": 2023,
"fingerprint": "sq-1-hPdOWUYtEMft3yQ",
"card_type": "CREDIT",
"prepaid_type": "NOT_PREPAID",
"bin": "371263"
},
"entry_method": "KEYED",
"cvv_status": "CVV_ACCEPTED",
"avs_status": "AVS_ACCEPTED",
"statement_description": "SQ *DEFAULT TEST ACCOUNT",
"card_payment_timeline": {
"authorized_at": "2021-03-31T20:56:13.334Z",
"captured_at": "2021-03-31T20:56:13.411Z"
}
},
"location_id": "VJN4XSBFTVPK9",
"total_money": {
"amount": 100,
"currency": "USD"
},
"approved_money": {
"amount": 100,
"currency": "USD"
}
}
}
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
.
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
}
}
}
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.
Create card
The Cards API returns the following response:
{
"card": {
"id": "ccof:uIbfJXhXETSP197M3GB",
"billing_address": {
"address_line_1": "500 Electric Ave",
"address_line_2": "Suite 600",
"locality": "New York",
"administrative_district_level_1": "NY",
"postal_code": "10003",
"country": "US"
},
"bin": "411111",
"card_brand": "VISA",
"card_type": "CREDIT",
"cardholder_name": "Amelia Earhart",
"customer_id": "Q6VKKKGW8GWQNEYMDRMV01QMK8",
"enabled": true,
"exp_month": 11,
"exp_year": 2018,
"last_4": "1111",
"prepaid_type": "NOT_PREPAID",
"reference_id": "user-id-1",
"version": 1
}
}
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.