Code Duplication    Length = 65-67 lines in 2 locations

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

@@ 115-179 (lines=65) @@
112
			// Make the request.
113
			$response = WC_Stripe_API::request( $this->generate_payment_request( $order, $source_object ) );
114
115
			if ( ! empty( $response->error ) ) {
116
				// If it is an API error such connection or server, let's retry.
117
				if ( 'api_connection_error' === $response->error->type || 'api_error' === $response->error->type ) {
118
					if ( $retry ) {
119
						sleep( 5 );
120
						return $this->process_redirect_payment( $order_id, false );
121
					} else {
122
						$localized_message = __( 'API connection error and retries exhausted.', 'woocommerce-gateway-stripe' );
123
						$order->add_order_note( $localized_message );
124
						throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
125
					}
126
				}
127
128
				// We want to retry.
129
				if ( $this->is_retryable_error( $response->error ) ) {
130
					if ( $retry ) {
131
						// Don't do anymore retries after this.
132
						if ( 5 <= $this->retry_interval ) {
133
							return $this->process_redirect_payment( $order_id, false );
134
						}
135
136
						sleep( $this->retry_interval );
137
138
						$this->retry_interval++;
139
						return $this->process_redirect_payment( $order_id, true );
140
					} else {
141
						$localized_message = __( 'On going requests error and retries exhausted.', 'woocommerce-gateway-stripe' );
142
						$order->add_order_note( $localized_message );
143
						throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
144
					}
145
				}
146
147
				// Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without.
148
				if ( preg_match( '/No such customer/i', $response->error->message ) && $retry ) {
149
					if ( WC_Stripe_Helper::is_pre_30() ) {
150
						delete_user_meta( $order->customer_user, '_stripe_customer_id' );
151
						delete_post_meta( $order_id, '_stripe_customer_id' );
152
					} else {
153
						delete_user_meta( $order->get_customer_id(), '_stripe_customer_id' );
154
						$order->delete_meta_data( '_stripe_customer_id' );
155
						$order->save();
156
					}
157
158
					return $this->process_redirect_payment( $order_id, false );
159
160
				} elseif ( preg_match( '/No such token/i', $response->error->message ) && $source_object->token_id ) {
161
					// Source param wrong? The CARD may have been deleted on stripe's end. Remove token and show message.
162
163
					$wc_token = WC_Payment_Tokens::get( $source_object->token_id );
164
					$wc_token->delete();
165
					$message = __( 'This card is no longer available and has been removed.', 'woocommerce-gateway-stripe' );
166
					$order->add_order_note( $message );
167
					throw new WC_Stripe_Exception( print_r( $response, true ), $message );
168
				}
169
170
				$localized_messages = WC_Stripe_Helper::get_localized_messages();
171
172
				if ( 'card_error' === $response->error->type ) {
173
					$message = isset( $localized_messages[ $response->error->code ] ) ? $localized_messages[ $response->error->code ] : $response->error->message;
174
				} else {
175
					$message = isset( $localized_messages[ $response->error->type ] ) ? $localized_messages[ $response->error->type ] : $response->error->message;
176
				}
177
178
				throw new WC_Stripe_Exception( print_r( $response, true ), $message );
179
			}
180
181
			do_action( 'wc_gateway_stripe_process_redirect_payment', $response, $order );
182

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

@@ 167-233 (lines=67) @@
164
			// Make the request.
165
			$response = WC_Stripe_API::request( $this->generate_payment_request( $order, $source_object ) );
166
167
			if ( ! empty( $response->error ) ) {
168
				// If it is an API error such connection or server, let's retry.
169
				if ( 'api_connection_error' === $response->error->type || 'api_error' === $response->error->type ) {
170
					if ( $retry ) {
171
						sleep( 5 );
172
						return $this->process_webhook_payment( $notification, false );
173
					} else {
174
						$localized_message = 'API connection error and retries exhausted.';
175
						$order->add_order_note( $localized_message );
176
						throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
177
					}
178
				}
179
180
				// We want to retry.
181
				if ( $this->is_retryable_error( $response->error ) ) {
182
					if ( $retry ) {
183
						// Don't do anymore retries after this.
184
						if ( 5 <= $this->retry_interval ) {
185
186
							return $this->process_webhook_payment( $notification, false );
187
						}
188
189
						sleep( $this->retry_interval );
190
191
						$this->retry_interval++;
192
						return $this->process_webhook_payment( $notification, true );
193
					} else {
194
						$localized_message = __( 'On going requests error and retries exhausted.', 'woocommerce-gateway-stripe' );
195
						$order->add_order_note( $localized_message );
196
						throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
197
					}
198
				}
199
200
				// Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without.
201
				if ( preg_match( '/No such customer/i', $response->error->message ) && $retry ) {
202
					if ( WC_Stripe_Helper::is_pre_30() ) {
203
						delete_user_meta( $order->customer_user, '_stripe_customer_id' );
204
						delete_post_meta( $order_id, '_stripe_customer_id' );
205
					} else {
206
						delete_user_meta( $order->get_customer_id(), '_stripe_customer_id' );
207
						$order->delete_meta_data( '_stripe_customer_id' );
208
						$order->save();
209
					}
210
211
					return $this->process_webhook_payment( $notification, false );
212
213
				} elseif ( preg_match( '/No such token/i', $response->error->message ) && $source_object->token_id ) {
214
					// Source param wrong? The CARD may have been deleted on stripe's end. Remove token and show message.
215
					$wc_token = WC_Payment_Tokens::get( $source_object->token_id );
216
					$wc_token->delete();
217
					$message = __( 'This card is no longer available and has been removed.', 'woocommerce-gateway-stripe' );
218
					$order->add_order_note( $message );
219
					throw new WC_Stripe_Exception( print_r( $response, true ), $message );
220
				}
221
222
				$localized_messages = WC_Stripe_Helper::get_localized_messages();
223
224
				if ( 'card_error' === $response->error->type ) {
225
					$localized_message = isset( $localized_messages[ $response->error->code ] ) ? $localized_messages[ $response->error->code ] : $response->error->message;
226
				} else {
227
					$localized_message = isset( $localized_messages[ $response->error->type ] ) ? $localized_messages[ $response->error->type ] : $response->error->message;
228
				}
229
230
				$order->add_order_note( $localized_message );
231
232
				throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
233
			}
234
235
			do_action( 'wc_gateway_stripe_process_webhook_payment', $response, $order );
236