Conditions | 13 |
Paths | 17 |
Total Lines | 84 |
Lines | 6 |
Ratio | 7.14 % |
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 | <?php |
||
143 | public function create_setup_intent() { |
||
144 | if ( |
||
145 | ! is_user_logged_in() |
||
146 | || ! isset( $_POST['stripe_source_id'] ) |
||
147 | || ! isset( $_POST['nonce'] ) |
||
148 | ) { |
||
149 | return; |
||
150 | } |
||
151 | |||
152 | try { |
||
153 | $source_id = wc_clean( $_POST['stripe_source_id'] ); |
||
154 | |||
155 | // 1. Verify. |
||
156 | View Code Duplication | if ( |
|
157 | ! wp_verify_nonce( sanitize_key( $_POST['nonce'] ), 'wc_stripe_create_si' ) |
||
158 | || ! preg_match( '/^src_.*$/', $source_id ) |
||
159 | ) { |
||
160 | throw new Exception( __( 'Unable to verify your request. Please reload the page and try again.', 'woocommerce-gateway-stripe' ) ); |
||
161 | } |
||
162 | |||
163 | |||
164 | // 2. Load the customer ID (and create a customer eventually). |
||
165 | $customer = new WC_Stripe_Customer( wp_get_current_user()->ID ); |
||
166 | |||
167 | // 3. Attach the source to the customer (Setup Intents require that). |
||
168 | $source_object = $customer->attach_source( $source_id ); |
||
169 | if ( is_wp_error( $source_object ) ) { |
||
170 | throw new Exception( $source_object->get_error_message() ); |
||
171 | } |
||
172 | |||
173 | // 4. Generate the setup intent |
||
174 | $setup_intent = WC_Stripe_API::request( |
||
175 | [ |
||
176 | 'customer' => $customer->get_id(), |
||
177 | 'confirm' => 'true', |
||
178 | 'payment_method' => $source_id, |
||
179 | ], |
||
180 | 'setup_intents' |
||
181 | ); |
||
182 | |||
183 | if ( $setup_intent->error ) { |
||
184 | $error_response_message = print_r( $setup_intent, true ); |
||
185 | WC_Stripe_Logger::log("Failed create Setup Intent while saving a card."); |
||
186 | WC_Stripe_Logger::log("Response: $error_response_message"); |
||
187 | throw new Exception( __( 'Your card could not be set up for future usage.', 'woocommerce-gateway-stripe' ) ); |
||
188 | } |
||
189 | |||
190 | // 5. Respond. |
||
191 | if ( 'requires_action' === $setup_intent->status ) { |
||
192 | $response = [ |
||
193 | 'status' => 'requires_action', |
||
194 | 'client_secret' => $setup_intent->client_secret, |
||
195 | ]; |
||
196 | } elseif ( 'requires_payment_method' === $setup_intent->status |
||
197 | || 'requires_confirmation' === $setup_intent->status |
||
198 | || 'canceled' === $setup_intent->status ) { |
||
199 | // These statuses should not be possible, as such we return an error. |
||
200 | $response = [ |
||
201 | 'status' => 'error', |
||
202 | 'error' => [ |
||
203 | 'type' => 'setup_intent_error', |
||
204 | 'message' => __( 'Failed to save payment method.', 'woocommerce-gateway-stripe' ), |
||
205 | ], |
||
206 | ]; |
||
207 | } else { |
||
208 | // This should only be reached when status is `processing` or `succeeded`, which are |
||
209 | // the only statuses that we haven't explicitly handled. |
||
210 | $response = [ |
||
211 | 'status' => 'success', |
||
212 | ]; |
||
213 | } |
||
214 | } catch ( Exception $e ) { |
||
215 | $response = [ |
||
216 | 'status' => 'error', |
||
217 | 'error' => array( |
||
218 | 'type' => 'setup_intent_error', |
||
219 | 'message' => $e->getMessage(), |
||
220 | ), |
||
221 | ]; |
||
222 | } |
||
223 | |||
224 | echo wp_json_encode( $response ); |
||
225 | exit; |
||
226 | } |
||
227 | } |
||
230 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.