{"id":1672,"date":"2021-03-28T11:39:17","date_gmt":"2021-03-28T11:39:17","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/use-payment-extension-attributes-in-magento-2\/"},"modified":"2025-07-17T09:32:11","modified_gmt":"2025-07-17T04:02:11","slug":"use-payment-extension-attributes-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/use-payment-extension-attributes-in-magento-2\/","title":{"rendered":"How to Use Payment Extension Attributes in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2\u00a0is the leading E-commerce platform that provides improved scalability and flexibility for an online store.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An example of Magento 2\u2019s flexibility is that store owners can customize and overwrite the core features of its framework according to the business requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One such requirement is to add custom fields into our entities. To serve this purpose, Magento 2 offers a flexible and declarative way to extend functionality that is called extension attributes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Magento 2&nbsp;extension attributes&nbsp;are primarily containers used for adding an additional piece of information to our entities that allow adding additional custom data into an entity such as Product, Customer, Order, etc.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The below solution is the method to use payment extension attributes in Magento 2 that allows saving the custom field values in the database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, the customers enter custom messages or comments while placing the order, and to save these comments, you need to pass it with the payment details using knockout JS.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The below method shows the same:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Use Payment Extension Attributes in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create&nbsp;<strong>registration.php<\/strong>&nbsp;file at&nbsp;<strong><strong>app\\code\\Vendor\\Module<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nuse Magento\\Framework\\Component\\ComponentRegistrar;\n\nComponentRegistrar::register(ComponentRegistrar::MODULE, 'Vendor_Module', __DIR__);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create&nbsp;<strong>module.xml<\/strong>&nbsp;file at&nbsp;<strong><strong><strong><strong>app\\code\\Vendor\\Module\\etc<\/strong><\/strong><\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n    &lt;module name=\"Vendor_Module\" setup_version=\"1.0.0\"\/>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Create an&nbsp;<strong>extension_attributes.xml<\/strong>&nbsp;file at&nbsp;<strong><strong><strong><strong>app\\code\\Vendor\\Module\\etc<\/strong><\/strong><\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Api\/etc\/extension_attributes.xsd\">\n    &lt;extension_attributes for=\"Magento\\Quote\\Api\\Data\\PaymentInterface\">\n        &lt;attribute code=\"custom\" type=\"string\"\/>\n    &lt;\/extension_attributes>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4. Create a&nbsp;<strong>requirejs-config.js<\/strong>&nbsp;file at&nbsp;<strong><strong><strong><strong>app\\code\\Vendor\\Module\\view\\frontend<\/strong><\/strong><\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var config = {\n    config: {\n        mixins: {\n            'Magento_Checkout\/js\/action\/place-order': {\n                'Vendor_Module\/js\/order\/place-order-mixin': true\n            },\n            'Magento_Checkout\/js\/action\/set-payment-information': {\n                'Vendor_Module\/js\/order\/set-payment-information-mixin': true\n            }\n        }\n    };<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">5. Create&nbsp;<strong>place-order-mixin.js<\/strong>&nbsp;file at&nbsp;<strong><strong><strong><strong>app\\code\\Vendor\\Module\\view\\frontend\\web\\js\\order<\/strong><\/strong><\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">define([\n    'jquery',\n    'mage\/utils\/wrapper',\n    'Vendor_Module\/js\/order\/custom-assigner'\n], function ($, wrapper, ordercommentAssigner) {\n    'use strict';\n\n    return function (placeOrderAction) {\n\n        return wrapper.wrap(placeOrderAction, function (originalAction, paymentData, messageContainer) {\n            ordercommentAssigner(paymentData);\n\n            return originalAction(paymentData, messageContainer);\n        });\n    };\n});<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">6. Create&nbsp;<strong>custom-assigner.js<\/strong>&nbsp;at&nbsp;<strong><strong><strong><strong>app\\code\\Vendor\\Module\\view\\frontend\\js\\order<\/strong><\/strong><\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">define([\n    'jquery'\n], function ($) {\n    'use strict';\n\n    return function (paymentData) {\n\n        if (paymentData['extension_attributes'] === undefined) {\n            paymentData['extension_attributes'] = {};\n        }\n\n        paymentData['extension_attributes']['custom'] = jQuery('[name=\"ordercomment[custom]\"]').val();\n    };\n});<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">7. Create&nbsp;<strong>set-payment-information-mixin.js<\/strong>&nbsp;at&nbsp;<strong><strong><strong><strong>app\\code\\Vendor\\Module\\view\\frontend\\js\\order<\/strong><\/strong><\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">define([\n    'jquery',\n    'mage\/utils\/wrapper',\n    'Vendor_Module\/js\/order\/custom-assigner'\n], function ($, wrapper, ordercommentAssigner) {\n    'use strict';\n\n    return function (placeOrderAction) {\n        return wrapper.wrap(placeOrderAction, function (originalAction, messageContainer, paymentData) {\n            ordercommentAssigner(paymentData);\n\n            return originalAction(messageContainer, paymentData);\n        });\n    };\n});<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">8. Create&nbsp;<strong>di.xml<\/strong>&nbsp;at&nbsp;<strong><strong><strong><strong>app\\code\\Vendor\\Module\\etc<\/strong><\/strong><\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\">\n\n    &lt;type name=\"Magento\\Checkout\\Model\\PaymentInformationManagement\">\n        &lt;plugin name=\"set_payment_data_before_save\"\n                type=\"Vendor\\Module\\Plugin\\Model\\SavePaymentPlugin\" sortOrder=\"10\"\/>\n    &lt;\/type>\n\n    &lt;type name=\"Magento\\Checkout\\Model\\GuestPaymentInformationManagement\">\n        &lt;plugin name=\"guest_set_payment_data_before_save\"\n                type=\"Vendor\\Module\\Plugin\\Model\\GuestSavePaymentPlugin\" sortOrder=\"10\"\/>\n    &lt;\/type>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">9. Create&nbsp;<strong>SavePaymentPlugin.php<\/strong>&nbsp;at&nbsp;<strong><strong><strong><strong>Vendor\\Module\\Plugin\\Model<\/strong><\/strong><\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Module\\Plugin\\Model;\n\nuse Magento\\Checkout\\Model\\PaymentInformationManagement;\nuse Magento\\Quote\\Api\\Data\\AddressInterface;\nuse Magento\\Quote\\Api\\Data\\PaymentInterface;\n\nclass SavePaymentPlugin\n{\n    protected $quoteRepository;\n\n    public function beforeSavePaymentInformationAndPlaceOrder(PaymentInformationManagement $subject,\n                                                              $cartId, PaymentInterface $paymentMethod,\n                                                              AddressInterface $billingAddress = null)\n    {\n        $orderCustom = $paymentMethod->getExtensionAttributes();\n\n        $objectManager = \\Magento\\Framework\\App\\ObjectManager::getInstance();\n        $quoteRepository = $objectManager->create('Magento\\Quote\\Api\\CartRepositoryInterface');\n\n        $quote = $quoteRepository->getActive($cartId);\n        $custom = $orderComment->getCustom();\n        $quote->setCustom(custom);\n        $quote->save();\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">10. Create&nbsp;<strong>GuestSavePaymentPlugin.php<\/strong>&nbsp;at&nbsp;<strong><strong><strong><strong>Vendor\\Module\\Plugin\\Model<\/strong><\/strong><\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Module\\Plugin\\Model;\n\nuse Magento\\Checkout\\Model\\PaymentInformationManagement;\nuse Magento\\Quote\\Api\\Data\\AddressInterface;\nuse Magento\\Quote\\Api\\Data\\PaymentInterface;\n\nclass GuestSavePaymentPlugin\n{\n    protected $quoteRepository;\n\n    public function beforeSavePaymentInformationAndPlaceOrder(PaymentInformationManagement $subject,\n                                                              $cartId, $email, PaymentInterface $paymentMethod,\n                                                              AddressInterface $billingAddress = null)\n    {\n        $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');\n        $quoteId = $quoteIdMask->getQuoteId();\n        $orderCustom = $paymentMethod->getExtensionAttributes();\n\n        $objectManager = \\Magento\\Framework\\App\\ObjectManager::getInstance();\n        $quoteRepository = $objectManager->create('Magento\\Quote\\Api\\CartRepositoryInterface');\n\n        $quote = $quoteRepository->get($quoteId);\n        $custom = $orderComment->getCustom();\n        $quote->setCustom(custom);\n        $quote->save();\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now you can easily save your additional field\u2019s data after placing an order.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Done!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the solution with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank You.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2\u00a0is the leading E-commerce platform that provides improved scalability and flexibility for an online store. An example of Magento 2\u2019s flexibility is that store&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1672","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1672","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1672"}],"version-history":[{"count":6,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1672\/revisions"}],"predecessor-version":[{"id":18158,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1672\/revisions\/18158"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}