You can create a Plugin for the method SavePaymentInformation() in Magento 2 to add your logic before or after Payment information is saved.
The method will be called after a click on the Place order button from the second step of the Checkout page.
Original Class to create plugin:
Magento\Checkout\Model\PaymentInformationManagement
You can create a before or after plugin as per your requirement.
Create a global scope di.xml file,
<?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\Checkout\Model\PaymentInformationManagement"> <plugin name="set_payment_data_before_save" type="Jesadiya\SavePayment\Plugin\Model\SavePaymentPlugin" sortOrder="10"/> </type> </config>
Create a Plugin Class for the Before saves payment information, We have used Before Plugin type,
<?php namespace Jesadiya\SavePayment\Plugin\Model; use Magento\Quote\Api\Data\AddressInterface; use Magento\Quote\Api\Data\PaymentInterface; use Magento\Checkout\Model\PaymentInformationManagement; use Magento\Framework\Exception\AbstractAggregateException; class SavePaymentPlugin { /** * @param PaymentInformationManagement $subject * @param int $cartId * @param PaymentInterface $paymentMethod * @param AddressInterface|null $billingAddress * * @return array * * @throws AbstractAggregateException * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforeSavePaymentInformation( PaymentInformationManagement $subject, int $cartId, PaymentInterface $paymentMethod, AddressInterface $billingAddress = null ): array { // Add your logic before Save Payment information return [$cartId, $paymentMethod, $billingAddress]; } }
Before Plugin:
$cartId: You get the current quote Cart id, You can get the details of the active quote by cart id from the method.
$paymentMethod contains the Payment related information, Payment method, Payment additional data
The $billingAddress field contains the billing data of the checkout billing section.
You can create an after plugin also for the above method if you need to do some logic after the Payment method successfully saved.
Above Code snippet, Save Payment Information Plugin for adding extra logic after or before Payment data save in Magento 2.