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