Code Duplication    Length = 46-50 lines in 3 locations

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

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

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

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

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

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