Code Duplication    Length = 51-55 lines in 3 locations

includes/class-wc-stripe-order-handler.php 1 location

@@ 127-177 (lines=51) @@
124
			$headers  = $response['headers'];
125
			$response = $response['body'];
126
127
			if ( ! empty( $response->error ) ) {
128
				// Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without.
129
				if ( $this->is_no_such_customer_error( $response->error ) ) {
130
					if ( WC_Stripe_Helper::is_wc_lt( '3.0' ) ) {
131
						delete_user_meta( $order->customer_user, '_stripe_customer_id' );
132
						delete_post_meta( $order_id, '_stripe_customer_id' );
133
					} else {
134
						delete_user_meta( $order->get_customer_id(), '_stripe_customer_id' );
135
						$order->delete_meta_data( '_stripe_customer_id' );
136
						$order->save();
137
					}
138
				}
139
140
				if ( $this->is_no_such_token_error( $response->error ) && $prepared_source->token_id ) {
141
					// Source param wrong? The CARD may have been deleted on stripe's end. Remove token and show message.
142
					$wc_token = WC_Payment_Tokens::get( $prepared_source->token_id );
143
					$wc_token->delete();
144
					$localized_message = __( 'This card is no longer available and has been removed.', 'woocommerce-gateway-stripe' );
145
					$order->add_order_note( $localized_message );
146
					throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
147
				}
148
149
				// We want to retry.
150
				if ( $this->is_retryable_error( $response->error ) ) {
151
					if ( $retry ) {
152
						// Don't do anymore retries after this.
153
						if ( 5 <= $this->retry_interval ) {
154
							return $this->process_redirect_payment( $order_id, false, $response->error );
155
						}
156
157
						sleep( $this->retry_interval );
158
159
						$this->retry_interval++;
160
						return $this->process_redirect_payment( $order_id, true, $response->error );
161
					} else {
162
						$localized_message = __( 'Sorry, we are unable to process your payment at this time. Please retry later.', 'woocommerce-gateway-stripe' );
163
						$order->add_order_note( $localized_message );
164
						throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
165
					}
166
				}
167
168
				$localized_messages = WC_Stripe_Helper::get_localized_messages();
169
170
				if ( 'card_error' === $response->error->type ) {
171
					$message = isset( $localized_messages[ $response->error->code ] ) ? $localized_messages[ $response->error->code ] : $response->error->message;
172
				} else {
173
					$message = isset( $localized_messages[ $response->error->type ] ) ? $localized_messages[ $response->error->type ] : $response->error->message;
174
				}
175
176
				throw new WC_Stripe_Exception( print_r( $response, true ), $message );
177
			}
178
179
			// To prevent double processing the order on WC side.
180
			if ( ! $this->is_original_request( $headers ) ) {

includes/payment-methods/class-wc-gateway-stripe-sepa.php 1 location

@@ 336-390 (lines=55) @@
333
				// Make the request.
334
				$response = WC_Stripe_API::request( $this->generate_payment_request( $order, $prepared_source ) );
335
336
				if ( ! empty( $response->error ) ) {
337
					// Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without.
338
					if ( $this->is_no_such_customer_error( $response->error ) ) {
339
						if ( WC_Stripe_Helper::is_wc_lt( '3.0' ) ) {
340
							delete_user_meta( $order->customer_user, '_stripe_customer_id' );
341
							delete_post_meta( $order_id, '_stripe_customer_id' );
342
						} else {
343
							delete_user_meta( $order->get_customer_id(), '_stripe_customer_id' );
344
							$order->delete_meta_data( '_stripe_customer_id' );
345
							$order->save();
346
						}
347
					}
348
349
					if ( $this->is_no_such_token_error( $response->error ) && $prepared_source->token_id ) {
350
						// Source param wrong? The CARD may have been deleted on stripe's end. Remove token and show message.
351
						$wc_token = WC_Payment_Tokens::get( $prepared_source->token_id );
352
						$wc_token->delete();
353
						$localized_message = __( 'This card is no longer available and has been removed.', 'woocommerce-gateway-stripe' );
354
						$order->add_order_note( $localized_message );
355
						throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
356
					}
357
358
					// We want to retry.
359
					if ( $this->is_retryable_error( $response->error ) ) {
360
						if ( $retry ) {
361
							// Don't do anymore retries after this.
362
							if ( 5 <= $this->retry_interval ) {
363
364
								return $this->process_payment( $order_id, false, $force_save_source );
365
							}
366
367
							sleep( $this->retry_interval );
368
369
							$this->retry_interval++;
370
371
							return $this->process_payment( $order_id, true, $force_save_source );
372
						} else {
373
							$localized_message = __( 'Sorry, we are unable to process your payment at this time. Please retry later.', 'woocommerce-gateway-stripe' );
374
							$order->add_order_note( $localized_message );
375
							throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
376
						}
377
					}
378
379
					$localized_messages = WC_Stripe_Helper::get_localized_messages();
380
381
					if ( 'card_error' === $response->error->type ) {
382
						$localized_message = isset( $localized_messages[ $response->error->code ] ) ? $localized_messages[ $response->error->code ] : $response->error->message;
383
					} else {
384
						$localized_message = isset( $localized_messages[ $response->error->type ] ) ? $localized_messages[ $response->error->type ] : $response->error->message;
385
					}
386
387
					$order->add_order_note( $localized_message );
388
389
					throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
390
				}
391
392
				do_action( 'wc_gateway_stripe_process_payment', $response, $order );
393

includes/class-wc-stripe-webhook-handler.php 1 location

@@ 202-255 (lines=54) @@
199
			$headers  = $response['headers'];
200
			$response = $response['body'];
201
202
			if ( ! empty( $response->error ) ) {
203
				// Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without.
204
				if ( $this->is_no_such_customer_error( $response->error ) ) {
205
					if ( WC_Stripe_Helper::is_wc_lt( '3.0' ) ) {
206
						delete_user_meta( $order->customer_user, '_stripe_customer_id' );
207
						delete_post_meta( $order_id, '_stripe_customer_id' );
208
					} else {
209
						delete_user_meta( $order->get_customer_id(), '_stripe_customer_id' );
210
						$order->delete_meta_data( '_stripe_customer_id' );
211
						$order->save();
212
					}
213
				}
214
215
				if ( $this->is_no_such_token_error( $response->error ) && $prepared_source->token_id ) {
216
					// Source param wrong? The CARD may have been deleted on stripe's end. Remove token and show message.
217
					$wc_token = WC_Payment_Tokens::get( $prepared_source->token_id );
218
					$wc_token->delete();
219
					$localized_message = __( 'This card is no longer available and has been removed.', 'woocommerce-gateway-stripe' );
220
					$order->add_order_note( $localized_message );
221
					throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
222
				}
223
224
				// We want to retry.
225
				if ( $this->is_retryable_error( $response->error ) ) {
226
					if ( $retry ) {
227
						// Don't do anymore retries after this.
228
						if ( 5 <= $this->retry_interval ) {
229
230
							return $this->process_webhook_payment( $notification, false );
231
						}
232
233
						sleep( $this->retry_interval );
234
235
						$this->retry_interval++;
236
						return $this->process_webhook_payment( $notification, true );
237
					} else {
238
						$localized_message = __( 'Sorry, we are unable to process your payment at this time. Please retry later.', 'woocommerce-gateway-stripe' );
239
						$order->add_order_note( $localized_message );
240
						throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
241
					}
242
				}
243
244
				$localized_messages = WC_Stripe_Helper::get_localized_messages();
245
246
				if ( 'card_error' === $response->error->type ) {
247
					$localized_message = isset( $localized_messages[ $response->error->code ] ) ? $localized_messages[ $response->error->code ] : $response->error->message;
248
				} else {
249
					$localized_message = isset( $localized_messages[ $response->error->type ] ) ? $localized_messages[ $response->error->type ] : $response->error->message;
250
				}
251
252
				$order->add_order_note( $localized_message );
253
254
				throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
255
			}
256
257
			// To prevent double processing the order on WC side.
258
			if ( ! $this->is_original_request( $headers ) ) {