src/includes/classes/Core/Utils/Stripe.php
public function plan($args)
    {
        if (is_string($args)) {
            $args = ['plan' => $args];
        } // Convert plan ID into args.

        $default_args = [
            'api_key' => '', // Stripe API key.
            'plan'    => '', // Get existing ID.

            // Everything else is for a new plan.

            'trial_period_days' => 0, // New plan trial.
            'interval'          => '', // New plan interval.
            // One of these: `day`, `week`, `month` or `year`.
            'interval_count' => '', // New plan interval count.
            // The number of intervals between each subscription billing.
            // For example, interval=month and interval_count=3, bills every 3 months.
            // Maximum of one year interval allowed (1 year, 12 months, 52 weeks, 365 days).

            'amount'   => .50, // New plan amount.
            'currency' => 'USD', // New plan currency.
            'name'     => '', // New plan name/description.

            'metadata' => '', // New charge metadata.
        ];
        $args = (array) $args; // Force array.
        $args = array_merge($default_args, $args);
        $args = array_intersect_key($args, $default_args);
        extract($this->parseArgs(__FUNCTION__, $args));

        $Plan = null; // Initialize.

        if ($args['plan']) {
            try { // Acquire plan by ID.
                $Plan = Plan::retrieve($args['plan'], $opts);
            } catch (\Throwable $Exception) {
                return $this->exceptionError($Exception);
            }
        } else { // Maybe create a new plan.
            $hash = 'pln_'.md5(serialize($args));

            try { // Check for existing plan.
                $Plan = Plan::retrieve($hash, $opts);
            } catch (\Throwable $Exception) {
                $Plan = null; // Soft failure.
            }
            if (!($Plan instanceof Plan)) {
                try { // New plan.
                    $new_plan_args = [
                        'id' => $hash, // Auto-generated ID.

                        'trial_period_days' => $args['trial_period_days'] ?: null,
                        'interval'          => $args['interval'],
                        'interval_count'    => $args['interval_count'],

                        'amount'   => $args['amount'],
                        'currency' => $args['currency'],
                        'name'     => $args['name'] ?: __('Subscription'),

                        'metadata' => $args['metadata'],
                    ];
                    $new_plan_args = $this->c::removeNulls($new_plan_args);
                    $Plan          = Plan::create($new_plan_args, $opts);
                } catch (\Throwable $Exception) {
                    return $this->exceptionError($Exception);
                }
            }
        }
        if (!($Plan instanceof Plan)) {
            $_error = sprintf('Failed to acquire plan.');
            return $this->exceptionError(new Exception($_error));
        }
        return $Plan;
    }
public function plan($args)
    {
        if (is_string($args)) {
            $args = ['plan' => $args];
        } // Convert plan ID into args.

        $default_args = [
            'api_key' => '', // Stripe API key.
            'plan'    => '', // Get existing ID.

            // Everything else is for a new plan.

            'trial_period_days' => 0, // New plan trial.
            'interval'          => '', // New plan interval.
            // One of these: `day`, `week`, `month` or `year`.
            'interval_count' => '', // New plan interval count.
            // The number of intervals between each subscription billing.
            // For example, interval=month and interval_count=3, bills every 3 months.
            // Maximum of one year interval allowed (1 year, 12 months, 52 weeks, 365 days).

            'amount'   => .50, // New plan amount.
            'currency' => 'USD', // New plan currency.
            'name'     => '', // New plan name/description.

            'metadata' => '', // New charge metadata.
        ];
        $args = (array) $args; // Force array.
        $args = array_merge($default_args, $args);
        $args = array_intersect_key($args, $default_args);
        extract($this->parseArgs(__FUNCTION__, $args));

        $Plan = null; // Initialize.

        if ($args['plan']) {
            try { // Acquire plan by ID.
                $Plan = Plan::retrieve($args['plan'], $opts);
            } catch (\Throwable $Exception) {
                return $this->exceptionError($Exception);
            }
        } else { // Maybe create a new plan.
            $hash = 'pln_'.md5(serialize($args));

            try { // Check for existing plan.
                $Plan = Plan::retrieve($hash, $opts);
            } catch (\Throwable $Exception) {
                $Plan = null; // Soft failure.
            }
            if (!($Plan instanceof Plan)) {
                try { // New plan.
                    $new_plan_args = [
                        'id' => $hash, // Auto-generated ID.

                        'trial_period_days' => $args['trial_period_days'] ?: null,
                        'interval'          => $args['interval'],
                        'interval_count'    => $args['interval_count'],

                        'amount'   => $args['amount'],
                        'currency' => $args['currency'],
                        'name'     => $args['name'] ?: __('Subscription'),

                        'metadata' => $args['metadata'],
                    ];
                    $new_plan_args = $this->c::removeNulls($new_plan_args);
                    $Plan          = Plan::create($new_plan_args, $opts);
                } catch (\Throwable $Exception) {
                    return $this->exceptionError($Exception);
                }
            }
        }
        if (!($Plan instanceof Plan)) {
            $_error = sprintf('Failed to acquire plan.');
            return $this->exceptionError(new Exception($_error));
        }
        return $Plan;
    }
Get (or create) a plan.

@since 170329.13807 Stripe utils.

@param string|array $args ID or args.

@return Plan|Error Plan on success.