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

Recommend this page to a friend!
  Info   View files Documentation   View files View files (121)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2020-01-04 (8 months ago) RSS 2.0 feedNot enough user ratingsTotal: 33 This week: 1All time: 9,978 This week: 267Up
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: 256112 in Nigeria Nigeria
Week rank: 68 Up5 in Nigeria Nigeria Up
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 folder image Files  
File Role Description
Files folder image.idea (8 files, 1 directory)
Files folder imagedocs (1 file, 1 directory)
Files folder imagesrc (2 files, 5 directories)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Read me

  Files folder image Files  /  .idea  
File Role Description
Files folder imageinspectionProfiles (1 file)
  Accessible without login Plain text file composerJson.xml Data Auxiliary data
  Accessible without login Plain text file misc.xml Data Auxiliary data
  Accessible without login Plain text file modules.xml Data Auxiliary data
  Accessible without login Plain text file paystack-mirror.iml Data Auxiliary data
  Accessible without login Plain text file php.xml Data Auxiliary data
  Accessible without login Plain text file symfony2.xml Data Auxiliary data
  Accessible without login Plain text file vcs.xml Data Auxiliary data
  Accessible without login Plain text file workspace.xml Data Auxiliary data

  Files folder image Files  /  .idea  /  inspectionProfiles  
File Role Description
  Accessible without login Plain text file Project_Default.xml Data Auxiliary data

  Files folder image Files  /  docs  
File Role Description
Files folder imagepages (2 files)
  Accessible without login Image file logo.png Data Auxiliary data

  Files folder image Files  /  docs  /  pages  
File Role Description
  Accessible without login Plain text file bulk-charges.md Data Auxiliary data
  Accessible without login Plain text file charges.md Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imageActions (1 file, 17 directories)
Files folder imageEvents (4 files)
Files folder imageExceptions (1 file)
Files folder imageServices (5 files)
Files folder imageTraits (10 files)
  Accessible without login Plain text file helpers.php Aux. Helpers
  Plain text file PaystackMirror.php Class Class source

  Files folder image Files  /  src  /  Actions  
File Role Description
Files folder imageBulkCharges (6 files)
Files folder imageCharges (7 files)
Files folder imageControlPanel (2 files)
Files folder imageCustomers (6 files)
Files folder imageInvoices (10 files)
Files folder imageMiscellaneous (2 files)
Files folder imagePages (5 files)
Files folder imagePlans (4 files)
Files folder imageRefunds (3 files)
Files folder imageSettlements (1 file)
Files folder imageSubAccounts (4 files)
Files folder imageSubscriptions (5 files)
Files folder imageTransactions (10 files)
Files folder imageTransferRecipients (4 files)
Files folder imageTransfers (4 files)
Files folder imageTransfersControl (4 files)
Files folder imageVerifications (4 files)
  Plain text file Action.php Class Class source

  Files folder image Files  /  src  /  Actions  /  BulkCharges  
File Role Description
  Plain text file FetchBulkChargeBatch.php Class Class source
  Plain text file FetchChargesBatch.php Class Class source
  Plain text file InitializeBulkCharge.php Class Class source
  Plain text file ListBulkChargeBatches.php Class Class source
  Plain text file PauseBulkChargeBatch.php Class Class source
  Plain text file ResumeBulkChargeBatch.php Class Class source

  Files folder image Files  /  src  /  Actions  /  Charges  
File Role Description
  Plain text file CheckPendingCharge.php Class Class source
  Plain text file InitializeCharge.php Class Class source
  Plain text file SubmitBirthdayToCharge.php Class Class source
  Plain text file SubmitOtpToCharge.php Class Class source
  Plain text file SubmitPhoneToCharge.php Class Class source
  Plain text file SubmitPinToCharge.php Class Class source
  Plain text file TokenizePaymentInstrument.php Class Class source

  Files folder image Files  /  src  /  Actions  /  ControlPanel  
File Role Description
  Plain text file FetchPaymentSessionTimeout.php Class Class source
  Plain text file UpdatePaymentSessionTimeout.php Class Class source

  Files folder image Files  /  src  /  Actions  /  Customers  
File Role Description
  Plain text file CreateCustomer.php Class Class source
  Plain text file DeactivateCustomerAuthorization.php Class Class source
  Plain text file FetchCustomer.php Class Class source
  Plain text file ListCustomers.php Class Class source
  Plain text file UpdateCustomer.php Class Class source
  Plain text file WhiteOrBlackListCustomer.php Class Class source

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

  Files folder image Files  /  src  /  Actions  /  Miscellaneous  
File Role Description
  Plain text file CheckBalance.php Class Class source
  Plain text file ListBanks.php Class Class source

  Files folder image Files  /  src  /  Actions  /  Pages  
File Role Description
  Plain text file CheckAvailablePageSlug.php Class Class source
  Plain text file CreatePage.php Class Class source
  Plain text file FetchPage.php Class Class source
  Plain text file ListPages.php Class Class source
  Plain text file UpdatePage.php Class Class source

  Files folder image Files  /  src  /  Actions  /  Plans  
File Role Description
  Plain text file CreatePlan.php Class Class source
  Plain text file FetchPlan.php Class Class source
  Plain text file ListPlans.php Class Class source
  Plain text file UpdatePlan.php Class Class source

  Files folder image Files  /  src  /  Actions  /  Refunds  
File Role Description
  Plain text file CreateRefund.php Class Class source
  Plain text file FetchRefund.php Class Class source
  Plain text file ListRefunds.php Class Class source

  Files folder image Files  /  src  /  Actions  /  Settlements  
File Role Description
  Plain text file FetchSettlements.php Class Class source

  Files folder image Files  /  src  /  Actions  /  SubAccounts  
File Role Description
  Plain text file CreateSubAccount.php Class Class source
  Plain text file FetchSubAccount.php Class Class source
  Plain text file ListSubAccounts.php Class Class source
  Plain text file UpdateSubAccount.php Class Class source

  Files folder image Files  /  src  /  Actions  /  Subscriptions  
File Role Description
  Plain text file CreateSubscription.php Class Class source
  Plain text file DisableSubscription.php Class Class source
  Plain text file EnableSubscription.php Class Class source
  Plain text file FetchSubscription.php Class Class source
  Plain text file ListSubscriptions.php Class Class source

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

  Files folder image Files  /  src  /  Actions  /  TransferRecipients  
File Role Description
  Plain text file CreateTransferRecipient.php Class Class source
  Plain text file DeleteTransferRecipient.php Class Class source
  Plain text file ListTransferRecipients.php Class Class source
  Plain text file UpdateTransferRecipient.php Class Class source

  Files folder image Files  /  src  /  Actions  /  Transfers  
File Role Description
  Plain text file FetchTransfer.php Class Class source
  Plain text file FinalizeTransfer.php Class Class source
  Plain text file InitializeTransfer.php Class Class source
  Plain text file InitiateBulkTransfer.php Class Class source

  Files folder image Files  /  src  /  Actions  /  TransfersControl  
File Role Description
  Plain text file DisableTransferOtp.php Class Class source
  Plain text file EnableTransferOtp.php Class Class source
  Plain text file FinalizeDisableTransferOtp.php Class Class source
  Plain text file ResendTransferOtp.php Class Class source

  Files folder image Files  /  src  /  Actions  /  Verifications  
File Role Description
  Plain text file ResolveAccountNumber.php Class Class source
  Plain text file ResolveBvn.php Class Class source
  Plain text file ResolveCardBin.php Class Class source
  Plain text file ResolvePhoneNumber.php Class Class source

  Files folder image Files  /  src  /  Events  
File Role Description
  Plain text file ActionEvent.php Class Class source
  Plain text file Event.php Class Class source
  Plain text file EventInterface.php Class Class source
  Plain text file SubscriptionCreated.php Class Class source

  Files folder image Files  /  src  /  Exceptions  
File Role Description
  Plain text file PaystackMirrorException.php Class Class source

  Files folder image Files  /  src  /  Services  
File Role Description
  Plain text file CurlCacheService.php Class Class source
  Plain text file CurlHttpResponseService.php Class Class source
  Plain text file CurlService.php Class Class source
  Plain text file ParamsBuilder.php Class Class source
  Plain text file RequestHandlerService.php Class Class source

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

 Version Control Unique User Downloads Download Rankings  
 100%
Total:33
This week:1
All time:9,978
This week:267Up

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