Customer.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace smladeoye\paystack;
  3. use yii\base\Component;
  4. class Customer extends Component
  5. {
  6. /** @var array holds the default customer operation configuration */
  7. private $customer = array(
  8. 'baseUrl'=>'/customer',
  9. 'riskActionUrl'=>'/set_risk_action',
  10. 'beforeSend'=>array(),
  11. 'afterSend'=>array()
  12. );
  13. /*Constructor method to setup paystack component consumer 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->customer = array_replace($this->customer,$paystack->customer);
  22. $this->setConfig($this->customer);
  23. parent::__construct($config);
  24. }
  25. /** create a customer
  26. * @param $options string|array
  27. * @return $this
  28. */
  29. public function create($options = null)
  30. {
  31. if (!empty($options))
  32. {
  33. if (is_array($options))
  34. $this->setRequestOptions($options);
  35. else
  36. $this->setRequestOptions(['email'=>$options]);
  37. }
  38. $this->sendRequest(Paystack::OP_CUST_CREATE,Paystack::METHOD_POST);
  39. $this->setResponseOptions();
  40. return $this;
  41. }
  42. /** fetch all customers
  43. * @param $page string|integer
  44. * @param $per_page string|integer
  45. * @return $this
  46. */
  47. public function fetchAll($page = null,$per_page = null)
  48. {
  49. $options = array();
  50. if (is_array($page))
  51. {
  52. $this->setRequestOptions($page);
  53. }
  54. else
  55. {
  56. if ($page)
  57. $options['page'] = $page;
  58. if ($per_page)
  59. $options['perPage'] = $per_page;
  60. $this->setRequestOptions($options);
  61. }
  62. $this->sendRequest(Paystack::OP_CUST_LIST);
  63. $this->setResponseOptions();
  64. return $this;
  65. }
  66. /** fetch a particular customer
  67. * @param $id string|integer customer id
  68. * @return $this
  69. */
  70. public function fetch($id = null)
  71. {
  72. $this->acceptArray(false);
  73. $this->setRequestOptions($id);
  74. $this->sendRequest(Paystack::OP_CUST_FETCH);
  75. $this->setResponseOptions();
  76. return $this;
  77. }
  78. /** update a particular customer record
  79. * @param $id string|integer customer id or reference
  80. * @param $options array, other parameters
  81. * @return $this
  82. */
  83. public function update($customer_id,$options = null)
  84. {
  85. if (is_array($customer_id) || empty($customer_id))
  86. throw new InvalidArgumentException('Invalid argument supplied for customer id, id must be string');
  87. $options['id'] = $customer_id;
  88. $this->setRequestOptions($options);
  89. $this->sendRequest(Paystack::OP_CUST_UPDATE,Paystack::METHOD_PUT);
  90. $this->setResponseOptions();
  91. return $this;
  92. }
  93. /** whitelist a particular customer
  94. * @param $customer_id string, customer id
  95. * @return $this
  96. */
  97. public function whitelist($customer_id)
  98. {
  99. $options['risk_action'] = Paystack::WHITELIST;
  100. $options['customer'] = $customer_id;
  101. $this->setRequestOptions($options);
  102. $this->sendRequest(Paystack::OP_CUST_WHITELIST,Paystack::METHOD_POST);
  103. $this->setResponseOptions();
  104. return $this;
  105. }
  106. /** blacklist a particular customer
  107. * @param $customer_id string, customer id
  108. * @return $this
  109. */
  110. public function blacklist($customer_id)
  111. {
  112. $options['risk_action'] = Paystack::BLACKLIST;
  113. $options['customer'] = $customer_id;
  114. $this->setRequestOptions($options);
  115. $this->sendRequest(Paystack::OP_CUST_BLACKLIST,Paystack::METHOD_POST);
  116. $this->setResponseOptions();
  117. return $this;
  118. }
  119. }