Jan 25, 2016

Magento 2 Payment Capture Partial Request

Magento 2 Payment Capture Partial Request
In this article we are going to review Partial Capture request processing flow. Payment methods such as Authorize.net, PayPal, Realex Payments, Braintree etc. allow you to configure payment action (System -> Configuration -> Sales -> Payment Methods -> _Method Name_ -> Payment Action) to be "Authorize only". It means that when customer place an order, an order amount to pay is authorized at customer's bank account. Merchant has to capture the order amount manually via Payment online terminal.

In most cases when you develop new Payment integration in Magento 2 you have to consider different scenarios and workflows. It allows you as a developer to implement feature rich user experience.

In the Magento 2 Payment Adapter Configuration article we have reviewed Payment Adapter class and it's ways of configuring isolated Payment Service Provider requests via di.xml configuration file.

Overview

In this article we are going to review Partial Capture request processing flow. Payment methods such as Authorize.net, PayPal, Realex Payments, Braintree etc. allow you to configure payment action (System -> Configuration -> Sales -> Payment Methods -> Method Name -> Payment Action) to be "Authorize only". It means that when customer place an order, an order amount to pay is authorized at customer's bank account. Merchant has to capture the order amount manually via Payment online terminal.

Magento 2 Payment module allows to implement Capture request by payment method. Capture request is triggered when Merchant creates an Invoice for an Order via Magento Admin panel. Payment method implementation might also support sending Partial Capture request to Payment Service Provider.

Where Partial Capture request is used?

It usefull when Merchant wants to edit Product's quantity in an Invoice which affect order total amount. In order to further send notification to Payment Service Provider about new order amount, partial capture request has to be send.

Partial Capture Support in Payment Method

In order to add partial capture support the config.xml configuration should set to can_capture_partial value to "1".

<config>
    <default>
        <payment>
            <method_code> <!-- Method Code of your Payment method implementation -->
                <can_capture_partial>1</can_capture_partial>
            </method_code>
        </payment>
    </default>
</config>

The can_capture_partial setting is verified Invoice page rendering in the Magento\Sales\Block\Adminhtml\Order\Invoice\Create\Items class:

public function canEditQty()
{
    if ($this->getInvoice()->getOrder()->getPayment()->canCapture()) {
        return $this->getInvoice()->getOrder()->getPayment()->canCapturePartial();
    }
    return true;
}

During implementation of the Partial Capture request I've found that both the Magento\Payment\Model\Method\Adapter class as well as the Magento\Payment\Model\MethodInterface interface have no capturePartial() method. Should capturePartial() method exists if canCapturePartial() method is used? Is the method missing from the MethodInterface implementation?

There are payment methods (Realex Payments, Cybersource, etc.) require different Capture and Partial Capture requests. As we know the Magento\Payment\Gateway framework allows to isolate each request via Magento\Payment\Gateway\Command\GatewayCommand class configuration. Each Gateway Command is configured as virtual type and added into paymentCommandPool instance:

    <virtualType name="paymentCommandPool" type="Magento\Payment\Gateway\Command\CommandPool">
        <arguments>
            <argument name="commands" xsi:type="array">
                <item name="capture" xsi:type="string">captureVirtualGatewayCommand</item>
                <item name="capture_partial" xsi:type="string">capturePartialVirtualGatewayCommand</item>
            </argument>
        </arguments>
    </virtualType>

With the configuration provided how do we call capture and capture_partial commands based on different conditions? For instance, parent Authorization Transaction has been created or captured order amount is less than order paid.

I am going to talk about Partial Capture command execution flow in my next article.

Don't forget to follow me @max_pronko on Twitter or Facebook Page.

magento 2 payment

Share Article

Related Articles

Comments

Next Article Previous Article

Join Devletter Community

Subscribe to FREE Magento 2 tips and tutorials email I send every week.

I will send you my 3 best tutorials with interesting content immediately.

LinkedIn Twitter Facebook