Transaction.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. namespace smladeoye\paystack;
  3. use yii\base\Component;
  4. use yii\base\Exception;
  5. use Yii;
  6. class Transaction extends Component
  7. {
  8. private $reference;
  9. public $authorization_url;
  10. private $callbackUrl;
  11. private $path;
  12. /** @var array holds the default transaction operation configuration */
  13. private $transaction = array(
  14. 'baseUrl'=>'/transaction',
  15. 'initializeUrl'=>'/initialize',
  16. 'verifyUrl'=>'/verify',
  17. 'chargeUrl'=>'/charge_authorization',
  18. 'timelineUrl'=>'/timeline',
  19. 'totalUrl'=>'/totals',
  20. 'exportUrl'=>'/export',
  21. 'beforeSend'=>array(),
  22. 'afterSend'=>array()
  23. );
  24. /*Constructor method to setup paystack component transaction operation configurations
  25. * @param $paystack, Paystack instance
  26. *@param config, Yii2 default object configuration array
  27. */
  28. public function __construct(Paystack $paystack,$config = [])
  29. {
  30. $this->attachBehavior('Resources',array('class'=> Resources::className()));
  31. $this->setPaystack($paystack);
  32. $this->transaction = array_replace($this->transaction,$paystack->transaction);
  33. $this->setConfig($this->transaction);
  34. parent::__construct($config);
  35. }
  36. /**set the callback url after any transaction is made
  37. * @param $callback*/
  38. public function setCallbackUrl($callback)
  39. {
  40. $this->callbackUrl = $callback;
  41. }
  42. /** @param $options array; initialize a transaction
  43. * @return $this
  44. */
  45. public function initialize($options = null)
  46. {
  47. if ($options)
  48. {
  49. $this->setRequestOptions($options);
  50. }
  51. if (!isset($this->requestOptions['callback_url']) && isset($this->callbackUrl))
  52. {
  53. $this->requestOptions = array_replace($this->requestOptions,array('callback_url'=>$this->callbackUrl));
  54. }
  55. $this->sendRequest(Paystack::OP_TRANS_INITIALIZE,Paystack::METHOD_POST);
  56. $this->setResponseOptions();
  57. return $this;
  58. }
  59. /**verify a particular transaction
  60. * @param $trx_ref transaction id or reference no
  61. * @return $this
  62. */
  63. public function verify($trx_ref = null)
  64. {
  65. $this->acceptArray(false);
  66. if ($trx_ref != null)
  67. $this->setRequestOptions($trx_ref);
  68. else
  69. $this->setRequestOptions($this->reference);
  70. $this->sendRequest(Paystack::OP_TRANS_VERIFY);
  71. $this->setResponseOptions();
  72. return $this;
  73. }
  74. /** fetch all transactions
  75. *@param $page array|integer, when is array it should be in key => value format with the required params
  76. *@param $per_page integer
  77. * @return $this
  78. */
  79. public function fetchAll($page = null, $per_page = null)
  80. {
  81. $options = array();
  82. if (is_array($page))
  83. {
  84. $this->setRequestOptions($page);
  85. }
  86. else
  87. {
  88. if ($page)
  89. $options['page'] = $page;
  90. if ($per_page)
  91. $options['perPage'] = $per_page;
  92. $this->setRequestOptions($options);
  93. }
  94. $this->sendRequest(Paystack::OP_TRANS_LIST);
  95. $this->setResponseOptions();
  96. return $this;
  97. }
  98. /** fetch a particular transaction
  99. * @param $id the transaction id or reference
  100. * @return $this
  101. */
  102. public function fetch($id = null)
  103. {
  104. $this->acceptArray(false);
  105. $this->setRequestOptions($id);
  106. $this->sendRequest(Paystack::OP_TRANS_FETCH);
  107. $this->setResponseOptions();
  108. return $this;
  109. }
  110. /**
  111. * @param $options
  112. * @return $this
  113. */
  114. public function charge($options = null)
  115. {
  116. $this->setRequestOptions($options);
  117. $this->sendRequest(Paystack::OP_TRANS_CHARGE,Paystack::METHOD_POST);
  118. $this->setResponseOptions();
  119. return $this;
  120. }
  121. /**
  122. * @param $id
  123. * $id could be any transaction id or reference
  124. * @return $this
  125. */
  126. public function timeline($id = null)
  127. {
  128. $this->acceptArray(false);
  129. $this->setRequestOptions($id);
  130. $this->sendRequest(Paystack::OP_TRANS_TIMELINE);
  131. $this->setResponseOptions();
  132. return $this;
  133. }
  134. /**
  135. * @param sting/array $from --> lower bound of date range for the transaction query
  136. * @param string (date) $to --> upper bound of date range
  137. * if an array is provided instead, both limit should be specified in the array
  138. * @return $this
  139. */
  140. public function total($from = null, $to = null)
  141. {
  142. $options = array();
  143. if (is_array($from))
  144. {
  145. $this->setRequestOptions($from);
  146. }
  147. else
  148. {
  149. if ($from)
  150. $options['from'] = $from;
  151. if ($to)
  152. $options['to'] = $to;
  153. $this->setRequestOptions($options);
  154. }
  155. $this->sendRequest(Paystack::OP_TRANS_TOTAL);
  156. $this->setResponseOptions();
  157. return $this;
  158. }
  159. /** @param $options, an array of options in key => value format
  160. * @return $this
  161. */
  162. public function export($options = null)
  163. {
  164. $this->setRequestOptions($options);
  165. $this->sendRequest(Paystack::OP_TRANS_EXPORT,Paystack::METHOD_GET);
  166. $this->setResponseOptions();
  167. return $this;
  168. }
  169. /** get the authorization url generated from initializing a transaction
  170. * @return $this
  171. */
  172. public function getAuthorizationUrl()
  173. {
  174. return $this->authorization_url;
  175. }
  176. /**
  177. * @param $refno string generated reference number
  178. * @return $this instance of the class
  179. */
  180. protected function setReference($refno)
  181. {
  182. $this->reference = $refno;
  183. return $this;
  184. }
  185. /**
  186. * @return string reference number which must have been set or generated
  187. */
  188. public function getReference()
  189. {
  190. return $this->reference;
  191. }
  192. /**
  193. * @return string reference number which must have been set or generated
  194. */
  195. protected function setPath($path)
  196. {
  197. $this->path = $path;
  198. }
  199. /** get the export url generated from performing export on transaction */
  200. public function getExportUrl()
  201. {
  202. return $this->path;
  203. }
  204. /** helper method to generate random reference which could be used in initiating a transaction*/
  205. public function generateRef($length = 10)
  206. {
  207. $this->reference = $this->paystack()->generateRef($length);
  208. return $this->reference;
  209. }
  210. /** redirect to the authorization url after successfully gettting authorization */
  211. public function redirect()
  212. {
  213. $url = $this->getAuthorizationUrl();
  214. if (empty($url))
  215. {
  216. throw new Exception('Authorization URL is empty');
  217. }
  218. Yii::$app->response->redirect($this->getAuthorizationUrl());
  219. }
  220. /** download the exported data from the export url gotten from export transaction method */
  221. public function download()
  222. {
  223. if (empty($this->path))
  224. {
  225. throw new Exception('Export link is empty');
  226. }
  227. Yii::$app->response->redirect($this->path);
  228. }
  229. }