Magento – Custom Payment Method in Magento 2

authorizecaptchacheckoutmagento-2.1payment-methods

I am working on implementing a new payment module using Magento 2.1.6 and want to understand the core concept behind this logic. I know I have to extend from Payment_Model_AbstractMethod or any of its children classes, but my problem is How does the payment module flow work (which functions are called specifically when pressing checkout and place order buttons). When to use and how to use capture and authorize methods in my model.
Finally, how can i check the transaction status is it failed or success?

Best Answer

When place order button press from checkout. It's call following class


//Magento/Checkout/Model/PaymentInformationManagement.php
/**
 * {@inheritDoc}
 */
public function savePaymentInformationAndPlaceOrder(
    $cartId,
    \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
    \Magento\Quote\Api\Data\AddressInterface $billingAddress = null
) {
    $this->savePaymentInformation($cartId, $paymentMethod, $billingAddress);
    try {
        $orderId = $this->cartManagement->placeOrder($cartId);
    } catch (\Exception $e) {
        throw new CouldNotSaveException(
            __('An error occurred on the server. Please try to place the order again.'),
            $e
        );
    }
    return $orderId;
}

Then

$orderId = $this->cartManagement->placeOrder($cartId);

Now open Magento/Quote/Model/QuoteManagement.php look placeOrder method. Now look following method in same class.


protected function submitQuote(QuoteEntity $quote, $orderData = [])
{
    .......
    $order = $this->orderManagement->place($order);
}

Now open Magento/Sales/Model/Service/OrderService.php place method.

Now open Magento/Sales/Model/Order.php


/**
 * Place order
 *
 * @return $this
 */
public function place()
{
    $this->_eventManager->dispatch('sales_order_place_before', ['order' => $this]);
    $this->_placePayment();
    $this->_eventManager->dispatch('sales_order_place_after', ['order' => $this]);
    return $this;
}

/**
     * Place order payments
     *
     * @return $this
     */
    protected function _placePayment()
    {
        $this->getPayment()->place();
        return $this;
    }

Look $this->getPayment()->place(); this is the main method who is responsible for payment.

Now open Magento/Sales/Model/Order/Payment.php

This line return actual payment method class object

Check this line here call actual class method

Related Topic