PHP code example of smladeoye / yii2-paystack

1. Go to this page and download the library: Download smladeoye/yii2-paystack 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/ */

    

smladeoye / yii2-paystack example snippets


'components'=>[
    //  ...
    'Paystack' => [
        'class' => 'smladeoye\paystack\Paystack',
    	'environment' => 'test',
    	'testPublicKey'=>'pk_test_365589cc6b0e95f1fb53fa0eeaef6b1819f1b0f2',
    	'testSecretKey'=>'sk_test_1a4a88eaec6f4f3b23771edb2c60fe8d8b95cbe',
    	'livePublicKey'=>'',
    	'liveSecretKey'=>'',
    ],
    //  ...
]


// Initializing a payment transaction

$paystack = Yii::$app->Paystack;

$transaction = $paystack->transaction();
$transaction->initialize(['email'=>'[email protected]','amount'=>'100000','currency'=>'NGN']);

// check if an error occured during the operation
if (!$transaction->hasError)
{
    //response property for response gotten for any operation
    $response = $transaction->getResponse()

    // redirect the user to the payment page gotten from the initialization
    $transaction->redirect();
}
else
{
    // display message
    echo $transaction->message;

    // get all the errors information regarding the operation from paystack
    $error = $transaction->getError();
}


  $paystack = Yii::$app->Paystack;
  $customer = $paystack->customer();
  

        $customer->whitelist($customer_id);
  

        $customer->blacklist($customer_id);
  

  $paystack = Yii::$app->Paystack;
  $transaction = $paystack->transaction();
  

  $transaction->initialize(['email'=>'[email protected]','amount'=>'10000']);
  if (!$transaction->hasError)
        $transaction->redirect();
  

        $transaction->verify($trans_reference);
  

  $transaction->charge($options = []);
  

  $transaction->timeline($trx_id);
  

   $transaction->total($from,$to);
   //An array could be provided instead with the available parameters in key => value format.
  

     $transaction->export($options = []);

    //get download link url
    $transaction->getExportUrl();
  

        $transaction->download();
  

  $paystack = Yii::$app->Paystack;
  $subscription = $paystack->subscription();
  

      $subscription->enable($code, $token);
      //an array can be provided instead, containing the necessary parameters as key => value
  

      $subscription->disable($code, $token);
      //an array can be provided instead, containing the necessary parameters as key => value
  

  $paystack = Yii::$app->Paystack;
  $subaccount = $paystack->subaccount();
  

          $subaccount->enable($code, $token);
          //an array can be provided instead, containing the necessary parameters as key => value
  

  $paystack = Yii::$app->Paystack;
  $plan = $paystack->plan();
  

  $paystack = Yii::$app->Paystack;
  $page = $paystack->page();
  

          $page->checkAvailability($slud_id);
  

  $paystack = Yii::$app->Paystack;
  $settlement = $paystack->settlement();
  

  $settlement->fetchAll($from_date,$to_date);
  //an array can be provided instead, containing the necessary parameters as key => value
  

        $customer->fetchAll(['page'=>'','perPage'=>'']);

        $customer->create(['email'=>'[email protected]']);

        $customer->fetch($customer_id);

        $customer->update($id,$info = array();

'Paystack' => [
            'class' => 'smladeoye\paystack\Paystack',
            ...//other configurations just like example above

            //setting the event handler for all operations before any request is made
            'beforeSend'=>'myFunction',

            //will set the event handler for all operations after any request is made
            'afterSend'=>'myFunction',

            //setting the event handler for the transaction operation; this will overwrite the event handlers above
            'transaction'=>array(
                //handler for the event before any request is made for a transaction operation
                'beforeSend'=>'myFunction',

                //handler for the event after any request is made for a transaction operation
                'afterSend'=>'myFunction',
            )
        ]

use smladeoye\paystack\widget\PaystackWidget;

    echo PaystackWidget::widget(
        [
        //set the text to be displayed on the button
            'buttonText'=>'PAY',
        //array to set other button attributes like id, class,style etc
            'buttonOptions'=>array(
                'class'=>'btn btn-danger',
                'style'=>'width: 80px;',
            ),
        //array to set all necessary paystack inline payment options
        //some values can be set dynamically by passing the element id as value (email,amount,currency,quantity)
            'options'=>[
                //your paystack public key
                'key'=>Yii::$app->Paystack->testPublicKey,
                'email'=>'[email protected]',
                'ref'=>'123456789',
                //'amount' => '#amount' --> the value is gotten from the html element with id = amount
                // OR
                'amount'=>'2000',
                //'currency' => '#currency' --> the value is gotten from the html element with id = currency
                // OR
                'currency' =>'NGN',
                'plan' =>'my-plan',
                //'quantity' => '#quantity' --> the value is gotten from the html element with id = quantity
                // OR
                'quantity' =>'2',
                //callbackUrl can be set, where the tansaction reference would be passed as GET parameter
                'callbackUrl' =>'www.google.com',
                //also u can override the default with the callback option, simply provide javascript anonymous function as a string
                //'callback'=>'function(response){alert(response.trxref);};',
            ],
        ]
    );