Usage
-
Initialize the Square payments object
Initialize the Square Payments object with your application ID and the ID of the seller location where the payment is taken.
const payments = Square.payments(APPLICATION_ID, LOCATION_ID);
-
Get a new Card object
const card = await payments.card();
This creates a new Card instance with all of the behaviors and properties needed to take a card payment.
-
Attach the Card instance to a DOM element
Attach the Card instance to an empty DOM element on your page. The payment card form renders into the empty element, turning it into a secure payment card form.
await card.attach('#card');
-
Get the payment token
Get the payment form token from the Card instance when the buyer completes the form by calling the tokenize method.
const tokenResult = await card.tokenize(); if (tokenResult.status === 'OK') { const token = tokenResult.token; } else { console.error(tokenResult.errors); }
If the payment card information is valid, it returns a one-time-use token which you can use to create a Payment using the Payments API CreatePayment endpoint to charge a customer. Otherwise, it will return an error that you can present to the customer.