Code Duplication    Length = 46-50 lines in 3 locations

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

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

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/payment-methods/class-wc-gateway-stripe-sepa.php 1 location

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