Resources.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. namespace smladeoye\paystack;
  3. use yii\base\Behavior;
  4. class Resources extends Behavior
  5. {
  6. CONST EVENT_AFTER_SEND = 'afterSend';
  7. CONST EVENT_BEFORE_SEND = 'beforeSend';
  8. public $hasError = false;
  9. private $error;
  10. private $errors;
  11. private $operationUrl;
  12. private $accept_array = true;
  13. private $response;
  14. private $status = true;
  15. private $message;
  16. private $data;
  17. private $meta;
  18. protected $requestOptions = array();
  19. private $_paystack;
  20. private $_config;
  21. private $_beforeSend;
  22. private $_afterSend;
  23. public function setPaystack(Paystack $paystack)
  24. {
  25. $this->_paystack = $paystack;
  26. }
  27. public function paystack()
  28. {
  29. return $this->_paystack;
  30. }
  31. public function getConfig()
  32. {
  33. return $this->_config;
  34. }
  35. private function verifyPeer()
  36. {
  37. return !($this->paystack()->verifyPeer === false);
  38. }
  39. public function onBeforeSend()
  40. {
  41. if (!empty($this->_beforeSend))
  42. {
  43. $this->owner->on('beforeSend',$this->_beforeSend);
  44. }
  45. }
  46. public function afterSend()
  47. {
  48. $this->owner->trigger(self::EVENT_AFTER_SEND);
  49. }
  50. public function beforeSend()
  51. {
  52. $this->owner->trigger(self::EVENT_BEFORE_SEND);
  53. }
  54. public function onAfterSend()
  55. {
  56. if (!empty($this->_afterSend))
  57. {
  58. $this->owner->on('afterSend', $this->_afterSend);
  59. }
  60. }
  61. public function setConfig($config)
  62. {
  63. $this->_config = $config;
  64. $this->_beforeSend = $config['beforeSend']?:$this->paystack()->beforeSend;
  65. $this->_afterSend = $config['afterSend']?:$this->paystack()->afterSend;
  66. $this->onBeforeSend();
  67. $this->onAfterSend();
  68. }
  69. public function setResponse($response)
  70. {
  71. $this->response = $response;
  72. foreach ($response as $key => $value)
  73. {
  74. if (property_exists($this,$key))
  75. {
  76. $this->$key = $value;
  77. }
  78. }
  79. if (!$this->status || isset($this->error) || isset($this->errors))
  80. {
  81. $this->hasError = true;
  82. }
  83. }
  84. public function acceptArray($value)
  85. {
  86. if (!is_bool($value))
  87. throw new \InvalidArgumentException('Value must be boolean');
  88. $this->accept_array = $value;
  89. }
  90. public function canAcceptArray()
  91. {
  92. return $this->accept_array;
  93. }
  94. public function getError()
  95. {
  96. return isset($this->error)?$this->error:$this->errors;
  97. }
  98. public function getOperationUrl()
  99. {
  100. return $this->operationUrl;
  101. }
  102. public function setRequestOptions($options = null)
  103. {
  104. if (!empty($options))
  105. {
  106. if (!$this->accept_array && is_array($options))
  107. {
  108. throw new InvalidArgumentException('Array provided, expecting string or integer');
  109. }
  110. if (is_array($options))
  111. $this->requestOptions = $options + $this->requestOptions;
  112. else
  113. $this->requestOptions = $options;
  114. }
  115. return $this->owner;
  116. }
  117. public function getRequestOptions()
  118. {
  119. if (!$this->accept_array && is_array($this->requestOptions))
  120. {
  121. throw new InvalidArgumentException('Array provided, expecting string or integer');
  122. }
  123. return $this->requestOptions;
  124. }
  125. public function getResponse()
  126. {
  127. return $this->response;
  128. }
  129. public function getStatus()
  130. {
  131. return $this->status;
  132. }
  133. public function getMessage()
  134. {
  135. return $this->message;
  136. }
  137. public function getMeta()
  138. {
  139. return $this->meta;
  140. }
  141. public function getData()
  142. {
  143. return $this->data;
  144. }
  145. public function sendRequest($operation,$method = Paystack::METHOD_GET)
  146. {
  147. $ch = curl_init();
  148. curl_setopt_array($ch, array(
  149. CURLOPT_RETURNTRANSFER => 1,
  150. CURLOPT_SSL_VERIFYPEER => $this->verifyPeer(),
  151. CURLOPT_URL => $this->setOperationUrl($operation),
  152. CURLOPT_HTTPHEADER => $this->paystack()->getHeader(),
  153. )
  154. );
  155. $this->beforeSend();
  156. if ($method== Paystack::METHOD_GET)
  157. {
  158. curl_setopt($ch,CURLOPT_POST, false );
  159. }
  160. elseif ($method == Paystack::METHOD_POST)
  161. {
  162. curl_setopt($ch, CURLOPT_POST, 1);
  163. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->getRequestOptions() );
  164. }
  165. elseif ($method == Paystack::METHOD_PUT)
  166. {
  167. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, Paystack::METHOD_PUT);
  168. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->getRequestOptions());
  169. }
  170. elseif ($method == Paystack::METHOD_DELETE)
  171. {
  172. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, Paystack::METHOD_DELETE);
  173. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->getRequestOptions());
  174. }
  175. $response = json_decode(curl_exec($ch),true);
  176. if (curl_error($ch))
  177. {
  178. $this->hasError = true;
  179. $this->error = curl_error($ch);
  180. }
  181. else
  182. {
  183. $this->setResponse($response);
  184. }
  185. $this->afterSend();
  186. curl_close($ch);
  187. return $this->response;
  188. }
  189. private function setOperationUrl($operation)
  190. {
  191. switch ($operation)
  192. {
  193. case Paystack::OP_TRANS_INITIALIZE:
  194. $opUrl = $this->getConfig()['baseUrl'].$this->getConfig()['initializeUrl'];
  195. break;
  196. case Paystack::OP_TRANS_VERIFY:
  197. $opUrl = $this->getConfig()['baseUrl'].$this->getConfig()['verifyUrl'].'/'.$this->getRequestOptions();
  198. break;
  199. case Paystack::OP_TRANS_FETCH:
  200. $opUrl = $this->getConfig()['baseUrl'].'/'.$this->getRequestOptions();
  201. break;
  202. case Paystack::OP_TRANS_CHARGE:
  203. $opUrl = $this->getConfig()['baseUrl'].$this->getConfig()['chargeUrl'];
  204. break;
  205. case Paystack::OP_TRANS_TIMELINE:
  206. $opUrl = $this->getConfig()['baseUrl'].$this->getConfig()['timelineUrl'].'/'.$this->getRequestOptions();
  207. break;
  208. case Paystack::OP_TRANS_TOTAL:
  209. $opUrl = $this->getConfig()['baseUrl'].$this->getConfig()['totalUrl'];
  210. break;
  211. case Paystack::OP_TRANS_EXPORT:
  212. $opUrl = $this->getConfig()['baseUrl'].$this->getConfig()['exportUrl'];
  213. break;
  214. case Paystack::OP_CUST_FETCH:
  215. $opUrl = $this->getConfig()['baseUrl'].'/'.$this->getRequestOptions();
  216. break;
  217. case Paystack::OP_CUST_UPDATE:
  218. $opUrl = $this->getConfig()['baseUrl'].'/'.$this->getRequestOptions()['id'];
  219. break;
  220. case Paystack::OP_CUST_WHITELIST:
  221. $opUrl = $this->getConfig()['baseUrl'].$this->getConfig()['riskActionUrl'];
  222. break;
  223. case Paystack::OP_CUST_BLACKLIST:
  224. $opUrl = $this->getConfig()['baseUrl'].$this->getConfig()['riskActionUrl'];
  225. break;
  226. case Paystack::OP_SUBACCOUNT_FETCH:
  227. $opUrl = $this->getConfig()['baseUrl'].'/'.$this->getRequestOptions();
  228. break;
  229. case Paystack::OP_SUBACCOUNT_UPDATE:
  230. $opUrl = $this->getConfig()['baseUrl'].'/'.$this->getRequestOptions()['id'];
  231. break;
  232. case Paystack::OP_SUBACCOUNT_BANKS:
  233. $opUrl = $this->getConfig()['listBank'];
  234. break;
  235. case Paystack::OP_PLAN_FETCH:
  236. $opUrl = $this->getConfig()['baseUrl'].'/'.$this->getRequestOptions();
  237. break;
  238. case Paystack::OP_PLAN_UPDATE:
  239. $opUrl = $this->getConfig()['baseUrl'].'/'.$this->getRequestOptions()['id'];
  240. break;
  241. case Paystack::OP_SUBSCRIPTION_FETCH:
  242. $opUrl = $this->getConfig()['baseUrl'].'/'.$this->getRequestOptions();
  243. break;
  244. case Paystack::OP_SUBSCRIPTION_ENABLE:
  245. $opUrl = $this->getConfig()['baseUrl'].$this->getConfig()['enableUrl'];
  246. break;
  247. case Paystack::OP_SUBSCRIPTION_DISABLE:
  248. $opUrl = $this->getConfig()['baseUrl'].$this->getConfig()['disableUrl'];
  249. break;
  250. case Paystack::OP_PAGE_FETCH:
  251. $opUrl = $this->getConfig()['baseUrl'].'/'.$this->getRequestOptions();
  252. break;
  253. case Paystack::OP_PAGE_UPDATE:
  254. $opUrl = $this->getConfig()['baseUrl'].'/'.$this->getRequestOptions()['id'];
  255. break;
  256. case Paystack::OP_PAGE_AVAILABILITY:
  257. $opUrl = $this->getConfig()['baseUrl'].$this->getConfig()['slugAvailabilityUrl'].'/'.$this->getRequestOptions();
  258. break;
  259. default:
  260. $opUrl = $this->getConfig()['baseUrl'];
  261. break;
  262. }
  263. $this->operationUrl = $this->paystack()->apiUrl.$opUrl;
  264. return $this->operationUrl;
  265. }
  266. public function setResponseOptions()
  267. {
  268. if (!empty($this->data))
  269. {
  270. foreach ($this->data as $key => $data)
  271. {
  272. if (property_exists($this->owner,$key))
  273. {
  274. $this->owner->$key = $data;
  275. }
  276. }
  277. }
  278. }
  279. }