sq-paymentform.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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, cardData, billingContact, shippingContact) {
  48. if (errors) {
  49. // Log errors from nonce generation to the Javascript console
  50. console.log("Encountered errors:");
  51. errors.forEach(function(error) {
  52. console.log(' ' + error.message);
  53. });
  54. return;
  55. }
  56. document.getElementById('card-nonce').value = nonce; // Assign the nonce value to the hidden form field
  57. document.getElementById('nonce-form').submit(); // POST the nonce form to the payment processing page
  58. }
  59. }
  60. });
  61. paymentForm.build();
  62. return paymentForm;
  63. }
  64. // ---------------------------------------------------------------------------------------------------------------------
  65. // Called when 'Pay With Card' is clicked
  66. function requestCardNonce(event) {
  67. event.preventDefault(); // Don't submit the form until SqPaymentForm returns with a nonce
  68. paymentForm.requestCardNonce(); // Request a nonce from the SqPaymentForm object
  69. }
  70. // ---------------------------------------------------------------------------------------------------------------------
  71. if (document.readyState === 'loading') {
  72. document.addEventListener('DOMContentLoaded', init);
  73. } else { // `DOMContentLoaded` already fired
  74. init();
  75. }