2

I want to make a Payment Gateway on magento 2 and I developed a Model. By this I want to get all values from magento 2 and post it our payment gateway means I want to redirect it please suggest me the code is below

namespace Xyz\Abc\Model;
class XyzPaymentMethod extends \Magento\Payment\Model\Method\AbstractMethod
{    
    protected $_code = 'xyzpaymentmethod';

    protected $_isOffline = false;

    public function capture(\Magento\Payment\Model\InfoInterface $payment, $amount)
    {
        echo '<pre>';

        print_r($payment);
        print_r($amount);

        exit();
    }

}
1

There are lots of changes, need to your Payment method class.

  1. Need to redirect to a Magento Custom Page.
  2. Then From this Custom page submit a form which will hit payment gateways.
  3. After Payment done/failure .paygate redirect to you a page

For 1st step1, you need do below change

add

   public function getCheckoutRedirectUrl()
    {
        return $this->_urlBuilder->getUrl('[RouteName]/[Controller]/[Action]');
    }

Your model class should be like this:

<?php

namespace Xyz\Abc\Model;
/* add by Amit Bera*/
use Magento\Sales\Api\Data\OrderPaymentInterface;
use Magento\Sales\Model\Order\Payment;
use Magento\Sales\Model\Order\Payment\Transaction;
use Magento\Quote\Model\Quote;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\Pricing\PriceCurrencyInterface;
/**
 * Bank Transfer payment method model
 *
 * @method \Magento\Quote\Api\Data\PaymentMethodExtensionInterface getExtensionAttributes()
 */
class XyzPaymentMethod extends \Magento\Payment\Model\Method\AbstractMethod
{
    const PAYMENT_METHOD_XYZ_CODE = 'xyzpaymentmethod';

    /**
     * Payment method code
     *
     * @var string
     */
    protected $_code = self::PAYMENT_METHOD_XYZ_CODE;

 /**
     * Availability option
     *
     * @var bool
     */
    protected $_isGateway = false;

    /**
     * Availability option
     *
     * @var bool
     */
    protected $_canOrder = true;

    /**
     * Availability option
     *
     * @var bool
     */
    protected $_canAuthorize = true;

    /**
     * Availability option
     *
     * @var bool
     */
    protected $_canCapture = true;

    /**
     * Availability option
     *
     * @var bool
     */
    protected $_canCapturePartial = true;

    /**
     * Availability option
     *
     * @var bool
     */
    protected $_canRefund = true;

    /**
     * Availability option
     *
     * @var bool
     */
    protected $_canRefundInvoicePartial = true;

    /**
     * Availability option
     *
     * @var bool
     */
    protected $_canVoid = true;

    /**
     * Availability option
     *
     * @var bool
     */
    protected $_canUseInternal = false;

    /**
     * Availability option
     *
     * @var bool
     */
    protected $_canUseCheckout = true;

    /**
     * Availability option
     *
     * @var bool
     */
    protected $_canFetchTransactionInfo = true;

    /**
     * Availability option
     *
     * @var bool
     */
    protected $_canReviewPayment = true;

    /**
     * Website Payments Pro instance
     *
     * @var \Magento\Paypal\Model\Pro
     */
    /**
     * Get instructions text from config
     *
     * @return string
     */
   /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

    /**
     * @var \Magento\Framework\UrlInterface
     */
    protected $_urlBuilder;

    /**
     * @var \Magento\Checkout\Model\Session
     */
    protected $_checkoutSession;

    /**
     * @var \Magento\Framework\Exception\LocalizedExceptionFactory
     */
    protected $_exception;
     /**
     * @var PriceCurrencyInterface
     */
    protected $priceCurrency;   

   /**
     * @param \Magento\Framework\Model\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
     * @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
     * @param \Magento\Payment\Helper\Data $paymentData
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     * @param Logger $logger
     * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
     * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
     * @param array $data
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
     */
      public function __construct(
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
        \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
        \Magento\Payment\Helper\Data $paymentData,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Payment\Model\Method\Logger $logger,
        /* add by Amit Bera */
        \Magento\Framework\UrlInterface $urlBuilder,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Framework\Exception\LocalizedExceptionFactory $exception,
         \Magento\Store\Model\StoreManagerInterface $storeManager,
          PriceCurrencyInterface $priceCurrency,
        /* add by Amit Bera */
        \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
        \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
        array $data = []
    ) {
        parent::__construct(
            $context,
            $registry,
            $extensionFactory,
            $customAttributeFactory,
            $paymentData,
            $scopeConfig,
            $logger,
            $resource,
            $resourceCollection,
            $data
            );



        $this->_storeManager = $storeManager;
        $this->_urlBuilder = $urlBuilder;
        $this->_checkoutSession = $checkoutSession;
        $this->_exception = $exception;         
      }

    public function getInstructions()
    {
        return trim($this->getConfigData('instructions'));
    }
     public function getCheckoutRedirectUrl()
    {
        return $this->_urlBuilder->getUrl('[RouteName]/[Controller]/[Action]');
    }


}

Step2

For create url you need a custom url which is need contoller file

For this follow inchoo block

http://inchoo.net/magento-2/how-to-create-a-basic-module-in-magento-2/

On this file at execute().. you can get curren placed order by below code

$LastorderObject=$this->_objectManager->get('Magento\Checkout\Model\Session');
        if (!$LastorderObject-> getLastRealOrder()->getId()) {
            return $this->resultRedirectFactory->create()->setPath('checkout/cart');
        }

then render block class with phtm file .. on phmtl submit form in hidden process nad redirect to payment gateway

0

In order to build payment module with redirect you have to use Payment Gateway API presented in the Magento/Payment module. First of all you have to remove usage of AbsractMethod class usage due to a fact that this class is deprecated and no longer supported. In the upcoming releases it likely to be removed.

There is Magento/Payment/Model/Method/Adapter class exist as a new Facade when configuring payment method. With this class you can create custom Builders for different payment requests e.g. Cancel payment, Refund payment, Recurring payment and Initialization of a payment during redirect.

This class however, is a starting point for redirect implementation. Please read my answer in the Payment gateway with redirects in Magento 2 where I explain steps of creating redirect to a payment provider

Please also read about Payment Gateway Configuration to configure Magento/Payment/Model/Method/Adapter class via di.xml file for your custom payment module.

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.