Skip to content

Instantly share code, notes, and snippets.

@sadam1807
Last active October 30, 2019 16:02
Show Gist options
  • Save sadam1807/418c62caa7fc366d5a0081831993a975 to your computer and use it in GitHub Desktop.
Save sadam1807/418c62caa7fc366d5a0081831993a975 to your computer and use it in GitHub Desktop.
const config = {
// Initialize the payment form elements
//TODO: Replace with your sandbox application ID
applicationId: process.env.REACT_APP_APLLICATION_ID,
inputClass: 'sq-input',
autoBuild: false,
// Customize the CSS for SqPaymentForm iframe elements
inputStyles: [{
fontSize: '16px',
lineHeight: '24px',
padding: '16px',
placeholderColor: '#a0a0a0',
backgroundColor: 'transparent',
}],
// Initialize the credit card placeholders
cardNumber: {
elementId: 'sq-card-number',
placeholder: 'Card Number'
},
cvv: {
elementId: 'sq-cvv',
placeholder: 'CVV'
},
expirationDate: {
elementId: 'sq-expiration-date',
placeholder: 'MM/YY'
},
postalCode: {
elementId: 'sq-postal-code',
placeholder: 'Postal'
},
// SqPaymentForm callback functions
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 browser developer console.
console.error('Encountered errors:');
errors.forEach(function (error) {
console.error(' ' + error.message);
});
alert('Encountered errors, check browser developer console for more details');
return;
}
//alert(`The generated nonce is:\n${nonce}`);
fetch('http://localhost:4000/process-payment', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
nonce: nonce
})
})
.catch(err => {
alert('Network error: ' + err);
})
.then(response => {
if (!response.ok) {
return response.text().then(errorInfo => Promise.reject(errorInfo));
}
return response.text();
})
.then(data => {
console.log(JSON.stringify(data));
alert('Payment complete successfully!\nCheck browser developer console form more details');
})
.catch(err => {
console.error(err);
alert('Payment failed to complete!\nCheck browser developer console form more details');
});
}
}
}
export default config;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment