simplify-commerce.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*global Simplify_commerce_params, SimplifyCommerce */
  2. (function ( $ ) {
  3. // Form handler
  4. function simplifyFormHandler() {
  5. var $form = $( 'form.checkout, form#order_review, form#add_payment_method' );
  6. if ( ( $( '#payment_method_simplify_commerce' ).is( ':checked' ) && 'new' === $( 'input[name="wc-simplify_commerce-payment-token"]:checked' ).val() ) || ( '1' === $( '#woocommerce_add_payment_method' ).val() ) ) {
  7. if ( 0 === $( 'input.simplify-token' ).length ) {
  8. $form.block({
  9. message: null,
  10. overlayCSS: {
  11. background: '#fff',
  12. opacity: 0.6
  13. }
  14. });
  15. var card = $( '#simplify_commerce-card-number' ).val(),
  16. cvc = $( '#simplify_commerce-card-cvc' ).val(),
  17. expiry = $.payment.cardExpiryVal( $( '#simplify_commerce-card-expiry' ).val() ),
  18. address1 = $form.find( '#billing_address_1' ).val() || '',
  19. address2 = $form.find( '#billing_address_2' ).val() || '',
  20. addressCountry = $form.find( '#billing_country' ).val() || '',
  21. addressState = $form.find( '#billing_state' ).val() || '',
  22. addressCity = $form.find( '#billing_city' ).val() || '',
  23. addressZip = $form.find( '#billing_postcode' ).val() || '';
  24. addressZip = addressZip.replace( /-/g, '' );
  25. card = card.replace( /\s/g, '' );
  26. SimplifyCommerce.generateToken({
  27. key: Simplify_commerce_params.key,
  28. card: {
  29. number: card,
  30. cvc: cvc,
  31. expMonth: expiry.month,
  32. expYear: ( expiry.year - 2000 ),
  33. addressLine1: address1,
  34. addressLine2: address2,
  35. addressCountry: addressCountry,
  36. addressState: addressState,
  37. addressZip: addressZip,
  38. addressCity: addressCity
  39. }
  40. }, simplifyResponseHandler );
  41. // Prevent the form from submitting
  42. return false;
  43. }
  44. }
  45. return true;
  46. }
  47. // Handle Simplify response
  48. function simplifyResponseHandler( data ) {
  49. var $form = $( 'form.checkout, form#order_review, form#add_payment_method' ),
  50. ccForm = $( '#wc-simplify_commerce-cc-form' );
  51. if ( data.error ) {
  52. // Show the errors on the form
  53. $( '.woocommerce-error, .simplify-token', ccForm ).remove();
  54. $form.unblock();
  55. // Show any validation errors
  56. if ( 'validation' === data.error.code ) {
  57. var fieldErrors = data.error.fieldErrors,
  58. fieldErrorsLength = fieldErrors.length,
  59. errorList = '';
  60. for ( var i = 0; i < fieldErrorsLength; i++ ) {
  61. errorList += '<li>' + Simplify_commerce_params[ fieldErrors[i].field ] + ' ' + Simplify_commerce_params.is_invalid + ' - ' + fieldErrors[i].message + '.</li>';
  62. }
  63. ccForm.prepend( '<ul class="woocommerce-error">' + errorList + '</ul>' );
  64. }
  65. } else {
  66. // Insert the token into the form so it gets submitted to the server
  67. ccForm.append( '<input type="hidden" class="simplify-token" name="simplify_token" value="' + data.id + '"/>' );
  68. $form.submit();
  69. }
  70. }
  71. $( function () {
  72. $( document.body ).on( 'checkout_error', function () {
  73. $( '.simplify-token' ).remove();
  74. });
  75. /* Checkout Form */
  76. $( 'form.checkout' ).on( 'checkout_place_order_simplify_commerce', function () {
  77. return simplifyFormHandler();
  78. });
  79. /* Pay Page Form */
  80. $( 'form#order_review' ).on( 'submit', function () {
  81. return simplifyFormHandler();
  82. });
  83. /* Pay Page Form */
  84. $( 'form#add_payment_method' ).on( 'submit', function () {
  85. return simplifyFormHandler();
  86. });
  87. /* Both Forms */
  88. $( 'form.checkout, form#order_review, form#add_payment_method' ).on( 'change', '#wc-simplify_commerce-cc-form input', function() {
  89. $( '.simplify-token' ).remove();
  90. });
  91. });
  92. }( jQuery ) );