| Conditions | 7 |
| Paths | 2 |
| Total Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* global wc_stripe_apple_pay_params, Stripe */ |
||
| 23 | $( document.body ).on( 'click', '.apple-pay-button', function( e ) { |
||
| 24 | e.preventDefault(); |
||
| 25 | |||
| 26 | // Clear any errors first. |
||
| 27 | $( '.wc-stripe-apple-pay-error' ).remove(); |
||
| 28 | |||
| 29 | // If shipping is needed, we need to force customer to calculate shipping on cart page. |
||
| 30 | if ( 'yes' === wc_stripe_apple_pay_params.is_cart_page && |
||
| 31 | 'yes' === wc_stripe_apple_pay_params.needs_shipping && |
||
| 32 | ( $( '#shipping_method input[type="radio"]' ).length && ( ! $( '#shipping_method input[type="radio"]' ).is( ':checked' ) ) || |
||
| 33 | 0 === $( wc_stripe_apple_pay_params.chosen_shipping ).length ) |
||
| 34 | ) { |
||
| 35 | $( '.apple-pay-button' ).before( '<p class="woocommerce-error wc-stripe-apple-pay-error">' + wc_stripe_apple_pay_params.needs_shipping_msg + '</p>' ); |
||
| 36 | |||
| 37 | // Scroll to error so user can see it. |
||
| 38 | $( document.body ).animate({ scrollTop: $( '.wc-stripe-apple-pay-error' ).offset().top }, 500 ); |
||
| 39 | return; |
||
| 40 | } |
||
| 41 | |||
| 42 | var paymentRequest = { |
||
| 43 | countryCode: wc_stripe_apple_pay_params.country_code, |
||
| 44 | currencyCode: wc_stripe_apple_pay_params.currency_code, |
||
| 45 | total: { |
||
| 46 | label: wc_stripe_apple_pay_params.label, |
||
| 47 | amount: wc_stripe_apple_pay_params.total |
||
| 48 | }, |
||
| 49 | lineItems: wc_stripe_apple_pay_params.line_items, |
||
| 50 | requiredBillingContactFields: ['postalAddress'], |
||
| 51 | requiredShippingContactFields: 'yes' === wc_stripe_apple_pay_params.needs_shipping ? ['postalAddress', 'phone', 'email', 'name'] : ['phone', 'email', 'name'] |
||
| 52 | }; |
||
| 53 | |||
| 54 | var applePaySession = Stripe.applePay.buildSession( paymentRequest, function( result, completion ) { |
||
| 55 | var data = { |
||
| 56 | 'action': 'wc_stripe_apple_pay', |
||
| 57 | 'nonce': wc_stripe_apple_pay_params.stripe_apple_pay_nonce, |
||
| 58 | 'result': result |
||
| 59 | }; |
||
| 60 | |||
| 61 | $.post( wc_stripe_apple_pay_params.ajaxurl, data ).done( function( response ) { |
||
| 62 | if ( 'true' === response.success ) { |
||
| 63 | completion( ApplePaySession.STATUS_SUCCESS ); |
||
|
|
|||
| 64 | window.location.href = response.redirect; |
||
| 65 | } |
||
| 66 | |||
| 67 | if ( 'false' === response.success ) { |
||
| 68 | completion( ApplePaySession.STATUS_FAILURE ); |
||
| 69 | |||
| 70 | $( '.apple-pay-button' ).before( '<p class="woocommerce-error wc-stripe-apple-pay-error">' + response.msg + '</p>' ); |
||
| 71 | |||
| 72 | // Scroll to error so user can see it. |
||
| 73 | $( document.body ).animate({ scrollTop: $( '.wc-stripe-apple-pay-error' ).offset().top }, 500 ); |
||
| 74 | } |
||
| 75 | |||
| 76 | }).fail( function( response ) { |
||
| 77 | completion( ApplePaySession.STATUS_FAILURE ); |
||
| 78 | }); |
||
| 79 | |||
| 80 | }, function(error) { |
||
| 81 | console.log(error.message); |
||
| 82 | }); |
||
| 83 | |||
| 84 | applePaySession.begin(); |
||
| 85 | }); |
||
| 86 | }, |
||
| 114 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.