7

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.

2

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.

  • can you please share redirect code? – Abdul Apr 28 '17 at 5:31
  • how to manage default magento 2 the Terms and Conditions checkbox value which has to be passed to the "set-payment-information" before redirecting to the external payment gateway ? – Ashwani Shukla Oct 18 '18 at 13:15
  • @MaxPronko should js component redirecting to external service contain any special logic for security reasons? – Bartosz Kubicki Jun 3 at 8:26
  • It should, in case Payment Service Provider gives some security features like token or authorization. – Max Pronko Jun 4 at 14:11
  • @MaxPronko you mean from magento I should request external payment api for some token and redirect with it in params to this external payment page? – Bartosz Kubicki Jun 5 at 13:21

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.