test
2 months ago
utils
4 months ago
a8c-address-autocomplete-service.js
11 months ago
a8c-address-autocomplete-service.min.js
11 months ago
account-i18n.js
2 years ago
account-i18n.min.js
2 years ago
add-payment-method.js
4 months ago
add-payment-method.min.js
4 months ago
add-to-cart-variation.js
1 month ago
add-to-cart-variation.min.js
1 month ago
add-to-cart.js
7 months ago
add-to-cart.min.js
7 months ago
address-autocomplete.js
2 months ago
address-autocomplete.min.js
2 months ago
address-i18n.js
2 months ago
address-i18n.min.js
2 months ago
back-in-stock-form.js
10 months ago
back-in-stock-form.min.js
10 months ago
cart-fragments.js
3 years ago
cart-fragments.min.js
2 years ago
cart.js
1 year ago
cart.min.js
1 year ago
checkout.js
2 months ago
checkout.min.js
2 months ago
country-select.js
1 year ago
country-select.min.js
1 year ago
credit-card-form.js
8 years ago
credit-card-form.min.js
8 years ago
geolocation.js
2 years ago
geolocation.min.js
2 years ago
lost-password.js
8 years ago
lost-password.min.js
8 years ago
order-attribution.js
5 months ago
order-attribution.min.js
5 months ago
order-review.js
1 month ago
order-review.min.js
1 month ago
password-strength-meter.js
1 year ago
password-strength-meter.min.js
1 year ago
price-slider.js
5 years ago
price-slider.min.js
2 years ago
single-product.js
7 months ago
single-product.min.js
7 months ago
tokenization-form.js
5 years ago
tokenization-form.min.js
2 years ago
woocommerce.js
2 months ago
woocommerce.min.js
2 months ago
wp-consent-api-integration.js
2 years ago
wp-consent-api-integration.min.js
2 years ago
add-payment-method.js
90 lines
| 1 | jQuery( function( $ ) { |
| 2 | |
| 3 | // woocommerce_params is required to continue, ensure the object exists |
| 4 | if ( typeof woocommerce_params === 'undefined' ) { |
| 5 | return false; |
| 6 | } |
| 7 | |
| 8 | var $form = $( '#add_payment_method' ); |
| 9 | |
| 10 | /** |
| 11 | * Create the API object passed to custom place order button render callbacks. |
| 12 | * This is specific to the add-payment-method page. |
| 13 | * |
| 14 | * @return {Object} API object with validate and submit methods |
| 15 | */ |
| 16 | function createAddPaymentMethodApi() { |
| 17 | |
| 18 | return { |
| 19 | /** |
| 20 | * Validate the form. |
| 21 | * For add payment method, there's minimal validation - the payment gateway handles most of it. |
| 22 | * |
| 23 | * @return {Promise<{hasError: boolean}>} Promise resolving to a validation result |
| 24 | */ |
| 25 | validate: function() { |
| 26 | return new Promise( function( resolve ) { |
| 27 | // The "add payment method" page has no form validation needs. |
| 28 | resolve( { hasError: false } ); |
| 29 | } ); |
| 30 | }, |
| 31 | |
| 32 | /** |
| 33 | * Submit the "add payment method" form. |
| 34 | */ |
| 35 | submit: function() { |
| 36 | $form.trigger( 'submit' ); |
| 37 | } |
| 38 | }; |
| 39 | } |
| 40 | |
| 41 | // When a gateway registers after a page load, render its button if it's selected. |
| 42 | $( document.body ).on( 'wc_custom_place_order_button_registered', function( e, gatewayId ) { |
| 43 | wc.customPlaceOrderButton.__maybeShow( gatewayId, createAddPaymentMethodApi() ); |
| 44 | } ); |
| 45 | |
| 46 | /* Payment option selection */ |
| 47 | $form.on( 'click init_add_payment_method', '.payment_methods input.input-radio', function() { |
| 48 | if ( $( '.payment_methods input.input-radio' ).length > 1 ) { |
| 49 | var target_payment_box = $( 'div.payment_box.' + $( this ).attr( 'ID' ) ); |
| 50 | if ( $( this ).is( ':checked' ) && ! target_payment_box.is( ':visible' ) ) { |
| 51 | $( 'div.payment_box' ).filter( ':visible' ).slideUp( 250 ); |
| 52 | if ( $( this ).is( ':checked' ) ) { |
| 53 | $( 'div.payment_box.' + $( this ).attr( 'ID' ) ).slideDown( 250 ); |
| 54 | } |
| 55 | } |
| 56 | } else { |
| 57 | $( 'div.payment_box' ).show(); |
| 58 | } |
| 59 | |
| 60 | // Handle custom place order button for selected gateway |
| 61 | wc.customPlaceOrderButton.__maybeShow( $( this ).val(), createAddPaymentMethodApi() ); |
| 62 | }); |
| 63 | |
| 64 | // Hide default button immediately if initially selected gateway has custom button. |
| 65 | // This must happen BEFORE triggering click to prevent flash of the default button. |
| 66 | var $initialPaymentMethod = $form.find( 'input[name="payment_method"]:checked' ); |
| 67 | if ( $initialPaymentMethod.length ) { |
| 68 | wc.customPlaceOrderButton.__maybeHideDefaultButtonOnInit( $initialPaymentMethod.val() ); |
| 69 | } |
| 70 | |
| 71 | // Trigger initial click |
| 72 | $form.find( 'input[name=payment_method]:checked' ).trigger( 'click' ); |
| 73 | |
| 74 | $form.on( 'submit', function() { |
| 75 | $form.block({ message: null, overlayCSS: { background: '#fff', opacity: 0.6 } }); |
| 76 | }); |
| 77 | |
| 78 | $( document.body ).trigger( 'init_add_payment_method' ); |
| 79 | |
| 80 | // Prevent firing multiple requests upon double clicking the buttons in payment methods table |
| 81 | $(' .woocommerce .payment-method-actions .button.delete' ).on( 'click' , function( event ) { |
| 82 | if ( $( this ).hasClass( 'disabled' ) ) { |
| 83 | event.preventDefault(); |
| 84 | } |
| 85 | |
| 86 | $( this ).addClass( 'disabled' ); |
| 87 | }); |
| 88 | |
| 89 | }); |
| 90 |