Find file History
Joan He
Latest commit 81c87e2 Mar 18, 2016

README.md

Synopsis

An extension to add integration with Payment Gateway. This payment method can be restricted to work only with specific Shipping method.

Motivation

This is one of a collection of examples to demonstrate the features of Magento 2. The intent of this sample is to demonstrate how to create own Payment Gateway integration

Technical feature

Module configuration

  1. Package details composer.json.
  2. Module configuration details (sequence) in module.xml.
  3. Module configuration available through Stores->Configuration system.xml

Payment gateway module depends on Sales, Payment and Checkout Magento modules. For more module configuration details, please look through module development docs.

Gateway configuration

On the next step, we might specify gateway domain configuration in config.xml.

Let's look into configuration attributes:
  • debug enables debug mode by default, e.g log for request/response
  • active is payment active by default
  • model Payment Method Facade used for integration with Sales and Checkout modules
  • merchant_gateway_key encrypted merchant credential
  • order_status default order status
  • payment_action default action of payment
  • title default title for a payment method
  • currency supported currency
  • can_authorize whether payment method supports authorization
  • can_capture whether payment method supports capture
  • can_void whether payment method supports void
  • can_use_checkout checkout availability
  • is_gateway is an integration with gateway
  • sort_order payment method order position on checkout/system configuration pages
  • debugReplaceKeys request/response fields, which will be masked in log
  • paymentInfoKeys transaction request/response fields displayed on payment information block
  • privateInfoKeys paymentInfoKeys fields which should not be displayed in customer payment information block

Dependency Injection configuration

To get more details about dependency injection configuration in Magento 2, please see DI docs.

In a case of Payment Gateway, DI configuration is used to define pools of Gateway Commands with related infrastructure and to configure Payment Method Facade (used by Sales and Checkout modules to perform commands)

Payment Method Facade configuration:

<!-- Payment Method Facade configuration -->
<virtualType name="SamplePaymentGatewayFacade" type="Magento\Payment\Model\Method\Adapter">
    <arguments>
        <argument name="code" xsi:type="const">\Magento\SamplePaymentGateway\Model\Ui\ConfigProvider::CODE</argument>
        <argument name="formBlockType" xsi:type="string">Magento\Payment\Block\Form</argument>
        <argument name="infoBlockType" xsi:type="string">Magento\SamplePaymentGateway\Block\Info</argument>
        <argument name="valueHandlerPool" xsi:type="object">SamplePaymentGatewayValueHandlerPool</argument>
        <argument name="commandPool" xsi:type="object">SamplePaymentGatewayCommandPool</argument>
    </arguments>
</virtualType>
  • code Payment Method code
  • formBlockType Block class name, responsible for Payment Gateway Form rendering on a checkout. Since Opepage Checkout uses knockout.js for rendering, this renderer is used only during Checkout process from Admin panel.
  • infoBlockType Block class name, responsible for Transaction/Payment Information details rendering in Order block.
  • valueHandlerPool Value handler pool used for queries to configuration
  • commandPool Pool of Payment Gateway commands

Pools

! All Payment\Gateway provided pools implementations use lazy loading for components, i.e configured with component type name instead of component object

Value Handlers

There should be at least one Value Handler with default key provided for ValueHandlerPool.

!-- Value handlers infrastructure -->
<virtualType name="SamplePaymentGatewayValueHandlerPool" type="Magento\Payment\Gateway\Config\ValueHandlerPool">
    <arguments>
        <argument name="handlers" xsi:type="array">
            <item name="default" xsi:type="string">SamplePaymentGatewayConfigValueHandler</item>
        </argument>
    </arguments>
</virtualType>
<virtualType name="SamplePaymentGatewayConfigValueHandler" type="Magento\Payment\Gateway\Config\ConfigValueHandler">
    <arguments>
        <argument name="configInterface" xsi:type="object">SamplePaymentGatewayConfig</argument>
    </arguments>
</virtualType>
Commands

All gateway commands should be added to CommandPool instance

<!-- Commands infrastructure -->
<virtualType name="SamplePaymentGatewayCommandPool" type="Magento\Payment\Gateway\Command\CommandPool">
    <arguments>
        <argument name="commands" xsi:type="array">
            <item name="authorize" xsi:type="string">SamplePaymentGatewayAuthorizeCommand</item>
            <item name="capture" xsi:type="string">SamplePaymentGatewayCaptureCommand</item>
            <item name="void" xsi:type="string">SamplePaymentGatewayVoidCommand</item>
        </argument>
    </arguments>
</virtualType>

Example of Authorization command configuration:

<!-- Authorize command -->
<virtualType name="SamplePaymentGatewayAuthorizeCommand" type="Magento\Payment\Gateway\Command\GatewayCommand">
    <arguments>
        <argument name="requestBuilder" xsi:type="object">SamplePaymentGatewayAuthorizationRequest</argument>
        <argument name="handler" xsi:type="object">SamplePaymentGatewayResponseHandlerComposite</argument>
        <argument name="transferFactory" xsi:type="object">Magento\SamplePaymentGateway\Gateway\Http\TransferFactory</argument>
        <argument name="client" xsi:type="object">Magento\SamplePaymentGateway\Gateway\Http\Client\ClientMock</argument>
    </arguments>
</virtualType>

<!-- Authorization Request -->
<virtualType name="SamplePaymentGatewayAuthorizationRequest" type="Magento\Payment\Gateway\Request\BuilderComposite">
    <arguments>
        <argument name="builders" xsi:type="array">
            <item name="transaction" xsi:type="string">Magento\SamplePaymentGateway\Gateway\Request\AuthorizationRequest</item>
            <item name="mockData" xsi:type="string">Magento\SamplePaymentGateway\Gateway\Request\MockDataRequest</item>
        </argument>
    </arguments>
</virtualType>
<type name="Magento\SamplePaymentGateway\Gateway\Request\AuthorizationRequest">
    <arguments>
        <argument name="config" xsi:type="object">SamplePaymentGatewayConfig</argument>
    </arguments>
</type>

<!-- Response handlers -->
<virtualType name="SamplePaymentGatewayResponseHandlerComposite" type="Magento\Payment\Gateway\Response\HandlerChain">
    <arguments>
        <argument name="handlers" xsi:type="array">
            <item name="txnid" xsi:type="string">Magento\SamplePaymentGateway\Gateway\Response\TxnIdHandler</item>
            <item name="fraud" xsi:type="string">Magento\SamplePaymentGateway\Gateway\Response\FraudHandler</item>
        </argument>
    </arguments>
</virtualType>
  • SamplePaymentGatewayAuthorizeCommand - instance of GatewayCommand provided by Payment\Gateway configured with request builders, response handlers and transfer client
  • SamplePaymentGatewayAuthorizationRequest - Composite of request parts used for Authorization
  • SamplePaymentGatewayResponseHandlerComposite - Composite\List of response handlers.

Installation

This module is intended to be installed using composer. After including this component and enabling it, you can verify it is installed by going the backend at: STORES -> Configuration -> ADVANCED/Advanced -> Disable Modules Output Once there check that the module name shows up in the list to confirm that it was installed correctly.

Tests

Unit tests could be found in the Test/Unit directory.

Contributors

Magento Core team

License

Open Source License