{"id":2095,"date":"2015-01-14T06:29:50","date_gmt":"2015-01-14T06:29:50","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2095"},"modified":"2015-01-14T06:29:50","modified_gmt":"2015-01-14T06:29:50","slug":"get-invoice-and-account-details-from-active-subscriptions-in-recurly","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2015\/01\/14\/get-invoice-and-account-details-from-active-subscriptions-in-recurly\/","title":{"rendered":"Get invoice and account details from active subscriptions in Recurly"},"content":{"rendered":"            <script type=\"text\/javascript\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushPhp.js\"><\/script>\n<p>This article assumes you are using the PHP library for Recurly. This library is available from here : <a title=\"PHP client for recurly\" href=\"https:\/\/github.com\/recurly\/recurly-client-php\" target=\"_blank\">https:\/\/github.com\/recurly\/recurly-client-php<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>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 &#8211; 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.<br \/>\n<pre class=\"brush: php\">\tsession_start();\r\n\terror_reporting(E_ALL ^ E_wARNING ^ E_NOTICE ^ E_DEPRECATED);\r\n\tset_time_limit(600);\r\n\tdate_default_timezone_set(&#039;America\/Los_Angeles&#039;);\r\n\r\n\t\t\/\/ credentials for recurly api\r\n\t$subdomain = &#039;your subdomain&#039;;\r\n\t$apiKey = &#039;your api key&#039;;\r\n\r\n\trequire_once(&quot;lib\/recurly.php&quot;);\r\n\r\n\tRecurly_Client::$subdomain = $subdomain;\r\n\tRecurly_Client::$apiKey = $apiKey;\r\n\r\n\t$data = getAllSubscriptions(10);\r\n\tif (count($data) &gt; 0) {\r\n\t    echo(&quot;&lt;b&gt;&quot; . count($data) . &quot; subscriptions found &lt;\/b&gt;&lt;br&gt;&quot;);\r\n\r\n\t    foreach($data as $d) {\r\n\t\t$uuid = $d[&quot;uuid&quot;];\r\n\t\t$details = getSubscriptionDetails($uuid);\r\n\t\t$plan_code = $details[&quot;plan&quot;];\r\n\t\t$started_at = $details[&quot;started_at&quot;]-&gt;format(&quot;Y-m-d h:m:s&quot;);\r\n\t\t$ends_at = $details[&quot;ends_at&quot;]-&gt;format(&quot;Y-m-d h:m:s&quot;);\r\n\t\t$amount =  $details[&quot;amount_in_cents&quot;]\/100;\r\n\t\t$account_code = $details[&quot;account_code&quot;];\r\n\t\t$invoice_number = $details[&quot;invoice_number&quot;];\r\n\r\n\t\t\r\n\t\techo(&quot;&lt;b&gt;&quot; . $account_code . &quot;&lt;\/b&gt; Invoice:&lt;b&gt;&quot; . $invoice_number . &quot;&lt;\/b&gt; Plan:&quot; . $plan_code . &quot;  USD:&quot; . number_format($amount,2). &quot; from &quot; . date(&quot;Y-m-d&quot;, strtotime($started_at)) . &quot; expires &quot; . date(&quot;Y-m-d&quot;, strtotime($ends_at)) . &quot;&lt;hr&gt;&quot;);\r\n\t\t\r\n\t    }\r\n\t}\r\n\t\r\n\r\n\r\n\t\/**\r\n\t * Get all subscriptions : max limit is set\r\n\t * @param int $limit max items to return\r\n\t * @return array $data associative array of subscriptions\r\n\t ***\/\r\n\tfunction getAllSubscriptions($limit) {\r\n\t    $data = array();\r\n\r\n\t    $counter = 0;\r\n\t    $subscriptions = Recurly_SubscriptionList::getActive();\r\n\t    foreach($subscriptions as $subscription) {\r\n\t    \t$single = array();\r\n\t\t$single[&quot;uuid&quot;] = $subscription-&gt;uuid;\r\n\t\t$single[&quot;state&quot;] = $subscription-&gt;state;\r\n\t\t$single[&quot;activated_at&quot;] = $subscription-&gt;activated_at;\r\n\t\t$single[&quot;started_at&quot;] = $subscription-&gt;current_period_started_at;\r\n\t\t$single[&quot;ends_at&quot;] = $subscription-&gt;current_period_ends_at;\r\n\t\t$single[&quot;account&quot;] = $subscription-&gt;account;\r\n\t\t$single[&quot;invoice&quot;] = $subscription-&gt;invoice;\r\n\r\n\t\t$endDate = strtotime($single[&quot;ends_at&quot;]-&gt;format(&quot;Y-m-d h:m:s&quot;));\r\n\t\t$counter ++;\r\n\t\t$data[] = $single;\r\n\r\n\t\tif ($counter &gt; $limit)\r\n\t\t    break;\r\n\r\n\t    }\r\n\t    return $data;\r\n\t} \r\n\r\n\r\n\r\n\t\/**\r\n\t * Get subscription details\r\n\t * @param string $uuid subscription id\r\n\t * @return array $data associative array of subscription fields\r\n\t ***\/\r\n\tfunction getSubscriptionDetails($uuid) {\r\n\t\t    $data = array();\r\n\r\n\t\t$subscription = Recurly_Subscription::get($uuid);\r\n\t    \t$single = array();\r\n\t\t$single[&quot;uuid&quot;] = $subscription-&gt;uuid;\r\n\t\t$single[&quot;state&quot;] = $subscription-&gt;state;\r\n\t\t$single[&quot;activated_at&quot;] = $subscription-&gt;activated_at;\r\n\t\t$single[&quot;started_at&quot;] = $subscription-&gt;current_period_started_at;\r\n\t\t$single[&quot;ends_at&quot;] = $subscription-&gt;current_period_ends_at;\r\n\t\t$single[&quot;plan&quot;] = $subscription-&gt;plan-&gt;plan_code;\r\n\t\t$single[&quot;amount_in_cents&quot;] = $subscription-&gt;unit_amount_in_cents;\r\n\t\t$single[&quot;account_code&quot;] = $subscription-&gt;account-&gt;get()-&gt;account_code;\r\n\t\t$single[&quot;invoice_number&quot;] = $subscription-&gt;invoice-&gt;get()-&gt;invoice_number;\r\n\r\n\t\t$data = $single;\r\n\t\treturn $data;\r\n\t} \r\n<\/pre><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>This article assumes you are using the PHP library for Recurly. This library is available from here : https:\/\/github.com\/recurly\/recurly-client-php &nbsp; The code snippet shows how <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2015\/01\/14\/get-invoice-and-account-details-from-active-subscriptions-in-recurly\/\" title=\"Get invoice and account details from active subscriptions in Recurly\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2100,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-2095","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apachephp"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2095","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=2095"}],"version-history":[{"count":4,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2095\/revisions"}],"predecessor-version":[{"id":2099,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2095\/revisions\/2099"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2100"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=2095"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2095"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2095"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}