PHP code example of artem328 / laravel-yandex-kassa

1. Go to this page and download the library: Download artem328/laravel-yandex-kassa 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/ */

    

artem328 / laravel-yandex-kassa example snippets



    return [
        //...
        'providers' => [
            //...
            Artem328\LaravelYandexKassa\YandexKassaServiceProvider::class,
        ],
        //...
    ];


    return [
        //...
        'aliases' => [
            //...
            'YandexKassa' => Artem328\LaravelYandexKassa\Facades\YandexKassa::class,
        ],
        //...
    ];


    namespace App\Listeners;

    use App\Order;
    use Artem328\LaravelYandexKassa\Events\BeforePaymentAvisoResponse;

    class ChangeOrderStatusWhenPaymentSuccessful
    {
        /**
         * @param \Artem328\LaravelYandexKassa\Events\BeforePaymentAvisoResponse
         * @return void
         */
        public function handle(BeforePaymentAvisoResponse $event)
        {
            // if hash is valid we know that payment is successful
            // and we can change order status
            if ($event->request->isValidHash()) {
                $order = Order::find($event->request->get('orderNumber'));
                $order->setStatus('packing');
                $order->save();
            } else {
                // Logic on non valid hash
                // You don't need to set response code to 1
                // YandexKassaRequest do it automatically
            }
        }
    }


    namespace App\Listeners;

    use App\Order;
    use Artem328\LaravelYandexKassa\Events\BeforeCheckOrderResponse;

    class CheckOrderRequisites
    {
        /**
         * @param \Artem328\LaravelYandexKassa\Events\BeforeCheckOrderResponse
         * @return array|null
         */
        public function handle(BeforeCheckOrderResponse $event)
        {
            // If you have some custom validation of payment form
            // You can change response parameters before controller
            // will show it
            if ($event->request->get('customField') != '1') {
                $event->responseParameters['code'] = 100;
                $event->responseParameters['message'] = 'Some checkbox was not checked';
                // You must to return response parameters array,
                // for apply changes
                return $event->responseParameters;
            }

            // If there's no parameters changes
            // just return null or empty array
            return null;
        }
    }


    namespace App\Providers;

    //...
    use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

    class EventServiceProvider extends ServiceProvider
    {
        //...
        protected $listen = [
            'Artem328\LaravelYandexKassa\Events\BeforeCheckOrderResponse' => [
                'App\Listeners\CheckOrderRequisites',
                // You can add more than one listener and every
                // listener can return own parameters. Incoming
                // parameters WILL NOT extend. But response
                // parameters WILL override in listeners order
                // 'App\Listeneres\AddCheckOrderRecord',
            ],
            'Artem328\LaravelYandexKassa\Events\BeforePaymentAvisoResponse' => [
                'App\Listeners\ChangeOrderStatusWhenPaymentSuccessful',
            ]
        ];
        //...
    }