Code Duplication    Length = 46-50 lines in 3 locations

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-order-handler.php 1 location

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

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

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