2

How to create payment module in Magento 2 with redirection to a payment gateway website?

6

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 and css file to pub/static folder because in few case payment js and template files is not moved to pub/static folder. In that case move this files, else it will not visible in checkout page.
| improve this answer | |
  • 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. – VishalPandita Sep 19 '16 at 9:53
  • Hi Pradeep, command to deploy static content is bin/magento setup:static-content:deploy – Sarvagya Apr 28 '17 at 11:32
  • hi Pradeep, given link is not working.Any help to get the module – Mujahidh Oct 23 '18 at 7:01

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.