rodion15/laravel-payok-io

payok.io payments for Laravel

v1.0.2 2024-01-12 23:01 UTC

This package is auto-updated.

Last update: 2024-09-13 00:47:07 UTC


README

Accept payments via PayokIo (payok.io) using this Laravel framework package (Laravel).

  • receive payments, adding just the two callbacks

Laravel >= 10.*, PHP >= 8.1

Installation

Require this package with composer.

composer require rodion15/laravel-payok-io

If you don't use auto-discovery, add the ServiceProvider to the providers array in config/app.php

Rodion15\PayokIo\PayokIoServiceProvider::class,

Add the PayokIo facade to your facades array:

'PayokIo' => Rodion15\PayokIo\Facades\PayokIo::class,

Copy the package config to your local config with the publish command:

php artisan vendor:publish --provider="Rodion15\PayokIo\PayokIoServiceProvider"

Configuration

Once you have published the configuration files, please edit the config file in config/payokio.php.

  • Create an account on payok.io
  • Add your project, copy the project_id, secret_key params and paste into config/payokio.php
  • After the configuration has been published, edit config/payokio.php
  • Set the callback static function for searchOrder and paidOrder
  • Create route to your controller, and call PayokIo::handle method

Usage

  1. Generate a payment url or get redirect:
$amount = 100; // Payment`s amount

$url = PayokIo::getPayUrl($amount, $order_id);

$redirect = PayokIo::redirectToPayUrl($amount, $order_id);

You can add custom fields to your payment:

$rows = [
    'time' => Carbon::now(),
    'info' => 'Local payment'
];

$url = PayokIo::getPayUrl($amount, $order_id, $desc, $payment_methood, $rows);

$redirect = PayokIo::redirectToPayUrl($amount, $order_id, $desc, $payment_methood, $rows);

$desc and $payment_methood can be null.

  1. Process the request from PayokIo:
PayokIo::handle(Request $request)

Important

You must define callbacks in config/payokio.php to search the order and save the paid order.

'searchOrder' => null  // PayokIoController@searchOrder(Request $request)
'paidOrder' => null  // PayokIoController@paidOrder(Request $request, $order)

Example

The process scheme:

  1. The request comes from payok.io GET / POST http://yourproject.com/payokio/result (with params).
  2. The functionPayokIoController@handlePayment runs the validation process (auto-validation request params).
  3. The method searchOrder will be called (see config/payokio.php searchOrder) to search the order by the unique id.
  4. If the current order status is NOT paid in your database, the method paidOrder will be called (see config/payokio.php paidOrder).

Add the route to routes/web.php:

 Route::get('/payokio/result', 'PayokIoController@handlePayment');

Note: don't forget to save your full route url (e.g. http://example.com/payokio/result ) for your project on payok.io.

Create the following controller: /app/Http/Controllers/PayokIoController.php:

class PayokIoController extends Controller
{
    /**
     * Search the order in your database and return that order
     * to paidOrder, if status of your order is 'paid'
     *
     * @param Request $request
     * @param $order_id
     * @return bool|mixed
     */
    public function searchOrder(Request $request, $order_id)
    {
        $order = Order::where('id', $order_id)->first();

        if($order) {
            $order['_orderSum'] = $order->sum;

            // If your field can be `paid` you can set them like string
            $order['_orderStatus'] = $order['status'];

            // Else your field doesn` has value like 'paid', you can change this value
            $order['_orderStatus'] = ('1' == $order['status']) ? 'paid' : false;

            return $order;
        }

        return false;
    }

    /**
     * When paymnet is check, you can paid your order
     *
     * @param Request $request
     * @param $order
     * @return bool
     */
    public function paidOrder(Request $request, $order)
    {
        $order->status = 'paid';
        $order->save();

        //

        return true;
    }

    /**
     * Start handle process from route
     *
     * @param Request $request
     * @return mixed
     */
    public function handlePayment(Request $request)
    {
        return PayokIo::handle($request);
    }
}

Changelog

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

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please send me an email at ya@sanek.dev instead of using the issue tracker.

Credits

License

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