/packages/chargify/ChargifySubscription.php

https://github.com/zetamedia/Marketing-Site-Laravel- · PHP · 187 lines · 154 code · 22 blank · 11 comment · 49 complexity · 688eed16a63c77118ecf2c43a02f0643 MD5 · raw file

  1. <?php
  2. //Reference Documentation: http://support.chargify.com/faqs/api/api-subscriptions
  3. class ChargifySubscription
  4. {
  5. //******************************
  6. //**** INPUT ONLY VARIABLES ****
  7. //******************************
  8. var $customer_id;
  9. var $customer_reference;
  10. var $credit_card_attributes;
  11. var $customer_attributes;
  12. var $product_handle;
  13. var $product_id;
  14. var $coupon_code;
  15. var $next_billing_at;
  16. //******************************
  17. //** INPUT & OUTPUT VARIABLES **
  18. //******************************
  19. var $customer;
  20. var $credit_card;
  21. var $cancellation_message;
  22. var $components;
  23. //******************************
  24. //*** OUTPUT ONLY VARIABLES ****
  25. //******************************
  26. var $product;
  27. var $id;
  28. var $state;
  29. var $balance_in_cents;
  30. var $current_period_started_at;
  31. var $current_period_ends_at;
  32. var $trial_started_at;
  33. var $trial_ended_at;
  34. var $activated_at;
  35. var $expires_at;
  36. var $created_at;
  37. var $updated_at;
  38. private $connector;
  39. public function __construct(SimpleXMLElement $subscription_xml_node = null, $test_mode = false)
  40. {
  41. $this->connector = new ChargifyConnector($test_mode);
  42. if ($subscription_xml_node) {
  43. //Load object dynamically and convert SimpleXMLElements into strings
  44. foreach($subscription_xml_node as $key => $element)
  45. {
  46. if($key == 'customer' || $key == 'customer_attributes') {
  47. $this->customer = new ChargifyCustomer($element);
  48. } else if($key == 'product') {
  49. $this->product = new ChargifyProduct($element);
  50. } else if($key == 'credit_card' || $key == 'credit_card_attributes') {
  51. $this->credit_card = new ChargifyCreditCard($element);
  52. } elseif ($key == 'components') {
  53. $this->components = array();
  54. foreach ($element as $component) {
  55. $this->components[] = new ChargifyQuantityBasedComponent($component);
  56. }
  57. } else {
  58. $this->$key = (string)$element;
  59. }
  60. }
  61. }
  62. }
  63. protected function format_timestamp($format, $timestamp)
  64. {
  65. $temp = explode('T', $timestamp);
  66. $temp = strtotime($temp[0]);
  67. return date($format, $temp);
  68. }
  69. public function getCurrentPeriodStart($date_format = NULL)
  70. {
  71. if($date_format == NULL) {
  72. return $this->current_period_started_at;
  73. } else {
  74. return $this->format_timestamp($date_format, $this->current_period_started_at);
  75. }
  76. }
  77. public function getCreatedAt($date_format = NULL)
  78. {
  79. if($date_format == NULL) {
  80. return $this->created_at;
  81. } else {
  82. return $this->format_timestamp($date_format, $this->created_at);
  83. }
  84. }
  85. public function getXML() {
  86. $xml = $this->getXMLObject();
  87. return $xml->asXML();
  88. }
  89. public function getXMLObject(&$xml = null) {
  90. if ($xml === null) {
  91. $xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><subscription></subscription>");
  92. }
  93. foreach (get_object_vars($this) as $key=>$val) {
  94. if($key == 'customer' || $key == 'product' || $key == 'credit_card' || $key == 'credit_card_attributes' || $key == 'customer_attributes') {
  95. if ($val) {
  96. $node = $xml->addChild($key);
  97. $val->getXMLObject($node);
  98. }
  99. } elseif ($key == 'components') {
  100. if (is_array($this->components)) {
  101. $node = $xml->addChild($key);
  102. foreach ($this->components as $component) {
  103. $component->getXMLObject($node);
  104. }
  105. }
  106. } elseif ($key != 'connector') {
  107. if ($val) {
  108. $xml->addChild($key,htmlentities($val, ENT_QUOTES));
  109. }
  110. }
  111. }
  112. return $xml;
  113. }
  114. public function getJSON() {
  115. return sprintf('{"subscription":%s}',json_encode($this->getXMLObject()));
  116. }
  117. public function create() {
  118. return $this->connector->createSubscription($this);
  119. }
  120. public function getAll($page = 1, $per_page = 2000) {
  121. return $this->connector->getSubscriptions($page, $per_page);
  122. }
  123. public function getByCustomerID($customer_id = null) {
  124. if ($customer_id == null) {
  125. $customer_id = $this->customer_id;
  126. }
  127. return $this->connector->getSubscriptionsByCustomerID($customer_id);
  128. }
  129. public function getByID($subscription_id = null) {
  130. if ($subscription_id == null) {
  131. $subscription_id = $this->id;
  132. }
  133. return $this->connector->getSubscriptionsByID($subscription_id);
  134. }
  135. public function updateProduct($chargify_product = null) {
  136. if ($chargify_product == null) {
  137. $chargify_product = $this->product;
  138. }
  139. return $this->connector->updateSubscriptionProduct($this->id, $chargify_product);
  140. }
  141. public function updateProductProrated($chargify_product = null) {
  142. if ($chargify_product == null) {
  143. $chargify_product = $this->product;
  144. }
  145. return $this->connector->updateSubscriptionProductProrated($this->id, $chargify_product);
  146. }
  147. public function updateCreditCard($credit_card_attributes = null) {
  148. if ($credit_card_attributes == null) {
  149. $credit_card_attributes = $this->credit_card_attributes;
  150. }
  151. return $this->connector->updateSubscriptionCreditCard($this->id, $credit_card_attributes);
  152. }
  153. public function cancel($cancellation_message = null) {
  154. if ($cancellation_message == null) {
  155. $cancellation_message = $this->cancellation_message;
  156. }
  157. return $this->connector->cancelSubscription($this->id, $cancellation_message);
  158. }
  159. public function reactivate() {
  160. return $this->connector->reactivateSubscription($this->id);
  161. }
  162. public function resetBalance() {
  163. return $this->connector->resetSubscriptionBalance($this->id);
  164. }
  165. }?>