mermberfix logo x
The MemberFix Team’s got you covered.
Membership plugins

Cancel PayKickstart Subscription Based on ConvertKit Tag

convertkit tag cancel subscription mm

Overview

Introduction

One of our customers uses the MemberMouse WordPress plugin as his membership solution and PayKickstart (PKS) as his checkout and funnels solution.

Normally, MemberMouse and PayKickStart don’t integrate very deeply so we created a tighter integration for him and explained exactly how to do it in a related post…

Related: MemberMouse integration with PayKickStart

Recently, this customer came to us requesting an additional integration between MemberMouse, PayKickStart and ConvertKit.

Namely, he wanted to be able to cancel a MemberMouse and PayKickstart subscription for a member when a certain tag gets assigned to that same member in ConvertKit.

We’ve created this solution for him by using a custom PHP script and Zapier. 

Now you get to use it too! 🙂

What You’ll Need

1 – MemberMouse – WordPress membership plugin

2 – Zapier account – Used for API integrations

3 – A PayKickStart account – An excellent checkout and funnels solution

Video Tutorial

Create the custom WordPress PHP template

First of all we will create a WordPress template.

IMPORTANT: I highly recommend that you first create a child theme, or use the My Custom Functions WordPress plugin whenever you add custom code to your theme files. Otherwise, any time you update your theme your changes will get wiped out!

1. You need access to your website FTP.

Just duplicate the page.php file from your theme files and name it convertkit-page.php.

Open convertkit-page.php and add this to the top of it:

<?php
/*
Template Name: ConvertKit
*/
?>

2. Go to your WordPress Dashboard and click on Pages => Add New.

Name the page “ConvertKit Page” and select “ConvertKit” as page template.

Set the permalink to /convertkit-page/ so that your page will be https://yourdomain.com/convertkit-page/.

im1 - Cancel PayKickstart Subscription Based on ConvertKit Tag

3. Next step is to add some php code to the page template which we have created.

Open convertkit-page.php and just below the opening tag add the below code:

<?php
if(!isset($_GET[“user_email”]) || empty($_GET[“user_email”]))
{
    exit;
}

global $wpdb; 
$user_email = $_GET[“user_email”];
$user = get_user_by( ’email’, $user_email );
$user_id = $user->ID;

echo $user_id;

// Cancel in PKS –

$status_message = $wpdb->get_var(“SELECT status_message FROM mm_user_data WHERE wp_user_id = ‘$user_id'”);

//get PKS subscription ID from the database; when MM – PKS integration is done, there is a custom field in database with the subscription ID
// more info on https://memberfix.rocks/membermouse-paykickstart/
// you need to replace x with the ID of the custom field
$subscription_id = $wpdb->get_results(“SELECT value FROM mm_custom_field_data WHERE user_id = ‘$user_id’ AND custom_field_id = x”); 

// if status message is different than Pending Cancelation then execute the code, if it is no code is executed and this avoids potentially nightmare loops!
if ( isset($subscription_id) && $status_message !== ‘Pending Cancelation’ ) {
    
$base_url = “https://app.paykickstart.com/api/”;
$route = “purchase/get”;
$url = $base_url . $route;
$post = false;

//Create request data string
$data = http_build_query();

//Execute cURL request
$ch = curl_init();
if ($post) {
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
} else {
    $url = $url . “?” . $data;
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$output = curl_exec($ch);
curl_close($ch);

$output = json_decode($output,true);

$cancel_date = $output[“subscriptions”][“next_date”];

$cancel_date_unix = strtotime($cancel_date);

//Set up API path and method
$base_url = “https://app.paykickstart.com/api/”;
$route = “subscriptions/cancel”;
$url = $base_url . $route;
$post = true;

//Create request data string
$data = http_build_query();

//Execute cURL request
$ch = curl_init();
if ($post) {
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
} else {
    $url = $url . “?” . $data;
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$output = curl_exec($ch);
curl_close($ch);

//add Pending Cancelation to mm_user_data table – status_message column – in order to prevent nightmare loops!
    
$wpdb->update( 
    ‘mm_user_data’, 
    array( 
        ‘status_message’ => ‘Pending Cancelation’
    ), 
    array( ‘wp_user_id’ => $user_id ), 
    array( 
        ‘%s’,   
    ), 
    array( ‘%d’  ) 
);

$wpdb->update( 
    ‘mm_user_data’, 
    array( 
        ‘status’ => 9,  
        ‘pending_status’ => 2,
        ‘cancellation_date’ => “$cancel_date”
    ), 
    array( ‘wp_user_id’ => $user_id ), 
    array( 
        ‘%d’,   
        ‘%d’,
        ‘%s’
    ), 
    array( ‘%d’  ) 
);
    
}

?>

As you can see in the above code, you will need your auth_token from PayKickstart.

You can find it on your Account Details page.

You will also need the ID of the custom field where the PKS subscription ID is stored.

(You can find more info about this in this article where we explain how to integrate MemberMouse with PayKickStart).

The connection between MemberMouse and PayKickstart is based on this subscription ID.

Create the Zap

In order to trigger the above code when a certain tag is added in ConvertKit, we will need to create a Zap.

1. Go to your Zapier account and click on “Create Zap” button:

zap1 - Cancel PayKickstart Subscription Based on ConvertKit Tag

2. For the first step, under the first tab choose “ConvertKit” for “Choose App” and “New Tag Subscriber” for “Choose Trigger Event“:

zap2 - Cancel PayKickstart Subscription Based on ConvertKit Tag

3. Under the second tab, “Choose Account“, you need to add your ConvertKit account.

In the pop-up which will appear you will need to add your ConvertKit API Key:

zap3 - Cancel PayKickstart Subscription Based on ConvertKit Tag

You will find this API Key in you ConvertKit Account under Account Settings:

zap4 - Cancel PayKickstart Subscription Based on ConvertKit Tag

4. For the next tab, “Customize Subscribe To Tag“, you just need to select the tag which you have already created in ConvertKit.

This is the tag which will start the cancel process.

zap5 - Cancel PayKickstart Subscription Based on ConvertKit Tag

5. For the step 2 of this Zap, under the first tab, “Choose App & Event“, choose “Webhooks by Zapier” and “GET”:

zap6 - Cancel PayKickstart Subscription Based on ConvertKit Tag

6. For the “Customize Request” tab, you will need to add the “yourdomain.com/convertkit-page/” in the URL field and for “Query String Params” you will need to add “user_email” and select “1. Subscriber Email” for it:

zap7 - Cancel PayKickstart Subscription Based on ConvertKit Tag

7. You can test the process on the next tab:

zap8 - Cancel PayKickstart Subscription Based on ConvertKit Tag

8. All is done! 🙂

9. Want our team to do this integration for you?

No problem, just visit our MemberFix product information page and send us a message!

Did you find this tutorial helpful?
0
1
User Review
100/100 (1 vote)

You may also enjoy...

WordPress based membership sites have certain requirements, and make use of certain applications that, in my experience, makes most of the popular hosting providers a poor choice.
John Oszajca is the founder of Music Marketing Manifesto, a membership site where he teaches musicians how to sell their music online!
Membership site success podcast where you learn from successful entrepreneurs, how to build and run a profitable membership site, so that you too can generate recurring revenue for your business

What’s The Best WordPress Management Tool For Multiple Websites? As a customer, you always want to feel assured that your WordPress support provider is using the absolute best possible technology

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments