SubAccount.php 3.6 KB

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