Code Duplication    Length = 82-82 lines in 2 locations

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

@@ 221-302 (lines=82) @@
218
	 * @param bool $retry Should we retry the process?
219
	 * @param object $previous_error
220
	 */
221
	public function process_subscription_payment( $amount = 0.0, $renewal_order, $retry = true, $previous_error ) {
222
		try {
223
			if ( $amount * 100 < WC_Stripe_Helper::get_minimum_amount() ) {
224
				/* translators: minimum amount */
225
				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 ) ) );
226
			}
227
228
			$order_id = WC_Stripe_Helper::is_wc_lt( '3.0' ) ? $renewal_order->id : $renewal_order->get_id();
229
230
			// Get source from order
231
			$prepared_source = $this->prepare_order_source( $renewal_order );
232
			$source_object   = $prepared_source->source_object;
233
234
			if ( ! $prepared_source->customer ) {
235
				return new WP_Error( 'stripe_error', __( 'Customer not found', 'woocommerce-gateway-stripe' ) );
236
			}
237
238
			WC_Stripe_Logger::log( "Info: Begin processing subscription payment for order {$order_id} for the amount of {$amount}" );
239
240
			/* If we're doing a retry and source is chargeable, we need to pass
241
			 * a different idempotency key and retry for success.
242
			 */
243
			if ( is_object( $source_object ) && empty( $source_object->error ) && $this->need_update_idempotency_key( $source_object, $previous_error ) ) {
244
				add_filter( 'wc_stripe_idempotency_key', array( $this, 'change_idempotency_key' ), 10, 2 );
245
			}
246
247
			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 ) ) {
248
				// Passing empty source will charge customer default.
249
				$prepared_source->source = '';
250
			}
251
252
			$request            = $this->generate_payment_request( $renewal_order, $prepared_source );
253
			$request['capture'] = 'true';
254
			$request['amount']  = WC_Stripe_Helper::get_stripe_amount( $amount, $request['currency'] );
255
			$response           = WC_Stripe_API::request( $request );
256
257
			if ( ! empty( $response->error ) ) {
258
				// We want to retry.
259
				if ( $this->is_retryable_error( $response->error ) ) {
260
					if ( $retry ) {
261
						// Don't do anymore retries after this.
262
						if ( 5 <= $this->retry_interval ) {
263
							return $this->process_subscription_payment( $amount, $renewal_order, false, $response->error );
264
						}
265
266
						sleep( $this->retry_interval );
267
268
						$this->retry_interval++;
269
270
						return $this->process_subscription_payment( $amount, $renewal_order, true, $response->error );
271
					} else {
272
						$localized_message = __( 'Sorry, we are unable to process your payment at this time. Please retry later.', 'woocommerce-gateway-stripe' );
273
						$renewal_order->add_order_note( $localized_message );
274
						throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
275
					}
276
				}
277
278
				$localized_messages = WC_Stripe_Helper::get_localized_messages();
279
280
				if ( 'card_error' === $response->error->type ) {
281
					$localized_message = isset( $localized_messages[ $response->error->code ] ) ? $localized_messages[ $response->error->code ] : $response->error->message;
282
				} else {
283
					$localized_message = isset( $localized_messages[ $response->error->type ] ) ? $localized_messages[ $response->error->type ] : $response->error->message;
284
				}
285
286
				$renewal_order->add_order_note( $localized_message );
287
288
				throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
289
			}
290
291
			do_action( 'wc_gateway_stripe_process_payment', $response, $renewal_order );
292
293
			$this->process_response( $response, $renewal_order );
294
		} catch ( WC_Stripe_Exception $e ) {
295
			WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
296
297
			do_action( 'wc_gateway_stripe_process_payment_error', $e, $renewal_order );
298
299
			/* translators: error message */
300
			$renewal_order->update_status( 'failed' );
301
		}
302
	}
303
304
	/**
305
	 * Don't transfer Stripe customer/token meta to resubscribe orders.

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

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