<commit_before><?php

/**
 * Checkout.com
 * Authorized and regulated as an electronic money institution
 * by the UK Financial Conduct Authority (FCA) under number 900816.
 *
 * PHP version 7
 *
 * @category  Magento2
 * @package   Checkout.com
 * @author    Platforms Development Team <<EMAIL>>
 * @copyright 2010-2019 Checkout.com
 * @license   https://opensource.org/licenses/mit-license.html MIT License
 * @link      https://docs.checkout.com/
 */

namespace CheckoutCom\Magento2\Plugin;

use Magento\Sales\Api\Data\TransactionInterface;
use Magento\Sales\Model\Order\Payment\Transaction;

/**
 * Class AfterSaveTransaction.
 */
class AfterSaveTransaction
{
    /**
     * @var Session
     */
    public $backendAuthSession;

    /**
     * @var TransactionHandlerService
     */
    public $transactionHandler;

    /**
     * @var String
     */
    public $methodId;

    /**
     * @var Order
     */
    public $order;

    /**
     * @var Transaction
     */
    public $transaction;

    /**
     * AfterSaveTransaction constructor.
     */
    public function __construct(
        \Magento\Backend\Model\Auth\Session $backendAuthSession,
        \Magento\Framework\Registry $registry,
        \CheckoutCom\Magento2\Model\Service\TransactionHandlerService $transactionHandler
    ) {
        $this->backendAuthSession = $backendAuthSession;
        $this->registry = $registry;
        $this->transactionHandler = $transactionHandler;
    }

    /**
     * Update a transaction after save
     */
    public function afterSave(TransactionInterface $subject, $transaction)
    {
        // Prepare the instance properties
        $this->init($transaction);

        // Open the transaction
        if ($this->needsOpening()) {
            $this->transaction->setIsClosed(0);
            $this->transaction->save();
            $this->registry->register(
                $this->getRegistryFlag(),
                true
            );
        }

        return $transaction;
    }

    /**
     * Prepare the instance properties
     */
    public function init($transaction) {
        // Set the loaded transaction
        $this->transaction = $transaction;
<commit_msg>Improved the backend capture transaction opening logic<commit_after><?php

/**
 * Checkout.com
 * Authorized and regulated as an electronic money institution
 * by the UK Financial Conduct Authority (FCA) under number 900816.
 *
 * PHP version 7
 *
 * @category  Magento2
 * @package   Checkout.com
 * @author    Platforms Development Team <<EMAIL>>
 * @copyright 2010-2019 Checkout.com
 * @license   https://opensource.org/licenses/mit-license.html MIT License
 * @link      https://docs.checkout.com/
 */

namespace CheckoutCom\Magento2\Plugin;

use Magento\Sales\Api\Data\TransactionInterface;
use Magento\Sales\Model\Order\Payment\Transaction;

/**
 * Class AfterSaveTransaction.
 */
class AfterSaveTransaction
{
    /**
     * @var Session
     */
    public $backendAuthSession;

    /**
     * @var TransactionHandlerService
     */
    public $transactionHandler;

    /**
     * @var String
     */
    public $methodId;

    /**
     * @var Order
     */
    public $order;

    /**
     * @var Transaction
     */
    public $transaction;

    /**
     * AfterSaveTransaction constructor.
     */
    public function __construct(
        \Magento\Backend\Model\Auth\Session $backendAuthSession,
        \Magento\Framework\Registry $registry,
        \CheckoutCom\Magento2\Model\Service\TransactionHandlerService $transactionHandler
    ) {
        $this->backendAuthSession = $backendAuthSession;
        $this->registry = $registry;
        $this->transactionHandler = $transactionHandler;
    }

    /**
     * Update a transaction after save
     */
    public function beforeSave(TransactionInterface $transaction)
    {
        // Prepare the instance properties
        $this->init($transaction);

        // Open the transaction
        if ($this->needsOpening()) {
            $this->transaction->setIsClosed(0);
            $this->registry->register(
                $this->getRegistryFlag(),
                true
            );
        }

        return $transaction;
    }

    /**
     * Prepare the instance properties
     */
    public function init($transaction) {
        // Set the loaded transaction
        $this->transaction = $transaction;

{'fraction_non_alphanumeric': 0.11254019292604502, 'fraction_numerical': 0.008727606798346348, 'mean_word_length': 2.3168316831683167, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 31, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4308758', 'n_tokens_mistral': 1199, 'n_tokens_neox': 1153, 'n_words': 260}