Subscription.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace smladeoye\paystack;
  3. use yii\base\Component;
  4. class Subscription extends Component
  5. {
  6. /** @var array holds the default subscription operation configuration */
  7. private $subscription = array(
  8. 'baseUrl'=>'/subscription',
  9. 'disableUrl'=>'/disable',
  10. 'enableUrl'=>'/enable',
  11. 'beforeSend'=>array(),
  12. 'afterSend'=>array()
  13. );
  14. /*Constructor method to setup paystack component, subscription operation configurations
  15. * @param $paystack, Paystack instance
  16. *@param config, Yii2 default object configuration array
  17. */
  18. public function __construct(Paystack $paystack, $config = [])
  19. {
  20. $this->attachBehavior('Resources',array('class'=> Resources::className()));
  21. $this->setPaystack($paystack);
  22. $this->subscription = array_replace($this->subscription,$paystack->subscription);
  23. $this->setConfig($this->subscription);
  24. parent::__construct($config);
  25. }
  26. /** create a subscription
  27. * @param $options array
  28. * @return $this
  29. */
  30. public function create($options = null)
  31. {
  32. $this->setRequestOptions($options);
  33. $this->sendRequest(Paystack::OP_SUBSCRIPTION_CREATE,Paystack::METHOD_POST);
  34. $this->setResponseOptions();
  35. return $this;
  36. }
  37. /** fetch all subscription
  38. * @param $page integer|array
  39. * @param $per_page string|integer
  40. * @return $this
  41. */
  42. public function fetchAll($page = null,$per_page = null)
  43. {
  44. $options = array();
  45. if (is_array($page))
  46. {
  47. $this->setRequestOptions($page);
  48. }
  49. else
  50. {
  51. if ($page)
  52. $options['page'] = $page;
  53. if ($per_page)
  54. $options['perPage'] = $per_page;
  55. $this->setRequestOptions($options);
  56. }
  57. $this->sendRequest(Paystack::OP_SUBSCRIPTION_LIST);
  58. $this->setResponseOptions();
  59. return $this;
  60. }
  61. /** fetch a particular subscription
  62. * @param $id string subscription code or id
  63. * @return $this
  64. */
  65. public function fetch($id = null)
  66. {
  67. $this->acceptArray(false);
  68. $this->setRequestOptions($id);
  69. $this->sendRequest(Paystack::OP_SUBSCRIPTION_FETCH);
  70. $this->setResponseOptions();
  71. return $this;
  72. }
  73. /** disable a customer subscription to a plan
  74. * @param $code string|array subscription plan code
  75. * @param $token string, the customer token
  76. * @return $this
  77. */
  78. public function disable($code = null, $token = null)
  79. {
  80. $options = array();
  81. if (is_array($code))
  82. {
  83. $this->setRequestOptions($code);
  84. }
  85. else
  86. {
  87. if ($code)
  88. $options['code'] = $code;
  89. if ($token)
  90. $options['token'] = $token;
  91. $this->setRequestOptions($options);
  92. }
  93. $this->sendRequest(Paystack::OP_SUBSCRIPTION_DISABLE, Paystack::METHOD_POST);
  94. $this->setResponseOptions();
  95. return $this;
  96. }
  97. /** enable a customer subscription to a plan
  98. * @param $code string|array subscription plan code
  99. * @param $token string, the customer token
  100. * @return $this
  101. */
  102. public function enable($code = null, $token = null)
  103. {
  104. $options = array();
  105. if (is_array($code))
  106. {
  107. $this->setRequestOptions($code);
  108. }
  109. else
  110. {
  111. if ($code)
  112. $options['code'] = $code;
  113. if ($token)
  114. $options['token'] = $token;
  115. $this->setRequestOptions($options);
  116. }
  117. $this->sendRequest(Paystack::OP_SUBSCRIPTION_ENABLE, Paystack::METHOD_POST);
  118. $this->setResponseOptions();
  119. return $this;
  120. }
  121. }