sq-paymentform.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Create and initialize a payment form object
  2. var paymentForm;
  3. // ---------------------------------------------------------------------------------------------------------------------
  4. function init() {
  5. paymentForm = new SqPaymentForm({
  6. // Initialize the payment form elements
  7. applicationId: applicationId,
  8. locationId: locationId,
  9. autoBuild: false,
  10. inputClass: 'sq-input',
  11. // Customize the CSS for SqPaymentForm iframe elements
  12. inputStyles: [{
  13. fontSize: '1.15em',
  14. fontFamily: 'courier, monospace',
  15. color: 'white',
  16. placeholderColor: '#EEE',
  17. backgroundColor: '#0f193c',
  18. fontWeight: 'normal',
  19. padding: '5px 10px'
  20. }],
  21. applePay: false,
  22. googlePay: false,
  23. masterpass: false,
  24. // Initialize the credit card placeholders
  25. cardNumber: {
  26. elementId: 'sq-card-number',
  27. placeholder: '•••• •••• •••• ••••'
  28. },
  29. cvv: {
  30. elementId: 'sq-cvv',
  31. placeholder: 'cvv'
  32. },
  33. expirationDate: {
  34. elementId: 'sq-expiration-date',
  35. placeholder: 'mm/yy'
  36. },
  37. postalCode: {
  38. elementId: 'sq-postal-code'
  39. },
  40. // SqPaymentForm callback functions
  41. callbacks: {
  42. unsupportedBrowserDetected: function() {
  43. alert('UNSUPPORTED BROWSER');
  44. },
  45. cardNonceResponseReceived: function(errors, nonce, cardData, billingContact, shippingContact) {
  46. if (errors) {
  47. // Log errors from nonce generation to the Javascript console
  48. console.log("Encountered errors:");
  49. errors.forEach(function(error) {
  50. console.log(' ' + error.message);
  51. });
  52. return;
  53. }
  54. document.getElementById('card-nonce').value = nonce; // Assign the nonce value to the hidden form field
  55. document.getElementById('nonce-form').submit(); // POST the nonce form to the payment processing page
  56. }
  57. }
  58. });
  59. paymentForm.build();
  60. return paymentForm;
  61. }
  62. // ---------------------------------------------------------------------------------------------------------------------
  63. // Called when 'Pay With Card' is clicked
  64. function requestCardNonce(event) {
  65. event.preventDefault(); // Don't submit the form until SqPaymentForm returns with a nonce
  66. paymentForm.requestCardNonce(); // Request a nonce from the SqPaymentForm object
  67. }
  68. // ---------------------------------------------------------------------------------------------------------------------
  69. if (document.readyState === 'loading') {
  70. document.addEventListener('DOMContentLoaded', init);
  71. } else { // `DOMContentLoaded` already fired
  72. init();
  73. }