rebel/rebel-rebelpay

Paystack Laravel package

v1.3.0 2023-07-27 13:59 UTC

README

Latest Version on Packagist

Total Downloads

This is a Laravel composer package that simplifies accepting payments on Paystack.

Installation

You can install the package via composer:

composer require rebel/rebel-rebelpay

Run this command to publish the config file and all necessary files:

php artisan rebel-rebelpay:install

You can publish the config file with:

php artisan vendor:publish --tag="rebel-rebelpay-config"

This is the contents of the published config file:

return [
    'publickey' => env('PAYSTACK_PUBLIC_KEY'),

    'secretkey' => env('PAYSTACK_SECRET_KEY'),

    'paystackurl' => env('PAYSTACK_PAYMENT_URL'),

    'merchantmail' => env('MERCHANT_EMAIL'),
];

Features

  • Payments
  • Get all transactions records from Paystack
  • Get single transaction record from Paystack
  • Fetch all successful transaction Paystack
  • Fetch all failed transaction Paystack
  • Fetch all abandoned transaction Paystack
  • Export transactions in csv format
  • Fetch transactions history

Workflow of this package

  1. The customer is redirected to the payment provider's site.
  • After customer completes the checkout form on your website, feed the package with the necessary data and the customer will be redirect to paystack to complete payment.
  1. The customer arrives on paystack platform
  • After the customer is redirected to paystack, they can choose from the available payment options based on your account settings with paystack and complete the transaction.
  1. Customer is redirect to website
  • After the customer has completed the transaction on Paystack's website, they will be redirected back to a route that we have set up in our Laravel application instead of relying on Paystack's callback webhook.

Environment Variables

  • PAYSTACK_PUBLIC_KEY= insert your Paystack public key
  • PAYSTACK_SECRET_KEY= insert your Paystack secret key
  • PAYSTACK_PAYMENT_URL=https://api.paystack.co
  • MERCHANT_EMAIL= kwadejeffrey@gmail.com

Usage/Examples

$rebelPay = new Rebel\RebelPay();
echo $rebelPay->echoPhrase('Hello, Rebel!');
<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Rebel\RebelPay\Facades\RebelPay;

class Controller extends BaseController
{
    public function paystackCheckout(Request $request)
    {
        //Let's validate form data
        $validated_data = $request->validate([
            'first_name' => 'required', 
            'last_name' => 'required',
            'email' => 'required', 
            'amount' => 'required',
        ]);

        /**
         * Let's build our Paystack data
         * Always multiply amount by 100
         * First, Last and phone name are optional for direct payments
         * callback_url is optional since we can set a default callback on Paystacks' dashboard but if we set a value for it, it'll overwrite the dashboard default value.
         * I personally prefer setting a value in my code
         * You use any of your applications web routes' name as the callback_url
         */
        $data = [
            'email' => $validated_data->email,
            'amount' => $validated_data->amount * 100,
            'first_name' => $validated_data->first_name,
            'last_name' => $validated_data->last_name,
            'phone' => $validated_data->tel,
            'callback_url' => route('callback')
        ];

        /**
         * Let's call Rebelpay with the makePayment method
         * And let's inject the data array into the method
         * We'll be redirect to the paystack website to complete transaction
         * A json_decoded with these attributes (Status, Message and Data) is returned
         * Status has a value of "True"
         * Message has a value of "Verification successful"
         * The important keys to note in the data attribute is (status, reference, amount, authorization, customer, channel )
         * You can deploy your code is diverse ways using these attributes
         * You can clear the cart from DB or simply invalidate it depending on your DB structure
         */
        $res = RebelPay::makePayment($data);
        // You could do something like this
        if($res->data->status){
            Cart::where('user_id', $user_id)->delete();
        }
        //You could pass the status as a session
        return redirect('/')->with('message', $res->data->status);
        return view('rebelPay.pageOne');
    }

    /**
     * Create a customer on Paystack
     * 
     * @param array $data
     * 
     * @return string JSON-encoded string representing the customer information.
     */
    public function letsCreateACustomer()
    {
        $data = [
            "email" => "customer@email.com",
            "first_name" => "Zero",
            "last_name" => "Sumthing",
            "phone" => "+233500005737"
        ];

        $res = RebelPay::createCustomer($data);
        dd($res);
    }

    public function letsUpdateCustomer()
    {
        /**
     * Update customer profile
     *
     * @param array $data
     * @param string $id
     * @return string JSON-encoded string representing the customer information.
     */
        $res = RebelPay::updateCustomer(array $data, string $id)
    }

    /**
     * Get a specific customer's profile from Paystack
     *
     * @param  string  $identifier Can be either email or customer ID
     * @return string JSON-encoded string representing the customer information.
     */
    public function letsGetACustomer($identifier)
    {
        
        return $res = RebelPay::getClient($identifier);
    }

    /**
     * We'll use this function to return all our transactions from Paystack
     */
    public function getAllTransactionsFromPaystack()
    {
        /**
         * By default this we'll return a status, message, data and meta attributes
         * The data is what we need so pass it to the blade view or vue component
         * This method return the 100 transactions, you can alter the number
         * This method holds two arguments ($perPage = 100, $page = 1)
         * You can adjust them as you see fit 
         */
        $res = RebelPay::getAllTransactions();
        dd($res);
        return view('rebelPay.pageOne', [
            'transactions' => $res->data
        ]);
    }


    /**
     * We'll use this function to return all our Failed transactions from Paystack
     */
    public function getFailedTransactionsFromPaystack()
    {
        /**
         * By default this we'll return a status, message, data and meta attributes
         * The data is what we need so pass it to the blade view or vue component
         * This method return the 100 transactions, you can alter the number
         * This method holds two arguments ($perPage = 100, $page = 1)
         * You can adjust them as you see fit 
         */
        $res = RebelPay::getFailedTransactions();
        dd($res);
        return view('rebelPay.pageOne', [
            'transactions' => $res->data
        ]);
    }


    /**
     * We'll use this function to return all our successful transactions from Paystack
     */
    public function getSuccessfulTransactionsFromPaystack()
    {
        /**
         * By default this we'll return a status, message, data and meta attributes
         * The data is what we need so pass it to the blade view or vue component
         * This method return the 100 transactions, you can alter the number
         * This method holds two arguments ($perPage = 100, $page = 1)
         * You can adjust them as you see fit 
         */
        $res = RebelPay::getSuccessfulTransactions();
        dd($res);
        return view('rebelPay.pageOne', [
            'transactions' => $res->data
        ]);
    }


    /**
     * We'll use this function to return all our abandoned transactions from Paystack
     */
    public function getAbandonedTransactionsFromPaystack()
    {
        /**
         * By default this we'll return a status, message, data and meta attributes
         * The data is what we need so pass it to the blade view or vue component
         * This method return the 100 transactions, you can alter the number
         * This method holds two arguments ($perPage = 100, $page = 1)
         * You can adjust them as you see fit 
         */
        $res = RebelPay::getAbandonedTransactions();
        dd($res);
        return view('rebelPay.pageOne', [
            'transactions' => $res->data
        ]);
    }

    /**
     * Method to get a targeted transaction from Paystack
     */
    public function getTransactionFromPaystack(int $id)
    {
        /**
         * Pass the transaction id as an argument
         * e.g 2749916096
         */
        $res = RebelPay::getTransaction($id);
    }


}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.