preloder

Magento 2 Payment Capture Partial Request

Prev Next

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.

Max with his team of Magento enthusiasts won the Best Magento 1 to Magento 2 Migration Award at the Magento Imagine 2017. As CEO at Pronko Consulting, he is actively working with Magento 2, delivering superior customer experience. As one of the most famous developers on a market and in the Magento Community, he launched Magento 2 blog, Magento DevChannel – YouTube channel in 2017 where he shares his knowledge, experience and the best practices in the Magento field.

Posted on Jan 25, 2016

The Devletter

Subscribe to my Magento 2 tips and tutorials content.
Every week I send an interesting email directly to your inbox

Related Posts

Show
Hide
Pre-order the Magento 2 payment integration course TODAY PRE-ORDER
Hello. Add your message here.