├── .gitignore ├── LICENSE ├── Paystack.php ├── README.md ├── SAMPLES.md └── config └── paystack.php /.gitignore: -------------------------------------------------------------------------------- 1 | */config/development 2 | */logs/log-*.php 3 | !*/logs/index.html 4 | */cache/* 5 | !*/cache/index.html 6 | !*/cache/.htaccess 7 | /Paystack-bak.php 8 | /t.php 9 | 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 YabaCon Valley 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Paystack.php: -------------------------------------------------------------------------------- 1 | secret_key = $secret_key; 51 | } 52 | 53 | public function useGuzzle() 54 | { 55 | $this->use_guzzle = true; 56 | } 57 | 58 | public static function disableFileGetContentsFallback() 59 | { 60 | Paystack::$fallback_to_file_get_contents = false; 61 | } 62 | 63 | public static function enableFileGetContentsFallback() 64 | { 65 | Paystack::$fallback_to_file_get_contents = true; 66 | } 67 | 68 | public function __call($method, $args) 69 | { 70 | if ($singular_form = PaystackHelpersRouter::singularFor($method)) { 71 | return $this->handlePlural($singular_form, $method, $args); 72 | } 73 | return $this->handleSingular($method, $args); 74 | } 75 | 76 | private function handlePlural($singular_form, $method, $args) 77 | { 78 | if ((count($args) === 1 && is_array($args[0]))||(count($args) === 0)) { 79 | return $this->{$singular_form}->__call('getList', $args); 80 | } 81 | throw new \InvalidArgumentException( 82 | 'Route "' . $method . '" can only accept an optional array of filters and ' 83 | .'paging arguments (perPage, page).' 84 | ); 85 | } 86 | 87 | private function handleSingular($method, $args) 88 | { 89 | if (count($args) === 1) { 90 | $args = [[], [ PaystackHelpersRouter::ID_KEY => $args[0] ] ]; 91 | return $this->{$method}->__call('fetch', $args); 92 | } 93 | throw new \InvalidArgumentException( 94 | 'Route "' . $method . '" can only accept an id or code.' 95 | ); 96 | } 97 | 98 | public function __get($name) 99 | { 100 | return new PaystackHelpersRouter($name, $this); 101 | } 102 | } 103 | 104 | interface PaystackContractsRouteInterface 105 | { 106 | 107 | const METHOD_KEY = 'method'; 108 | const ENDPOINT_KEY = 'endpoint'; 109 | const PARAMS_KEY = 'params'; 110 | const ARGS_KEY = 'args'; 111 | const REQUIRED_KEY = 'required'; 112 | const POST_METHOD = 'post'; 113 | const PUT_METHOD = 'put'; 114 | const GET_METHOD = 'get'; 115 | 116 | public static function root(); 117 | } 118 | 119 | class PaystackEvent 120 | { 121 | public $raw = ''; 122 | protected $signature = ''; 123 | public $obj; 124 | const SIGNATURE_KEY = 'HTTP_X_PAYSTACK_SIGNATURE'; 125 | 126 | protected function __construct() 127 | { 128 | } 129 | 130 | public static function capture() 131 | { 132 | $evt = new Event(); 133 | $evt->raw = @file_get_contents('php://input'); 134 | $evt->signature = ( isset($_SERVER[self::SIGNATURE_KEY]) ? $_SERVER[self::SIGNATURE_KEY] : '' ); 135 | $evt->loadObject(); 136 | return $evt; 137 | } 138 | 139 | protected function loadObject() 140 | { 141 | $this->obj = json_decode($this->raw); 142 | } 143 | 144 | public function discoverOwner(array $keys) 145 | { 146 | if (!$this->obj || !property_exists($this->obj, 'data')) { 147 | return; 148 | } 149 | foreach ($keys as $index => $key) { 150 | if ($this->validFor($key)) { 151 | return $index; 152 | } 153 | } 154 | } 155 | 156 | public function validFor($key) 157 | { 158 | if ($this->signature === hash_hmac('sha512', $this->raw, $key)) { 159 | return true; 160 | } 161 | return false; 162 | } 163 | 164 | public function package(array $additional_headers = [], $method = 'POST') 165 | { 166 | $pack = new PaystackHttpRequest(); 167 | $pack->method = $method; 168 | $pack->headers = $additional_headers; 169 | $pack->headers["X-Paystack-Signature"] = $this->signature; 170 | $pack->headers["Content-Type"] = "application/json"; 171 | $pack->body = $this->raw; 172 | return $pack; 173 | } 174 | 175 | public function forwardTo($url, array $additional_headers = [], $method = 'POST') 176 | { 177 | if (!filter_var($url, FILTER_VALIDATE_URL)) { 178 | return false; 179 | } 180 | $packed = $this->package($additional_headers, $method); 181 | $packed->endpoint = $url; 182 | return $packed->send()->wrapUp(); 183 | } 184 | } 185 | 186 | class PaystackExceptionApiException extends PaystackExceptionPaystackException 187 | { 188 | private $PaystackHttpResponseObject; 189 | 190 | public function __construct($message, $object) 191 | { 192 | parent::__construct($message); 193 | $this->PaystackHttpResponseObject = $object; 194 | } 195 | 196 | public function getPaystackHttpResponseObject() 197 | { 198 | return $this->PaystackHttpResponseObject; 199 | } 200 | } 201 | 202 | class PaystackExceptionBadMetaNameException extends PaystackExceptionPaystackException 203 | { 204 | public $errors; 205 | public function __construct($message, array $errors = []) 206 | { 207 | parent::__construct($message); 208 | $this->errors = $errors; 209 | } 210 | } 211 | 212 | class PaystackExceptionPaystackException extends Exception 213 | { 214 | public function __construct($message) 215 | { 216 | parent::__construct($message); 217 | } 218 | } 219 | 220 | class PaystackExceptionValidationException extends PaystackExceptionPaystackException 221 | { 222 | public $errors; 223 | public function __construct($message, array $errors = []) 224 | { 225 | parent::__construct($message); 226 | $this->errors = $errors; 227 | } 228 | } 229 | 230 | 231 | class PaystackFee 232 | { 233 | const DEFAULT_PERCENTAGE = 0.015; 234 | const DEFAULT_ADDITIONAL_CHARGE = 10000; 235 | const DEFAULT_THRESHOLD = 250000; 236 | const DEFAULT_CAP = 200000; 237 | 238 | public static $default_percentage = Fee::DEFAULT_PERCENTAGE; 239 | public static $default_additional_charge = Fee::DEFAULT_ADDITIONAL_CHARGE; 240 | public static $default_threshold = Fee::DEFAULT_THRESHOLD; 241 | public static $default_cap = Fee::DEFAULT_CAP; 242 | 243 | private $percentage; 244 | private $additional_charge; 245 | private $threshold; 246 | private $cap; 247 | 248 | private $chargeDivider; 249 | private $crossover; 250 | private $flatlinePlusCharge; 251 | private $flatline; 252 | 253 | public function __construct() 254 | { 255 | $this->percentage = Fee::$default_percentage; 256 | $this->additional_charge = Fee::$default_additional_charge; 257 | $this->threshold = Fee::$default_threshold; 258 | $this->cap = Fee::$default_cap; 259 | $this->__setup(); 260 | } 261 | 262 | public function withPercentage($percentage) 263 | { 264 | $this->percentage = $percentage; 265 | $this->__setup(); 266 | } 267 | 268 | public static function resetDefaults() 269 | { 270 | Fee::$default_percentage = Fee::DEFAULT_PERCENTAGE; 271 | Fee::$default_additional_charge = Fee::DEFAULT_ADDITIONAL_CHARGE; 272 | Fee::$default_threshold = Fee::DEFAULT_THRESHOLD; 273 | Fee::$default_cap = Fee::DEFAULT_CAP; 274 | } 275 | 276 | public function withAdditionalCharge($additional_charge) 277 | { 278 | $this->additional_charge = $additional_charge; 279 | $this->__setup(); 280 | } 281 | 282 | public function withThreshold($threshold) 283 | { 284 | $this->threshold = $threshold; 285 | $this->__setup(); 286 | } 287 | 288 | public function withCap($cap) 289 | { 290 | $this->cap = $cap; 291 | $this->__setup(); 292 | } 293 | 294 | private function __setup() 295 | { 296 | $this->chargeDivider = $this->__chargeDivider(); 297 | $this->crossover = $this->__crossover(); 298 | $this->flatlinePlusCharge = $this->__flatlinePlusCharge(); 299 | $this->flatline = $this->__flatline(); 300 | } 301 | 302 | private function __chargeDivider() 303 | { 304 | return 1 - $this->percentage; 305 | } 306 | 307 | private function __crossover() 308 | { 309 | return ($this->threshold * $this->chargeDivider) - $this->additional_charge; 310 | } 311 | 312 | private function __flatlinePlusCharge() 313 | { 314 | return ($this->cap - $this->additional_charge) / $this->percentage; 315 | } 316 | 317 | private function __flatline() 318 | { 319 | return $this->flatlinePlusCharge - $this->cap; 320 | } 321 | 322 | public function addFor($amountinkobo) 323 | { 324 | if ($amountinkobo > $this->flatline) { 325 | return intval(ceil($amountinkobo + $this->cap)); 326 | } elseif ($amountinkobo > $this->crossover) { 327 | return intval(ceil(($amountinkobo + $this->additional_charge) / $this->chargeDivider)); 328 | } else { 329 | return intval(ceil($amountinkobo / $this->chargeDivider)); 330 | } 331 | } 332 | 333 | public function calculateFor($amountinkobo) 334 | { 335 | $fee = $this->percentage * $amountinkobo; 336 | if ($amountinkobo >= $this->threshold) { 337 | $fee += $this->additional_charge; 338 | } 339 | if ($fee > $this->cap) { 340 | $fee = $this->cap; 341 | } 342 | return intval(ceil($fee)); 343 | } 344 | } 345 | 346 | class PaystackHelpersCaller 347 | { 348 | private $paystackObj; 349 | 350 | public function __construct($paystackObj) 351 | { 352 | $this->paystackObj = $paystackObj; 353 | } 354 | 355 | public function callEndpoint($interface, $payload = [ ], $sentargs = [ ]) 356 | { 357 | $builder = new PaystackHttpRequestBuilder($this->paystackObj, $interface, $payload, $sentargs); 358 | return $builder->build()->send()->wrapUp(); 359 | } 360 | } 361 | 362 | class PaystackHelpersRouter 363 | { 364 | private $route; 365 | private $route_class; 366 | private $methods; 367 | public static $ROUTES = [ 368 | 'customer', 'page', 'plan', 'subscription', 'transaction', 'subaccount', 369 | 'balance', 'bank', 'decision', 'integration', 'settlement', 370 | 'transfer', 'transferrecipient' 371 | ]; 372 | public static $ROUTE_SINGULAR_LOOKUP = [ 373 | 'customers'=>'customer', 374 | 'pages'=>'page', 375 | 'plans'=>'plan', 376 | 'subscriptions'=>'subscription', 377 | 'transactions'=>'transaction', 378 | 'banks'=>'bank', 379 | 'settlements'=>'settlement', 380 | 'transfers'=>'transfer', 381 | 'transferrecipients'=>'transferrecipient', 382 | ]; 383 | 384 | const ID_KEY = 'id'; 385 | const PAYSTACK_API_ROOT = 'https://api.paystack.co'; 386 | 387 | public function __call($methd, $sentargs) 388 | { 389 | $method = ($methd === 'list' ? 'getList' : $methd ); 390 | if (array_key_exists($method, $this->methods) && is_callable($this->methods[$method])) { 391 | return call_user_func_array($this->methods[$method], $sentargs); 392 | } else { 393 | throw new \Exception('Function "' . $method . '" does not exist for "' . $this->route . '".'); 394 | } 395 | } 396 | 397 | public static function singularFor($method) 398 | { 399 | return ( 400 | array_key_exists($method, PaystackHelpersRouter::$ROUTE_SINGULAR_LOOKUP) ? 401 | PaystackHelpersRouter::$ROUTE_SINGULAR_LOOKUP[$method] : 402 | null 403 | ); 404 | } 405 | 406 | public function __construct($route, $paystackObj) 407 | { 408 | if (!in_array($route, PaystackHelpersRouter::$ROUTES)) { 409 | throw new ValidationException( 410 | "Route '{$route}' does not exist." 411 | ); 412 | } 413 | 414 | $this->route = strtolower($route); 415 | $this->route_class = 'PaystackRoutes' . ucwords($route); 416 | 417 | $mets = get_class_methods($this->route_class); 418 | if (empty($mets)) { 419 | throw new \InvalidArgumentException('Class "' . $this->route . '" does not exist.'); 420 | } 421 | // add methods to this object per method, except root 422 | foreach ($mets as $mtd) { 423 | if ($mtd === 'root') { 424 | continue; 425 | } 426 | $mtdFunc = function ( 427 | array $params = [ ], 428 | array $sentargs = [ ] 429 | ) use ( 430 | $mtd, 431 | $paystackObj 432 | ) { 433 | $interface = call_user_func($this->route_class . '::' . $mtd); 434 | // TODO: validate params and sentargs against definitions 435 | $PaystackHelpersCaller = new PaystackHelpersCaller($paystackObj); 436 | return $PaystackHelpersCaller->callEndpoint($interface, $params, $sentargs); 437 | }; 438 | $this->methods[$mtd] = \Closure::bind($mtdFunc, $this, get_class()); 439 | } 440 | } 441 | } 442 | 443 | 444 | class PaystackHttpRequest 445 | { 446 | public $method; 447 | public $endpoint; 448 | public $body = ''; 449 | public $headers = []; 450 | protected $PaystackHttpResponse; 451 | protected $paystackObj; 452 | 453 | public function __construct($paystackObj = null) 454 | { 455 | $this->PaystackHttpResponse = new PaystackHttpResponse(); 456 | $this->paystackObj = $paystackObj; 457 | $this->PaystackHttpResponse->forApi = !is_null($paystackObj); 458 | if ($this->PaystackHttpResponse->forApi) { 459 | $this->headers['Content-Type'] = 'application/json'; 460 | } 461 | } 462 | 463 | public function getPaystackHttpResponse() 464 | { 465 | return $this->PaystackHttpResponse; 466 | } 467 | 468 | public function flattenedHeaders() 469 | { 470 | $_ = []; 471 | foreach ($this->headers as $key => $value) { 472 | $_[] = $key . ": " . $value; 473 | } 474 | return $_; 475 | } 476 | 477 | public function send() 478 | { 479 | $this->attemptGuzzle(); 480 | if (!$this->PaystackHttpResponse->okay) { 481 | $this->attemptCurl(); 482 | } 483 | if (!$this->PaystackHttpResponse->okay) { 484 | $this->attemptFileGetContents(); 485 | } 486 | return $this->PaystackHttpResponse; 487 | } 488 | 489 | public function attemptGuzzle() 490 | { 491 | if (isset($this->paystackObj) && !$this->paystackObj->use_guzzle) { 492 | $this->PaystackHttpResponse->okay = false; 493 | return; 494 | } 495 | if (class_exists('\\GuzzleHttp\\Exception\\BadPaystackHttpResponseException') 496 | && class_exists('\\GuzzleHttp\\Exception\\ClientException') 497 | && class_exists('\\GuzzleHttp\\Exception\\ConnectException') 498 | && class_exists('\\GuzzleHttp\\Exception\\PaystackHttpRequestException') 499 | && class_exists('\\GuzzleHttp\\Exception\\ServerException') 500 | && class_exists('\\GuzzleHttp\\Client') 501 | && class_exists('\\GuzzleHttp\\Psr7\\PaystackHttpRequest') 502 | ) { 503 | $PaystackHttpRequest = new \GuzzleHttp\Psr7\PaystackHttpRequest( 504 | strtoupper($this->method), 505 | $this->endpoint, 506 | $this->headers, 507 | $this->body 508 | ); 509 | $client = new \GuzzleHttp\Client(); 510 | try { 511 | $psr7PaystackHttpResponse = $client->send($PaystackHttpRequest); 512 | $this->PaystackHttpResponse->body = $psr7PaystackHttpResponse->getBody()->getContents(); 513 | $this->PaystackHttpResponse->okay = true; 514 | } catch (\Exception $e) { 515 | if (($e instanceof \GuzzleHttp\Exception\BadPaystackHttpResponseException 516 | || $e instanceof \GuzzleHttp\Exception\ClientException 517 | || $e instanceof \GuzzleHttp\Exception\ConnectException 518 | || $e instanceof \GuzzleHttp\Exception\PaystackHttpRequestException 519 | || $e instanceof \GuzzleHttp\Exception\ServerException) 520 | ) { 521 | if ($e->hasPaystackHttpResponse()) { 522 | $this->PaystackHttpResponse->body = $e->getPaystackHttpResponse()->getBody()->getContents(); 523 | } 524 | $this->PaystackHttpResponse->okay = true; 525 | } 526 | $this->PaystackHttpResponse->messages[] = $e->getMessage(); 527 | } 528 | } 529 | } 530 | 531 | public function attemptFileGetContents() 532 | { 533 | if (!Paystack::$fallback_to_file_get_contents) { 534 | return; 535 | } 536 | $context = stream_context_create( 537 | [ 538 | 'http'=>array( 539 | 'method'=>$this->method, 540 | 'header'=>$this->flattenedHeaders(), 541 | 'content'=>$this->body, 542 | 'ignore_errors' => true 543 | ) 544 | ] 545 | ); 546 | $this->PaystackHttpResponse->body = file_get_contents($this->endpoint, false, $context); 547 | if ($this->PaystackHttpResponse->body === false) { 548 | $this->PaystackHttpResponse->messages[] = 'file_get_contents failed with PaystackHttpResponse: \'' . error_get_last() . '\'.'; 549 | } else { 550 | $this->PaystackHttpResponse->okay = true; 551 | } 552 | } 553 | 554 | public function attemptCurl() 555 | { 556 | //open connection 557 | $ch = \curl_init(); 558 | \curl_setopt($ch, \CURLOPT_URL, $this->endpoint); 559 | ($this->method === PaystackContractsRouteInterface::POST_METHOD) && \curl_setopt($ch, \CURLOPT_POST, true); 560 | ($this->method === PaystackContractsRouteInterface::PUT_METHOD) && \curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'PUT'); 561 | 562 | if ($this->method !== PaystackContractsRouteInterface::GET_METHOD) { 563 | \curl_setopt($ch, \CURLOPT_POSTFIELDS, $this->body); 564 | } 565 | \curl_setopt($ch, \CURLOPT_HTTPHEADER, $this->flattenedHeaders()); 566 | \curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1); 567 | $this->PaystackHttpResponse->forApi && \curl_setopt($ch, \CURLOPT_SSLVERSION, 6); 568 | 569 | $this->PaystackHttpResponse->body = \curl_exec($ch); 570 | 571 | if (\curl_errno($ch)) { 572 | $cerr = \curl_error($ch); 573 | $this->PaystackHttpResponse->messages[] = 'Curl failed with response: \'' . $cerr . '\'.'; 574 | } else { 575 | $this->PaystackHttpResponse->okay = true; 576 | } 577 | 578 | \curl_close($ch); 579 | } 580 | } 581 | 582 | class PaystackHttpRequestBuilder 583 | { 584 | protected $paystackObj; 585 | protected $interface; 586 | protected $PaystackHttpRequest; 587 | 588 | public $payload = [ ]; 589 | public $sentargs = [ ]; 590 | 591 | public function __construct($paystackObj, $interface, array $payload = [ ], array $sentargs = [ ]) 592 | { 593 | $this->PaystackHttpRequest = new PaystackHttpRequest($paystackObj); 594 | $this->paystackObj = $paystackObj; 595 | $this->interface = $interface; 596 | $this->payload = $payload; 597 | $this->sentargs = $sentargs; 598 | } 599 | 600 | public function build() 601 | { 602 | $this->PaystackHttpRequest->headers["Authorization"] = "Bearer " . $this->paystackObj->secret_key; 603 | $this->PaystackHttpRequest->headers["User-Agent"] = "Paystack/v1 PhpBindings/" . Paystack::VERSION; 604 | $this->PaystackHttpRequest->endpoint = PaystackHelpersRouter::PAYSTACK_API_ROOT . $this->interface[PaystackContractsRouteInterface::ENDPOINT_KEY]; 605 | $this->PaystackHttpRequest->method = $this->interface[PaystackContractsRouteInterface::METHOD_KEY]; 606 | $this->moveArgsToSentargs(); 607 | $this->putArgsIntoEndpoint($this->PaystackHttpRequest->endpoint); 608 | $this->packagePayload(); 609 | return $this->PaystackHttpRequest; 610 | } 611 | 612 | public function packagePayload() 613 | { 614 | if (is_array($this->payload) && count($this->payload)) { 615 | if ($this->PaystackHttpRequest->method === PaystackContractsRouteInterface::GET_METHOD) { 616 | $this->PaystackHttpRequest->endpoint = $this->PaystackHttpRequest->endpoint . '?' . http_build_query($this->payload); 617 | } else { 618 | $this->PaystackHttpRequest->body = json_encode($this->payload); 619 | } 620 | } 621 | } 622 | 623 | public function putArgsIntoEndpoint(&$endpoint) 624 | { 625 | foreach ($this->sentargs as $key => $value) { 626 | $endpoint = str_replace('{' . $key . '}', $value, $endpoint); 627 | } 628 | } 629 | 630 | public function moveArgsToSentargs() 631 | { 632 | if (!array_key_exists(PaystackContractsRouteInterface::ARGS_KEY, $this->interface)) { 633 | return; 634 | } 635 | $args = $this->interface[PaystackContractsRouteInterface::ARGS_KEY]; 636 | foreach ($this->payload as $key => $value) { 637 | if (in_array($key, $args)) { 638 | $this->sentargs[$key] = $value; 639 | unset($this->payload[$key]); 640 | } 641 | } 642 | } 643 | } 644 | 645 | 646 | 647 | class PaystackHttpResponse 648 | { 649 | public $okay; 650 | public $body; 651 | public $forApi; 652 | public $messages = []; 653 | 654 | private function parsePaystackPaystackHttpResponse() 655 | { 656 | $resp = \json_decode($this->body); 657 | 658 | if ($resp === null || !property_exists($resp, 'status') || !$resp->status) { 659 | throw new ApiException( 660 | "Paystack Request failed with Response: '" . 661 | $this->messageFromApiJson($resp)."'", 662 | $resp 663 | ); 664 | } 665 | 666 | return $resp; 667 | } 668 | 669 | private function messageFromApiJson($resp) 670 | { 671 | $message = $this->body; 672 | if ($resp !== null) { 673 | if (property_exists($resp, 'message')) { 674 | $message = $resp->message; 675 | } 676 | if (property_exists($resp, 'errors') && ($resp->errors instanceof \stdClass)) { 677 | $message .= "\nErrors:\n"; 678 | foreach ($resp->errors as $field => $errors) { 679 | $message .= "\t" . $field . ":\n"; 680 | foreach ($errors as $_unused => $error) { 681 | $message .= "\t\t" . $error->rule . ": "; 682 | $message .= $error->message . "\n"; 683 | } 684 | } 685 | } 686 | } 687 | return $message; 688 | } 689 | 690 | private function implodedMessages() 691 | { 692 | return implode("\n\n", $this->messages); 693 | } 694 | 695 | public function wrapUp() 696 | { 697 | if ($this->okay && $this->forApi) { 698 | return $this->parsePaystackPaystackHttpResponse(); 699 | } 700 | if (!$this->okay && $this->forApi) { 701 | throw new \Exception($this->implodedMessages()); 702 | } 703 | if ($this->okay) { 704 | return $this->body; 705 | } 706 | error_log($this->implodedMessages()); 707 | return false; 708 | } 709 | } 710 | 711 | 712 | class PaystackMetadataBuilder 713 | { 714 | private $meta; 715 | public static $auto_snake_case = true; 716 | 717 | public function __construct() 718 | { 719 | $this->meta = []; 720 | } 721 | 722 | private function with($name, $value) 723 | { 724 | if ($name === 'custom_fields') { 725 | throw new PaystackExceptionBadMetaNameException('Please use the withCustomField method to add custom fields'); 726 | } 727 | $this->meta[$name] = $value; 728 | return $this; 729 | } 730 | 731 | private function toSnakeCase($input) 732 | { 733 | preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches); 734 | $ret = $matches[0]; 735 | foreach ($ret as &$match) { 736 | $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match); 737 | } 738 | return implode('_', $ret); 739 | } 740 | 741 | public function __call($method, $args) 742 | { 743 | if ((strpos($method, 'with') === 0) && ($method !== 'with')) { 744 | $name = substr($method, 4); 745 | if (PaystackMetadataBuilder::$auto_snake_case) { 746 | $name = $this->toSnakeCase($name); 747 | } 748 | return $this->with($name, $args[0]); 749 | } 750 | throw new \BadMethodCallException('Call to undefined function: ' . get_class($this) . '::' . $method); 751 | } 752 | 753 | public function withCustomField($title, $value) 754 | { 755 | if (!array_key_exists('custom_fields', $this->meta)) { 756 | $this->meta['custom_fields'] = []; 757 | } 758 | $this->meta['custom_fields'][] = [ 759 | 'display_name' => strval($title), 760 | 'variable_name' => strval($title), 761 | 'value' => strval($value), 762 | ]; 763 | return $this; 764 | } 765 | 766 | public function build() 767 | { 768 | return json_encode($this->meta); 769 | } 770 | } 771 | 772 | class PaystackRoutesBalance implements PaystackContractsRouteInterface 773 | { 774 | 775 | public static function root() 776 | { 777 | return '/balance'; 778 | } 779 | 780 | public static function getList() 781 | { 782 | return [ PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::GET_METHOD, 783 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesBalance::root() ]; 784 | } 785 | } 786 | 787 | 788 | class PaystackRoutesBank implements PaystackContractsRouteInterface 789 | { 790 | 791 | public static function root() 792 | { 793 | return '/bank'; 794 | } 795 | public static function getList() 796 | { 797 | return [ PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::GET_METHOD, 798 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesBank::root() ]; 799 | } 800 | 801 | public static function resolveBvn() 802 | { 803 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::GET_METHOD, 804 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesBank::root() . '/resolve_bvn/{bvn}', 805 | PaystackContractsRouteInterface::ARGS_KEY => ['bvn'] ]; 806 | } 807 | 808 | public static function resolve() 809 | { 810 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::GET_METHOD, 811 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesBank::root() . '/resolve', 812 | PaystackContractsRouteInterface::PARAMS_KEY => ['account_number', 813 | 'bank_code' ] ]; 814 | } 815 | } 816 | 817 | 818 | class PaystackRoutesCustomer implements PaystackContractsRouteInterface 819 | { 820 | 821 | public static function root() 822 | { 823 | return '/customer'; 824 | } 825 | 826 | public static function create() 827 | { 828 | return [ 829 | PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::POST_METHOD, 830 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesCustomer::root(), 831 | PaystackContractsRouteInterface::PARAMS_KEY => ['first_name', 832 | 'last_name', 833 | 'email', 834 | 'metadata', 835 | 'phone' ], 836 | PaystackContractsRouteInterface::REQUIRED_KEY => [ 837 | PaystackContractsRouteInterface::PARAMS_KEY => ['first_name', 838 | 'last_name', 839 | 'email' ] 840 | ] 841 | ]; 842 | } 843 | 844 | public static function fetch() 845 | { 846 | return [ 847 | PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::GET_METHOD, 848 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesCustomer::root() . '/{id}', 849 | PaystackContractsRouteInterface::ARGS_KEY => ['id' ], 850 | PaystackContractsRouteInterface::REQUIRED_KEY => [PaystackContractsRouteInterface::ARGS_KEY => ['id' ] ] 851 | ]; 852 | } 853 | 854 | public static function getList() 855 | { 856 | return [ 857 | PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::GET_METHOD, 858 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesCustomer::root(), 859 | PaystackContractsRouteInterface::PARAMS_KEY => ['perPage', 860 | 'page' ] 861 | ]; 862 | } 863 | 864 | public static function update() 865 | { 866 | return [ 867 | PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::PUT_METHOD, 868 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesCustomer::root() . '/{id}', 869 | PaystackContractsRouteInterface::PARAMS_KEY => ['first_name', 870 | 'last_name', 871 | 'email', 872 | 'metadata', 873 | 'phone' ], 874 | PaystackContractsRouteInterface::ARGS_KEY => ['id' ] 875 | ]; 876 | } 877 | } 878 | 879 | 880 | class PaystackRoutesDecision implements PaystackContractsRouteInterface 881 | { 882 | 883 | public static function root() 884 | { 885 | return '/decision'; 886 | } 887 | 888 | public static function bin() 889 | { 890 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::GET_METHOD, 891 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesDecision::root() . '/bin/{bin}', 892 | PaystackContractsRouteInterface::ARGS_KEY => ['bin' ] ]; 893 | } 894 | } 895 | 896 | 897 | class PaystackRoutesIntegration implements PaystackContractsRouteInterface 898 | { 899 | 900 | public static function root() 901 | { 902 | return '/integration'; 903 | } 904 | 905 | public static function paymentSessionTimeout() 906 | { 907 | return [ PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::GET_METHOD, 908 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesIntegration::root() . '/payment_session_timeout', 909 | PaystackContractsRouteInterface::PARAMS_KEY => [] ]; 910 | } 911 | 912 | public static function updatePaymentSessionTimeout() 913 | { 914 | return [ 915 | PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::PUT_METHOD, 916 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesCustomer::root() . '/payment_session_timeout', 917 | PaystackContractsRouteInterface::PARAMS_KEY => ['timeout'], 918 | ]; 919 | } 920 | } 921 | 922 | 923 | class PaystackRoutesPage implements PaystackContractsRouteInterface 924 | { 925 | 926 | public static function root() 927 | { 928 | return '/page'; 929 | } 930 | 931 | public static function create() 932 | { 933 | return [ 934 | PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::POST_METHOD, 935 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesPage::root(), 936 | PaystackContractsRouteInterface::PARAMS_KEY => [ 937 | 'name', 'description', 938 | 'amount' ] 939 | ]; 940 | } 941 | 942 | public static function fetch() 943 | { 944 | return [ 945 | PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::GET_METHOD, 946 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesPage::root() . '/{id}', 947 | PaystackContractsRouteInterface::ARGS_KEY => ['id' ] 948 | ]; 949 | } 950 | 951 | public static function getList() 952 | { 953 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::GET_METHOD, 954 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesPage::root() ]; 955 | } 956 | 957 | public static function update() 958 | { 959 | return [ 960 | PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::PUT_METHOD, 961 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesPage::root() . '/{id}', 962 | PaystackContractsRouteInterface::PARAMS_KEY => [ 963 | 'name', 964 | 'description', 965 | 'amount', 966 | 'active' ], 967 | PaystackContractsRouteInterface::ARGS_KEY => ['id' ] 968 | ]; 969 | } 970 | } 971 | 972 | 973 | class PaystackRoutesPlan implements PaystackContractsRouteInterface 974 | { 975 | 976 | public static function root() 977 | { 978 | return '/plan'; 979 | } 980 | 981 | public static function create() 982 | { 983 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::POST_METHOD, 984 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesPlan::root(), 985 | PaystackContractsRouteInterface::PARAMS_KEY => [ 986 | 'name', 987 | 'description', 988 | 'amount', 989 | 'interval', 990 | 'send_invoices', 991 | 'send_sms', 992 | 'hosted_page', 993 | 'hosted_page_url', 994 | 'hosted_page_summary', 995 | 'currency' ] 996 | ]; 997 | } 998 | 999 | public static function fetch() 1000 | { 1001 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::GET_METHOD, 1002 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesPlan::root() . '/{id}', 1003 | PaystackContractsRouteInterface::ARGS_KEY => ['id' ] ]; 1004 | } 1005 | 1006 | public static function getList() 1007 | { 1008 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::GET_METHOD, 1009 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesPlan::root() ]; 1010 | } 1011 | 1012 | public static function update() 1013 | { 1014 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::PUT_METHOD, 1015 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesPlan::root() . '/{id}', 1016 | PaystackContractsRouteInterface::PARAMS_KEY => [ 1017 | 'name', 1018 | 'description', 1019 | 'amount', 1020 | 'interval', 1021 | 'send_invoices', 1022 | 'send_sms', 1023 | 'hosted_page', 1024 | 'hosted_page_url', 1025 | 'hosted_page_summary', 1026 | 'currency' ], 1027 | PaystackContractsRouteInterface::ARGS_KEY => ['id' ] ]; 1028 | } 1029 | } 1030 | 1031 | 1032 | class PaystackRoutesSettlement implements PaystackContractsRouteInterface 1033 | { 1034 | 1035 | public static function root() 1036 | { 1037 | return '/settlement'; 1038 | } 1039 | 1040 | public static function getList() 1041 | { 1042 | return [ PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::GET_METHOD, 1043 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesSettlement::root() ]; 1044 | } 1045 | } 1046 | 1047 | 1048 | class PaystackRoutesSubaccount implements PaystackContractsRouteInterface 1049 | { 1050 | 1051 | public static function root() 1052 | { 1053 | return '/subaccount'; 1054 | } 1055 | 1056 | public static function create() 1057 | { 1058 | return [ 1059 | PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::POST_METHOD, 1060 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesSubaccount::root(), 1061 | PaystackContractsRouteInterface::PARAMS_KEY => [ 1062 | 'business_name', 'settlement_bank', 1063 | 'account_number','percentage_charge', 1064 | 'primary_contact_email','primary_contact_name', 1065 | 'primary_contact_phone', 1066 | 'metadata','settlement_schedule', 1067 | ], 1068 | ]; 1069 | } 1070 | 1071 | public static function fetch() 1072 | { 1073 | return [ 1074 | PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::GET_METHOD, 1075 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesSubaccount::root() . '/{id}', 1076 | PaystackContractsRouteInterface::ARGS_KEY => ['id'] 1077 | ]; 1078 | } 1079 | 1080 | public static function getList() 1081 | { 1082 | return [ 1083 | PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::GET_METHOD, 1084 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesSubaccount::root() 1085 | ]; 1086 | } 1087 | 1088 | public static function update() 1089 | { 1090 | return [ 1091 | PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::PUT_METHOD, 1092 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesSubaccount::root() . '/{id}', 1093 | PaystackContractsRouteInterface::PARAMS_KEY => [ 1094 | 'business_name', 'settlement_bank', 1095 | 'account_number','percentage_charge', 1096 | 'primary_contact_email','primary_contact_name', 1097 | 'primary_contact_phone', 1098 | 'metadata','settlement_schedule' 1099 | ], 1100 | PaystackContractsRouteInterface::ARGS_KEY => ['id'] 1101 | ]; 1102 | } 1103 | } 1104 | 1105 | 1106 | class PaystackRoutesSubscription implements PaystackContractsRouteInterface 1107 | { 1108 | 1109 | public static function root() 1110 | { 1111 | return '/subscription'; 1112 | } 1113 | 1114 | public static function create() 1115 | { 1116 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::POST_METHOD, 1117 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesSubscription::root(), 1118 | PaystackContractsRouteInterface::PARAMS_KEY => [ 1119 | 'customer', 1120 | 'plan', 1121 | 'authorization' ] 1122 | ]; 1123 | } 1124 | 1125 | public static function fetch() 1126 | { 1127 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::GET_METHOD, 1128 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesSubscription::root() . '/{id}', 1129 | PaystackContractsRouteInterface::ARGS_KEY => ['id' ] ]; 1130 | } 1131 | 1132 | public static function getList() 1133 | { 1134 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::GET_METHOD, 1135 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesSubscription::root() ]; 1136 | } 1137 | 1138 | public static function disable() 1139 | { 1140 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::POST_METHOD, 1141 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesSubscription::root(). '/disable', 1142 | PaystackContractsRouteInterface::PARAMS_KEY => [ 1143 | 'code', 1144 | 'token'] ]; 1145 | } 1146 | 1147 | public static function enable() 1148 | { 1149 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::POST_METHOD, 1150 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesSubscription::root() . '/enable', 1151 | PaystackContractsRouteInterface::PARAMS_KEY => [ 1152 | 'code', 1153 | 'token'] ]; 1154 | } 1155 | } 1156 | 1157 | 1158 | class PaystackRoutesTransaction implements PaystackContractsRouteInterface 1159 | { 1160 | 1161 | public static function root() 1162 | { 1163 | return '/transaction'; 1164 | } 1165 | 1166 | public static function initialize() 1167 | { 1168 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::POST_METHOD, 1169 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesTransaction::root() . '/initialize', 1170 | PaystackContractsRouteInterface::PARAMS_KEY => ['reference', 1171 | 'callback_url', 1172 | 'amount', 1173 | 'email', 1174 | 'plan' ] 1175 | ]; 1176 | } 1177 | 1178 | public static function charge() 1179 | { 1180 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::POST_METHOD, 1181 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesTransaction::root() . '/charge_authorization', 1182 | PaystackContractsRouteInterface::PARAMS_KEY => ['reference', 1183 | 'authorization_code', 1184 | 'email', 1185 | 'amount' ] ]; 1186 | } 1187 | 1188 | public static function chargeToken() 1189 | { 1190 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::POST_METHOD, 1191 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesTransaction::root() . '/charge_token', 1192 | PaystackContractsRouteInterface::PARAMS_KEY => ['reference', 1193 | 'token', 1194 | 'email', 1195 | 'amount' ] ]; 1196 | } 1197 | 1198 | public static function fetch() 1199 | { 1200 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::GET_METHOD, 1201 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesTransaction::root() . '/{id}', 1202 | PaystackContractsRouteInterface::ARGS_KEY => ['id' ] ]; 1203 | } 1204 | 1205 | public static function getList() 1206 | { 1207 | return [ PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::GET_METHOD, 1208 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesTransaction::root() ]; 1209 | } 1210 | 1211 | public static function export() 1212 | { 1213 | return [ PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::GET_METHOD, 1214 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesTransaction::root() . '/export', 1215 | PaystackContractsRouteInterface::PARAMS_KEY => ['from', 1216 | 'to', 1217 | 'settled', 1218 | 'payment_page' ] ]; 1219 | } 1220 | 1221 | public static function totals() 1222 | { 1223 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::GET_METHOD, 1224 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesTransaction::root() . '/totals' ]; 1225 | } 1226 | 1227 | public static function verify() 1228 | { 1229 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::GET_METHOD, 1230 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesTransaction::root() . '/verify/{reference}', 1231 | PaystackContractsRouteInterface::ARGS_KEY => ['reference' ] ]; 1232 | } 1233 | 1234 | public static function verifyAccessCode() 1235 | { 1236 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::GET_METHOD, 1237 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesTransaction::root() . '/verify_access_code/{access_code}', 1238 | PaystackContractsRouteInterface::ARGS_KEY => ['access_code' ] ]; 1239 | } 1240 | } 1241 | 1242 | 1243 | class PaystackRoutesTransfer implements PaystackContractsRouteInterface 1244 | { 1245 | 1246 | public static function root() 1247 | { 1248 | return '/transfer'; 1249 | } 1250 | 1251 | public static function initiate() 1252 | { 1253 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::POST_METHOD, 1254 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesTransfer::root(), 1255 | PaystackContractsRouteInterface::PARAMS_KEY => ['source', 1256 | 'amount', 1257 | 'currency', 1258 | 'reason', 1259 | 'recipient' ] 1260 | ]; 1261 | } 1262 | 1263 | public static function finalizeTransfer() 1264 | { 1265 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::POST_METHOD, 1266 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesTransfer::root() . '/finalize_transfer', 1267 | PaystackContractsRouteInterface::PARAMS_KEY => ['reference', 1268 | 'transfer_code', 1269 | 'otp' ] ]; 1270 | } 1271 | 1272 | public static function resendOtp() 1273 | { 1274 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::POST_METHOD, 1275 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesTransfer::root() . '/resend_otp', 1276 | PaystackContractsRouteInterface::PARAMS_KEY => ['transfer_code', 1277 | 'reason'] ]; 1278 | } 1279 | 1280 | public static function disableOtp() 1281 | { 1282 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::POST_METHOD, 1283 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesTransfer::root() . '/disable_otp' ]; 1284 | } 1285 | 1286 | public static function enableOtp() 1287 | { 1288 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::POST_METHOD, 1289 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesTransfer::root() . '/enable_otp' ]; 1290 | } 1291 | 1292 | public static function disableOtpFinalize() 1293 | { 1294 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::POST_METHOD, 1295 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesTransfer::root() . '/disable_otp_finalize', 1296 | PaystackContractsRouteInterface::PARAMS_KEY => ['otp'] ]; 1297 | } 1298 | 1299 | public static function fetch() 1300 | { 1301 | return [PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::GET_METHOD, 1302 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesTransfer::root() . '/{id}', 1303 | PaystackContractsRouteInterface::ARGS_KEY => ['id' ] ]; 1304 | } 1305 | 1306 | public static function getList() 1307 | { 1308 | return [ PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::GET_METHOD, 1309 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesTransfer::root() ]; 1310 | } 1311 | } 1312 | 1313 | 1314 | class PaystackRoutesTransferrecipient implements PaystackContractsRouteInterface 1315 | { 1316 | 1317 | public static function root() 1318 | { 1319 | return '/transferrecipient'; 1320 | } 1321 | 1322 | public static function create() 1323 | { 1324 | return [ 1325 | PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::POST_METHOD, 1326 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesTransferrecipient::root(), 1327 | PaystackContractsRouteInterface::PARAMS_KEY => ['type', 1328 | 'name', 1329 | 'description', 1330 | 'metadata', 1331 | 'bank_code', 1332 | 'currency', 1333 | 'account_number' ], 1334 | PaystackContractsRouteInterface::REQUIRED_KEY => [ 1335 | PaystackContractsRouteInterface::PARAMS_KEY => ['type', 1336 | 'name', 1337 | 'bank_code', 1338 | 'account_number' ] 1339 | ] 1340 | ]; 1341 | } 1342 | 1343 | public static function getList() 1344 | { 1345 | return [ 1346 | PaystackContractsRouteInterface::METHOD_KEY => PaystackContractsRouteInterface::GET_METHOD, 1347 | PaystackContractsRouteInterface::ENDPOINT_KEY => PaystackRoutesTransferrecipient::root(), 1348 | PaystackContractsRouteInterface::PARAMS_KEY => ['perPage', 1349 | 'page' ] 1350 | ]; 1351 | } 1352 | } 1353 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ==================== 2 | # PHP Paystack Class 3 | A class to make Paystack API calls by including a single file or from within codeigniter. 4 | 5 | ## Requirements 6 | - Curl 7.34.0 or more recent 7 | - PHP 5.4.0 or more recent 8 | - OpenSSL v1.0.1 or more recent 9 | 10 | ##Get it 11 | Download [Paystack.php](Paystack.php) 12 | 13 | ##Usage - Direct use 14 | The main idea here is to be as simple as possible, basically you just instantiate the library and execute 15 | any of the methods in it, all the public API methods available for the moment are implemented. 16 | 17 | ```php 18 | // Require the paystack class 19 | require_once 'Paystack.php'; 20 | 21 | // Configuration options 22 | $config['paystack_key_test_secret'] = 'sk_test_xxxx'; 23 | $config['paystack_key_live_secret'] = 'sk_live_xxxx'; 24 | $config['paystack_test_mode'] = TRUE; // set to false when you are ready to go live 25 | 26 | // Create the library object 27 | $paystack = new Paystack( $config ); 28 | 29 | // Run the required operations 30 | $response = $paystack->customer_list(); 31 | $response = $paystack->customer->list(['perPage'=>5,'page'=>2]); 32 | // list the second page at 5 customers per page 33 | 34 | $response = $paystack->customer->create([ 35 | 'first_name'=>'Dafe', 36 | 'last_name'=>'Aba', 37 | 'email'=>"dafe@aba.c", 38 | 'phone'=>'08012345678' 39 | ]); 40 | $response = $paystack->transaction->initialize([ 41 | 'reference'=>'unique_refencecode', 42 | 'amount'=>'120000', 43 | 'email'=>'dafe@aba.c' 44 | ]); 45 | $response = $paystack->transaction->verify([ 46 | 'reference'=>'refencecode' 47 | ]); 48 | ``` 49 | 50 | That's it! Have fun. 51 | 52 | ##Usage - Codeigniter 53 | Paste the file as your {APPLICATION}/libraries/Paystack.php 54 | 55 | This library is completely functional as standalone but is developed as a Codeigniter library, 56 | to use it that way you simply create a config file in: {APPLICATION}/config/paystack.php to store the config array. 57 | 58 | Remember to uncomment the CodeIgniter access check before using. 59 | 60 | A sample config file is here: [config/paystack.php](config/paystack.php) 61 | 62 | ```php 63 | // Create the library object 64 | $this->load->library( 'paystack' ); 65 | 66 | // Run the required operations 67 | $response = $this->paystack->customer_list(); 68 | // list the second page at 5 customers per page 69 | $response = $this->paystack->transaction->initialize([ 70 | 'reference'=>'unique_refencecode', 71 | 'amount'=>'120000', 72 | 'email'=>'dafe@aba.c' 73 | ]); 74 | $response = $this->paystack->transaction->verify([ 75 | 'reference'=>'refencecode' 76 | ]); 77 | ``` 78 | 79 | --------- 80 | ##Samples 81 | Check [SAMPLES](SAMPLES.md) for more sample API calls -------------------------------------------------------------------------------- /SAMPLES.md: -------------------------------------------------------------------------------- 1 | #Sample calls 2 | 3 | Assumes that you already copied and configured the Paystack class. And that you have created and 4 | configured the $paystack object as you want. Check [README](README.md) for details. 5 | 6 | ``` php 7 | // Make a call to the resource/method 8 | // $this->paystack->{resource}->{method}(); 9 | // for gets, use $this->paystack->{resource}(id) 10 | 11 | // customer 12 | $this->paystack->customer(12); 13 | $this->paystack->customer->list(); 14 | $this->paystack->customer->create([ 15 | 'first_name'=>'name', 16 | 'last_name'=>'name', 17 | 'email'=>'email', 18 | 'phone'=>'phone' 19 | ]); 20 | $this->paystack->customer->update([ 21 | 'id'=>233, 22 | 'first_name'=>'name', 23 | 'last_name'=>'name', 24 | 'email'=>'email', 25 | 'phone'=>'phone' 26 | ]); 27 | $this->paystack->customer->list(['perPage'=>5,'page'=>2]); // list the second page at 5 customers per page 28 | 29 | // plan 30 | $this->paystack->plan(12); 31 | $this->paystack->plan->list(); 32 | $this->paystack->plan->create([ 33 | 'name'=>'name', 34 | 'description'=>'Describe at length', 35 | 'amount'=>1000, // in kobo 36 | 'interval'=>7, 37 | 'send_invoices'=>true, 38 | 'send_sms'=>true, 39 | 'hosted_page'=>'url', 40 | 'hosted_page_url'=>'url', 41 | 'hosted_page_summary'=>'details', 42 | 'currency'=>'NGN' 43 | ]); 44 | $this->paystack->plan->update([ 45 | 'name'=>'name', 46 | 'description'=>'Describe at length', 47 | 'amount'=>1000, // in kobo 48 | 'interval'=>7, 49 | 'send_invoices'=>true, 50 | 'send_sms'=>true, 51 | 'hosted_page'=>'url', 52 | 'hosted_page_url'=>'url', 53 | 'hosted_page_summary'=>'details', 54 | 'currency'=>'NGN' 55 | ],['id'=>233]); 56 | $this->paystack->plan->list(['perPage'=>5,'page'=>2]); // list the second page at 5 plans per page 57 | 58 | // transaction 59 | $this->paystack->transaction(12); 60 | $this->paystack->transaction->list(); 61 | $this->paystack->transaction->initialize([ 62 | 'reference'=>'unique', 63 | 'amount'=>19000, // in kobo 64 | 'email'=>'e@ma.il', 65 | 'plan'=>1 // optional, don't include unless it has a value 66 | ]); 67 | $this->paystack->transaction->charge([ 68 | 'reference'=>'unique', 69 | 'authorization_code'=>'auth_code', 70 | 'email'=>'e@ma.il', 71 | 'amount'=>1000 // in kobo 72 | ]); 73 | $this->paystack->transaction->chargeToken([ 74 | 'reference'=>'unique', 75 | 'token'=>'pstk_token', 76 | 'email'=>'e@ma.il', 77 | 'amount'=>1000 // in kobo 78 | ]); 79 | $this->paystack->transaction->list(['perPage'=>5,'page'=>2]); // list the second page at 5 transactions per page 80 | 81 | $this->paystack->transaction->verify([ 82 | 'reference'=>'unique_refencecode' 83 | ]); 84 | $this->paystack->transaction->totals(); 85 | 86 | 87 | ``` -------------------------------------------------------------------------------- /config/paystack.php: -------------------------------------------------------------------------------- 1 |