kna/yandex-checkout-bundle

A Symfony Wrapper for the yandex-money/yandex-checkout-sdk-php library

v1.0.8 2020-08-30 15:55 UTC

This package is auto-updated.

Last update: 2024-03-29 03:32:39 UTC


README

Build Status

A Symfony wrapper for the yandex-money/yandex-checkout-sdk-php library.

Installation

composer require kna/yandex-checkout-bundle

Configuring

Add config:

// config/packages/kna_yandex_checkout.yaml

kna_yandex_checkout:
  shop_id: '%env('YANDEX_CHECKOUT_SHOP_ID')%'
  secret_key: '%env('YANDEX_CHECKOUT_SECRET_KEY')%'
  validate_ip: true
  valid_ips:
  - 192.168.1.0/16

Add routing:

// config/routes.yaml

// ...

kna_yandex_checkout:
  resource: "@KnaYandexCheckoutBundle/Resources/config/routes.yaml"
  prefix: <prefix>

// ...

Set https://<domain.tld>/<prefix>/<secret_key> as URL for notifications and events in the store settings.

Usage

Use dependency injection:

<?php
// src/EventListener/DefaultController.php

namespace App\Controller;


use YandexCheckout\Client;

public function __constructor(Client $client)
{
    $this->client = $client;
}

Create event listener:

<?php
// src/EventListener/YandexCheckoutSubscriber.php

namespace App\EventListener;


use Kna\YandexCheckoutBundle\Event\NotificationEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class YandexCheckoutSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
            NotificationEvent::class => 'onNotificationReceived'
        ];
    }

    public function onNotificationReceived(NotificationEvent $event)
    {
        $notification = $event->getNotification();
        
        // dispatch notification

        $event->setAccepted(true);
    }
}

Payum support

payum/payum-bundle and kna/payum-yandex-checkout should be installed.

Configuring

Add config:

// config/packages/kna_yandex_checkout.yaml

kna_yandex_checkout:
  // ...
  payum:
    enable: true
    payment_class: App\Entity\Payment # default
    payment_id_key: payment_id
    force_payment_id: true

Create event listener:

<?php
// src/EventListener/YandexCheckoutSubscriber.php

namespace App\EventListener;


use Kna\YandexCheckoutBundle\Event\CaptureRequestedEvent;
use Kna\YandexCheckoutBundle\Event\PaymentCanceledEvent;
use Kna\YandexCheckoutBundle\Event\PaymentCapturedEvent;
use Kna\YandexCheckoutBundle\Event\PaymentSucceededEvent;
use Kna\YandexCheckoutBundle\Event\RefundSucceededEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class YandexCheckoutSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
            CaptureRequestedEvent::class => 'onCaptureRequested',
            PaymentCanceledEvent::class => 'onPaymentCancelled',
            PaymentCapturedEvent::class => 'onPaymentCaptured',
            PaymentSucceededEvent::class => 'onPaymentSucceeded',
            RefundSucceededEvent::class => 'onRefundSucceeded',
        ];
    }

    public function onCaptureRequested(CaptureRequestedEvent $event)
    {
        // ...
    }
    
    public function onPaymentCancelled(PaymentCanceledEvent $event)
    {
        // ...
    }
    
    public function onPaymentCaptured(PaymentCapturedEvent $event)
    {
        // ...
    }
    
    public function onPaymentSucceeded(PaymentSucceededEvent $event)
    {
        // ...
    }
    
    public function onRefundSucceeded(RefundSucceededEvent $event)
    {
        // ...
    }
}