Square Payments Example
Make a Payment
<form id="payment-form">
<label for="card-number">Card Number:</label>
<input type="text" id="card-number" placeholder="Card Number" pattern="\d*" maxlength="16" required>
<br>
<label for="cardholder-name">Cardholder's Name:</label>
<input type="text" id="cardholder-name" placeholder="Cardholder's Name" required>
<br>
<label for="expiration-month">Expiration Date:</label>
<input type="text" id="expiration-month" placeholder="MM" pattern="\d{1,2}" maxlength="2" required>
<span>/</span>
<input type="text" id="expiration-year" placeholder="YYYY" pattern="\d{4}" maxlength="4" required>
<br>
<label for="cvv">CVV:</label>
<input type="text" id="cvv" placeholder="CVV" pattern="\d*" maxlength="4" required>
<br>
<label for="amount">Amount:</label>
<input type="text" id="amount" placeholder="Amount" value="$100.00" readonly>
<br>
<button type="submit">Pay Now</button>
</form>
<!-- Load Square Payment Form script securely via HTTPS -->
<script src="https://js.squareup.com/v2/paymentform"></script>
<script>
const applicationId = 'YOUR_PRODUCTION_APPLICATION_ID';
const locationId = 'YOUR_PRODUCTION_LOCATION_ID';
const paymentForm = new SqPaymentForm({
applicationId: applicationId,
locationId: locationId,
inputClass: 'sq-input',
inputStyles: [{
fontSize: '16px'
}],
cardNumber: {
elementId: 'card-number',
placeholder: 'Card Number'
},
cardholderName: {
elementId: 'cardholder-name',
placeholder: 'Cardholder\'s Name'
},
expirationDate: {
elementId: 'expiration-month',
placeholder: 'MM'
},
expirationYear: {
elementId: 'expiration-year',
placeholder: 'YYYY'
},
cvv: {
elementId: 'cvv',
placeholder: 'CVV'
},
callbacks: {
cardNonceResponseReceived: function (errors, nonce, cardData) {
if (errors) {
console.error('Encountered errors:');
errors.forEach(function (error) {
console.error(' ' + error.message);
});
return;
}
console.log('Nonce received:', nonce);
console.log('Card data:', cardData);
// Post the tokenized data to http://localhost:8080/
const xhr = new XMLHttpRequest();
xhr.open('POST', 'http://localhost:8080/');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onload = function () {
if (xhr.status === 200) {
console.log('Payment tokenization successful');
// Clear input fields on successful tokenization
document.getElementById('card-number').value = '';
document.getElementById('cardholder-name').value = '';
document.getElementById('expiration-month').value = '';
document.getElementById('expiration-year').value = '';
document.getElementById('cvv').value = '';
} else {
console.error('Failed to tokenize payment data');
}
};
xhr.onerror = function () {
console.error('Failed to make POST request');
};
xhr.send(JSON.stringify({
nonce: nonce,
cardData: cardData,
amount: document.getElementById('amount').value
}));
},
unsupportedBrowserDetected: function () {
// Handle unsupported browsers
}
}
});
document.getElementById('payment-form').addEventListener('submit', function (event) {
event.preventDefault();
paymentForm.requestCardNonce();
});
// Input validation for card number field
document.getElementById('card-number').addEventListener('input', function (event) {
const input = event.target;
input.value = input.value.replace
I’m Square Dev AI, here to help answer your question!
Based on the information you’ve provided, the following documentation will help answer your question:
Additional Documentation
Payment API: Process Online and In-Person Payments
Take a Credit Card Payment
Build on Mobile Web
If this documentation helped you find a solution, please check the box indicating that this reply solves the problem.
Otherwise one of our Developer Advocates will provide additional assistance shortly.
It looks like you’re referencing the now-retired SqPaymentForm
. Please note that this has been fully decommissioned and is no longer supported. To continue processing payments, you’ll need to switch to our Web Payments SDK. We have a Web Payments SDK Quickstart guide. This resource contains practical examples to help you get set up quickly and start building your payment solutions with the new SDK.