sq-paymentform.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. methodsSupported: function (methods) {
  43. },
  44. unsupportedBrowserDetected: function() {
  45. alert('UNSUPPORTED BROWSER');
  46. },
  47. cardNonceResponseReceived: function(errors, nonce) {
  48. var f = document.getElementById('nonce-form');
  49. var translatedErrors = [];
  50. if (!f.name.value.trim()) {
  51. translatedErrors.push(translateError({field: 'name'}));
  52. }
  53. if (!f.email.value.trim()) {
  54. translatedErrors.push(translateError({field: 'email'}));
  55. }
  56. if (!f.date.value.trim()) {
  57. translatedErrors.push(translateError({field: 'date'}));
  58. }
  59. if (!f.booker.value.trim()) {
  60. translatedErrors.push(translateError({field: 'booker'}));
  61. }
  62. if (translatedErrors.length) {
  63. document.getElementById('card-error-area').innerHTML = translatedErrors.join('<br>');
  64. document.getElementById('card-error-area').classList.remove('hidden');
  65. return;
  66. }
  67. if (errors) {
  68. // Log errors from nonce generation to the Javascript console
  69. errors.forEach(function(error) {
  70. translatedErrors.push(translateError(error));
  71. });
  72. document.getElementById('card-error-area').innerHTML = translatedErrors.join('<br>');
  73. document.getElementById('card-error-area').classList.remove('hidden');
  74. return;
  75. }
  76. document.getElementById('card-error-area').classList.add('hidden');
  77. document.getElementById('card-nonce').value = nonce; // Assign the nonce value to the hidden form field
  78. document.getElementById('nonce-form').submit(); // POST the nonce form to the payment processing page
  79. }
  80. }
  81. });
  82. paymentForm.build();
  83. return paymentForm;
  84. }
  85. // ---------------------------------------------------------------------------------------------------------------------
  86. // Called when 'Pay With Card' is clicked
  87. function requestCardNonce(event) {
  88. event.preventDefault(); // Don't submit the form until SqPaymentForm returns with a nonce
  89. paymentForm.requestCardNonce(); // Request a nonce from the SqPaymentForm object
  90. }
  91. // ---------------------------------------------------------------------------------------------------------------------
  92. function translateError(error) {
  93. switch(error.field) {
  94. case 'name':
  95. return 'Please enter your name';
  96. case 'email':
  97. return 'Please enter your email address';
  98. case 'date':
  99. return 'Please enter the date of your booking with The Grand Expedition';
  100. case 'booker':
  101. return 'Please enter the name of the person who made the original booking';
  102. case 'cardNumber':
  103. return 'Please enter a valid card number';
  104. case 'cvv':
  105. return 'Please enter a valid CVV (the 3-digit number on the back of your card)';
  106. case 'expirationDate':
  107. return 'Please enter a valid expiry date';
  108. case 'postalCode':
  109. return 'Please enter a valid post code';
  110. default:
  111. return error.message;
  112. }
  113. }
  114. // ---------------------------------------------------------------------------------------------------------------------
  115. if (document.readyState === 'loading') {
  116. document.addEventListener('DOMContentLoaded', init);
  117. } else { // `DOMContentLoaded` already fired
  118. init();
  119. }