Set additional information (additional_data) form Magento 2 payment Object

Set Additional Payment Information as part of custom integration with a Payment  Provider.

add extra field Payment form can be found in the  “Bluethink/Payment/view/frontend/web/template/payment” form.html template file.

<div class="field required">
<label data-bind="attr: {for: getCode() + 'iban'}" class="label">
<span><b><!-- ko i18n: '* IBAN'--><!-- /ko --></b></span>
</label>

<div class="control">
<input name="payment[iban]" type="text" class="text" data-bind=" optionsText: 'iban', value: iban">
</div>
</div>

Knockout Component prepares Payment data and puts it inside the array with keyadditional_data together with other quote related data once customer submits the payment form.

The “Bluethink/Payment/view/frontend/web/js/view/payment/method-renderer” payment_gateway.js JavaScript Component’s getData method listed below.


getData: function () {
return {
'method': this.item.method,
'additional_data': {
"stripeToken": this.iban()
}
};
}

Once prepared, payment data is sent via request knockout Component.

Assigning Additional Payment Data

In order to assign Additional Payment Data to the object, weMagento\Sales\Model\Order\Payment have to create an observer in our payment module and listen to the eventpayment_method_assign_data_<payment_code> from the classAdapter.

Create file events.xml in folder “Bluethink/Payment/etc”


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="payment_method_assign_data_payment">
<observer name="bluethink_payment_gateway_data_assign" instance="Bluethink\Payemnt\Observer\DataAssignObserver" />
</event>
</config>

The payment is a code of your payment method.

The Data Assign Observer class

The observerDataAssignObserver class extends the classAbstractDataAssignObserver and listens for the eventpayment_method_assign_data_payment. The following example shows one of the ways setting payment data into the objectMagento\Sales\Model\Order\Payment.


namespace Bluethink\Payment\Observer;

use Magento\Framework\Event\Observer;
use Magento\Payment\Observer\AbstractDataAssignObserver;
use Magento\Quote\Api\Data\PaymentInterface;

class DataAssignObserver extends AbstractDataAssignObserver
{
/**
* @var array
*/
protected $additionalInformationList = [
'bluethinkToken'

];

/**
* @param Observer $observer
* @return void
*/
public function execute(Observer $observer)
{
$data = $this->readDataArgument($observer);

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

if (!is_array($additionalData)) {
return;
}

$paymentInfo = $this->readPaymentModelArgument($observer);

$paymentInfo->setAdditionalInformation(
$additionalData
);
}
}

Accessing addtional data like this at payemnt model page.

/** @var PaymentDataObjectInterface $paymentDO */
 $paymentDO = $buildSubject['payment'];
 $order = $paymentDO->getOrder();
 $payment = $paymentDO->getPayment();
 $paymentAdditionalInformation = $payment->getAdditionalInformation();
$token=$paymentAdditionalInformation['bluethinkToken'];