ROx
ROx

Reputation: 11

Cancel a Stripe subscription auto renewal after first Payment -Wordpress

I have used stripe plugin and gravity form for payment in my WordPress site. Subscription payment transaction is working. Now I want to cancel auto renewal after one payment. I have used this code in my functions.php. I have added the my feed name $feed_names = array( 'Stripe Feed 1' ); and set the count if ( $count == 1 ) { but its not working. I cant understand how to solve this? I have not used any other code of gravity form in functions.php.

LINK

 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' );
        $feed_names = array( 'Stripe Feed 1' ); // update this line

        if ( in_array( $feed_name, $feed_names ) ) {
            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 == 1 ) { // update this line
                $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 ) );
            }
        }
    }
} );

Stripe Feeds Name

Upvotes: 1

Views: 838

Answers (1)

fool-dev
fool-dev

Reputation: 7777

Yes, this code is OK but I don't know where is your issue. I have a suggestion which is you can check the table {$wpdb->prefix}gf_addon_payment_transaction has been added data successfully while Subscription going on if your data has been added successfully then this add-on hook will work otherwise not because the table is empty.

How you check the data?

It's very easy you can just open up your database's table & check data exists or not. You can check your Stripe add-on log for seeing what's going on.

Could try the following third-party add-on

You can check these necessary links

Creating a Stripe Feed

logging-and-debugging

theme/plugin conflict test

Note:

When testing you'll need to wait a day before cancelling the subscription to give Stripe time to capture the subscription payment and send the webhook.

I think will help.

Upvotes: 1

Related Questions