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/class-wc-stripe-webhook-handler.php 1 location

@@ 170-223 (lines=54) @@
167
			$headers  = $response['headers'];
168
			$response = $response['body'];
169
170
			if ( ! empty( $response->error ) ) {
171
				// Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without.
172
				if ( $this->is_no_such_customer_error( $response->error ) ) {
173
					if ( WC_Stripe_Helper::is_wc_lt( '3.0' ) ) {
174
						delete_user_meta( $order->customer_user, '_stripe_customer_id' );
175
						delete_post_meta( $order_id, '_stripe_customer_id' );
176
					} else {
177
						delete_user_meta( $order->get_customer_id(), '_stripe_customer_id' );
178
						$order->delete_meta_data( '_stripe_customer_id' );
179
						$order->save();
180
					}
181
				}
182
183
				if ( $this->is_no_such_token_error( $response->error ) && $prepared_source->token_id ) {
184
					// Source param wrong? The CARD may have been deleted on stripe's end. Remove token and show message.
185
					$wc_token = WC_Payment_Tokens::get( $prepared_source->token_id );
186
					$wc_token->delete();
187
					$localized_message = __( 'This card is no longer available and has been removed.', 'woocommerce-gateway-stripe' );
188
					$order->add_order_note( $localized_message );
189
					throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
190
				}
191
192
				// We want to retry.
193
				if ( $this->is_retryable_error( $response->error ) ) {
194
					if ( $retry ) {
195
						// Don't do anymore retries after this.
196
						if ( 5 <= $this->retry_interval ) {
197
198
							return $this->process_webhook_payment( $notification, false );
199
						}
200
201
						sleep( $this->retry_interval );
202
203
						$this->retry_interval++;
204
						return $this->process_webhook_payment( $notification, true );
205
					} else {
206
						$localized_message = __( 'Sorry, we are unable to process your payment at this time. Please retry later.', 'woocommerce-gateway-stripe' );
207
						$order->add_order_note( $localized_message );
208
						throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
209
					}
210
				}
211
212
				$localized_messages = WC_Stripe_Helper::get_localized_messages();
213
214
				if ( 'card_error' === $response->error->type ) {
215
					$localized_message = isset( $localized_messages[ $response->error->code ] ) ? $localized_messages[ $response->error->code ] : $response->error->message;
216
				} else {
217
					$localized_message = isset( $localized_messages[ $response->error->type ] ) ? $localized_messages[ $response->error->type ] : $response->error->message;
218
				}
219
220
				$order->add_order_note( $localized_message );
221
222
				throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
223
			}
224
225
			// To prevent double processing the order on WC side.
226
			if ( ! $this->is_original_request( $headers ) ) {

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

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