| Conditions | 11 |
| Paths | 163 |
| Total Lines | 143 |
| Lines | 15 |
| Ratio | 10.49 % |
| 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 |
||
| 151 | public function process_payment( $donation_data ) { |
||
| 152 | |||
| 153 | // Bailout, if the current gateway and the posted gateway mismatched. |
||
| 154 | if ( $this->id !== $donation_data['post_data']['give-gateway'] ) { |
||
| 155 | return; |
||
| 156 | } |
||
| 157 | |||
| 158 | // Make sure we don't have any left over errors present. |
||
| 159 | give_clear_errors(); |
||
| 160 | |||
| 161 | $source_id = ! empty( $donation_data['post_data']['give_stripe_source'] ) |
||
| 162 | ? $donation_data['post_data']['give_stripe_source'] |
||
| 163 | : $this->check_for_source( $donation_data ); |
||
| 164 | |||
| 165 | // Any errors? |
||
| 166 | $errors = give_get_errors(); |
||
| 167 | |||
| 168 | // No errors, proceed. |
||
| 169 | if ( ! $errors ) { |
||
| 170 | |||
| 171 | $form_id = ! empty( $donation_data['post_data']['give-form-id'] ) ? intval( $donation_data['post_data']['give-form-id'] ) : 0; |
||
| 172 | $price_id = ! empty( $donation_data['post_data']['give-price-id'] ) ? $donation_data['post_data']['give-price-id'] : 0; |
||
| 173 | $donor_email = ! empty( $donation_data['post_data']['give_email'] ) ? $donation_data['post_data']['give_email'] : 0; |
||
| 174 | $intent_id = ! empty( $donation_data['post_data']['give_stripe_intent_id'] ) ? $donation_data['post_data']['give_stripe_intent_id'] : 0; |
||
| 175 | $donation_summary = give_payment_gateway_donation_summary( $donation_data, false ); |
||
| 176 | |||
| 177 | // Get an existing Stripe customer or create a new Stripe Customer and attach the source to customer. |
||
| 178 | $give_stripe_customer = new Give_Stripe_Customer( $donor_email, $source_id ); |
||
| 179 | $stripe_customer = $give_stripe_customer->customer_data; |
||
| 180 | $stripe_customer_id = $give_stripe_customer->get_id(); |
||
| 181 | |||
| 182 | // We have a Stripe customer, charge them. |
||
| 183 | if ( $stripe_customer_id ) { |
||
| 184 | |||
| 185 | // Proceed to get stripe source details on if stripe checkout is not enabled. |
||
| 186 | $source = $give_stripe_customer->attached_source; |
||
| 187 | $source_id = $source->id; |
||
| 188 | |||
| 189 | // Setup the payment details. |
||
| 190 | $payment_data = array( |
||
| 191 | 'price' => $donation_data['price'], |
||
| 192 | 'give_form_title' => $donation_data['post_data']['give-form-title'], |
||
| 193 | 'give_form_id' => $form_id, |
||
| 194 | 'give_price_id' => $price_id, |
||
| 195 | 'date' => $donation_data['date'], |
||
| 196 | 'user_email' => $donation_data['user_email'], |
||
| 197 | 'purchase_key' => $donation_data['purchase_key'], |
||
| 198 | 'currency' => give_get_currency( $form_id ), |
||
| 199 | 'user_info' => $donation_data['user_info'], |
||
| 200 | 'status' => 'pending', |
||
| 201 | 'gateway' => $this->id, |
||
| 202 | ); |
||
| 203 | |||
| 204 | // Record the pending payment in Give. |
||
| 205 | $donation_id = give_insert_payment( $payment_data ); |
||
| 206 | |||
| 207 | // Save Stripe Customer ID to Donation note, Donor and Donation for future reference. |
||
| 208 | give_insert_payment_note( $donation_id, 'Stripe Customer ID: ' . $stripe_customer_id ); |
||
| 209 | $this->save_stripe_customer_id( $stripe_customer_id, $donation_id ); |
||
| 210 | give_update_meta( $donation_id, '_give_stripe_customer_id', $stripe_customer_id ); |
||
| 211 | |||
| 212 | // Save Source ID to donation note and DB. |
||
| 213 | give_insert_payment_note( $donation_id, 'Stripe Source ID: ' . $source_id ); |
||
| 214 | give_update_meta( $donation_id, '_give_stripe_source_id', $source_id ); |
||
| 215 | |||
| 216 | // Save donation summary to donation. |
||
| 217 | give_update_meta( $donation_id, '_give_stripe_donation_summary', $donation_summary ); |
||
| 218 | |||
| 219 | /** |
||
| 220 | * This filter hook is used to update the payment intent arguments. |
||
| 221 | * |
||
| 222 | * @since 2.5.0 |
||
| 223 | */ |
||
| 224 | $intent_args = apply_filters( |
||
| 225 | 'give_stripe_create_intent_args', |
||
| 226 | array( |
||
| 227 | 'amount' => $this->format_amount( $donation_data['price'] ), |
||
| 228 | 'currency' => give_get_currency( $form_id ), |
||
| 229 | 'payment_method_types' => [ 'card' ], |
||
| 230 | 'statement_descriptor' => give_stripe_get_statement_descriptor(), |
||
| 231 | 'receipt_email' => $donation_data['user_email'], |
||
| 232 | 'description' => give_payment_gateway_donation_summary( $donation_data ), |
||
| 233 | 'metadata' => $this->prepare_metadata( $donation_id ), |
||
| 234 | 'customer' => $stripe_customer_id, |
||
| 235 | 'source' => $source_id, |
||
| 236 | 'save_payment_method' => true, |
||
| 237 | 'confirm' => true, |
||
| 238 | 'return_url' => give_get_success_page_uri(), |
||
| 239 | ) |
||
| 240 | ); |
||
| 241 | $intent = $this->payment_intent->create( $intent_args ); |
||
| 242 | |||
| 243 | // Save Payment Intent ID to donation note and DB. |
||
| 244 | give_insert_payment_note( $donation_id, 'Stripe Payment Intent ID: ' . $intent->id ); |
||
| 245 | give_update_meta( $donation_id, '_give_stripe_payment_intent_id', $intent->id ); |
||
| 246 | |||
| 247 | // Save Payment Intent Client Secret to donation note and DB. |
||
| 248 | give_insert_payment_note( $donation_id, 'Stripe Payment Intent Client Secret: ' . $intent->client_secret ); |
||
| 249 | give_update_meta( $donation_id, '_give_stripe_payment_intent_client_secret', $intent->client_secret ); |
||
| 250 | |||
| 251 | $charge_id = $intent->charges['data'][0]->id; |
||
| 252 | |||
| 253 | if ( ! empty( $charge_id ) ) { |
||
| 254 | // Set Charge ID as transaction ID for the donation. |
||
| 255 | give_set_payment_transaction_id( $donation_id, $charge_id ); |
||
| 256 | give_insert_payment_note( $donation_id, 'Stripe Charge ID: ' . $charge_id ); |
||
| 257 | } |
||
| 258 | |||
| 259 | // Additional steps required when payment intent status is set to `requires_action`. |
||
| 260 | if ( 'requires_action' === $intent->status ) { |
||
| 261 | |||
| 262 | $action_url = $intent->next_action->redirect_to_url->url; |
||
| 263 | |||
| 264 | // Save Payment Intent requires action related information to donation note and DB. |
||
| 265 | give_insert_payment_note( $donation_id, 'Stripe requires additional action to be fulfilled.' ); |
||
| 266 | give_update_meta( $donation_id, '_give_stripe_payment_intent_require_action_url', $action_url ); |
||
| 267 | |||
| 268 | wp_redirect( $action_url ); |
||
| 269 | exit; |
||
| 270 | } |
||
| 271 | |||
| 272 | // Send them to success page. |
||
| 273 | give_send_to_success_page(); |
||
| 274 | |||
| 275 | View Code Duplication | } else { |
|
| 276 | |||
| 277 | // No customer, failed. |
||
| 278 | give_record_gateway_error( |
||
| 279 | __( 'Stripe Customer Creation Failed', 'give' ), |
||
| 280 | sprintf( |
||
| 281 | /* translators: %s Donation Data */ |
||
| 282 | __( 'Customer creation failed while processing the donation. Details: %s', 'give' ), |
||
| 283 | wp_json_encode( $donation_data ) |
||
| 284 | ) |
||
| 285 | ); |
||
| 286 | give_set_error( 'stripe_error', __( 'The Stripe Gateway returned an error while processing the donation.', 'give' ) ); |
||
| 287 | give_send_back_to_checkout( "?payment-mode={$this->id}" ); |
||
| 288 | |||
| 289 | } // End if(). |
||
| 290 | } else { |
||
| 291 | give_send_back_to_checkout( "?payment-mode={$this->id}" ); |
||
| 292 | } // End if(). |
||
| 293 | } |
||
| 294 | |||
| 387 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.