| Conditions | 12 |
| Paths | 43 |
| Total Lines | 88 |
| Lines | 25 |
| Ratio | 28.41 % |
| 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 |
||
| 111 | public function process( $event_json ) { |
||
| 112 | |||
| 113 | // Next, proceed with additional webhooks. |
||
| 114 | if ( isset( $event_json->id ) ) { |
||
| 115 | |||
| 116 | status_header( 200 ); |
||
| 117 | |||
| 118 | try { |
||
| 119 | |||
| 120 | $event = \Stripe\Event::retrieve( $event_json->id ); |
||
| 121 | |||
| 122 | // Update time of webhook received whenever the event is retrieved. |
||
| 123 | give_update_option( 'give_stripe_last_webhook_received_timestamp', current_time( 'timestamp', 1 ) ); |
||
| 124 | |||
| 125 | } catch ( \Stripe\Error\Authentication $e ) { |
||
| 126 | |||
| 127 | if ( strpos( $e->getMessage(), 'Platform access may have been revoked' ) !== false ) { |
||
| 128 | give_stripe_connect_delete_options(); |
||
| 129 | } |
||
| 130 | } catch ( Exception $e ) { |
||
| 131 | die( 'Invalid event ID' ); |
||
| 132 | } |
||
| 133 | |||
| 134 | // Bailout, if event type doesn't exists. |
||
| 135 | if ( empty( $event->type ) ) { |
||
| 136 | return false; |
||
| 137 | } |
||
| 138 | |||
| 139 | switch ( $event->type ) { |
||
| 140 | |||
| 141 | View Code Duplication | case 'payment_intent.succeeded': |
|
| 142 | $intent = $event->data->object; |
||
| 143 | |||
| 144 | if ( 'succeeded' === $intent->status ) { |
||
| 145 | $donation_id = give_stripe_get_donation_id_by( $intent->id, 'intent_id' ); |
||
| 146 | |||
| 147 | // Update payment status to donation. |
||
| 148 | give_update_payment_status( $donation_id, 'publish' ); |
||
| 149 | |||
| 150 | // Insert donation note to inform admin that charge succeeded. |
||
| 151 | give_insert_payment_note( $donation_id, __( 'Charge succeeded in Stripe.', 'give' ) ); |
||
| 152 | } |
||
| 153 | |||
| 154 | break; |
||
| 155 | |||
| 156 | View Code Duplication | case 'payment_intent.payment_failed': |
|
| 157 | $intent = $event->data->object; |
||
| 158 | $donation_id = give_stripe_get_donation_id_by( $intent->id, 'intent_id' ); |
||
| 159 | |||
| 160 | // Update payment status to donation. |
||
| 161 | give_update_payment_status( $donation_id, 'failed' ); |
||
| 162 | |||
| 163 | // Insert donation note to inform admin that charge succeeded. |
||
| 164 | give_insert_payment_note( $donation_id, __( 'Charge failed in Stripe.', 'give' ) ); |
||
| 165 | |||
| 166 | break; |
||
| 167 | |||
| 168 | case 'charge.refunded': |
||
| 169 | global $wpdb; |
||
| 170 | |||
| 171 | $charge = $event->data->object; |
||
| 172 | |||
| 173 | if ( $charge->refunded ) { |
||
| 174 | |||
| 175 | $payment_id = $wpdb->get_var( $wpdb->prepare( "SELECT donation_id FROM {$wpdb->donationmeta} WHERE meta_key = '_give_payment_transaction_id' AND meta_value = %s LIMIT 1", $charge->id ) ); |
||
| 176 | |||
| 177 | if ( $payment_id ) { |
||
| 178 | |||
| 179 | give_update_payment_status( $payment_id, 'refunded' ); |
||
| 180 | give_insert_payment_note( $payment_id, __( 'Charge refunded in Stripe.', 'give' ) ); |
||
| 181 | |||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | break; |
||
| 186 | } |
||
| 187 | |||
| 188 | do_action( 'give_stripe_event_' . $event->type, $event ); |
||
| 189 | |||
| 190 | return $event->type; |
||
| 191 | |||
| 192 | } else { |
||
| 193 | status_header( 500 ); |
||
| 194 | // Something went wrong outside of Stripe. |
||
| 195 | give_record_gateway_error( __( 'Stripe Error', 'give' ), sprintf( __( 'An error occurred while processing a webhook.', 'give' ) ) ); |
||
| 196 | die( '-1' ); // Failed. |
||
| 197 | } // End if(). |
||
| 198 | } |
||
| 199 | } |
||
| 203 |