cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with custom payment module

Problem with custom payment module

Hello, we are developing a payment gateway module for Magento 2.X but we have trouble figuring out why it is not working. We think maybe the /etc/di.xml file is not correct:

 

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Girosolution All rights reserved.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <virtualType name="Girosolution\Girocheckout\Block\Form\Girocheckoutmethod\Default" type="Magento\Girocheckout\Block\Form" shared="false">
        <arguments>
            <argument name="data" xsi:type="array">
                <item name="template" xsi:type="string">Girosolution_Girocheckout::form/paymentmethod/default.phtml</item>
            </argument>
        </arguments>
    </virtualType>

    <virtualType name="GirosolutionCommandPool" type="Magento\Payment\Gateway\Command\CommandPool">
        <arguments>
            <argument name="commands" xsi:type="array">
                <item name="giropay_sale" xsi:type="string">Girosolution\Girocheckout\Gateway\Giropay\Command\SaleCommand</item>
            </argument>
        </arguments>
    </virtualType>

    <virtualType name="GirosolutionCommandManager" type="Magento\Payment\Gateway\Command\CommandManager">
        <arguments>
            <argument name="commandPool" xsi:type="object">GirosolutionCommandPool</argument>
        </arguments>
    </virtualType>

    <type name="Magento\Payment\Gateway\Command\CommandManagerPool">
        <arguments>
            <argument name="executors" xsi:type="array">
                <item name="girosolution" xsi:type="string">GirosolutionCommandManager</item>
            </argument>
        </arguments>
    </type>
</config>

Then we have the file app\code\Girosolution\Girocheckout\Gateway\Giropay\Command\SaleCommand.php but the function execute() is never triggered when the customer try to pay with our payment method giropay. The code is:

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Girosolution\Girocheckout\Gateway\Giropay\Command;

use Magento\Payment\Gateway\Command;
use Magento\Payment\Gateway\Command\ResultInterface;
use Magento\Payment\Gateway\CommandInterface;
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
use Magento\Payment\Helper\Formatter;
use Magento\Paypal\Model\Payflow\Transparent;

class SaleCommand implements CommandInterface
{
    use Formatter;

    /**
     * @var Transparent
     */
    private $girocheckoutFacade;

    /**
     * SaleCommand constructor.
     * @param Transparent $girocheckoutFacade
     */
    public function __construct(
        Transparent $girocheckoutFacade
    ) {
        $this->girocheckoutFacade = $girocheckoutFacade;
    }

    /**
     * Executes command basing on business object
     *
     * @param array $commandSubject
     * @return null|ResultInterface
     */
    public function execute(array $commandSubject)
    {
        error_log("execute() in SaleCommand.php");
        /** @var double $amount */
        $amount = $commandSubject['amount'];
        /** @var PaymentDataObjectInterface $paymentDO */
        $paymentDO = $commandSubject['payment'];
        $payment = $paymentDO->getPayment();

        $storeId = $paymentDO->getOrder()->getStoreId();
        $this->girocheckoutFacade->setStore($storeId);

        /** @var \Magento\Vault\Api\Data\PaymentTokenInterface $token */
        $token = $payment->getExtensionAttributes()->getVaultPaymentToken();

        $request = $this->girocheckoutFacade->buildBasicRequest();
        $request->setAmt($this->formatPrice($amount));
        $request->setTrxtype(Transparent::TRXTYPE_SALE);
        $request->setOrigid($token->getGatewayToken());

        $this->girocheckoutFacade->addRequestOrderInfo($request, $payment->getOrder());

        $response = $this->girocheckoutFacade->postRequest($request, $this->girocheckoutFacade->getConfig());
        $this->girocheckoutFacade->processErrors($response);
        $this->girocheckoutFacade->setTransStatus($payment, $response);
    }
}

We really need some help, thanks in advance.

 

1 REPLY 1

Re: Problem with custom payment module

You have to create "capture" or "authorize" commands in order to see "execute()" method in action.

 

    <virtualType name="GirosolutionCommandPool" type="Magento\Payment\Gateway\Command\CommandPool">
        <arguments>
            <argument name="commands" xsi:type="array">
                <item name="capture" xsi:type="string">Girosolution\Girocheckout\Gateway\Giropay\Command\SaleCommand</item>
            </argument>
        </arguments>
    </virtualType>

Preferable with "CaptureCommand" class name and its implementation of "execute()" method.

 

Then once you have "capture" command you have to have configuration Facade for custom payment method.

 

<virtualType name="GiropayRemoteFacade" type="Magento\Payment\Model\Method\Adapter">
<arguments>
<argument name="code" xsi:type="string">realex</argument>
<argument name="valueHandlerPool" xsi:type="object">GiropayValueHandlerPool</argument>
<argument name="validatorPool" xsi:type="object">GiropayValidatorPool</argument>
<argument name="commandPool" xsi:type="object">GiropayommandPool</argument>
</arguments>
</virtualType>

 And then include this facade name into config.xml for your custom payment.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<payment>
<realex>
<model>GiropayRemoteFacade</model>
</realex>
</payment>
</default>
</config>

Once you have all these set up, Magento Payment will trigger "\Magento\Payment\Model\Method\Adapter::capture()" method and trigger your command.

public function capture(InfoInterface $payment, $amount)
{
$this->executeCommand(
'capture',
['payment' => $payment, 'amount' => $amount]
);

return $this;
}

Hope this helps.