Paystack.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. namespace smladeoye\paystack;
  3. use yii\base\Component;
  4. class Paystack extends Component
  5. {
  6. /** Operation type to initialize transaction */
  7. CONST OP_TRANS_INITIALIZE = 0;
  8. /** Operation type to verify transaction */
  9. CONST OP_TRANS_VERIFY = 1;
  10. /* Operation type to list all transaction */
  11. CONST OP_TRANS_LIST = 2;
  12. /** Operation type to fetch transaction */
  13. CONST OP_TRANS_FETCH = 3;
  14. /** Operation type to charge authorization */
  15. CONST OP_TRANS_CHARGE = 4;
  16. /** Operation type to get transaction timeline */
  17. CONST OP_TRANS_TIMELINE = 5;
  18. /** Operation type to get transaction total */
  19. CONST OP_TRANS_TOTAL = 6;
  20. /** Operation type to expot transaction data
  21. * returns a link for the export
  22. */
  23. CONST OP_TRANS_EXPORT = 7;
  24. /**
  25. * Operation type to create customers
  26. */
  27. CONST OP_CUST_CREATE = 8;
  28. /** Operation type to list all customers */
  29. CONST OP_CUST_LIST = 9;
  30. /**
  31. * Operation type to fetch a particular customer
  32. * The cutomer code or email or id is required.
  33. */
  34. CONST OP_CUST_FETCH = 10;
  35. /**
  36. * Operation type to update a particular customer information
  37. * The customer code or email or id will be required.
  38. */
  39. CONST OP_CUST_UPDATE = 11;
  40. /** Operation type to whitelist a particular customer
  41. * The customer code or email or id will be required*/
  42. CONST OP_CUST_WHITELIST = 12;
  43. /** Operation type to blacklist a particular customer
  44. * The customer code or email or id will be required*/
  45. CONST OP_CUST_BLACKLIST = 13;
  46. /** Operation code to create a subaccount
  47. * The business_name, settlement_name, account_number and percentage_charge are required*/
  48. CONST OP_SUBACCOUNT_CREATE = 14;
  49. /**
  50. * Operation code to list all subaccounts
  51. */
  52. CONST OP_SUBACCOUNT_LIST = 15;
  53. /**
  54. * Operation code to fetch a particular subaccount
  55. * the account id is required
  56. */
  57. CONST OP_SUBACCOUNT_FETCH = 16;
  58. /**
  59. * Operation code to update a particular subaccount
  60. * the account id is required along with the updated info
  61. */
  62. CONST OP_SUBACCOUNT_UPDATE = 17;
  63. /**
  64. * Operation code to list banks associated with a subaccount
  65. * the account id is required along with the updated info
  66. */
  67. CONST OP_SUBACCOUNT_BANKS = 18;
  68. /** Operation code to create a plan */
  69. CONST OP_PLAN_CREATE = 19;
  70. /** Operation code to list all plans */
  71. CONST OP_PLAN_LIST = 20;
  72. /** Operation code to detch a plan */
  73. CONST OP_PLAN_FETCH = 21;
  74. /** Operation code to update a plan */
  75. CONST OP_PLAN_UPDATE = 22;
  76. CONST OP_SUBSCRIPTION_CREATE = 23;
  77. CONST OP_SUBSCRIPTION_LIST = 24;
  78. CONST OP_SUBSCRIPTION_FETCH = 25;
  79. CONST OP_SUBSCRIPTION_ENABLE = 26;
  80. CONST OP_SUBSCRIPTION_DISABLE = 27;
  81. CONST OP_PAGE_CREATE = 28;
  82. CONST OP_PAGE_LIST = 29;
  83. CONST OP_PAGE_FETCH = 30;
  84. CONST OP_PAGE_UPDATE = 31;
  85. CONST OP_PAGE_AVAILABILITY = 32;
  86. /** operation code to list settlements */
  87. CONST OP_SETTLEMENT_LIST = 33;
  88. /** Paystack whitelist code */
  89. CONST WHITELIST = 'allow';
  90. /** Paystack blacklist code */
  91. CONST BLACKLIST = 'deny';
  92. /** GET method for request */
  93. CONST METHOD_GET = 'GET';
  94. /** POST method for request */
  95. CONST METHOD_POST = 'POST';
  96. /** PUT method for request */
  97. CONST METHOD_PUT = 'PUT';
  98. /** DELETE method for request */
  99. CONST METHOD_DELETE = 'DELETE';
  100. //plan interval code for hourly
  101. CONST PLAN_INTERVAL_HOURLY = 'hourly';
  102. //plan interval code for daily
  103. CONST PLAN_INTERVAL_DAILY = 'daily';
  104. //plan interval code for weekly
  105. CONST PLAN_INTERVAL_WEEKLY = 'weekly';
  106. //plan interval code for monthly
  107. CONST PLAN_INTERVAL_MONTHLY = 'monthly';
  108. //plan interval code for yearly
  109. CONST PLAN_INTERVAL_YEARLY = 'yearly';
  110. /**
  111. * Test environmet
  112. */
  113. CONST ENV_TEST = 'TEST';
  114. /**
  115. * Live environment
  116. */
  117. CONST ENV_LIVE = 'LIVE';
  118. /** Callback url after a transaction has been performed */
  119. public $callbackUrl;
  120. /** @var string test-key for the test environment */
  121. /** @var boolean sets the environment for the request to LIVE when test is false */
  122. public $environment = self::ENV_TEST;
  123. /** @var bool set to false to allow unsecured connection to the paystack api */
  124. public $verifyPeer;
  125. public $testSecretKey;
  126. public $testPublicKey;
  127. /** @var string live-key for the test environment */
  128. public $livePublicKey;
  129. /** @var string live secret key used in live environment */
  130. public $liveSecretKey;
  131. /** @var string Paystack API base url, replaces the defaultApiUrl when set */
  132. public $apiUrl;
  133. /** @var string Paystack API default base url*/
  134. private $defaultApiUrl = "https://api.paystack.co";
  135. private static $reference;
  136. public $header;
  137. public $authHeader;
  138. public $beforeSend;
  139. public $afterSend;
  140. public $transaction = array(
  141. 'baseUrl'=>'/transaction',
  142. 'initializeUrl'=>'/initialize',
  143. 'verifyUrl'=>'/verify',
  144. 'chargeUrl'=>'/charge_authorization',
  145. 'beforeSend'=>'',
  146. 'afterSend'=>''
  147. );
  148. public $customer = array(
  149. 'baseUrl'=>'/customer',
  150. 'beforeSend'=>'',
  151. 'afterSend'=>''
  152. );
  153. public $subaccount = array(
  154. 'baseUrl'=>'/subaccount',
  155. 'beforeSend'=>'',
  156. 'afterSend'=>''
  157. );
  158. public $plan = array(
  159. 'baseUrl'=>'/plan',
  160. 'beforeSend'=>'',
  161. 'afterSend'=>''
  162. );
  163. public $page = array(
  164. 'baseUrl'=>'/page',
  165. 'slugAvailabilityUrl'=>'/check_slug_availability',
  166. 'beforeSend'=>'',
  167. 'afterSend'=>''
  168. );
  169. public $subscription = array(
  170. 'baseUrl'=>'/subscription',
  171. 'beforeSend'=>'',
  172. 'afterSend'=>''
  173. );
  174. public $settlement = array(
  175. 'baseUrl'=>'/settlement',
  176. 'beforeSend'=>'',
  177. 'afterSend'=>''
  178. );
  179. public function transaction()
  180. {
  181. return new Transaction($this);
  182. }
  183. public function customer()
  184. {
  185. return new Customer($this);
  186. }
  187. public function subaccount()
  188. {
  189. return new SubAccount($this);
  190. }
  191. public function plan()
  192. {
  193. return new Plan($this);
  194. }
  195. public function subscription()
  196. {
  197. return new Subscription($this);
  198. }
  199. public function page()
  200. {
  201. return new Page($this);
  202. }
  203. public function settlement()
  204. {
  205. return new Settlement($this);
  206. }
  207. public function init()
  208. {
  209. parent::init();
  210. $this->setApiUrl();
  211. $this->setAuthorization();
  212. }
  213. protected function getSecretKey()
  214. {
  215. return (strtolower($this->environment) == strtolower(self::ENV_LIVE))?$this->liveSecretKey:$this->testSecretKey;
  216. }
  217. public function getAuthKeys()
  218. {
  219. if (strtolower($this->environment) == strtolower(self::ENV_LIVE))
  220. {
  221. return array('secret_key'=>$this->liveSecretKey,'public_key'=>$this->livePublicKey);
  222. }
  223. else
  224. {
  225. return array('secret_key'=>$this->testSecretKey,'public_key'=>$this->testPublicKey);
  226. }
  227. }
  228. protected function setAuthorization()
  229. {
  230. $this->authHeader = array('Authorization: Bearer '.$this->getSecretKey());
  231. }
  232. public function setHeader($headers = array())
  233. {
  234. $this->header = array_merge($this->authHeader,$headers);
  235. }
  236. public function getHeader()
  237. {
  238. return $this->header?$this->header:$this->authHeader;
  239. }
  240. protected function setApiUrl($url = null)
  241. {
  242. if ($url == null)
  243. {
  244. if (!isset($this->apiUrl) || empty($this->apiUrl))
  245. $this->apiUrl = $this->defaultApiUrl;
  246. }
  247. else
  248. {
  249. $this->apiUrl = $url;
  250. }
  251. return $this;
  252. }
  253. public function getApiUrl()
  254. {
  255. if (!isset($this->apiUrl) || empty($this->apiUrl))
  256. $this->apiUrl = $this->defaultApiUrl;
  257. return $this->apiUrl;
  258. }
  259. public static function generateRef($length = 10)
  260. {
  261. $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  262. $charactersLength = strlen($characters);
  263. $randomString = '';
  264. for ($i = 0; $i < $length; $i++)
  265. {
  266. $randomString .= $characters[rand(0, $charactersLength - 1)];
  267. }
  268. return $randomString;
  269. }
  270. }