Base.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. *
  4. * Description
  5. *
  6. * @package Paystack
  7. * @category Source
  8. * @author Michael Akanji <matscode@gmail.com>
  9. * @date 2017-06-26
  10. * @copyright (c) 2016 - 2017, TECRUM (http://www.tecrum.com)
  11. *
  12. */
  13. namespace Matscode\Paystack;
  14. use Matscode\Paystack\CURL;
  15. use Matscode\Paystack\Utility\Text;
  16. class Base
  17. {
  18. private
  19. $_apiBaseUrl = 'https://api.paystack.co/', // with trailing slash
  20. $_curl,
  21. $_secretKey,
  22. $_endPoint,
  23. /*Getting Error Infomation*/
  24. $_errorMessages = [];
  25. public
  26. $resource,
  27. $action,
  28. $args,
  29. $data,
  30. // response from the endpoint
  31. $response;
  32. public function __construct( $secretKey )
  33. {
  34. // save key in memory
  35. $this->_secretKey = $secretKey;
  36. return $this;
  37. }
  38. public function setResource( $resource )
  39. {
  40. $this->resource = $resource;
  41. return $this;
  42. }
  43. public function setAction( $action, array $args = [] )
  44. {
  45. if ( ! is_array( $args ) ) {
  46. throw new \Exception( 'Action arguments can only be of datatype Array' );
  47. }
  48. $this->action = $action;
  49. $this->args = $args;
  50. return $this;
  51. }
  52. /**
  53. * Initiate Request to the paystack RESTful API and return response Obj
  54. *
  55. * @param array $withData
  56. * @param string $requestMethod
  57. * @param bool $returnArray set to true to return response as associate array
  58. *
  59. * @todo Utilize the third argument..
  60. *
  61. * @return mixed
  62. * @throws \Exception
  63. */
  64. public function sendRequest( array $withData = [], $requestMethod = 'POST', $returnArray = false )
  65. {
  66. if ( ! is_array( $withData ) ) {
  67. throw new \Exception( 'sendRequest arguments can only be of datatype Array' );
  68. }
  69. $this->data = $withData;
  70. $this->_endPoint = $this->_apiBaseUrl .
  71. Text::removeSlashes( $this->resource ) . '/' .
  72. Text::removeSlashes( $this->action );
  73. // append parameters to endPoint
  74. if ( count( $this->args ) > 0 ) {
  75. $this->_endPoint .= '/' . implode( '/', $this->args );
  76. }
  77. // send the request and return result as json object
  78. $this->_curl =
  79. ( new CURL(
  80. $this->_endPoint,
  81. $requestMethod ) )
  82. ->setRequestHeader( 'Authorization', 'Bearer ' . $this->_secretKey );
  83. $this->response =
  84. json_decode(
  85. $this->_curl
  86. ->run( $this->data, 'json' ) );
  87. return $this->response;
  88. }
  89. /**
  90. * @return mixed
  91. */
  92. public function getEndPoint()
  93. {
  94. // this works only after executing sendRequest
  95. return $this->_endPoint;
  96. }
  97. /**
  98. * @param mixed $errorMessages
  99. */
  100. public function setErrorMessages( $errorMessages )
  101. {
  102. //if errorMessages is string
  103. if ( is_string( $errorMessages ) ) {
  104. $this->_errorMessages[] = $errorMessages;
  105. }
  106. //if errorMessages is array
  107. if ( is_array( $errorMessages ) ) {
  108. $this->_errorMessages = array_merge( $this->_errorMessages, $errorMessages );
  109. }
  110. }
  111. /**
  112. * @param bool $toString
  113. * @param string $delimiter
  114. *
  115. * @return array|string
  116. */
  117. public function getErrorMessages( $toString = false, $delimiter = '<br>' )
  118. {
  119. $errorMessages = $this->_errorMessages;
  120. if ( $toString ) {
  121. // return errorMessage as String
  122. unset( $errorMessages ); //to avoid datatype conflict
  123. $errorMessages = join( $delimiter, $this->_errorMessages );
  124. }
  125. return $errorMessages;
  126. }
  127. }