Plan.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace smladeoye\paystack;
  3. use yii\base\Component;
  4. class Plan extends Component
  5. {
  6. /** @var array holds the default plan operation configuration */
  7. private $plan = array(
  8. 'baseUrl'=>'/plan',
  9. 'beforeSend'=>array(),
  10. 'afterSend'=>array()
  11. );
  12. /*Constructor method to setup paystack component, plan operation configurations
  13. * @param $paystack, Paystack instance
  14. *@param config, Yii2 default object configuration array
  15. */
  16. public function __construct(Paystack $paystack, $config = [])
  17. {
  18. $this->attachBehavior('Resources',array('class'=> Resources::className()));
  19. $this->setPaystack($paystack);
  20. $this->plan = array_replace($this->plan,$paystack->plan);
  21. $this->setConfig($this->plan);
  22. parent::__construct($config);
  23. }
  24. /** create a plan
  25. * @param $options array
  26. * @return $this
  27. */
  28. public function create($options = null)
  29. {
  30. $this->setRequestOptions($options);
  31. $this->sendRequest(Paystack::OP_PLAN_CREATE,Paystack::METHOD_POST);
  32. $this->setResponseOptions();
  33. return $this;
  34. }
  35. /** fetch all plans
  36. * @param $page integer|array
  37. * @param $per_page string|integer
  38. * @return $this
  39. */
  40. public function fetchAll($page = null,$per_page = null)
  41. {
  42. $options = array();
  43. if (is_array($page))
  44. {
  45. $this->setRequestOptions($page);
  46. }
  47. else
  48. {
  49. if ($page)
  50. $options['page'] = $page;
  51. if ($per_page)
  52. $options['perPage'] = $per_page;
  53. $this->setRequestOptions($options);
  54. }
  55. $this->sendRequest(Paystack::OP_PLAN_LIST);
  56. $this->setResponseOptions();
  57. return $this;
  58. }
  59. /** fetch a particular plan
  60. * @param $id string|integer plan id or slug
  61. * @return $this
  62. */
  63. public function fetch($id = null)
  64. {
  65. $this->acceptArray(false);
  66. $this->setRequestOptions($id);
  67. $this->sendRequest(Paystack::OP_PLAN_FETCH);
  68. $this->setResponseOptions();
  69. return $this;
  70. }
  71. /** update a particular plan info
  72. * @param $id string|integer page id or slug
  73. * @param $options array, other parameters
  74. * @throws InvalidArgumentException when account_id is not provided
  75. * @return $this
  76. */
  77. public function update($account_id,$options = null)
  78. {
  79. if (is_array($account_id) || empty($account_id))
  80. throw new InvalidArgumentException('Invalid argument supplied for plan id, id must be an integer or sting');
  81. $options['id'] = $account_id;
  82. $this->setRequestOptions($options);
  83. $this->sendRequest(Paystack::OP_PLAN_UPDATE,Paystack::METHOD_PUT);
  84. $this->setResponseOptions();
  85. return $this;
  86. }
  87. }