Magento 2 Authorize Payment Command – Guide

magento2.2payment-gateway

I'm having trouble understanding how an "Authorize" command is executed in a payment method.

Magento 2 uses Javascript components for payment methods and a "Place order" click initiates the whole process, I'm just not sure how/when the Authorize request gets built & sent to a payment provider.

For example, if I use the sample payment gateway that Magento provides: https://github.com/magento/magento2-samples/tree/master/sample-module-payment-gateway, how is the request builder going to be called?

EDIT

This is a reference to Joni Jones's answer

When Magento performs place order actions, it calls authorize or
capture (used for sale operation too) payment operation (according to
an operation configured for the selected payment method).

This is the part that I'm having trouble with.

Do I understand you correctly that the command, which is specified in the payment modules di.xml (authorize/capture/sale) gets automatically called when the "Place order" action gets triggered?

If so, what is the entry point, i.e. which class/file gets called first in a hypothetical situation where you have only 1 command in a command pool (e.g. the authorize command)?

And, if it's not asking too much, what is the bare minimum that you need to have to start execution of a command? Is it really enough to just have a simple custom payment method and the command specified in the payment method's di.xml?

Best Answer

When Magento performs place order actions, it calls authorize or capture (used for sale operation too) payment operation (according to an operation configured for the selected payment method).

This action triggers a chain of methods and if your payment integration based on the Magento payment provider gateway the sales infrastructure tries to call authorize or capture payment gateway command from the command pool.

As you can see from the documentation, the command pool contains a list of available commands, in most cases there are authorize, sale, capture, refund, cancel, void but you can specify own commands and use command pool as dependency in your classes to perform available commands.

As an example, Braintree implementation uses GetPaymentNonceCommand to generate payment nonce.

The command interaction explains this diagram on the Magento dev docs.

UPD: Authorize.net has a good tutorial about different types of payment transactions, maybe, it will help to understand why Magento has different actions like authorize and capture.

UPD2: When a customer clicks Place Order and if the default action is used(payment-information WEB API entry point) when Magento calls the next abstract chain:

Quote::submit
   ->Order::place
      ->OrderPayment::place (authorize|sale) - depends on configured payment action for a payment method
          ->PaymentAdapter::authorize|sale
              ->GatewayCommand::authorize|sale - the needed command will be read from the di.xml
                  -> ... - other payment gateway stuff

The "border" between Sales and Payment module is \Magento\Sales\Model\Order\Payment::place() and \Magento\Payment\Model\Method\Adapter::authorize()|sale().

For simple flow, at least you need payment method skeleton (etc folder with di.xml, module.xml, etc. - can be found in the Magento sample repository) and gateway command with related infrastructure - request builders, validations, response handlers, http|soap clients, etc.

The official documentation covers step by step all requirements.

Related Topic