How to set Payment additional data in Magento 2?

Magento 2 You can set additional data in payment using setAdditionalData( ) method defined under Quote Module Payment.php class.

Magento\Quote\Model\Quote\Payment class used for set payment related data from quote to order. This class is used for set payment method, additional data, and other payment-related data to Order.

if you want to add any extra data in payment information you can use setAdditionalData( ) function.

You can create a plugin for Payment class and call before method with setAdditionalData( ) to add extra data.

create file app\code\Rbj\Mymodule\etc\di.xml,

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
     <type name="Magento\Quote\Model\Quote\Payment">
        <plugin sortOrder="1" name="set_extra_data"
                type="Rbj\Mymodule\Plugin\Payment"/>
    </type>
</config>

create file app\code\Rbj\Mymodule\Plugin\Payment.php,

<?php
namespace Rbj\Mymodule\Plugin;

class Paymentt
{
    /**
     *
     * @param \Magento\Quote\Model\Quote\Payment $subject
     * @param array $additionalData
     * @return array
     */
    public function beforeSetAdditionalData(
        \Magento\Quote\Model\Quote\Payment $subject,
        $additionalData
    ) {
        // add your extra data here....
        //$additionalData[KEY => VALUE];
    }
}

Based on the above plugin you can add your custom additional data On Payment related stuff.

 

 

One Reply to “How to set Payment additional data in Magento 2?”

  1. Hi Rakesh,
    The idea with plugin definitely a good one, but we should remember to add as a first line of every such plugin an if statement veryfing that this plugin should be executed only for our own method, otherwise it will be executed for all payment methods.

    The Adapter class from MagentoPayment module provides an assign() method where we can assign a specific to our payment method observer and execute it only in scope of the payment method.

Leave a Reply