Cancel Stripe subscription

Gravity Form is one of the most robust WordPress from builder. Though a premium plugin I think it’s worth it.

Gravity Form does put lot of effort behind their work. This is one of the example of it. By default (at the point of writing) Stripe doesn’t allow to have instalment base payment or cancel the subscriptions after certain number of times. But there is an workaround.

If you are using Gravity Form, you can cancel subscription using gform_post_add_subscription_payment which basically looks at the number of times each subscription is charged before cancelling it.

Here is the source of code that does it.

<?php add_action( 'gform_post_add_subscription_payment', function ( $entry ) { if ( rgar( $entry, 'payment_status' ) == 'Active' ) { $feed = gf_stripe()->get_payment_feed( $entry );
        $feed_name  = rgars( $feed, 'meta/feedName' );
        // All the feednames you need to cancel after n number of times
        $feed_names = array( 'subscription feed one', 'subscription feed two' ); 
        if ( in_array( $feed_name, $feed_names ) ) {
            //look for entries in the custom gravity form table for subscriptions
            // count the number of rows for each subscription
            global $wpdb;
            //count number of rows where id [column] matches
            $count = $wpdb->get_var( $wpdb->prepare( "SELECT count(id) FROM {$wpdb->prefix}gf_addon_payment_transaction WHERE lead_id=%d", $entry['id'] ) );
            // i want to cancel after 5 paymnets
            if ( $count >= 5 ) { 
                $result = gf_stripe()->cancel( $entry, $feed );
} );

Above code is borrowed from here –

Do test your code before publishing it live.

About Author

I am Robin - WordPress Developer based in Sydney. I work as Digital Developer. I do some projects now and then, and love blogging about WordPress and Web Design.