Code Duplication    Length = 51-54 lines in 2 locations

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

@@ 123-173 (lines=51) @@
120
			$headers  = $response['headers'];
121
			$response = $response['body'];
122
123
			if ( ! empty( $response->error ) ) {
124
				// Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without.
125
				if ( $this->is_no_such_customer_error( $response->error ) ) {
126
					if ( WC_Stripe_Helper::is_pre_30() ) {
127
						delete_user_meta( $order->customer_user, '_stripe_customer_id' );
128
						delete_post_meta( $order_id, '_stripe_customer_id' );
129
					} else {
130
						delete_user_meta( $order->get_customer_id(), '_stripe_customer_id' );
131
						$order->delete_meta_data( '_stripe_customer_id' );
132
						$order->save();
133
					}
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 );
151
						}
152
153
						sleep( $this->retry_interval );
154
155
						$this->retry_interval++;
156
						return $this->process_redirect_payment( $order_id, true );
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

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