Add Stripe free trial with coupon

I’ve hardcoded a “coupon” to give the customer a free trial; since Stripe coupons only have the option of giving a percentage or dollar amount discount I’m doing this entirely on the WordPress side.

I think I’m on the right track with a hook to test for this coupon and set the subscription parameters to include a trial, but I’m getting hung up because Stripe seems to need a customer ID (even though this offer is only for a new customer).

I think it might be possible to use a hook to set the customer ID after it’s created but BEFORE the subscription parameters are changed and submitted, as I’m getting my test customers created in the Stripe customer database, even though I get

“There was a problem with your submission: Missing required param: customer.”

…in the form feedback.

Here is the function I’m thinking of for customer ID with the hook omitted because I can’t find one that I’m sure will work, as well as my coupon processing hook function. Any thoughts?


add_filter('CUSTOMER_CREATION_HOOK_HERE', 'retrieve_stripe_customer_id', 10, 3);
function retrieve_stripe_customer_id($customer_id, $feed, $entry) {
    // Store customer ID in a transient
    set_transient('new_customer_id_' . $entry['id'], $customer_id, HOUR_IN_SECONDS);
    return $customer_id; // Return the original value to avoid altering it
}

add_filter('gform_stripe_subscription_params_pre_update_customer', 'customize_stripe_subscription', 10, 4);
function customize_stripe_subscription($subscription_params, $entry, $feed, $submission_data) {
    // Retrieve the stored customer ID
    $customer_id = get_transient('new_customer_id_' . $entry['id']);

    if ($customer_id) {
        $subscription_params['customer'] = $customer_id;

        // Get the value of your hidden field (replace 'X' with the field ID)
        $free_trial = rgar($entry, 'X');

        // If the hidden field has the trigger value, set the trial period
        if ($free_trial == 'trigger_value') {
                $trial_period_days = 30; // Set the length of the free trial in days
                $subscription_params['trial_period_days'] = $trial_period_days;
        }
    }

    return $subscription_params;
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.