back to home page

Chargify API PHP Class For Statements

Posted on July 29th, 2011

This was made because the original Chargify PHP Client did not have a class for the Statements, so I added it to the library myself, and now I am sharing the code for others who may need this functionality. The original code can be found at

https://github.com/jforrest/Chargify-PHP-Client

The only changes made were:

  • ChargifyConnector.php
  • ChargifyStatement.php (added)
  • It’s pretty simple and below I will show you how I use the class in code. You can also go ahead and download the class below.

    Here’s the additional class:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    <?php
    /* This is an additional class for the Chargify Statements API
    /  by: Ronald A. Richardson
    /  author: www.ronaldarichardson.com  */

    //Reference Documentation: http://docs.chargify.com/api-statements

    class ChargifyStatement extends ChargifyBase
    {
       //******************************
       //*** READ ONLY VARIABLES ****
       //******************************
       var $id;
       var $subscription_id;
       var $opened_at;
       var $closed_at;
       var $settled_at;
       var $text_view;
       var $basic_html_view;
       var $html_view;
       var $future_payments;
       var $starting_balance_in_cents;
       var $ending_balance_in_cents;
       var $customer_first_name;
       var $customer_last_name;
       var $customer_organization;
       var $customer_shipping_address;
       var $customer_shipping_address_2;
       var $customer_shipping_city;
       var $customer_shipping_state;
       var $customer_shipping_country;
       var $customer_shipping_zip;
       var $customer_billing_address;
       var $customer_billing_address_2;
       var $customer_billing_city;
       var $customer_billing_state;
       var $customer_billing_country;
       var $customer_billing_zip;
       var $transactions;
       var $events;
       var $created_at;
       var $updated_at;

       private $connector;
       public function __construct(SimpleXMLElement $product_xml_node = null, $test_mode = false)
       {
          $this->connector = new ChargifyConnector($test_mode);
          if ($product_xml_node) {
           //Load object dynamically and convert SimpleXMLElements into strings
           foreach($product_xml_node as $key => $element) {
                $this->$key = (string)$element;
           }
          }
       }

       protected function getName() {
          return "statement";
       }
       
       public function getAll($options = array()) {
          return $this->connector->getAllStatements($options);
       }
       
       public function getByStatementID($subscription_id, $options = array()) {
          return $this->connector->getStatementsBySubscriptionID($subscription_id, $options);
       }
       
    }

    Here’s how we can use this class below.
    So I created a function called getClientStatements(); Here is what it looks like below:

    1
    2
    3
    4
    5
    6
    7
    function getClientStatements($id)
    {
       require_once('lib/Chargify.php');
       $statement = new ChargifyStatement();
       $statement = $statement->getByStatementID($id);
       return $statement;
    }

    Basically this function will return the Statement Objects in an array for the clients subscription id.

    Once you have that function wherever, likely in a class such as client.class.php
    You can use the code to get the statements. Below I will show an example, let’s say we have a client with an ID of 7700 we can use our newly created function to get an array of all of his statements like so:

    1
    2
    3
    4
    5
    6
    // This is example code
    $client = new Client();
    $statements = array_reverse($client->getClientStatements("7700")); // I did the array reverse so that the statements will come out in order from the latest.
    for($i=0;$i<count($statements);$i++){
       echo $statements[$i]->id;
    }

    The code below will display a list of statement ID’s, if you want you can get html-view as well. Just check out the class and see everything you can display for statements. I am currently working on a function that will generate them into a PDF file.

    Download: ChargifyStatement


    Leave a Reply

      To syntax highlight code just use [cc lang="whatever language here"]your code here[/cc]