John Davidson

php - Magento 2 event to extend payment methods functionality

0 comments
Message:


I need help. I am creating a custom payment method for Magento 2. In the plugin, user needs to enter the keys. While saving the config, I want to check if the keys entered by the user are correct or not. If the keys are incorrect, I want to show a message informing user about the issue.


I have googled about it and I thought I have found the event admin_system_config_changed_section_{sectionname}. I implemented it, cleared the cache and recompiled the code but when I save the config data for the payment method plugin, it does not trigger the class. I looked into Magento documentation. I could not find the exact full event name for it. These are the full event name I tried:


admin_system_config_changed_section_payment_methods
admin_system_config_changed_section_paymentmethods
admin_system_config_changed_section_sales


MyPayment/Payment/etc/adminhtml/events.xml


<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="admin_system_config_changed_section_payment_methods">
<observer name="my_payment_admin_system_config_changed_section_paymentmethods" instance="MyPayment\Payment\Observer\CustomPaymentMethodConfigObserver" />
</event>
</config>

MyPayment/Payment/Observer/CustomPaymentMethodConfigObserver


<?php

namespace MyPayment\Payment\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;

class CustomPaymentMethodConfigObserver implements ObserverInterface
{
public function execute(Observer $observer)
{
// some codes that did not trigger
}
}


My Answer:
In Magento 2, the event you are looking for is `admin_system_config_changed_section_{sectionname}`. However, the section name for payment methods in Magento 2 is `payment`. So the correct event name you should use in your `events.xml` file is `admin_system_config_changed_section_payment`.

Here is an example of how you can define the event in your `events.xml` file:

xml








Then, you need to create the observer class `ValidateKeys` in your module:

php
namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class ValidateKeys implements ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer)
{
// Your logic to validate the keys entered by the user
// You can get the data from the observer like this:
$section = $observer->getEvent()->getData('section');
$website = $observer->getEvent()->getData('website');
$store = $observer->getEvent()->getData('store');

// Check the keys and show a message if they are incorrect
// You can use \Magento\Framework\Message\ManagerInterface to show a message to the user

return $this;
}
}


Make sure to replace `Vendor\Module` with your actual module namespace and name.

After implementing this, clear the cache and try saving the config data for your payment method plugin again. The `ValidateKeys` observer should now be triggered when the payment section is saved, allowing you to validate the keys entered by the user.

Rate this post

3 of 5 based on 5699 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us