Magento 2 – Payment Gateway with Redirects

magento2payment-gatewaypayment-methods

How would I implement this exact logic in M2 with the new gateway API? https://github.com/ergopalpatel/Magento_Payment_Gateway_Skeleton

Steps

  1. Customer selects a custom payment method
  2. When 'Place Order' is clicked, customer is redirected to the external payment page with some additional POST data
  3. Payment page redirects on success/failure back to a specified magento controller, which processes the result

I had a look around and found that \Magento\Payment\Model\Method\Cc is deprecated and that you should use the gateway api. I read the articles by Max Pronko and https://www.maxpronko.com/blog/magento-2-payment-gateway-api and tried to analyze the sample magento module and Braintree module, but I just can't wrap my head around those, either too much or too little going on.

Where do I even start if I want to implement this flow with redirects?
Is there a comprehensive documentation somewhere or a module implemented on the gateway API that does exactly what I need?

EDIT

I dug into the Magento_Payment code and it seems like this kind flow is not supported at all, classes implementing the Magento\Payment\Gateway\Http\ClientInterface, which handle the requests and responses, seems to be only working with synchronous requests against a webapi.

So what are the options then? Use deprecated components?

EDIT

Seems like using the Magento\Payment\Model\MethodInterface (which has an @api annotation, so I'm hoping it won't get deprecated anytime soon) and implementing your own logic for redirects/validations etc is the way to go as of now.

Best Answer

In order to implement custom payment module with redirect you have to implement the following logic:

  1. Create Frontend logic responsible for rendering custom payment method with "Place Order" button.
  2. Configure payment method for "initialisation" during placing order, so Order will be placed with "Pending Payment" status.
  3. Once "place order" logic is executed and order is saved via default JavaScript Payment component you have to send WebAPI POST request to backend server (in case you need to send HTTP POST to a payment method before redirecting customer).

You should implement whole Payment Gateway infrastructure of classes:

  • Builders classes for pre-populating POST parameters for request
  • Handlers for processing response from payment gateway
  • Validators for verifying response from payment gateway
  • Custom commands in case GatewayCommand does not work for you and you need own processing logic when sending request to a payment provider.
  • New Magento/Payment/Model/Method/Adapter class is now a centric for payment integrations, it is a must have to configure it with custom commands.

-

  1. Retrieve result to custom JavaScript component from Web API call execution and then redirect Customer to a Payment page.
  2. Create controllers, 1 for success and second one for failure in case Payment provider allows to set 2 URLs for each state. If not, create 1 controller where depending on a validation and additional logic you need to add in order to change status and create "capture" or "authorise" payment render different layout updates "custompayment_success" or "custompayment_error".

Few clarifications:

  • Class Magento\Payment\Model\Method\Cc is indeed deprecated and should not be used.
  • Interface Magento\Payment\Gateway\Http\ClientInterface should be used for sending background requests to a payment gateway. There is even 2 implementations for ClientInterface

Should you have any questions please let me know.

Related Topic