Last active
July 9, 2023 15:26
-
-
Save JohnMAustin78/40f2984af92268a1c4fe41c78ca69188 to your computer and use it in GitHub Desktop.
Square Payment Form script reference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
The 1st script tag is necssary to reference the SqPaymentForm library. The | |
2nd and 3rd script tags reference the javascript and stylesheet code that | |
this setup guide asks you to add to your web application | |
link to the SqPaymentForm library --> | |
<script type="text/javascript" src="https://js.squareup.com/v2/paymentform"></script> | |
<!-- link to a javascript file where you put your SqPaymentForm logic --> | |
<script type="text/javascript" src="/sqpaymentform.js"></script> | |
<!-- link to your stylesheet that declares custom styles for SqPaymentForm --> | |
<link rel="stylesheet" type="text/css" href="/sqpaymentform.css"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* function: requestCardNonce | |
* | |
* requestCardNonce is triggered when the "Pay with credit card" button is | |
* clicked | |
* | |
* Modifying this function is not required, but can be customized if you | |
* wish to take additional action when the form button is clicked. | |
*/ | |
function requestCardNonce(event) { | |
// Don't submit the form until SqPaymentForm returns with a nonce | |
event.preventDefault(); | |
// Request a nonce from the SqPaymentForm object | |
paymentForm.requestCardNonce(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Create and initialize a payment form object. This example is the minimum necessary | |
initializing script. Follow the steps in the setup guide to configure your page | |
with a Payment Form that shows typical customization. | |
The setup guide shows you how to get REPLACE ME values from your Square Application | |
Dashboard. | |
*/ | |
var paymentForm = new SqPaymentForm({ | |
applicationId: "REPLACE_ME", | |
locationId: "REPLACE_ME", | |
inputClass: 'sq-input', | |
//Disable automatically building Payment Form | |
autoBuild: false, | |
inputStyles: [{ | |
fontSize: '.9em' | |
}], | |
cardNumber: { | |
elementId: 'sq-card-number', | |
placeholder: '•••• •••• •••• ••••' | |
}, | |
cvv: { | |
elementId: 'sq-cvv', | |
placeholder: 'CVV' | |
}, | |
expirationDate: { | |
elementId: 'sq-expiration-date', | |
placeholder: 'MM/YY' | |
}, | |
postalCode: { | |
elementId: 'sq-postal-code' | |
}, | |
callbacks: { | |
/* | |
* callback function: cardNonceResponseReceived | |
* Triggered when: SqPaymentForm completes a card nonce request | |
*/ | |
cardNonceResponseReceived: function(errors, nonce, cardData) { | |
if (errors) { | |
// Log errors from nonce generation to the Javascript console | |
console.log("Encountered errors:"); | |
errors.forEach(function(error) { | |
console.log(' ' + error.message); | |
}); | |
return; | |
} | |
// Assign the nonce value to the hidden form field | |
document.getElementById('card-nonce').value = nonce; | |
// POST the nonce form to the payment processing page | |
document.getElementById('nonce-form').submit(); | |
} | |
} | |
}); | |
/* | |
Call the `buildForm()` function after the page DOM contains the Payment Form | |
placeholder elements. | |
*/ | |
function buildForm(){ | |
if (paymentForm.isSupportedBrowser()) { | |
paymentForm.build() | |
} | |
} | |
/* | |
* Removes SqPaymentForm event listeners from a page. | |
*/ | |
function stopPaymentFormEventListeners() { | |
paymentForm.destroy(); | |
} | |
/* | |
* function: requestCardNonce | |
* | |
* requestCardNonce is invoked when the "Pay with credit card" button is | |
* clicked | |
* | |
* Modifying this function is not required, but can be customized if you | |
* wish to take additional action when the form button is clicked. | |
*/ | |
function requestCardNonce(event) { | |
// Don't submit the form until SqPaymentForm returns with a nonce | |
event.preventDefault(); | |
// Request a nonce from the SqPaymentForm object | |
paymentForm.requestCardNonce(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Create and initialize a payment form object. This example is the minimum necessary | |
initializing script. Follow the steps in the setup guide to configure your page | |
with a Payment Form that shows typical customization. | |
The setup guide shows you how to get REPLACE ME values from your Square Application | |
Dashboard. | |
*/ | |
var paymentForm = new SqPaymentForm({ | |
applicationId: "REPLACE_ME", | |
locationId: "REPLACE_ME", | |
inputClass: 'sq-input', | |
inputStyles: [{ | |
fontSize: '.9em' | |
}], | |
cardNumber: { | |
elementId: 'sq-card-number', | |
placeholder: '•••• •••• •••• ••••' | |
}, | |
cvv: { | |
elementId: 'sq-cvv', | |
placeholder: 'CVV' | |
}, | |
expirationDate: { | |
elementId: 'sq-expiration-date', | |
placeholder: 'MM/YY' | |
}, | |
postalCode: { | |
elementId: 'sq-postal-code' | |
}, | |
callbacks: { | |
/* | |
* callback function: cardNonceResponseReceived | |
* Triggered when: SqPaymentForm completes a card nonce request | |
*/ | |
cardNonceResponseReceived: function(errors, nonce, cardData) { | |
if (errors) { | |
// Log errors from nonce generation to the Javascript console | |
console.log("Encountered errors:"); | |
errors.forEach(function(error) { | |
console.log(' ' + error.message); | |
}); | |
return; | |
} | |
// Assign the nonce value to the hidden form field | |
document.getElementById('card-nonce').value = nonce; | |
// POST the nonce form to the payment processing page | |
document.getElementById('nonce-form').submit(); | |
} | |
} | |
}); | |
/* | |
* function: requestCardNonce | |
* | |
* requestCardNonce is triggered when the "Pay with credit card" button is | |
* clicked | |
* | |
* Modifying this function is not required, but can be customized if you | |
* wish to take additional action when the form button is clicked. | |
*/ | |
function requestCardNonce(event) { | |
// Don't submit the form until SqPaymentForm returns with a nonce | |
event.preventDefault(); | |
// Request a nonce from the SqPaymentForm object | |
paymentForm.requestCardNonce(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Single-page apps typically render the Payment Form explicitly with the | |
build() function. You must call the paymentForm.isSupportedBrowser() function | |
before calling `paymentForm.build()` to confirm the Payment Form will load | |
properly. | |
Call the `buildForm()` function after the page DOM contains the Payment Form | |
placeholder elements. | |
*/ | |
function buildForm(){ | |
if (paymentForm.isSupportedBrowser()) { | |
paymentForm.build() | |
} | |
} | |
/* | |
* function: requestCardNonce | |
* | |
* requestCardNonce is triggered when the "Pay with credit card" button is | |
* clicked | |
* | |
* Modifying this function is not required, but can be customized if you | |
* wish to take additional action when the form button is clicked. | |
*/ | |
function requestCardNonce(event) { | |
// Don't submit the form until SqPaymentForm returns with a nonce | |
event.preventDefault(); | |
// Request a nonce from the SqPaymentForm object | |
paymentForm.requestCardNonce(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Defines how SqPaymentForm iframes should look */ | |
.sq-input { | |
border: 1px solid rgb(223, 223, 223); | |
outline-offset: -2px; | |
margin-bottom: 5px; | |
display: inline-block; | |
} | |
/* Define how SqPaymentForm iframes should look when they have focus */ | |
.sq-input--focus { | |
outline: 5px auto rgb(59, 153, 252); | |
} | |
/* Define how SqPaymentForm iframes should look when they contain invalid values */ | |
.sq-input--error { | |
outline: 5px auto rgb(255, 97, 97); | |
} | |
/* Customize the "Pay with Credit Card" button */ | |
.button-credit-card { | |
min-width: 200px; | |
min-height: 20px; | |
padding: 0; | |
margin: 5px; | |
line-height: 20px; | |
box-shadow: 2px 2px 1px rgb(200, 200, 200); | |
background: rgb(255, 255, 255); | |
border-radius: 5px; | |
border: 1px solid rgb(200, 200, 200); | |
font-weight: bold; | |
cursor:pointer; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment