<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

/**
 * Debitpayment Observer
 */
namespace Threepartment\Debitpayment\Observer;

use Magento\Framework\Event\ObserverInterface;
use Threepartment\Debitpayment\Model\Debitpayment;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;

class PaymentMethodIsActiveObserver implements ObserverInterface
{
    /**
     * Sets current instructions for bank transfer account
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        /* Checks if payment method is enabled for customer's group */
        $paymentMethod = $observer->getEvent()->getMethodInstance()->getCode();
        $result = $observer->getEvent()->getResult();
        $quote  = $observer->getEvent()->getQuote();
        if (!$quote) {
            return;
        }

        if ($paymentMethod == 'debitpayment') {
            $current_customer_group = $quote->getCustomerGroupId();
            $enabled_groups = $observer->getEvent()->getMethodInstance()->getEnabledGroups();
            if(!in_array($current_customer_group, explode(',',$enabled_groups))){
                $result->setData('is_available', false);
            }
            return;
        }
        
    }
}
