Magento – Magento 2 – Custom payment module

extensionsmagento2paymentpayment-gatewaypayment-methods

I'm trying to build a custom Payment module, but it's very hard to get things to work because there's no real documentation yet. There are a few example extensions, but these are all outdated.

What I got working:

  • The user selects the Payment method
  • The user can select his bank from a list of dutch banks
  • After the user presses 'Place order' the user is redirected to the success page

What I cannot figure out to do:

  • Redirect the user to payment provider after 'Place order' is pressed
  • I should provide a redirect url to the payment provider where the user is redirected to after payment.
    • The ability to fetch the response of the Payment provider and show the success page to the user.

I'm working on this for days now, but I cannot figure out how to do it. Can someone point me in the right direction?

Thanks.

Best Answer

Redirection can be maintained by place order button handler.
For example please refer to Paypal Express checkout integration, here are several components required:

  1. Payment Method form template - where you need to define a click event handler which processes a place order button.

Example app/code/Magento/Paypal/view/frontend/web/template/payment/paypal-express.html

<button class="action primary checkout" type="submit" data-bind="click: placeOrderAction, enable: (getCode() == isChecked())" disabled> <span data-bind="i18n: 'Continue to Gateway'"></span> </button>

  1. View model(renderer) - a model for defined template.

Example app/code/Magento/Paypal/view/frontend/web/js/view/payment/method-renderer/paypal-express-abstract.js

/** Redirect to Gateway */
        placeOrderAction: function () {
                this.selectPaymentMethod(); // save selected payment method in Quote
                setPaymentMethodAction(this.messageContainer);
                return false;
            }
        }
  1. Define setPaymentMethodAction - which will place order and then redirect user to a gateway.

Example app/code/Magento/Paypal/view/frontend/web/js/action/set-payment-method.js

And "The ability to fetch the response of the Payment provider and show the success page to the user.".

The suggestion depends on Payment Gateway a communication pattern between Store and Payment Gateway. But as I understand your case is Redirect to Payment gateway side and then back to store.

This can be gained by defining a response action and route responsible for handling requests from Payment Gateway with redirect logic to Success page

Related Topic