llms_can_gateway_be_used_for_plan_or_order( string $gateway_id, LLMS_Order|LLMS_Access_Plan|int $plan_or_order, boolean $wp_err = false, boolean $enabled_only = true )

Determines if a payment gateway can be used to process transactions for an LLMS_Order or an LLMS_Access_Plan.


Description Description

  • The plan/order must exist
    • The gateway must exist.
    • The gateway must be enabled unless $enabled_only is false.
    • The gateway must support the order/plan’s type (recurring or single).

Top ↑

Parameters Parameters

$gateway_id

(string) (Required) Payment gateway ID.

$plan_or_order

(LLMS_Order|LLMS_Access_Plan|int) (Required) The WP_Post id of a plan or order, a plan object, or an order object.

$wp_err

(boolean) (Optional) Determines the return type when the gateway cannot be used.

Default value: false

$enabled_only

(boolean) (Optional) If true requires the specified gateway to be enabled for use. This property exists to ensure the manual payment gateway can be used to record free transactions regardless of the gateway's status.

Default value: true


Top ↑

Return Return

(boolean|WP_Error) Returns true if the gateway can be used. If the gateway cannot be used, returns false if $wp_error is false and a WP_Error if $wp_err is true.


Top ↑

Source Source

File: includes/functions/llms.functions.order.php

 */
function llms_can_gateway_be_used_for_plan_or_order( $gateway_id, $plan_or_order, $wp_err = false, $enabled_only = true ) {

	$can_use = true;

	$plan_or_order = is_numeric( $plan_or_order ) ? llms_get_post( $plan_or_order ) : $plan_or_order;
	$err_data      = compact( 'gateway_id', 'plan_or_order' );
	$order         = is_a( $plan_or_order, 'LLMS_Order' ) ? $plan_or_order : null;
	$plan          = ! $order && is_a( $plan_or_order, 'LLMS_Access_Plan' ) ? $plan_or_order : null;

	if ( is_null( $order ) && is_null( $plan ) ) {
		$can_use = new WP_Error( 'post-invalid', __( 'A valid order or access plan must be supplied.', 'lifterlms' ), $err_data );
	} else {

		$gateway = llms()->payment_gateways()->get_gateway_by_id( $gateway_id );
		if ( ! $gateway ) {
			$can_use = new WP_Error( 'gateway-invalid', __( 'The selected payment gateway is not valid.', 'lifterlms' ), $err_data );
		} elseif ( $enabled_only && ! $gateway->is_enabled() ) {
			$can_use = new WP_Error( 'gateway-disabled', __( 'The selected payment gateway is not available.', 'lifterlms' ), $err_data );
		} elseif ( ! $gateway->can_process_access_plan( $plan, $order ) ) {
			// Check whether the gateway can process the plan or the order's plan (which is the plan at the moment of the order's creation).
			$can_use = new WP_Error( 'gateway-support-plan', __( 'The selected payment gateway is not available for the given plan.', 'lifterlms' ), $err_data );
		} elseif ( $plan_or_order->is_recurring() && ! $gateway->supports( 'recurring_payments' ) ) {
			$can_use = new WP_Error( 'gateway-support-recurring', __( 'The selected payment gateway does not support recurring payments.', 'lifterlms' ), $err_data );
		} elseif ( ! $plan_or_order->is_recurring() && ! $gateway->supports( 'single_payments' ) ) {
			$can_use = new WP_Error( 'gateway-support-single', __( 'The selected payment gateway does not support one-time payments.', 'lifterlms' ), $err_data );
		}
	}

	/**
	 * Filters whether or not a gateway can be used for a given plan or order.
	 *
	 * @since 7.0.0
	 *
	 * @param boolean|WP_Error $can_be_used Whether or not the gateway can be used for the plan. This value will be `true`
	 *                                      when the gateway can be used an an error object when it cannot.
	 * @param string           $gateway_id  The LLMS_Payment_Gateway ID.
	 * @param LLMS_Access_plan $plan        The access plan object.


Top ↑

Changelog Changelog

Changelog
Version Description
7.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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