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:
On the next step, you need to add created observer to list of events in your module (
CustomModule/etc/events.xml
):Created observer will be triggered when all data will be set to
additional_data
property (for now, more preferable to useadditional_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:UPD: This topic describes how to read payment additional data.