PHP code example of coderatio / paystack-mirror

1. Go to this page and download the library: Download coderatio/paystack-mirror library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

coderatio / paystack-mirror example snippets




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();




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();




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();




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();



// 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 = '[email protected]';
$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 = '[email protected]';
$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.
}


    $actionParams = new ParamsBuilder();
    $actionParams->email = '[email protected]';
    $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);
        



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());
    }
}




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




namespace Coderatio\PaystackMirror\Events;

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




use Coderatio\PaystackMirror\Events\SubscriptionCreated;

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

// Or 

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




// 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




use \Coderatio\PaystackMirror\PaystackMirror;

$reference = PaystackMirror::generateReference();