/application/modules/Payment/Model/Subscription.php

https://github.com/grandison/budo16 · PHP · 339 lines · 226 code · 57 blank · 56 comment · 45 complexity · 15e470ba070be8110e4ee5e8afd335a2 MD5 · raw file

  1. <?php
  2. /**
  3. * SocialEngine
  4. *
  5. * @category Application_Core
  6. * @package Payment
  7. * @copyright Copyright 2006-2010 Webligo Developments
  8. * @license http://www.socialengine.net/license/
  9. * @version $Id: Subscription.php 8906 2011-04-21 00:22:33Z john $
  10. * @author John Boehr <j@webligo.com>
  11. */
  12. /**
  13. * @category Application_Core
  14. * @package Payment
  15. * @copyright Copyright 2006-2010 Webligo Developments
  16. * @license http://www.socialengine.net/license/
  17. */
  18. class Payment_Model_Subscription extends Core_Model_Item_Abstract
  19. {
  20. protected $_searchTriggers = false;
  21. protected $_modifiedTriggers = false;
  22. protected $_user;
  23. protected $_gateway;
  24. protected $_package;
  25. protected $_statusChanged;
  26. public function getUser()
  27. {
  28. if( empty($this->user_id) ) {
  29. return null;
  30. }
  31. if( null === $this->_user ) {
  32. $this->_user = Engine_Api::_()->getItem('user', $this->user_id);
  33. }
  34. return $this->_user;
  35. }
  36. public function getGateway()
  37. {
  38. if( empty($this->gateway_id) ) {
  39. return null;
  40. }
  41. if( null === $this->_gateway ) {
  42. $this->_gateway = Engine_Api::_()->getItem('payment_gateway', $this->gateway_id);
  43. }
  44. return $this->_gateway;
  45. }
  46. public function getPackage()
  47. {
  48. if( empty($this->package_id) ) {
  49. return null;
  50. }
  51. if( null === $this->_package ) {
  52. $this->_package = Engine_Api::_()->getItem('payment_package', $this->package_id);
  53. }
  54. return $this->_package;
  55. }
  56. // Actions
  57. public function upgradeUser()
  58. {
  59. $user = $this->getUser();
  60. $level = $this->getPackage()->getLevel();
  61. if( $user && $level && $user->level_id != $level->level_id ) {
  62. $user->level_id = $level->level_id;
  63. }
  64. $user->enabled = true; // This will get set correctly in the update hook
  65. $user->save();
  66. return $this;
  67. }
  68. public function downgradeUser()
  69. {
  70. $user = $this->getUser();
  71. //$level = $this->getPackage()->getDowngradeLevel();
  72. $level = Engine_Api::_()->getDbtable('levels', 'authorization')->getDefaultLevel();
  73. if( $user && $level && $user->level_id != $level->level_id ) {
  74. $user->level_id = $level->level_id;
  75. }
  76. $user->enabled = true; // This will get set correctly in the update hook
  77. $user->save();
  78. return $this;
  79. }
  80. public function cancel()
  81. {
  82. // Try to cancel recurring payments in the gateway
  83. if( !empty($this->gateway_id) && !empty($this->gateway_profile_id) ) {
  84. try {
  85. $gateway = Engine_Api::_()->getItem('payment_gateway', $this->gateway_id);
  86. $gatewayPlugin = $gateway->getPlugin();
  87. if( method_exists($gatewayPlugin, 'cancelSubscription') ) {
  88. $gatewayPlugin->cancelSubscription($this->gateway_profile_id);
  89. }
  90. } catch( Exception $e ) {
  91. // Silence?
  92. }
  93. }
  94. // Cancel this row
  95. $this->active = false; // Need to do this to prevent clearing the user's session
  96. $this->onCancel();
  97. return $this;
  98. }
  99. // Active
  100. public function setActive($flag = true, $deactivateOthers = null)
  101. {
  102. $this->active = true;
  103. if( (true === $flag && null === $deactivateOthers) ||
  104. $deactivateOthers === true ) {
  105. $table = $this->getTable();
  106. $select = $table->select()
  107. ->where('user_id = ?', $this->user_id)
  108. ->where('active = ?', true)
  109. ;
  110. foreach( $table->fetchAll($select) as $otherSubscription ) {
  111. $otherSubscription->setActive(false);
  112. }
  113. }
  114. $this->save();
  115. return $this;
  116. }
  117. // Events
  118. public function clearStatusChanged()
  119. {
  120. $this->_statusChanged = null;
  121. return $this;
  122. }
  123. public function didStatusChange()
  124. {
  125. return (bool) $this->_statusChanged;
  126. }
  127. public function onPaymentSuccess()
  128. {
  129. $this->_statusChanged = false;
  130. if( in_array($this->status, array('initial', 'trial', 'pending', 'active')) ) {
  131. // If the subscription is in initial or pending, set as active and
  132. // cancel any other active subscriptions
  133. if( in_array($this->status, array('initial', 'pending')) ) {
  134. $this->setActive(true);
  135. Engine_Api::_()->getDbtable('subscriptions', 'payment')
  136. ->cancelAll($this->getUser(), 'User cancelled the subscription.', $this);
  137. }
  138. // Update expiration to expiration + recurrence or to now + recurrence?
  139. $package = $this->getPackage();
  140. $expiration = $package->getExpirationDate();
  141. if( $expiration ) {
  142. $this->expiration_date = date('Y-m-d H:i:s', $expiration);
  143. }
  144. // Change status
  145. if( $this->status != 'active' ) {
  146. $this->status = 'active';
  147. $this->_statusChanged = true;
  148. }
  149. // Update user if active
  150. if( $this->active ) {
  151. $this->upgradeUser();
  152. }
  153. }
  154. $this->save();
  155. // Check if the member should be enabled
  156. $user = $this->getUser();
  157. $user->enabled = true; // This will get set correctly in the update hook
  158. $user->save();
  159. return $this;
  160. }
  161. public function onPaymentPending()
  162. {
  163. $this->_statusChanged = false;
  164. if( in_array($this->status, array('initial', 'trial', 'pending', 'active')) ) {
  165. // Change status
  166. if( $this->status != 'pending' ) {
  167. $this->status = 'pending';
  168. $this->_statusChanged = true;
  169. }
  170. // Downgrade and log out user if active
  171. if( $this->active ) {
  172. // @todo should we do this?
  173. // Downgrade user
  174. $this->downgradeUser();
  175. // Remove active sessions?
  176. //Engine_Api::_()->getDbtable('session', 'core')->removeSessionByAuthId($this->user_id);
  177. }
  178. }
  179. $this->save();
  180. // Check if the member should be enabled
  181. $user = $this->getUser();
  182. $user->enabled = true; // This will get set correctly in the update hook
  183. $user->save();
  184. return $this;
  185. }
  186. public function onPaymentFailure()
  187. {
  188. $this->_statusChanged = false;
  189. if( in_array($this->status, array('initial', 'trial', 'pending', 'active', 'overdue')) ) {
  190. // Change status
  191. if( $this->status != 'overdue' ) {
  192. $this->status = 'overdue';
  193. $this->_statusChanged = true;
  194. }
  195. // Downgrade and log out user if active
  196. if( $this->active ) {
  197. // Downgrade user
  198. $this->downgradeUser();
  199. // Remove active sessions?
  200. Engine_Api::_()->getDbtable('session', 'core')->removeSessionByAuthId($this->user_id);
  201. }
  202. }
  203. $this->save();
  204. // Check if the member should be enabled
  205. $user = $this->getUser();
  206. $user->enabled = true; // This will get set correctly in the update hook
  207. $user->save();
  208. return $this;
  209. }
  210. public function onCancel()
  211. {
  212. $this->_statusChanged = false;
  213. if( in_array($this->status, array('initial', 'trial', 'pending', 'active', 'overdue', 'cancelled')) ) {
  214. // Change status
  215. if( $this->status != 'cancelled' ) {
  216. $this->status = 'cancelled';
  217. $this->_statusChanged = true;
  218. }
  219. // Downgrade and log out user if active
  220. if( $this->active ) {
  221. // Downgrade user
  222. $this->downgradeUser();
  223. // Remove active sessions?
  224. Engine_Api::_()->getDbtable('session', 'core')->removeSessionByAuthId($this->user_id);
  225. }
  226. }
  227. $this->save();
  228. // Check if the member should be enabled
  229. $user = $this->getUser();
  230. $user->enabled = true; // This will get set correctly in the update hook
  231. $user->save();
  232. return $this;
  233. }
  234. public function onExpiration()
  235. {
  236. $this->_statusChanged = false;
  237. if( in_array($this->status, array('initial', 'trial', 'pending', 'active', 'expired')) ) {
  238. // Change status
  239. if( $this->status != 'expired' ) {
  240. $this->status = 'expired';
  241. $this->_statusChanged = true;
  242. }
  243. // Downgrade and log out user if active
  244. if( $this->active ) {
  245. // Downgrade user
  246. $this->downgradeUser();
  247. // Remove active sessions?
  248. Engine_Api::_()->getDbtable('session', 'core')->removeSessionByAuthId($this->user_id);
  249. }
  250. }
  251. $this->save();
  252. // Check if the member should be enabled
  253. $user = $this->getUser();
  254. $user->enabled = true; // This will get set correctly in the update hook
  255. $user->save();
  256. return $this;
  257. }
  258. public function onRefund()
  259. {
  260. $this->_statusChanged = false;
  261. if( in_array($this->status, array('initial', 'trial', 'pending', 'active', 'refunded')) ) {
  262. // Change status
  263. if( $this->status != 'refunded' ) {
  264. $this->status = 'refunded';
  265. $this->_statusChanged = true;
  266. }
  267. // Downgrade and log out user if active
  268. if( $this->active ) {
  269. // Downgrade user
  270. $this->downgradeUser();
  271. // Remove active sessions?
  272. Engine_Api::_()->getDbtable('session', 'core')->removeSessionByAuthId($this->user_id);
  273. }
  274. }
  275. $this->save();
  276. // Check if the member should be enabled
  277. $user = $this->getUser();
  278. $user->enabled = true; // This will get set correctly in the update hook
  279. $user->save();
  280. return $this;
  281. }
  282. }