0

I am trying to add an extension attribute in the Payment API. but value not saved in sales_order table.

etc/extension_attributes.xml

<extension_attributes for="Magento\Quote\Api\Data\PaymentInterface">
        <attribute code="comment" type="string"/>
 </extension_attributes>

etc/di.xml

<type name="Magento\Checkout\Api\PaymentInformationManagementInterface">
        <plugin name="order_comments_save-in-order" type="Custom\Module\Plugin\Checkout\PaymentInformationManagementPlugin" sortOrder="10"/>
</type>

Plugin/Checkout/PaymentInformationManagementPlugin.php

class PaymentInformationManagementPlugin
{

    protected $orderRepository;

    public function __construct(
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
    ) {
        $this->orderRepository = $orderRepository;
    }
     public function aroundSavePaymentInformationAndPlaceOrder(
        \Magento\Checkout\Api\PaymentInformationManagementInterface $subject,
        \Closure $proceed,
        $cartId,
        \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
        \Magento\Quote\Api\Data\AddressInterface $billingAddress = null
    ) {
        $result = $proceed($cartId, $paymentMethod, $billingAddress);

         if($result){
            $orderComment =$paymentMethod->getExtensionAttributes();
             if ($orderComment->getComment())
               $comment = trim($orderComment->getComment());
           else
               $comment = ''; 
            $orderComment->setComment($comment);                
         }

        return $result;
    }
}

The above code is not working.

0

Below Code is working for me.

Create required custom module files.

Setup/UpgradeSchema.php

<?php

namespace Custom\Module\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class UpgradeSchema implements UpgradeSchemaInterface
{
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        if (version_compare($context->getVersion(), '3.0.0', '<')) {
            $setup->getConnection()->addColumn(
                $setup->getTable('quote'),
                'po_number',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'nullable' => false,
                    'comment' => 'PO Number'
                ]
            );

            $setup->getConnection()->addColumn(
                $setup->getTable('sales_order'),
                'po_number',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'nullable' => false,
                    'comment' => 'PO Number'
                ]
            );

            $setup->getConnection()->addColumn(
                $setup->getTable('sales_order_grid'),
                'po_number',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'nullable' => false,
                    'comment' => 'PO Number'
                ]
            );
        }
        $setup->endSetup();
    }
}

etc/extension_attributes.xml

<extension_attributes for="Magento\Quote\Api\Data\PaymentInterface">
        <attribute code="po_number" type="string"/>
</extension_attributes>

etc/di.xml

 <virtualType name="Magento\Sales\Model\ResourceModel\Order\Grid">
        <arguments>
            <argument name="columns" xsi:type="array">
                <item name="po_number" xsi:type="string">sales_order.po_number</item>
            </argument>
        </arguments>
    </virtualType>

<type name="Magento\Checkout\Api\PaymentInformationManagementInterface">
        <plugin name="order_ponumber_save-in-order" type="Custom\Module\Plugin\Checkout\PaymentInformationManagementPlugin" sortOrder="10"/>
  </type>

Custom/Module/Plugin/Checkout/PaymentInformationManagementPlugin.php

<?php

namespace Custom\Module\Plugin\Checkout;

use Magento\Quote\Api\Data\AddressInterface;

class PaymentInformationManagementPlugin
{
    protected $orderRepository;
    protected $paymentFactory;
    protected $logger;
    protected $quoteRepository;

    public function __construct(
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
        \Magento\Quote\Api\Data\PaymentExtensionFactory $paymentFactory,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
    )
    {
        $this->orderRepository = $orderRepository;
        $this->paymentFactory = $paymentFactory;
        $this->logger = $logger;
        $this->quoteRepository = $quoteRepository;
    }

    public function beforeSavePaymentInformationAndPlaceOrder(
        \Magento\Checkout\Api\PaymentInformationManagementInterface $subject,
        $cartId,
        \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
    )
    {
        if ($paymentMethod->getExtensionAttributes()->getPoNumber()) {
            $quote = $this->quoteRepository->getActive($cartId);
            $quote->setPoNumber($paymentMethod->getExtensionAttributes()->getPoNumber());
            $this->quoteRepository->save($quote);
        }
    }
}

etc/events.xml

<event name="sales_model_service_quote_submit_before">
        <observer name="sales_model_service_quote_submit_before" instance="Custom\Module\Observer\SalesModelServiceQuoteSubmitBefore"/>
</event>

Custom/Module/Observer/SalesModelServiceQuoteSubmitBefore.php

<?php

namespace Custom\Module\Observer;

class SalesModelServiceQuoteSubmitBefore implements \Magento\Framework\Event\ObserverInterface
{
    protected $logger;
    protected $objectCopyService;
    private $quoteRepository;

    public function __construct(
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\DataObject\Copy $objectCopyService,
        \Magento\Quote\Model\QuoteRepository $quoteRepository
    )
    {
        $this->logger = $logger;
        $this->objectCopyService = $objectCopyService;
        $this->quoteRepository = $quoteRepository;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $order = $observer->getEvent()->getOrder();
        $quote = $observer->getEvent()->getQuote();
        $quotes = $this->quoteRepository->get($order->getQuoteId());
        if ($quotes->getPoNumber()) {
            $order->setPoNumber($quotes->getPoNumber());
        }
        return $this;
    }
}

API - /V1/carts/mine/payment-information

Request:

{
   "billingAddress":{
      "countryId":"IN",
      "regionId":"564",
      "regionCode":"TG",
      "region":"Telangana",
      "street":[
         "high tech city"
      ],
      "company":"pt",
      "telephone":"09878787878",
      "postcode":"500005",
      "city":"sadsad",
      "firstname":"sdasd",
      "lastname":"sadsd",
      "saveInAddressBook":null,
      "extension_attributes": {
      "landmark": "test"
    }
   },
   "paymentMethod":{
      "method":"ccavenue",
      "extension_attributes":{
         "po_number":"1996"
      }
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.