How to create payment module in Magento 2 with redirection to a payment gateway website?
1 Answer
To create payment module i used below payment module creator:
http://cedcommerce.com/magento-2-payment-module-creator
Then, for redirection, below is code replaced in
app\code\YOUR_NAME_SPACE\Testpayment\view\frontend\web\js\view\payment\method-renderer\testpayment-method.js
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
/*browser:true*/
/*global define*/
define(
[
'jquery',
'Magento_Checkout/js/view/payment/default',
'Magento_Checkout/js/action/place-order',
'Magento_Checkout/js/action/select-payment-method',
'Magento_Customer/js/model/customer',
'Magento_Checkout/js/checkout-data',
'Magento_Checkout/js/model/payment/additional-validators',
'mage/url',
],
function (
$,
Component,
placeOrderAction,
selectPaymentMethodAction,
customer,
checkoutData,
additionalValidators,
url) {
'use strict';
return Component.extend({
defaults: {
template: 'YOUR_NAME_SPACE_Testpayment/payment/testpayment'
},
placeOrder: function (data, event) {
if (event) {
event.preventDefault();
}
var self = this,
placeOrder,
emailValidationResult = customer.isLoggedIn(),
loginFormSelector = 'form[data-role=email-with-possible-login]';
if (!customer.isLoggedIn()) {
$(loginFormSelector).validation();
emailValidationResult = Boolean($(loginFormSelector + ' input[name=username]').valid());
}
if (emailValidationResult && this.validate() && additionalValidators.validate()) {
this.isPlaceOrderActionAllowed(false);
placeOrder = placeOrderAction(this.getData(), false, this.messageContainer);
$.when(placeOrder).fail(function () {
self.isPlaceOrderActionAllowed(true);
}).done(this.afterPlaceOrder.bind(this));
return true;
}
return false;
},
selectPaymentMethod: function() {
selectPaymentMethodAction(this.getData());
checkoutData.setSelectedPaymentMethod(this.item.method);
return true;
},
afterPlaceOrder: function () {
window.location.replace(url.build('mymodule/standard/redirect/'));
},
/** Returns send check to info */
getMailingAddress: function() {
return window.checkoutConfig.payment.checkmo.mailingAddress;
}
});
}
);
Last step and most important, run
- bin\magento setup:upgrade to install the module
- bin\magento sampledata:deploy to move all
js
andcss
file topub/static
folder because in few case paymentjs
and template files is not moved topub/static
folder. In that case move this files, else it will not visible in checkout page.
-
Hi Pradeep, I tried above but it is not redirecting at all, can you please let me know how you have done it or can you share your code which you have implemented. Sep 19, 2016 at 9:53
-
Hi Pradeep, command to deploy static content is bin/magento setup:static-content:deploy– SarvagyaApr 28, 2017 at 11:32
-