Skip to content

Instantly share code, notes, and snippets.

@chrisegg
Last active September 6, 2021 14:43
Show Gist options
  • Save chrisegg/825a5643d1e9f0e7363892bebcc0d117 to your computer and use it in GitHub Desktop.
Save chrisegg/825a5643d1e9f0e7363892bebcc0d117 to your computer and use it in GitHub Desktop.
Limit Stripe Subscriptions to a Specified Number of Payments
<?php
//Code provided by Gravity Forms here: https://rocketgenius.pxf.io/limit-payments
//Start copying below this line...
add_action( 'gform_post_add_subscription_payment', function ( $entry ) {
gf_stripe()->log_debug( "Running..." );
if ( rgar( $entry, 'payment_status' ) == 'Active' ) {
$feed = gf_stripe()->get_payment_feed( $entry );
$feed_name = rgars( $feed, 'meta/feedName' );
$feed_names = array( 'feed name one', 'feed name two' ); // update this line
if ( in_array( $feed_name, $feed_names ) ) {
gf_stripe()->log_debug( 'Feed: ' . $feed_name . 'Entry: ' . $entry['id'] );
global $wpdb;
$count = $wpdb->get_var( $wpdb->prepare( "SELECT count(id) FROM {$wpdb->prefix}gf_addon_payment_transaction WHERE lead_id=%d", $entry['id'] ) );
if ( $count >= 3 ) { // Update this line to the Total number of payments including the first one.
$result = gf_stripe()->cancel( $entry, $feed );
gf_stripe()->log_debug( "gform_post_add_subscription_payment: Cancelling subscription (feed #{$feed['id']} - {$feed_name}) for entry #{$entry['id']}. Result: " . print_r( $result, 1 ) );
}
}
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment