Recurly

A SaaS-based company providing recurring billing management as an outsourced service.

This integration method uses Recurly API get the details of the purchased plan and to record a commission with the subscription values.

The integration requires the Recurring Commissions feature for tracking the recurring payments/trial conversions.

1.

Recurly configuration

This integration method uses external "Thank you page" especially PHP one, so if you do not have one, please create it. When you have the site, navigate to Configuration->Plans section of your Recurly admin panel and edit your plans. Towards the bottom of the edit plan screen check the checkbox for Bypass Recurly Confirmation and set Return URL After Success to your thank you page, where Recurly API will be used.
Add this string to each URL:

?account_code={{account_code}}&plan={{plan_code}}

In case you already have question mark in the link, change the one in above code to ampersand (&) symbol.

2.

Enable API

In Recurly admin panel go to Integrations->API Credentials section enable your API if it is not enabled yet, and get your API key.

Please, download Recurly API library (ideally the PHP one) since you will need it in the next step.
Now, navigate to Integrations->Webhooks and use Configure button to add new Endpoint and point its URL to http://URL_TO_PostAffiliatePro/plugins/Recurly/recurly.php

Finally, enable Recurly plugin in your Plugins section of your Post Affiliate Pro merchant panel.

3.

Integration code

Open your thank you page for editing and paste this code to the file:

<?php
if (isset($_GET['account_code']) && $_GET['account_code'] != '' && isset($_GET['plan']) && $_GET['plan'] != '') {
	require 'lib/recurly.php';
	/* https://<your-subdomain>.recurly.com */
	Recurly_Client::$subdomain = 'your-subdomain';
	/* your private API key */
	Recurly_Client::$apiKey = '012345678901234567890123456789ab';	
    papTrack();
}

function papTrack() {
    try {
        $subscriptions = Recurly_SubscriptionList::getForAccount($_GET['account_code']);
    } catch (Recurly_NotFoundError $e) {
        // No subscriptions found for received account - echo $e->getMessage();
        return;
    }
	$purchasedSubscription = null;
    foreach ($subscriptions as $subscription) {
        if ($subscription->plan->plan_code != $_GET['plan']) {
            continue;
        }
        $purchasedSubscription = $subscription;
    }
	if ($purchasedSubscription === null) {
		return;
	}
    $subscriptionId = $purchasedSubscription->uuid;
    $subscriptionCode = $purchasedSubscription->plan->plan_code;
    $invoice = $purchasedSubscription->invoice->get();
	$totalCost = $invoice->subtotal_in_cents;
    $currency = $invoice->currency;
	echo '<script id="pap_x2s6df8d" src="http://URL_TO_PostAffiliatePro/scripts/trackjs.js" type="text/javascript"></script>'.PHP_EOL;
	echo "<script type='text/javascript'>
var sale = PostAffTracker.createSale();
PostAffTracker.setAccountId('Account_ID');
sale.setTotalCost('{$totalCost}');
sale.setOrderID('{$subscriptionId}');
sale.setProductID('{$subscriptionCode}');
sale.setCurrency('{$currency}');
sale.setData1('{$_GET['account_code']}');

PostAffTracker.register();
</script>";
}
?>

Keep in mind you have to upload files from the Recurly API client to the directory where your Thank you page is. As you can see, the third line of the code includes /lib/recurly.php file from the API client.

The next lines define your Recurly subdomain and your API key, so please add your credentials there!


Do not forget to integrate your site with the click tracking code.