sq-paymentform.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.25em',
  14. fontFamily: 'sans-serif',
  15. color: 'white',
  16. backgroundColor: '#0f193c',
  17. fontWeight: 'normal',
  18. padding: '5px 10px'
  19. }],
  20. applePay: false,
  21. googlePay: false,
  22. masterpass: false,
  23. // Initialize the credit card placeholders
  24. cardNumber: {
  25. elementId: 'sq-card-number',
  26. placeholder: '•••• •••• •••• ••••'
  27. },
  28. cvv: {
  29. elementId: 'sq-cvv',
  30. placeholder: ''
  31. },
  32. expirationDate: {
  33. elementId: 'sq-expiration-date',
  34. placeholder: 'MM/YY'
  35. },
  36. postalCode: {
  37. elementId: 'sq-postal-code'
  38. },
  39. // SqPaymentForm callback functions
  40. callbacks: {
  41. methodsSupported: function (methods) {
  42. },
  43. unsupportedBrowserDetected: function() {
  44. alert('UNSUPPORTED BROWSER');
  45. },
  46. cardNonceResponseReceived: function(errors, nonce, cardData, billingContact, shippingContact) {
  47. if (errors) {
  48. // Log errors from nonce generation to the Javascript console
  49. console.log("Encountered errors:");
  50. errors.forEach(function(error) {
  51. console.log(' ' + error.message);
  52. });
  53. return;
  54. }
  55. document.getElementById('card-nonce').value = nonce; // Assign the nonce value to the hidden form field
  56. document.getElementById('nonce-form').submit(); // POST the nonce form to the payment processing page
  57. }
  58. }
  59. });
  60. paymentForm.build();
  61. return paymentForm;
  62. }
  63. // ---------------------------------------------------------------------------------------------------------------------
  64. // Called when 'Pay With Card' is clicked
  65. function requestCardNonce(event) {
  66. event.preventDefault(); // Don't submit the form until SqPaymentForm returns with a nonce
  67. paymentForm.requestCardNonce(); // Request a nonce from the SqPaymentForm object
  68. }
  69. // ---------------------------------------------------------------------------------------------------------------------
  70. if (document.readyState === 'loading') {
  71. document.addEventListener('DOMContentLoaded', init);
  72. } else { // `DOMContentLoaded` already fired
  73. init();
  74. }