Payment

Creates a payment method

Generated Payment methods can be found in Magento Adminpanel > Stores > Settings > Configuration > Sales > Payment Methods

It allows you to add extra payment methods to Magento. For example if you need to have a payment method which can only be used in the backend or if you need a payment that directly creates an invoice.

Use the Payment snippet in the Magento 2 module creator.

Files

Model/Payment/Test.php

<?php


namespace Mage2Gen\Module\Model\Payment;

class Test extends \Magento\Payment\Model\Method\AbstractMethod
{

    protected $_code = "test";
    protected $_isOffline = true;

    public function isAvailable(
        \Magento\Quote\Api\Data\CartInterface $quote = null
    ) {
        return parent::isAvailable($quote);
    }
}

view/frontend/web/js/view/payment/test.js

define(
    [
        'uiComponent',
        'Magento_Checkout/js/model/payment/renderer-list'
    ],
    function (
        Component,
        rendererList
    ) {
        'use strict';
        rendererList.push(
            {
                type: 'test',
                component: 'Mage2Gen_Module/js/view/payment/method-renderer/test-method'
            }
        );
        return Component.extend({});
    }
);

view/frontend/web/js/view/payment/method-renderer/test-method.js

define(
    [
        'Magento_Checkout/js/view/payment/default'
    ],
    function (Component) {
        'use strict';
        return Component.extend({
            defaults: {
                template: 'Mage2Gen_Module/payment/test'
            },
            getMailingAddress: function () {
                return window.checkoutConfig.payment.checkmo.mailingAddress;
            },
        });
    }
);

view/frontend/web/template/payment/test.html

<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<div class="payment-method" data-bind="css: {'_active': (getCode() == isChecked())}">
    <div class="payment-method-title field choice">
        <input type="radio"
               name="payment[method]"
               class="radio"
               data-bind="attr: {'id': getCode()}, value: getCode(), checked: isChecked, click: selectPaymentMethod, visible: isRadioButtonVisible()"/>
        <label data-bind="attr: {'for': getCode()}" class="label"><span data-bind="text: getTitle()"></span></label>
    </div>
    <div class="payment-method-content">
        <!-- ko foreach: getRegion('messages') -->
        <!-- ko template: getTemplate() --><!-- /ko -->
        <!--/ko-->
        <div class="payment-method-billing-address">
            <!-- ko foreach: $parent.getRegion(getBillingAddressFormName()) -->
            <!-- ko template: getTemplate() --><!-- /ko -->
            <!--/ko-->
        </div>
        <div class="checkout-agreements-block">
            <!-- ko foreach: $parent.getRegion('before-place-order') -->
                <!-- ko template: getTemplate() --><!-- /ko -->
            <!--/ko-->
        </div>
        <div class="actions-toolbar">
            <div class="primary">
                <button class="action primary checkout"
                        type="submit"
                        data-bind="
                        click: placeOrder,
                        attr: {title: $t('Place Order')},
                        css: {disabled: !isPlaceOrderActionAllowed()},
                        enable: (getCode() == isChecked())
                        "
                        disabled>
                    <span data-bind="i18n: 'Place Order'"></span>
                </button>
            </div>
        </div>
    </div>
</div>
        

view/frontend/layout/checkout_index_index.xml

<?xml version="1.0" ?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
	<body>
		<referenceBlock name="checkout.root">
			<arguments>
				<argument name="jsLayout" xsi:type="array">
					<item name="components" xsi:type="array">
						<item name="checkout" xsi:type="array">
							<item name="children" xsi:type="array">
								<item name="steps" xsi:type="array">
									<item name="children" xsi:type="array">
										<item name="billing-step" xsi:type="array">
											<item name="children" xsi:type="array">
												<item name="payment" xsi:type="array">
													<item name="children" xsi:type="array">
														<item name="renders" xsi:type="array">
															<item name="children" xsi:type="array">
																<item name="offline-payments" xsi:type="array">
																	<item name="component" xsi:type="string">Mage2Gen_Module/js/view/payment/offline-payments</item>
																	<item name="methods" xsi:type="array">
																		<item name="test" xsi:type="array">
																			<item name="isBillingAddressRequired" xsi:type="boolean">true</item>
																		</item>
																	</item>
																</item>
															</item>
														</item>
													</item>
												</item>
											</item>
										</item>
									</item>
								</item>
							</item>
						</item>
					</item>
				</argument>
			</arguments>
		</referenceBlock>
	</body>
</page>

etc/payment.xml

<?xml version="1.0" ?>
<payment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Payment:etc/payment.xsd">
	<groups>
		<group id="offline">
			<label>Offline Payment Methods</label>
		</group>
	</groups>
	<methods>
		<method name="test">
			<allow_multiple_address>1</allow_multiple_address>
		</method>
	</methods>
</payment>

etc/config.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
	<default>
		<payment>
			<test>
				<active>1</active>
				<model>Mage2Gen\Module\Model\Payment\Test</model>
				<order_status>pending</order_status>
				<title>Test</title>
				<allowspecific>0</allowspecific>
				<group>Offline</group>
			</test>
		</payment>
	</default>
</config>

etc/adminhtml/system.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
	<system>
		<section id="payment" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="1000" translate="label">
			<group id="test" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label">
				<label>Test</label>
				<field id="active" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label" type="select">
					<label>Enabled</label>
					<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
				</field>
				<field id="title" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="20" translate="label" type="text">
					<label>Title</label>
				</field>
				<field id="order_status" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="30" translate="label" type="select">
					<label>New Order Status</label>
					<source_model>Magento\Sales\Model\Config\Source\Order\Status\NewStatus</source_model>
				</field>
				<field id="allowspecific" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="40" translate="label" type="allowspecific">
					<label>Payment from Applicable Countries</label>
					<source_model>Magento\Payment\Model\Config\Source\Allspecificcountries</source_model>
				</field>
				<field id="specificcountry" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="50" translate="label" type="multiselect">
					<label>Payment from Applicable Countries</label>
					<source_model>Magento\Directory\Model\Config\Source\Country</source_model>
					<can_be_empty>1</can_be_empty>
				</field>
				<field id="sort_order" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="60" translate="label" type="text">
					<label>Sort Order</label>
				</field>
				<field id="instructions" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="70" translate="label" type="textarea">
					<label>Instructions</label>
				</field>
			</group>
		</section>
	</system>
</config>