| Conditions | 1 |
| Paths | 2 |
| Total Lines | 116 |
| 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 */ |
||
| 26 | init: function() { |
||
| 27 | var self = this; |
||
|
|
|||
| 28 | |||
| 29 | Stripe.applePay.checkAvailability( function( available ) { |
||
| 30 | if ( available ) { |
||
| 31 | $( '.apple-pay-button' ).show(); |
||
| 32 | |||
| 33 | wc_stripe_apple_pay.generate_cart(); |
||
| 34 | } |
||
| 35 | }); |
||
| 36 | |||
| 37 | $( document.body ).on( 'click', '.apple-pay-button', function( e ) { |
||
| 38 | e.preventDefault(); |
||
| 39 | |||
| 40 | var paymentRequest = { |
||
| 41 | countryCode: wc_stripe_apple_pay_params.country_code, |
||
| 42 | currencyCode: wc_stripe_apple_pay_params.currency_code, |
||
| 43 | total: { |
||
| 44 | label: wc_stripe_apple_pay_params.label, |
||
| 45 | amount: wc_stripe_apple_pay_params.total |
||
| 46 | }, |
||
| 47 | lineItems: wc_stripe_apple_pay_params.line_items, |
||
| 48 | requiredBillingContactFields: ['postalAddress'], |
||
| 49 | requiredShippingContactFields: 'yes' === wc_stripe_apple_pay_params.needs_shipping ? ['postalAddress', 'phone', 'email', 'name'] : ['phone', 'email', 'name'] |
||
| 50 | }; |
||
| 51 | |||
| 52 | var applePaySession = Stripe.applePay.buildSession( paymentRequest, function( result, completion ) { |
||
| 53 | var data = { |
||
| 54 | 'nonce': wc_stripe_apple_pay_params.stripe_apple_pay_nonce, |
||
| 55 | 'result': result |
||
| 56 | }; |
||
| 57 | |||
| 58 | $.ajax({ |
||
| 59 | type: 'POST', |
||
| 60 | data: data, |
||
| 61 | url: wc_stripe_apple_pay.getAjaxURL( 'apple_pay' ), |
||
| 62 | success: function( response ) { |
||
| 63 | if ( 'true' === response.success ) { |
||
| 64 | completion( ApplePaySession.STATUS_SUCCESS ); |
||
| 65 | window.location.href = response.redirect; |
||
| 66 | } |
||
| 67 | |||
| 68 | if ( 'false' === response.success ) { |
||
| 69 | completion( ApplePaySession.STATUS_FAILURE ); |
||
| 70 | |||
| 71 | $( '.apple-pay-button' ).before( '<p class="woocommerce-error wc-stripe-apple-pay-error">' + response.msg + '</p>' ); |
||
| 72 | |||
| 73 | // Scroll to error so user can see it. |
||
| 74 | $( document.body ).animate({ scrollTop: $( '.wc-stripe-apple-pay-error' ).offset().top }, 500 ); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | }); |
||
| 78 | }); |
||
| 79 | |||
| 80 | // If shipping is needed -- get shipping methods. |
||
| 81 | if ( 'yes' === wc_stripe_apple_pay_params.needs_shipping ) { |
||
| 82 | // After the shipping contact/address has been selected |
||
| 83 | applePaySession.onshippingcontactselected = function( shipping ) { |
||
| 84 | var data = { |
||
| 85 | 'nonce': wc_stripe_apple_pay_params.stripe_apple_pay_get_shipping_methods_nonce, |
||
| 86 | 'address': shipping.shippingContact |
||
| 87 | }; |
||
| 88 | |||
| 89 | $.ajax({ |
||
| 90 | type: 'POST', |
||
| 91 | data: data, |
||
| 92 | url: wc_stripe_apple_pay.getAjaxURL( 'apple_pay_get_shipping_methods' ), |
||
| 93 | success: function( response ) { |
||
| 94 | var total = { |
||
| 95 | 'label': wc_stripe_apple_pay_params.label, |
||
| 96 | 'amount': response.total |
||
| 97 | }; |
||
| 98 | |||
| 99 | if ( 'true' === response.success ) { |
||
| 100 | applePaySession.completeShippingContactSelection( ApplePaySession.STATUS_SUCCESS, response.shipping_methods, total, response.line_items ); |
||
| 101 | } |
||
| 102 | |||
| 103 | if ( 'false' === response.success ) { |
||
| 104 | applePaySession.completeShippingContactSelection( ApplePaySession.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS, response.shipping_methods, total, response.line_items ); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | }); |
||
| 108 | }; |
||
| 109 | |||
| 110 | // After the shipping method has been selected |
||
| 111 | applePaySession.onshippingmethodselected = function( event ) { |
||
| 112 | var data = { |
||
| 113 | 'nonce': wc_stripe_apple_pay_params.stripe_apple_pay_update_shipping_method_nonce, |
||
| 114 | 'selected_shipping_method': event.shippingMethod |
||
| 115 | }; |
||
| 116 | |||
| 117 | $.ajax({ |
||
| 118 | type: 'POST', |
||
| 119 | data: data, |
||
| 120 | url: wc_stripe_apple_pay.getAjaxURL( 'apple_pay_update_shipping_method' ), |
||
| 121 | success: function( response ) { |
||
| 122 | var newTotal = { |
||
| 123 | 'label': wc_stripe_apple_pay_params.label, |
||
| 124 | 'amount': parseFloat( response.total ).toFixed(2) |
||
| 125 | }; |
||
| 126 | |||
| 127 | if ( 'true' === response.success ) { |
||
| 128 | applePaySession.completeShippingMethodSelection( ApplePaySession.STATUS_SUCCESS, newTotal, response.line_items ); |
||
| 129 | } |
||
| 130 | |||
| 131 | if ( 'false' === response.success ) { |
||
| 132 | applePaySession.completeShippingMethodSelection( ApplePaySession.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS, newTotal, response.line_items ); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | }); |
||
| 136 | }; |
||
| 137 | } |
||
| 138 | |||
| 139 | applePaySession.begin(); |
||
| 140 | }); |
||
| 141 | }, |
||
| 142 | |||
| 173 |