LLMS_Order::maybe_schedule_retry()

Handles scheduling recurring payment retries when the gateway supports them


Return Return

(null|boolean) Returns null if the order cannot be retried, false when all retry rules have been tried (or none exist), and true when a retry is scheduled.


Top ↑

Source Source

File: includes/models/model.llms.order.php

	 * @return void
	 */
	public function maybe_schedule_payment( $recalc = true ) {

		if ( ! $this->is_recurring() ) {
			return;
		}

		if ( $recalc ) {
			$this->set( 'date_next_payment', $this->calculate_next_payment_date() );
		}

		$date = $this->get_next_payment_due_date();

		// Unschedule and reschedule.
		if ( $date && ! is_wp_error( $date ) ) {

			$this->schedule_recurring_payment( $date );

		} elseif ( is_wp_error( $date ) ) {

			if ( 'plan-ended' === $date->get_error_code() ) {

				// Unschedule the next action (does nothing if no action scheduled).
				$this->unschedule_recurring_payment();

				// Add a note that the plan has completed.
				$this->add_note( __( 'Order payment plan completed.', 'lifterlms' ) );
				$this->set( 'plan_ended', 'yes' );

			}
		}

	}

	/**
	 * Handles scheduling recurring payment retries when the gateway supports them
	 *
	 * @since 3.10.0
	 * @since 7.0.0 Added return value.
	 *
	 * @return null|boolean Returns `null` if the order cannot be retried, `false` when all retry rules have been tried (or none exist), and `true`
	 *                      when a retry is scheduled.
	 */
	public function maybe_schedule_retry() {

		if ( ! $this->can_be_retried() ) {
			return null;
		}

		// Get the index of the rule to use for this retry.
		$current_rule_index = $this->get( 'last_retry_rule' );
		if ( '' === $current_rule_index ) {
			$current_rule_index = 0;
		} else {
			++$current_rule_index;
		}

		$rules        = $this->get_retry_rules();
		$current_rule = $rules[ $current_rule_index ] ?? false;

		// No rule to run.
		if ( ! $current_rule ) {

			$this->set_status( 'failed' );
			$this->set( 'last_retry_rule', '' );

			$this->add_note( esc_html__( 'Maximum retry attempts reached.', 'lifterlms' ) );

			/**
			 * Action triggered when there are not more recurring payment retry rules.
			 *
			 * @since 3.10.0
			 *
			 * @param LLMS_Order $order The order object.
			 */


Top ↑

Changelog Changelog

Changelog
Version Description
7.0.0 Added return value.
3.10.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.