Code Duplication    Length = 82-82 lines in 2 locations

includes/compat/class-wc-stripe-sepa-subs-compat.php 1 location

@@ 171-252 (lines=82) @@
168
	 * @param bool $retry Should we retry the process?
169
	 * @param object $previous_error
170
	 */
171
	public function process_subscription_payment( $amount = 0.0, $renewal_order, $retry = true, $previous_error ) {
172
		try {
173
			if ( $amount * 100 < WC_Stripe_Helper::get_minimum_amount() ) {
174
				/* translators: minimum amount */
175
				return new WP_Error( 'stripe_error', sprintf( __( 'Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( WC_Stripe_Helper::get_minimum_amount() / 100 ) ) );
176
			}
177
178
			$order_id = WC_Stripe_Helper::is_pre_30() ? $renewal_order->id : $renewal_order->get_id();
179
180
			// Get source from order
181
			$prepared_source = $this->prepare_order_source( $renewal_order );
182
			$source_object   = $prepared_source->source_object;
183
184
			if ( ! $prepared_source->customer ) {
185
				return new WP_Error( 'stripe_error', __( 'Customer not found', 'woocommerce-gateway-stripe' ) );
186
			}
187
188
			WC_Stripe_Logger::log( "Info: Begin processing subscription payment for order {$order_id} for the amount of {$amount}" );
189
190
			/* If we're doing a retry and source is chargeable, we need to pass
191
			 * a different idempotency key and retry for success.
192
			 */
193
			if ( is_object( $source_object ) && empty( $source_object->error ) && $this->need_update_idempotency_key( $source_object, $previous_error ) ) {
194
				add_filter( 'wc_stripe_idempotency_key', array( $this, 'change_idempotency_key' ), 10, 2 );
195
			}
196
197
			if ( ( $this->is_no_such_source_error( $previous_error ) || $this->is_no_linked_source_error( $previous_error ) ) && apply_filters( 'wc_stripe_use_default_customer_source', true ) ) {
198
				// Passing empty source will charge customer default.
199
				$prepared_source->source = '';
200
			}
201
202
			$request            = $this->generate_payment_request( $renewal_order, $prepared_source );
203
			$request['capture'] = 'true';
204
			$request['amount']  = WC_Stripe_Helper::get_stripe_amount( $amount, $request['currency'] );
205
			$response           = WC_Stripe_API::request( $request );
206
207
			if ( ! empty( $response->error ) ) {
208
				// We want to retry.
209
				if ( $this->is_retryable_error( $response->error ) ) {
210
					if ( $retry ) {
211
						// Don't do anymore retries after this.
212
						if ( 5 <= $this->retry_interval ) {
213
							return $this->process_subscription_payment( $amount, $renewal_order, false, $response->error );
214
						}
215
216
						sleep( $this->retry_interval );
217
218
						$this->retry_interval++;
219
220
						return $this->process_subscription_payment( $amount, $renewal_order, true, $response->error );
221
					} else {
222
						$localized_message = __( 'Sorry, we are unable to process your payment at this time. Please retry later.', 'woocommerce-gateway-stripe' );
223
						$renewal_order->add_order_note( $localized_message );
224
						throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
225
					}
226
				}
227
228
				$localized_messages = WC_Stripe_Helper::get_localized_messages();
229
230
				if ( 'card_error' === $response->error->type ) {
231
					$localized_message = isset( $localized_messages[ $response->error->code ] ) ? $localized_messages[ $response->error->code ] : $response->error->message;
232
				} else {
233
					$localized_message = isset( $localized_messages[ $response->error->type ] ) ? $localized_messages[ $response->error->type ] : $response->error->message;
234
				}
235
236
				$renewal_order->add_order_note( $localized_message );
237
238
				throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
239
			}
240
241
			do_action( 'wc_gateway_stripe_process_payment', $response, $renewal_order );
242
243
			$this->process_response( $response, $renewal_order );
244
		} catch ( WC_Stripe_Exception $e ) {
245
			WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
246
247
			do_action( 'wc_gateway_stripe_process_payment_error', $e, $renewal_order );
248
249
			/* translators: error message */
250
			$renewal_order->update_status( 'failed' );
251
		}
252
	}
253
254
	/**
255
	 * Don't transfer Stripe customer/token meta to resubscribe orders.

includes/compat/class-wc-stripe-subs-compat.php 1 location

@@ 186-267 (lines=82) @@
183
	 * @param bool $retry Should we retry the process?
184
	 * @param object $previous_error
185
	 */
186
	public function process_subscription_payment( $amount = 0.0, $renewal_order, $retry = true, $previous_error ) {
187
		try {
188
			if ( $amount * 100 < WC_Stripe_Helper::get_minimum_amount() ) {
189
				/* translators: minimum amount */
190
				return new WP_Error( 'stripe_error', sprintf( __( 'Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( WC_Stripe_Helper::get_minimum_amount() / 100 ) ) );
191
			}
192
193
			$order_id = WC_Stripe_Helper::is_pre_30() ? $renewal_order->id : $renewal_order->get_id();
194
195
			// Get source from order
196
			$prepared_source = $this->prepare_order_source( $renewal_order );
197
			$source_object   = $prepared_source->source_object;
198
199
			if ( ! $prepared_source->customer ) {
200
				return new WP_Error( 'stripe_error', __( 'Customer not found', 'woocommerce-gateway-stripe' ) );
201
			}
202
203
			WC_Stripe_Logger::log( "Info: Begin processing subscription payment for order {$order_id} for the amount of {$amount}" );
204
205
			/* If we're doing a retry and source is chargeable, we need to pass
206
			 * a different idempotency key and retry for success.
207
			 */
208
			if ( is_object( $source_object ) && empty( $source_object->error ) && $this->need_update_idempotency_key( $source_object, $previous_error ) ) {
209
				add_filter( 'wc_stripe_idempotency_key', array( $this, 'change_idempotency_key' ), 10, 2 );
210
			}
211
212
			if ( ( $this->is_no_such_source_error( $previous_error ) || $this->is_no_linked_source_error( $previous_error ) ) && apply_filters( 'wc_stripe_use_default_customer_source', true ) ) {
213
				// Passing empty source will charge customer default.
214
				$prepared_source->source = '';
215
			}
216
217
			$request            = $this->generate_payment_request( $renewal_order, $prepared_source );
218
			$request['capture'] = 'true';
219
			$request['amount']  = WC_Stripe_Helper::get_stripe_amount( $amount, $request['currency'] );
220
			$response           = WC_Stripe_API::request( $request );
221
222
			if ( ! empty( $response->error ) ) {
223
				// We want to retry.
224
				if ( $this->is_retryable_error( $response->error ) ) {
225
					if ( $retry ) {
226
						// Don't do anymore retries after this.
227
						if ( 5 <= $this->retry_interval ) {
228
							return $this->process_subscription_payment( $amount, $renewal_order, false, $response->error );
229
						}
230
231
						sleep( $this->retry_interval );
232
233
						$this->retry_interval++;
234
235
						return $this->process_subscription_payment( $amount, $renewal_order, true, $response->error );
236
					} else {
237
						$localized_message = __( 'Sorry, we are unable to process your payment at this time. Please retry later.', 'woocommerce-gateway-stripe' );
238
						$renewal_order->add_order_note( $localized_message );
239
						throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
240
					}
241
				}
242
243
				$localized_messages = WC_Stripe_Helper::get_localized_messages();
244
245
				if ( 'card_error' === $response->error->type ) {
246
					$localized_message = isset( $localized_messages[ $response->error->code ] ) ? $localized_messages[ $response->error->code ] : $response->error->message;
247
				} else {
248
					$localized_message = isset( $localized_messages[ $response->error->type ] ) ? $localized_messages[ $response->error->type ] : $response->error->message;
249
				}
250
251
				$renewal_order->add_order_note( $localized_message );
252
253
				throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
254
			}
255
256
			do_action( 'wc_gateway_stripe_process_payment', $response, $renewal_order );
257
258
			$this->process_response( $response, $renewal_order );
259
		} catch ( WC_Stripe_Exception $e ) {
260
			WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
261
262
			do_action( 'wc_gateway_stripe_process_payment_error', $e, $renewal_order );
263
264
			/* translators: error message */
265
			$renewal_order->update_status( 'failed' );
266
		}
267
	}
268
269
	/**
270
	 * Updates other subscription sources.