Magento – Add additional information to payment method

magento-2.1.9magento2paymentpayment-methods

I'm trying to add additional data to the checkmo payment method. I then need this additional data appear at the order Endpoint.

I plan on using this as a way of passing over a sort code / account number for direct debit payments.

I have a system set up that places orders for Magento 2 with the API. A user enters name / address etc and this is sent over using the API.

For payment method I need to pass over sort code / account number. Rather than creating a custom payment method for this, I would like to try and shoehorn this information into an existing one that won't be used on the actual Magento 2 site.

The issue seems to be actually adding data into this,

The Swagger documentation seems to have –

payment": {
"account_status": "string",
"additional_data": "string",
"additional_information": [
  "string"
],

I have tried using this –

{
"paymentMethod": {
"method": "checkmo",
"additional_data": {
"sortcode" :"00-00-00",
"account_number" : "12345678"
   }
 }
}

But this still comes out at the Endpoint as –

"payment": {
    "account_status": null,
    "additional_information": [
        "Check / Money order",
        null,
        null
    ],

If I try to use additonal information instead –

{
"paymentMethod": {
"method": "checkmo",
"additional_information": {
"sortcode" :"00-00-00",
"account_number" : "12345678"
 }
}

I get the error

Message: Property "AdditionalInformation" does not have corresponding setter in class "Magento\Quote\Api\Data\PaymentInterface".

Is there anyway to add this to an order preferably using checkmo payment method, and then have it appear at the endpoint?

Best Answer

I've ended up adding a plugin to pass the additional_information to the quote payment.

In my di.xml:

<type name="Magento\Quote\Model\Quote\Payment">
    <plugin name="vendorModuleQuotePayment" type="Vendor\Module\Plugin\QuotePaymentPlugin"/>
</type>

The plugin:

<?php

namespace Vendor\Module\Plugin;

class QuotePaymentPlugin
{
    /**
     * @param \Magento\Quote\Model\Quote\Payment $subject
     * @param array $data
     * @return array
     */
    public function beforeImportData(\Magento\Quote\Model\Quote\Payment $subject, array $data)
    {
        if (array_key_exists('additional_information', $data)) {
            $subject->setAdditionalInformation($data['additional_information']);
        }

        return [$data];
    }
}
Related Topic