Creating a subscription in Stripe using the Gravity Forms Stripe Add-On is a pretty simple task, and there is a good deal of documentation available to help. However, if you have a variable subscription—such as a per-user per-month pricing model—you might be unpleasantly surprised at how Gravity Forms handles that.
The Problem
Out-of-the-box, the Gravity Forms Stripe Add-On creates a separate product in Stripe for each product-quantity combination created by subscribers. This means that if one customer subscribes for 1 user, another customer for 2 users, and a third customer for 5 users, there will be three separate subscription products in your Stripe Dashboard. Furthermore, the unit price that appears in these subscriptions will be incorrect; it will reflect the total for the selected number of users rather than the actual unit price. This is confusing to the customer, makes it difficult to adjust the subscription in the future, and creates far more products in Stripe than are necessary.
In my opinion it would be better if the unit price remained the same regardless of the quantity selected, and for the quantity to reflect the actual quantity in the subscription. For example, instead of 1 x $100 appearing on the invoice, I’d rather see 5 x $20 to reflect the 5 users in my team.
The Fix
While Gravity Forms Stripe Add-On does not allow this configuration out-of-the-box, it’s possible to accomplish this with the following snippet:
/** * Changes the Stripe subscription amount to use the unit price and quantity when setting up the subscription. * * @param array $submission_data The customer and transaction data. * @param array $feed The Feed Object. * * @return array $submission_data */ add_filter( 'gform_submission_data_pre_process_payment', function ( $submission_data, $feed ) { if ( $feed['addon_slug'] !== 'gravityformsstripe' || rgars( $feed, 'meta/transactionType' ) !== 'subscription' ) { return $submission_data; } $recurring_amount_field_id = (int) rgars( $feed, 'meta/recurringAmount' ); if ( ! $recurring_amount_field_id ) { return $submission_data; } $line_items = []; foreach ( $submission_data['line_items'] as $line_item ) { $line_items[ $line_item['id'] ] = $line_item; } // Check to make sure there is a single product field being used for the subscription amount // rather than the form total or some other more complex scenario. if ( ! isset( $line_items[ $recurring_amount_field_id ] ) ) { return $submission_data; } $submission_data['payment_amount'] = $line_items[ $recurring_amount_field_id ]['unit_price']; $quantity = max( (int) $line_items[ $recurring_amount_field_id ]['quantity'], 1 ); /** * Set the quantity for the subscription. * * @param array $subscription_params The subscription parameters. */ add_filter( 'gform_stripe_subscription_params_pre_update_customer', function ( $subscription_params ) use ( $quantity ) { $subscription_params['items'][0]['quantity'] = $quantity; return $subscription_params; } ); return $submission_data; }, 10, 2 );
When you create your Stripe feed, make sure to map the price field—not the form total—to the “Recurring Amount” setting:
Conclusion
With that snippet in place and the Stripe feed configured properly, we can now create subscriptions that accurately reflect the quantity in the subscription!
David Bentley says
Great workaround!!! We will use this in the future. We’ve run into the same issues. Another recommendation – use the free gravitystripe plugin in the repository – https://wordpress.org/plugins/manage-gravity-forms-stripe-subscriptions/ so that subscribers can access their subscription details and cancel without having to contact the site owner.
Here’s a demo site – https://gformsdemo.com