| Conditions | 10 |
| Paths | 257 |
| Total Lines | 103 |
| Code Lines | 65 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 19 | public function process_donation( $data ) { |
||
| 20 | // Check for any stored errors. |
||
| 21 | $errors = give_get_errors(); |
||
| 22 | |||
| 23 | if ( ! $errors ) { |
||
| 24 | |||
| 25 | $form_id = ! empty( $data['post_data']['give-form-id'] ) ? intval( $data['post_data']['give-form-id'] ) : false; |
||
| 26 | $first_name = ! empty( $data['post_data']['give_first'] ) ? $data['post_data']['give_first'] : ''; |
||
| 27 | $last_name = ! empty( $data['post_data']['give_last'] ) ? $data['post_data']['give_last'] : ''; |
||
| 28 | $email = ! empty( $data['post_data']['give_email'] ) ? $data['post_data']['give_email'] : ''; |
||
| 29 | $donation_key = ! empty( $data['purchase_key'] ) ? $data['purchase_key'] : ''; |
||
| 30 | $currency = give_get_currency( $form_id ); |
||
| 31 | |||
| 32 | // Setup the donation details. |
||
| 33 | $data_to_send = array( |
||
| 34 | 'price' => $data['price'], |
||
| 35 | 'give_form_title' => $data['post_data']['give-form-title'], |
||
| 36 | 'give_form_id' => $form_id, |
||
| 37 | 'give_price_id' => isset( $data['post_data']['give-price-id'] ) ? $data['post_data']['give-price-id'] : '', |
||
| 38 | 'date' => $data['date'], |
||
| 39 | 'user_email' => $email, |
||
| 40 | 'purchase_key' => $data['purchase_key'], |
||
| 41 | 'currency' => $currency, |
||
| 42 | 'user_info' => $data['user_info'], |
||
| 43 | 'status' => 'pending', |
||
| 44 | 'gateway' => $data['gateway'], |
||
| 45 | ); |
||
| 46 | |||
| 47 | // Record the pending payment. |
||
| 48 | $donation_id = give_insert_payment( $data_to_send ); |
||
| 49 | |||
| 50 | // Verify donation payment. |
||
| 51 | if ( ! $donation_id ) { |
||
| 52 | |||
| 53 | // Record the error. |
||
| 54 | give_record_gateway_error( |
||
| 55 | __( 'Payment Error', 'paiementpro-for-give' ), |
||
| 56 | sprintf( |
||
| 57 | /* translators: %s: payment data */ |
||
| 58 | __( 'Payment creation failed before processing payment via PaiementPro. Payment data: %s', 'paiementpro-for-give' ), |
||
| 59 | wp_json_encode( $data ) |
||
| 60 | ), |
||
| 61 | $donation_id |
||
| 62 | ); |
||
| 63 | |||
| 64 | // Problems? Send back. |
||
| 65 | give_send_back_to_checkout( '?payment-mode=' . $data['post_data']['payment-mode'] ); |
||
| 66 | } |
||
| 67 | |||
| 68 | // Auto set payment to abandoned in one hour if donor is not able to donate in that time. |
||
| 69 | wp_schedule_single_event( current_time( 'timestamp', 1 ) + HOUR_IN_SECONDS, 'paiementpro4give_set_donation_abandoned', [ $donation_id ] ); |
||
| 70 | |||
| 71 | |||
| 72 | $url = paiementpro4give_get_api_url(); |
||
| 73 | $merchant_id = paiementpro4give_get_merchant_id(); |
||
| 74 | $args = [ |
||
| 75 | 'headers' => [ |
||
| 76 | 'Content-Type' => 'application/x-www-form-urlencoded' |
||
| 77 | ], |
||
| 78 | 'body' => [ |
||
| 79 | 'merchantId' => $merchant_id, |
||
| 80 | 'currency' => 952, |
||
| 81 | 'amount' => $data['price'], |
||
| 82 | 'channel' => 'MOMOCI', |
||
| 83 | 'customer_id' => '', |
||
| 84 | 'description' => 'Give plugin', |
||
| 85 | 'email' => $email, |
||
| 86 | 'firstname' => $first_name, |
||
| 87 | 'lastname' => $last_name, |
||
| 88 | 'phone_mobile' => '9876543210', |
||
| 89 | 'referenceNumber' => $donation_key, |
||
| 90 | 'notificationURL' => give_get_success_page_uri(), |
||
| 91 | 'returnContext' => wp_json_encode( [ |
||
| 92 | 'id_order' => $donation_id, |
||
| 93 | ] ), |
||
| 94 | ], |
||
| 95 | ]; |
||
| 96 | $response = wp_remote_post( "{$url}init2.php", $args ); |
||
| 97 | $responseBody = wp_remote_retrieve_body( $response ); |
||
| 98 | $responseCode = wp_remote_retrieve_response_code( $response ); |
||
| 99 | |||
| 100 | |||
| 101 | if ( 200 === $responseCode ) { |
||
| 102 | |||
| 103 | $responseBodyParts = explode( '|', $responseBody ); |
||
| 104 | $sessionId = $responseBodyParts[1]; |
||
| 105 | |||
| 106 | $redirect_to_url = add_query_arg( |
||
| 107 | [ |
||
| 108 | 'sessionid' => $sessionId, |
||
| 109 | 'id' => $donation_id, |
||
| 110 | ], |
||
| 111 | "{$url}processing.php" |
||
| 112 | ); |
||
| 113 | |||
| 114 | } else { |
||
| 115 | // Send user to failed page and change donation status to failed as well. |
||
| 116 | give_update_payment_status( $donation_id, 'failed' ); |
||
| 117 | $redirect_to_url = give_get_failed_transaction_uri(); |
||
| 118 | } |
||
| 119 | |||
| 120 | wp_redirect( $redirect_to_url ); |
||
| 121 | give_die(); |
||
| 122 | } |
||
| 126 | new PaiementPro4Give_MTN_Money_Ci(); |