重定向支付网关magento2

时间:2016-10-23 00:30:05

标签: redirect post payment-gateway magento2 payment-method

我是Magento的新手,我正在magento2.1中实施自定义付款方式,但我完全迷失了。所需的信息流如下:

  1. 客户前往结帐并输入所有信息,然后转到付款方式,他将选择我的自定义方式并按“下订单”按钮。

  2. 按下按钮后,我必须捕获订单,产品,金额,送货地址的信息,并添加其他信息,如('签名' - 验证哈希,'urlResponse'和'urlConfirmation'然后我需要将Post请求中的这些参数发送到Gateway Provider URL。我不需要进行任何类型的验证,只需抓取数据,再添加一些并发送即可。

  3. 阅读了Max Pronko的教程后

    https://www.maxpronko.com/blog/magento-2-payment-gateway-api

    (我无法复制其他链接,因为缺少积分,但在这一点的末尾有一个对另一个的引用)。

    我试图实现它,但我没有运气。据我所知,在按下“下订单”按钮后,请求被发送到捕获方法,我可以执行必要的逻辑,然后创建一个TransferObject,然后发送它(如何?)。

    这是我的结构:

    custom payment file structure

    在Vendor / PayU / etc / frontend / di.xml中我有:

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <preference for="Magento\Payment\Gateway\Command\CommandPoolInterface" type="Magento\Payment\Gateway\Command\CommandPool" />
    
        <virtualType name="Vendor\PayU\Model\Payment\Command\CaptureGateway" type="Magento\Payment\Gateway\Command\GatewayCommand">
            <arguments>
                <argument name="requestBuilder" xsi:type="object">Vendor\PayU\Model\Payment\Request\Capture</argument>
            </arguments>
        </virtualType>
    
        <virtualType name="Vendor\PayU\Gateway\Command\CommandPool" type="Magento\Payment\Gateway\Command\CommandPool">
            <arguments>
                <argument name="commands" xsi:type="array">
                    <item name="capture" xsi:type="string">Vendor\PayU\Model\Payment\Command\CaptureGateway</item>
                </argument>
            </arguments>
        </virtualType>
    
        <virtualType name="PayUPaymentMethodAdapter" type="Magento\Payment\Model\Method\Adapter">
            <arguments>
                <argument name="code" xsi:type="const">Vendor\PayU\Model\Payment::METHOD_CODE</argument>
                <argument name="valueHandlerPool" xsi:type="object">PayUValueHandlerPool</argument>
                <argument name="validatorPool" xsi:type="object">PayUValidatorPool</argument>
                <argument name="commandPool" xsi:type="object">PayUCommandPool</argument>
                <argument name="formBlockType" xsi:type="object">Magento\Payment\Block\Form\Cc</argument>
                <argument name="infoBlockType" xsi:type="object">Magento\Payment\Block\Info\Cc</argument>
            </arguments>
        </virtualType>
    
        <virtualType name="PayUConfig" type="Magento\Payment\Gateway\Config\Config">
            <arguments>
                <argument name="methodCode" xsi:type="const">Vendor\PayU\Model\Payment::METHOD_CODE</argument>
            </arguments>
        </virtualType>
    
        <virtualType name="PayUConfigValueHandler" type="Magento\Payment\Gateway\Config\ConfigValueHandler">
            <arguments>
                <argument name="configInterface" xsi:type="object">PayUConfig</argument>
            </arguments>
        </virtualType>
    
        <virtualType name="PayUValueHandlerPool" type="Magento\Payment\Gateway\Config\ValueHandlerPool">
            <arguments>
                <argument name="handlers" xsi:type="array">
                    <item name="default" xsi:type="string">PayUConfigValueHandler</item>
                </argument>
            </arguments>
        </virtualType>
    
        <virtualType name="CountryValidator" type="Magento\Payment\Gateway\Validator\CountryValidator">
            <arguments>
                <argument name="config" xsi:type="object">PayUConfig</argument>
            </arguments>
        </virtualType>
    
        <virtualType name="PayUGlobalValidator" type="Magento\Payment\Gateway\Validator\ValidatorComposite">
            <arguments>
                <argument name="validators" xsi:type="array">
                    <item name="country" xsi:type="string">CountryValidator</item>
                </argument>
            </arguments>
        </virtualType>
    
        <virtualType name="PayUValidatorPool" type="Magento\Payment\Gateway\Validator\ValidatorPool">
            <arguments>
                <argument name="validators" xsi:type="array">
                    <item name="global" xsi:type="string">PayUGlobalValidator</item>
                </argument>
            </arguments>
        </virtualType>
    
        <virtualType name="PayUCaptureGatewayCommand" type="Magento\Payment\Gateway\Command\GatewayCommand">
            <arguments>
                <argument name="requestBuilder" xsi:type="object">Vendor\PayU\Model\Payment\Request\Capture</argument>
                <argument name="handler" xsi:type="object">Vendor\PayU\Model\Payment\Response\Capture</argument>
                <argument name="transferFactory" xsi:type="object">Vendor\PayU\Gateway\Http\TransferFactory</argument>
            </arguments>
        </virtualType>
    
        <virtualType name="PayUCommandPool" type="Magento\Payment\Gateway\Command\CommandPool">
            <arguments>
                <argument name="commands" xsi:type="array">
                    <item name="capture" xsi:type="string">PayUCaptureGatewayCommand</item>
                </argument>
            </arguments>
        </virtualType>
    
        <type name="Vendor\PayU\Model\Payment">
            <arguments>
                <argument name="commandPool" xsi:type="object">Vendor\PayU\Gateway\Command\CommandPool</argument>
            </arguments>
        </type>
    </config>
    

    在Vendor / PayU / Model / Payment.php中我有:

    <?php
    
    namespace Vendor\PayU\Model;
    
    use Magento\Payment\Model\InfoInterface;
    use Magento\Payment\Gateway\Command\CommandPoolInterface;
    use Magento\Payment\Gateway\CommandInterface;
    
    class Payment implements MethodInterface, PaymentMethodInterface
    {
        /**
         * @var \Magento\Payment\Gateway\Command\CommandPoolInterface
        */
        protected $commandPool;
    
        /**
         * @var CommandPoolInterface
        */
        public function __construct(CommandPoolInterface $commandPool) {
            $this->commandPool = $commandPool;
        }
    
        /**
         * @param InfoInterface $payment
         * @param float $amount
         * @return $this
         * @api
        */
        public function capture(InfoInterface $payment, $amount)
        {
            /** @var CommandInterface $captureGatewayCommand */
            $captureGatewayCommand = $this->commandPool->get('capture');
    
            $captureGatewayCommand->execute([
                'payment' => $payment,
                'amount' => $amount
            ]);
        }     
    }
    

    我是否正在实施合适的课程?我还需要哪些其他文件? 如果有人能指出我正确的方向,我将不胜感激。

2 个答案:

答案 0 :(得分:0)

首先,看看magento 2模块的官方样本。 https://github.com/magento/magento2-samples/tree/2.1/sample-module-payment-gateway 据我所知,你不应该有真正的支付模式。您刚刚在di.xml中创建了付款模型。按代码:

<virtualType name="PayUPaymentMethodAdapter" type="Magento\Payment\Model\Method\Adapter">
    <arguments>
        <argument name="code" xsi:type="const">Vendor\PayU\Model\Payment::METHOD_CODE</argument>
        <argument name="valueHandlerPool" xsi:type="object">PayUValueHandlerPool</argument>
        <argument name="validatorPool" xsi:type="object">PayUValidatorPool</argument>
        <argument name="commandPool" xsi:type="object">PayUCommandPool</argument>
        <argument name="formBlockType" xsi:type="object">Magento\Payment\Block\Form\Cc</argument>
        <argument name="infoBlockType" xsi:type="object">Magento\Payment\Block\Info\Cc</argument>
    </arguments>
</virtualType>

PayUPaymentMethodAdapter是您的支付模式,其中包含所有需要的数据,您应该只放置所有必需的东西(命令,验证器等),Magento会自动调用。所以Magento \ Payment \ Model \ Method \ Adapter只是Facade,它包含所有支付模块功能。 Magento 2团队使用命令设计模式进行所有支付操作,例如(void,capture,authorize)。您应该识别适当的命令,Magento \ Payment \ Model \ Method \ Adapter将自动调用它们。请检查magento-samples付款方式。

答案 1 :(得分:0)

在我的Magento 2 17 payment extensions中,我已通过以下方式实施了从Magento付款页面到付款服务提供商或Magento«结帐成功»页面的重定向: https://github.com/mage2pro/core/blob/2.12.5/Payment/view/frontend/web/mixin.js#L350-L401