Get invoice and account details from active subscriptions in Recurly

This article assumes you are using the PHP library for Recurly. This library is available from here : https://github.com/recurly/recurly-client-php

 

The code snippet shows how to obtain a list of all active subscriptions in recurly and then fetch the details of each subscription. Note that it takes a while for recurly to return the list of subscriptions and it does not support paging of the data – it returns it all in one go. So if you have a large number of subscriptions active, then it could take a couple of minutes to return the entire data. I have put in a check to return only x number of active subscriptions.

	session_start();
	error_reporting(E_ALL ^ E_wARNING ^ E_NOTICE ^ E_DEPRECATED);
	set_time_limit(600);
	date_default_timezone_set('America/Los_Angeles');

		// credentials for recurly api
	$subdomain = 'your subdomain';
	$apiKey = 'your api key';

	require_once("lib/recurly.php");

	Recurly_Client::$subdomain = $subdomain;
	Recurly_Client::$apiKey = $apiKey;

	$data = getAllSubscriptions(10);
	if (count($data) > 0) {
	    echo("<b>" . count($data) . " subscriptions found </b><br>");

	    foreach($data as $d) {
		$uuid = $d["uuid"];
		$details = getSubscriptionDetails($uuid);
		$plan_code = $details["plan"];
		$started_at = $details["started_at"]->format("Y-m-d h:m:s");
		$ends_at = $details["ends_at"]->format("Y-m-d h:m:s");
		$amount =  $details["amount_in_cents"]/100;
		$account_code = $details["account_code"];
		$invoice_number = $details["invoice_number"];

		
		echo("<b>" . $account_code . "</b> Invoice:<b>" . $invoice_number . "</b> Plan:" . $plan_code . "  USD:" . number_format($amount,2). " from " . date("Y-m-d", strtotime($started_at)) . " expires " . date("Y-m-d", strtotime($ends_at)) . "<hr>");
		
	    }
	}
	


	/**
	 * Get all subscriptions : max limit is set
	 * @param int $limit max items to return
	 * @return array $data associative array of subscriptions
	 ***/
	function getAllSubscriptions($limit) {
	    $data = array();

	    $counter = 0;
	    $subscriptions = Recurly_SubscriptionList::getActive();
	    foreach($subscriptions as $subscription) {
	    	$single = array();
		$single["uuid"] = $subscription->uuid;
		$single["state"] = $subscription->state;
		$single["activated_at"] = $subscription->activated_at;
		$single["started_at"] = $subscription->current_period_started_at;
		$single["ends_at"] = $subscription->current_period_ends_at;
		$single["account"] = $subscription->account;
		$single["invoice"] = $subscription->invoice;

		$endDate = strtotime($single["ends_at"]->format("Y-m-d h:m:s"));
		$counter ++;
		$data[] = $single;

		if ($counter > $limit)
		    break;

	    }
	    return $data;
	} 



	/**
	 * Get subscription details
	 * @param string $uuid subscription id
	 * @return array $data associative array of subscription fields
	 ***/
	function getSubscriptionDetails($uuid) {
		    $data = array();

		$subscription = Recurly_Subscription::get($uuid);
	    	$single = array();
		$single["uuid"] = $subscription->uuid;
		$single["state"] = $subscription->state;
		$single["activated_at"] = $subscription->activated_at;
		$single["started_at"] = $subscription->current_period_started_at;
		$single["ends_at"] = $subscription->current_period_ends_at;
		$single["plan"] = $subscription->plan->plan_code;
		$single["amount_in_cents"] = $subscription->unit_amount_in_cents;
		$single["account_code"] = $subscription->account->get()->account_code;
		$single["invoice_number"] = $subscription->invoice->get()->invoice_number;

		$data = $single;
		return $data;
	} 

 

Be the first to comment

Leave a Reply

Your email address will not be published.


*