I am working on the Magento 2 payment gateway. The backend is completed but in frontend we stuck for getting the Magento 2 customer detail and post it to our 3rd party API. Please suggest.
Magento – How to redirect from magento 2 checkout page to the 3rd party payment gateway api
apimagento2payment-gatewayPHP
Related Solutions
You don't want to use any curl or server side stuff. This would mean the user submits his credentials to you and not the payment provider. Man in the middle
for free.
Have a look on the paypal redirect: \Mage_Paypal_Block_Standard_Redirect
.
Use a <form>
with all the informations and submit it with JavaScript
There are lots of changes, need to your Payment method class.
- Need to redirect to a Magento Custom Page.
- Then From this Custom page submit a form which will hit payment gateways.
- 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
Best Answer
To implement payment gateway on Magento 2, I would recommend you to use the following reference:
http://inchoo.net/magento-2/implementing-payment-gateway-magento-2/
This is simple in words and also a best solution to solve your problem.