Cancel Stripe subscription

Introduction 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 […]

Table of Content

Introduction

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.

Solution

Here is the source of code that does it.

[code language="php"]
<?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 );
}
}
}
} );
?>
[/code]

Above code is borrowed from here - httpss://docs.gravityforms.com/cancel-stripe-subscription-payments/

Do test your code before publishing it live.

About Author

Robin Thebe

Digital Strategist based in Sydney.

I am multi disciplined web developer based in Sydney focusing around WordPress web design, wordpress development, SEO Services, SEM and Email Marketing.