Transaction.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /**
  3. *
  4. * Class for Transaction Logic
  5. *
  6. * @package Paystack
  7. * @category Source
  8. * @author Michael Akanji <matscode@gmail.com>
  9. * @date 2017-06-25
  10. * @copyright (c) 2016 - 2017, TECRUM (http://www.tecrum.com)
  11. *
  12. */
  13. namespace Matscode\Paystack;
  14. use Matscode\Paystack\Utility\Text;
  15. class Transaction extends Base
  16. {
  17. public
  18. $amount = 0,
  19. $email = null,
  20. $reference = null,
  21. $transactionResponse =
  22. [
  23. 'verify' => null,
  24. 'initialize' => null
  25. ];
  26. private
  27. $_callbackUrl = null;
  28. public function __construct( $secretKey = null )
  29. {
  30. if ( ! is_null( $secretKey ) ) {
  31. parent::__construct( $secretKey );
  32. }
  33. // set the default resource for current class
  34. $this
  35. ->setResource( 'transaction' );
  36. return $this;
  37. }
  38. /**
  39. * This method must be called to request for payment. which return an initial transaction obj
  40. *
  41. * @param array $data
  42. * @param bool $rawResponse
  43. *
  44. * @return mixed|\stdClass
  45. */
  46. public function initialize( array $data = [], $rawResponse = false )
  47. {
  48. // values set via mutator
  49. $data['callback_url'] = $this->_callbackUrl;
  50. // override refernce $data['reference'] value
  51. if ( $this->reference ) {
  52. $data['reference'] = $this->reference;
  53. } elseif ( ! isset( $data['reference'] ) ) {
  54. $this->reference = $data['reference'] = Text::uniqueRef();
  55. } else {
  56. $this->reference = $data['reference'];
  57. }
  58. // override amount $data['amount'] value
  59. if ( $this->amount ) {
  60. $data['amount'] = $this->amount;
  61. } else {
  62. // save amount in memory
  63. $this->amount = $data['amount'];
  64. }
  65. // override email $data['email'] value
  66. if ( ! is_null( $this->email ) ) {
  67. $data['email'] = $this->email;
  68. } else {
  69. // save amount in memory
  70. $this->email = $data['email'];
  71. }
  72. $this->transactionResponse['initialize'] =
  73. $this
  74. ->setAction( 'initialize' )
  75. ->sendRequest( $data );
  76. if ( $rawResponse ) {
  77. $response =
  78. $this->transactionResponse['initialize'];
  79. } else {
  80. // Initialize a new Obj to save Striped response
  81. $response = new \stdClass();
  82. if ( isset( $this->transactionResponse['initialize']->data ) &&
  83. is_object( $this->transactionResponse['initialize']->data )
  84. ) {
  85. $response->authorizationUrl = $this->transactionResponse['initialize']->data->authorization_url;
  86. $response->reference = $this->transactionResponse['initialize']->data->reference;
  87. } else {
  88. // return the raw response
  89. $response =
  90. $this->transactionResponse['initialize'];
  91. }
  92. }
  93. return $response;
  94. }
  95. /**
  96. * Is used to Check if a transaction is successful and return the transaction object datd
  97. *
  98. * @param null $reference
  99. *
  100. * @todo Use session to keep reference temporary per transaction To enhance Transaction reference guessing.
  101. *
  102. * @return mixed
  103. * @throws \Exception
  104. */
  105. public function verify( $reference = null )
  106. {
  107. // try to guess reference if not set
  108. if ( is_null( $reference ) ) {
  109. // guess reference
  110. if ( isset( $_GET['reference'] ) ) {
  111. $reference = $_GET['reference'];
  112. } else {
  113. // return false
  114. return false;
  115. }
  116. }
  117. $this->transactionResponse['verify'] =
  118. $this
  119. ->setAction( 'verify', [ $reference ] )
  120. ->sendRequest( [], 'GET' );
  121. return $this->transactionResponse['verify'];
  122. }
  123. /**
  124. * Like verify(), but it only checks to see if a transactions is successful returning boolean
  125. *
  126. * @param null $reference
  127. *
  128. * @return bool
  129. */
  130. public function isSuccessful( $reference = null )
  131. {
  132. // get verify response
  133. $response = $this->verify( $reference );
  134. // Initialize as !isSuccessful
  135. $isSuccessful = false;
  136. // check if transaction is successful
  137. if ( isset($response->data) && is_object( $response->data ) &&
  138. $response->status == true &&
  139. $response->data->status == 'success'
  140. ) {
  141. $isSuccessful = true;
  142. }
  143. return $isSuccessful;
  144. }
  145. /**
  146. * Compares the amount paid by customer to the amount passed into it
  147. *
  148. * @param $amountExpected
  149. *
  150. * @return bool
  151. */
  152. public function amountEquals( $amountExpected )
  153. {
  154. // $this->verify(); // call verify() or isSuccessful() before calling this method
  155. $transactionResponse = $this->transactionResponse['verify'];
  156. if ( is_object( $transactionResponse ) ) {
  157. return
  158. ( (int) $transactionResponse->data->amount === $amountExpected );
  159. }
  160. return false;
  161. }
  162. /**
  163. * @param null $reference
  164. *
  165. * @return string|null
  166. */
  167. public function getAuthorizationCode( $reference = null )
  168. {
  169. $authorizationCode = null;
  170. // get verify response
  171. if ( $this->isSuccessful( $reference ) ) {
  172. $response = $this->verify( $reference );
  173. $authorizationCode = $response->data->authorization->authorization_code;
  174. }
  175. return $authorizationCode;
  176. }
  177. /**
  178. * @param $email
  179. *
  180. * @return $this
  181. */
  182. public function setEmail( $email )
  183. {
  184. // setting the email
  185. $this->email = $email;
  186. return $this;
  187. }
  188. public function getEmail( $email )
  189. {
  190. // setting the email
  191. $this->email = $email;
  192. }
  193. /**
  194. * @param int $amount
  195. *
  196. * @todo Allow to set kobo using '.' syntax
  197. * @return $this
  198. */
  199. public function setAmount( $amount )
  200. {
  201. // setting amount in naira //TODO: Allow to set kobo using '.' syntax
  202. $this->amount = ( $amount * 100 );
  203. return $this;
  204. }
  205. /**
  206. * @return int
  207. */
  208. public function getAmount()
  209. {
  210. return $this->amount;
  211. }
  212. /**
  213. * Sets the transaction reference code/id
  214. *
  215. * @param null $reference
  216. */
  217. public function setReference( $reference )
  218. {
  219. $this->reference = $reference;
  220. }
  221. /**
  222. * @param bool $afterInitialize
  223. *
  224. * @return null
  225. *
  226. */
  227. public function getReference( $afterInitialize = false )
  228. {
  229. if ( $afterInitialize ) {
  230. $reference = $this->response->data->reference;
  231. } else {
  232. $reference = $this->reference;
  233. }
  234. return $reference;
  235. }
  236. /**
  237. * To set callback URL, can be used to override callback URL set on paystack dashboard
  238. *
  239. * @param string $callbackUrl
  240. *
  241. * @return $this
  242. */
  243. public function setCallbackUrl( $callbackUrl )
  244. {
  245. $this->_callbackUrl = $callbackUrl;
  246. return $this;
  247. }
  248. }