Paystack PHP Library: Process payments in Africa using the Paystack API

Recommend this page to a friend!
  Info   Documentation   View files (121)   Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2020-01-04 (7 months ago) Not enough user ratingsTotal: 30 All time: 9,944 This week: 241
Version License PHP version Categories
paystack-mirror 1.0Custom (specified...5PHP 5, E-Commerce, Web services
Description Author

This package can process payments in Africa using the Paystack API.

It provides a set of classes that can send HTTP requests to the PayStack API to perform several types of operations with payments using the Paystack API.

Currently it can call the Paystack API to perform operations supporting multiple accounts related with:

- Bulk charges
- Charges
- Control panel
- Customers
- Invoices
- Miscellaneous
- Pages
- Plans
- Refunds
- Settlements
- Sub-accounts
- Subscriptions
- Transactions
- Transfer recipients
- Transfers
- Transfers control
- Verifications

Picture of Josiah Ovye Yahaya
  Performance   Level  
Name: Josiah Ovye Yahaya is available for providing paid consulting. Contact Josiah Ovye Yahaya .
Classes: 6 packages by
Country: Nigeria Nigeria
Age: 30
All time rank: 259613 in Nigeria Nigeria
Week rank: 61 5 in Nigeria Nigeria
Innovation award
Innovation award
Nominee: 1x

Details

<img src="https://github.com/coderatio/paystack-mirror/blob/master/docs/logo.png" width=200/>

The missing Paystack PHP Library. ___

Overview

Paystack Mirror is a clean, simple and fluent php library for paystack payment gateway. This library is birthed out of the fact that i needed something flexible than what is in existance for myself and the php community.

What was wrong with the existing one?

The official paystack library for php is the yabacon/paystack-php on github. It's cool. Supports almost all paystack end-points and some other cool features like a dedicated Fee class, MetaDataBuilder class and Event handler class. Many people have been using the library but i wanted something that can give people freedom over what they do. I wanted something that supports multiple accounts at once. It's why i decided to work on this library.

What is unique about the library?

  1. The conversion of paystack end-points to full fletch extendable php classes.
  2. Supports for multiple accounts
  3. Clean and better event handler class
  4. Fluent Params Builder class
  5. Nigerian money conversion e.g 1k => 1,000 naira and 1,000 naira => 100000 kobo and even (millions(xM), billions(xB), trillions(xT)) to kobo e.t.c.

Server Requirements

> PHP version ^7.1.3 requirement was done intentionally. Reason been that it's safer, better and faster than 5.6 and 7.0. So, if you have been using php version less than that, kindly upgrade before using this library. * php ^7.1.3 * cURL extension enabled * OpenSSL extension enabled

> This library has suppports for all Paystack end-points which we refers to as Actions. _Let's take a look at the available actions._

List of actions groups

Below are actions groups supported by the library in alphabetical order.

  1. BULK CHARGES
  2. CHARGES
  3. CONTROL PANEL
  4. CUSTOMERS
  5. INVOICES
  6. MISCELLANEOUS
  7. PAGES
  8. PLANS
  9. REFUNDS
  10. SETTLEMENTS
  11. SUB-ACCOUNTS
  12. SUBSCRIPTIONS
  13. TRANSACTIONS
  14. TRANSFER RECIPIENTS
  15. TRANSFERS
  16. TRANSFERS CONTROL
  17. VERIFICATIONS

>Note: To see the list of all actions under each action groups available on this library, kindly Click Here.

> Arrangements are on the way to list all of them like the first two above.

Installation

composer require coderatio/paystack-mirror

Usage

With Single Paystack Account

_Method One_


require 'venodr/autoload.php';

use Coderatio\PaystackMirror\PaystackMirror;
use Coderatio\PaystackMirror\Actions\Transactions\ListTransactions;

$queryParams = new ParamsBuilder();
$queryParams->perPage = 10;

$result = PaystackMirror::run($secretKey, new ListTransactions($queryParams));

echo $result->getResponse();

_Method Two_


require 'venodr/autoload.php';

use Coderatio\PaystackMirror\PaystackMirror;
use Coderatio\PaystackMirror\Actions\Transactions\ListTransactions;

$queryParams = new ParamsBuilder();
$queryParams->perPage = 10;

$result = PaystackMirror::run($secretKey, ListTransactions::class, $queryParams);

echo $result->getResponse();

_Method Three_


require 'venodr/autoload.php';

use Coderatio\PaystackMirror\PaystackMirror;
use Coderatio\PaystackMirror\Actions\Transactions\ListTransactions;

$queryParams = new ParamsBuilder();
$queryParams->perPage = 10;

$result = PaystackMirror::setKey($secretKey)->mirror(new ListTransactions($queryParams));

echo $result->getResponse();

_Method Four_


require 'venodr/autoload.php';

use Coderatio\PaystackMirror\PaystackMirror;
use Coderatio\PaystackMirror\Actions\Transactions\ListTransactions;

$queryParams = new ParamsBuilder();
$queryParams->perPage = 10;

$result = PaystackMirror::setKey($secretKey)->mirror(ListTransactions::class, $queryParams);

echo $result->getResponse();

>Notice: By default, ->getResponse() returns a json object. But, you can chain ->asArray() to convert the response to php array or ->asObject() to conver the response to php object at runtime.

With Multiple Paystack Accounts

With this library, you can mirror single action on multiple paystack accounts. This is super cool for multitenants applications or firms with multiple paystack accounts.

Let's see how to do it.


// Let's use ParamsBuilder to build our data to be sent to paystack.

// First account
$firstAccountParams = new ParamsBuilder();
$firstAccountParams->first_name = 'Josiah';
$firstAccountParams->last_name = 'Yahaya';
$firstAccountParams->email = 'example1@email.com';
$firstAccountParams->amount = short_naira_to_kobo('25.5k');
$firstAccountParams->reference = PaystackMirror::generateReference();

$firstAccount = new ParamsBuilder();
$firstAccount->key = $firstAccountKey;
$firstAccount->data = $firstAccountParams;

// Second account
$secondAccountParams = new ParamsBuilder();
$secondAccountParams->first_name = 'Ovye';
$secondAccountParams->last_name = 'Yahaya';
$secondAccountParams->email = 'example2@email.com';
$secondAccountParams->amount = short_naira_to_kobo('10k');
$secondAccountParams->reference = PaystackMirror::generateReference();

$secondAccount = new ParamsBuilder();
$secondAccount->key = $secondAccountKey;
$secondAccount->data = $firstAccountParams;

$results = PaystackMirror::setAccounts([$firstAccount, $secondAccount])
    ->mirrorMultipleAccountsOn(new InitializeTransaction());
    
// OR

$results = PaystackMirror::setAccounts([$firstAccount, $secondAccount])
    ->mirrorMultipleAccountsOn(InitializeTransaction::class);

foreach ($results as $result) {
    // Do something with $result.
    
   ...
   
   // The $result variable holds two main object properties; 
   // $result->account which holds an account key and $result->response which holds the response for an account. 
   // The best thing to do is to dump the $result variable to see what's contain there in.
}

You can overwrite all the accounts data by providing your params on the action. When that is done, the library will use the parameters supplied on the action for all the accounts instead e.g


    $actionParams = new ParamsBuilder();
    $actionParams->email = 'johndoe@email.com';
    $actionParams->amount = naira_to_kobo('1,000');
    $actionParams->reference = PaystackMirror::generateReference();

    $results = PaystackMirror::setAccounts([$firstAccount, $secondAccount])
        ->mirrorMultipleAccountsOn(new InitializeTransaction($actionParams));
        
    // OR
    
    $results = PaystackMirror::setAccounts([$firstAccount, $secondAccount])
            ->mirrorMultipleAccountsOn(InitializeTransaction::class, $actionParams);
        

>Quick Note: All the query or body params used on paystack api documentation site are all available in this library. The only different is, they must be sent as an array or as ParamBuilder::class object.

Create your action

One good thing about this library is the ability to plug and play actions. You can replace existing actions by creating yours.

<?php

use \Coderatio\PaystackMirror\Actions\Action;
use Coderatio\PaystackMirror\Services\CurlService;

class MyCustomAction extends Action
{
    // The paystack endpoint for this action
    protected $url = '';
    
    public function handle(CurlService $curlService) : void 
    {
        // Use the $curlService to handle this action's request.
        // E.g to send a post request, see below:
        
        $curlService->post($this->url, $this->getData());
    }
}

>Please note that $this->data property returns an array. If you want to send parameters as json to paystack, use $this->getData().

Webhook Event Handling

This library ships with a fluent Event handling class to be used at your webhook url set on your paystack dashboard. See example below on how to listen to different events.

<?php

use Coderatio\PaystackMirror\Events\Event;

// $secretKeys array structure should be like this:
$secretKeys = [
    'test' => 'sk_testxxxxxxxxxxxxx',
    'live' => 'sk_live_xxxxxxxxxxxxxx'
];

$eventData = Event::capture()->thenValidate(string $secretKey or array $secretKeys)
    ->thenListenOn('subscription.create')->thenGetData();

// Do something with the $eventData

Create a separate event class

With this library, you can write a separate event class for a single event. For example, the subscription.create event can be created like this:

<?php

namespace Coderatio\PaystackMirror\Events;

class SubscriptionCreated implements ActionEvent
{
    public static function validate($keys): Event
    {
        return Event::capture()->thenValidate($keys)
            ->thenListenOn('subscription.create');
    }
}

This can then be used like this:

<?php

use Coderatio\PaystackMirror\Events\SubscriptionCreated;

$eventData = SubscriptionCreated::validate($key)->thenGetData();

// Or 

$event = SubscriptionCreated::validate($key)->thenGetEvent();

Here, you can see that we are just implementing the ActionEvent::class interface and extending the Event::class on the ::validate() method.

Addons

The library has a few of in-built functionalities to do somethings quicker and better. Let's take a look at them:

1. Nairas to kobo

Since paystack accepts amount in kobo only, there should be a quick way to do that. Below are helper functions to help you out.

<?php

// Normal naira amount to kobo
$amount = naira_to_kobo('1000'); // Returns: 100000

// Naira with commas
$amount = naira_to_kobo('1,000'); // Returns: 100000

// Human readable nairas to kobo
$amount = short_naira_to_kobo('2k'); // Returns: 200000

$amount = short_naira_to_kobo('1.5m'); // Returns: 150000000

>Note: The short_naira_to_kobo() helper function, supports only k as thousands, m as millions, b as billions and t as trillions notations.

2. Reference Generator

You can easily generate especially, transaction reference easily by doing this:

<?php

use \Coderatio\PaystackMirror\PaystackMirror;

$reference = PaystackMirror::generateReference();

Todo

  1. Build a dedicated docs site

Tests

composer test

// OR

./vendor/bin/phpunit

Contributions

Correcting a typographical error is a huge a contribution to this project. Do well to do that. You can fork the repo and send pull request or reach out easily to me via twitter here => Josiah Ovye Yahaya.

Collaborators

  1. Josiah O. Yahaya

Licence

This project is built and used with GPL.3.0 licence.

  Files  
File Role Description
.idea (8 files, 1 directory)
docs (1 file, 1 directory)
src (2 files, 5 directories)
composer.json Data Auxiliary data
composer.lock Data Auxiliary data
LICENSE Lic. License text
phpunit.xml Data Auxiliary data
README.md Doc. Read me

  Files  /  .idea  
File Role Description
inspectionProfiles (1 file)
   composerJson.xml Data Auxiliary data
   misc.xml Data Auxiliary data
   modules.xml Data Auxiliary data
   paystack-mirror.iml Data Auxiliary data
   php.xml Data Auxiliary data
   symfony2.xml Data Auxiliary data
   vcs.xml Data Auxiliary data
   workspace.xml Data Auxiliary data

  Files  /  .idea  /  inspectionProfiles  
File Role Description
   Project_Default.xml Data Auxiliary data

  Files  /  docs  
File Role Description
pages (2 files)
   logo.png Data Auxiliary data

  Files  /  docs  /  pages  
File Role Description
   bulk-charges.md Data Auxiliary data
   charges.md Data Auxiliary data

  Files  /  src  
File Role Description
Actions (1 file, 17 directories)
Events (4 files)
Exceptions (1 file)
Services (5 files)
Traits (10 files)
   helpers.php Aux. Helpers
   PaystackMirror.php Class Class source

  Files  /  src  /  Actions  
File Role Description
BulkCharges (6 files)
Charges (7 files)
ControlPanel (2 files)
Customers (6 files)
Invoices (10 files)
Miscellaneous (2 files)
Pages (5 files)
Plans (4 files)
Refunds (3 files)
Settlements (1 file)
SubAccounts (4 files)
Subscriptions (5 files)
Transactions (10 files)
TransferRecipients (4 files)
Transfers (4 files)
TransfersControl (4 files)
Verifications (4 files)
   Action.php Class Class source

  Files  /  src  /  Actions  /  BulkCharges  
File Role Description
   FetchBulkChargeBatch.php Class Class source
   FetchChargesBatch.php Class Class source
   InitializeBulkCharge.php Class Class source
   ListBulkChargeBatches.php Class Class source
   PauseBulkChargeBatch.php Class Class source
   ResumeBulkChargeBatch.php Class Class source

  Files  /  src  /  Actions  /  Charges  
File Role Description
   CheckPendingCharge.php Class Class source
   InitializeCharge.php Class Class source
   SubmitBirthdayToCharge.php Class Class source
   SubmitOtpToCharge.php Class Class source
   SubmitPhoneToCharge.php Class Class source
   SubmitPinToCharge.php Class Class source
   TokenizePaymentInstrument.php Class Class source

  Files  /  src  /  Actions  /  ControlPanel  
File Role Description
   FetchPaymentSessionTimeout.php Class Class source
   UpdatePaymentSessionTimeout.php Class Class source

  Files  /  src  /  Actions  /  Customers  
File Role Description
   CreateCustomer.php Class Class source
   DeactivateCustomerAuthorization.php Class Class source
   FetchCustomer.php Class Class source
   ListCustomers.php Class Class source
   UpdateCustomer.php Class Class source
   WhiteOrBlackListCustomer.php Class Class source

  Files  /  src  /  Actions  /  Invoices  
File Role Description
   ArchiveInvoice.php Class Class source
   CreateInvoice.php Class Class source
   FinalizeInvoiceDraft.php Class Class source
   InvoiceTotals.php Class Class source
   ListInvoices.php Class Class source
   MarkAsPaidInvoice.php Class Class source
   SendInvoiceNotification.php Class Class source
   UpdateInvoice.php Class Class source
   VerifyInvoice.php Class Class source
   ViewInvoice.php Class Class source

  Files  /  src  /  Actions  /  Miscellaneous  
File Role Description
   CheckBalance.php Class Class source
   ListBanks.php Class Class source

  Files  /  src  /  Actions  /  Pages  
File Role Description
   CheckAvailablePageSlug.php Class Class source
   CreatePage.php Class Class source
   FetchPage.php Class Class source
   ListPages.php Class Class source
   UpdatePage.php Class Class source

  Files  /  src  /  Actions  /  Plans  
File Role Description
   CreatePlan.php Class Class source
   FetchPlan.php Class Class source
   ListPlans.php Class Class source
   UpdatePlan.php Class Class source

  Files  /  src  /  Actions  /  Refunds  
File Role Description
   CreateRefund.php Class Class source
   FetchRefund.php Class Class source
   ListRefunds.php Class Class source

  Files  /  src  /  Actions  /  Settlements  
File Role Description
   FetchSettlements.php Class Class source

  Files  /  src  /  Actions  /  SubAccounts  
File Role Description
   CreateSubAccount.php Class Class source
   FetchSubAccount.php Class Class source
   ListSubAccounts.php Class Class source
   UpdateSubAccount.php Class Class source

  Files  /  src  /  Actions  /  Subscriptions  
File Role Description
   CreateSubscription.php Class Class source
   DisableSubscription.php Class Class source
   EnableSubscription.php Class Class source
   FetchSubscription.php Class Class source
   ListSubscriptions.php Class Class source

  Files  /  src  /  Actions  /  Transactions  
File Role Description
   ChargeAuthorization.php Class Class source
   CheckAuthorization.php Class Class source
   DeactivateAuthorization.php Class Class source
   ExportTransactions.php Class Class source
   FetchTransaction.php Class Class source
   InitializeTransaction.php Class Class source
   ListTransactions.php Class Class source
   TransactionTotals.php Class Class source
   VerifyTransaction.php Class Class source
   ViewTransactionTimeline.php Class Class source

  Files  /  src  /  Actions  /  TransferRecipients  
File Role Description
   CreateTransferRecipient.php Class Class source
   DeleteTransferRecipient.php Class Class source
   ListTransferRecipients.php Class Class source
   UpdateTransferRecipient.php Class Class source

  Files  /  src  /  Actions  /  Transfers  
File Role Description
   FetchTransfer.php Class Class source
   FinalizeTransfer.php Class Class source
   InitializeTransfer.php Class Class source
   InitiateBulkTransfer.php Class Class source

  Files  /  src  /  Actions  /  TransfersControl  
File Role Description
   DisableTransferOtp.php Class Class source
   EnableTransferOtp.php Class Class source
   FinalizeDisableTransferOtp.php Class Class source
   ResendTransferOtp.php Class Class source

  Files  /  src  /  Actions  /  Verifications  
File Role Description
   ResolveAccountNumber.php Class Class source
   ResolveBvn.php Class Class source
   ResolveCardBin.php Class Class source
   ResolvePhoneNumber.php Class Class source

  Files  /  src  /  Events  
File Role Description
   ActionEvent.php Class Class source
   Event.php Class Class source
   EventInterface.php Class Class source
   SubscriptionCreated.php Class Class source

  Files  /  src  /  Exceptions  
File Role Description
   PaystackMirrorException.php Class Class source

  Files  /  src  /  Services  
File Role Description
   CurlCacheService.php Class Class source
   CurlHttpResponseService.php Class Class source
   CurlService.php Class Class source
   ParamsBuilder.php Class Class source
   RequestHandlerService.php Class Class source

  Files  /  src  /  Traits  
File Role Description
   HasBulkChargeBatchIdOrCode.php Class Class source
   HasBvn.php Class Class source
   HasCardBin.php Class Class source
   HasIdOrCustomerCode.php Class Class source
   HasIdOrPlanCode.php Class Class source
   HasIdOrReference.php Class Class source
   HasIdOrSlug.php Class Class source
   HasIdOrSubscriptionCode.php Class Class source
   HasInvoiceIdOrCode.php Class Class source
   HasRecipientCodeOrId.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:30
This week:0
All time:9,944
This week:241

For more information send a message to info at phpclasses dot org.