Magento – How to get payment form data in the observer execute method

event-observermagento2payment-methods

i am trying to save the payment method custom field in magento2 to payment related table. my observer is triggering but i am not able to get the custom field data.
observer class

namespace Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;

class PaymentAssignData implements ObserverInterface {

    /**
     * @var ObjectManagerInterface
     */
    protected $_objectManager;

    /**
     * @param \Magento\Framework\ObjectManagerInterface $objectManager
     */
    public function __construct(
    \Magento\Framework\ObjectManagerInterface $objectManager,
            \Psr\Log\LoggerInterface $logger
    ) {
        //die(__METHOD__);
        $this->_objectManager = $objectManager;
        $this->logger = $logger;
    }


    public function execute(Observer $observer) {
        $quoteId = $order->getQuoteId();
         $payment = $order->getPayment()->getData('additional_data','custom_field');;
         $quote = $this->quoteFactory->create()->load($quoteId);


    }

}

my custom field are added thhrough LayoutProcessor.

Best Answer

To get payment details Magento already has the observer - \Magento\Payment\Observer\AbstractDataAssignObserver.

In most cases it will be enough to extend it:

use Magento\Payment\Observer\AbstractDataAssignObserver;

class AdditionalDataReader extends AbstractDataAssignObserver
{

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $data = $this->readDataArgument($observer);

        $additionalData = $data->getData(PaymentInterface::KEY_ADDITIONAL_DATA);

        if (!is_array($additionalData)) {
            return;
        }
        // your custom code
    }
}

On the next step, you need to add created observer to list of events in your module (CustomModule/etc/events.xml):

<event name="payment_method_assign_data">
    <observer name="additional_data_reader" instance="CustomPayment\Observer\AdditionalDataReader" />
</event>

Created observer will be triggered when all data will be set to additional_data property (for now, more preferable to use additional_information).

But, your custom payment should sent additional properties from Storefront|Admin panel to the backend.

For example, for Storefront your payment component should overrides getData method:

define(
    ['Magento_Payment/js/view/payment/cc-form'],
    function (Component) {
        'use strict';

        return Component.extend({
            // Your component code
            ...
            getData: function () {
                var data = {
                    'method': 'custom_payment_code',
                    'additional_data': {
                        'custom_field': 'some custom data'
                    }
                };

                return data;
            }
        });
    }
);

UPD: This topic describes how to read payment additional data.