The issue
I'm trying to integrate an online credit card payment service in Magento 2.2, but it's tough to understand all the docs on this topic at first, so I'm looking for some guidance.
What I need to achieve is to post several parameters like shop ID
, secret_key
order ID
, order value
, success url
, error url
etc. to the payment providers URL (form), where the customer can complete the credit card payment.
The provider handles all the security related stuff like storing and processing CC data.
This needs to happen when the customer presses the "Place order" button on checkout if this payment method is selected.
After the payment is complete, the customer should get redirected to the success URL
or error URL
.
My attempts & research
I've created a working payment method so far and I've created a simple payment gateway based on:
-
the official docs http://devdocs.magento.com/guides/v2.2/payments-integrations/payment-gateway/payment-gateway-intro.html on how to create a payment gateway.
-
the official sample payment gateway https://github.com/magento/magento2-samples/tree/master/sample-module-payment-gateway
And I've looked at Paypal Express to see how they do it -> As far as I can tell, it calls a controller in Module_Paypal/Controllers/Express/AbstractExpress/Start.php
with the "Continue to Paypal" button.
Haven't gone into the details of Paypal's approach cause I'm just not sure if this is the right way to go about this…
Questions
-
What is the recommended way to post data and redirect customer to an external Payment processor/provider after an order is placed?
-
Should I go about it as per the official docs – to create a payment gateway? Or is there a better/easier approach?
-
If I need to create a payment gateway, do I need to use an authorize or capture command? (taking into account that I only need an order placed, no invoice processing is necessary)
-
Depending on the approach, I would like some guidance as to how to call the payment provider's payment-form URL, post data to it and redirect the customer to it after the "Place order" button is clicked.
-
Also any suggestions on how to call
checkout/onepage/success
if the transaction was successful for the customer? Or should I redirect to a custom controller if the payment was (un)successful?
EDIT
There is no complex API documentation, all I have to do is call the payment provider's URL with the post
method and pass some parameters to it.
For example, a simple html form would do the trick like this:
<form name="payment-provider-authorize-form" action="https://payment-provider.com/authorize-form" method="post" >
<input type="hidden" name="shop_id" value="$shop_id" />
<input type="hidden" name="order_id" value="$cart_id" />
<input type="hidden" name="amount" value="$total" />
<input type="hidden" name="success_url" value="$success_url" />
...
<input type="submit" value="Buy with Custom Payment provider" />
</form>
I'm currently thinking of:
- redirecting the customer to the payment provider's url with
$.post()
inside theafterPlaceOrder
function and getting the post data from system config throughwindow.checkoutConfig
- or redirecting to a custom controller with the
afterPlaceOrder
function, getting all post parameters there and somehow redirecting to payment provider's url (if this is the way to go, a small code example on how to do this in the controller would be very helpful)
And again I'm still not sure where Magento's Payment gateway commands like authorize
fit into all of this. Do I even need them? Seems like they are meant for more complex integrations and aren't necessary in this situation…
@Joni Jones, could you please expand on your answer to question 4?
How would I post data with $.mage.redirect(window.checkoutConfig.payment...)
? My first thought was using $.post().
Also is the Web API set-payment-information
call really necessary even if I use the placeOrderAfter
function?
Best Answer
As @Yogesh said in the comment, it depends on payment gateway and it's API. I see at least few possible solutions with different variations and without API documentation for payment gateway, it would be just assumption.
If
shop ID
,secret_key
,order ID
,order value
,success url
,error url
are not secure data you can retrieve them from the server, add to your payment form and submit a simple form to the payment gateway. In a similar way, the Transparent Redirect implemented in Magento.As you mentioned in your question, PayPal Express Checkout looks similar to Transparent Redirect but uses a different approach.
Otherwise, if needed data is secure, you need to submit a request to Magento controller and after that from controller redirect to payment gateway.
Now, according to your questions:
afterPlaceOrder()
method fromMagento_Checkout/js/view/payment/default.js
which triggers afterplaceOrder()
method.authorize
orsale
(authorize&capture
) commands, because when Magento places an order it callsauthorize
orcapture
(can be used forsale
, this post describes how to implement it) payment actions. Your integration (according to the provided description) looks different and I suppose you don't need these commands.It looks similar to
Continue to PayPal
button action. You need to call Magento WEB APIset-payment-information
and on the success callback redirect the customer to the payment gateway.setPaymentMethodAction(messageContainer).done( function () { $.mage.redirect(...); } );
UPD: to redirect customer via post you need to submit a form, the
$.post()
will just perform a request to the payment gateway without redirection. Theset-payment-information
required because this request sets selected payment method into a quote and without it, the order creation behavior might be unexpected because the quote can be in an inconsistent state.According to required data (
success url
anderror url
), you will need at least two controllers. As I understand, the payment gateway will return customer to them. In the success controller, you will need to do everything that you need and redirect tocheckout/onepage/success
. In the error controller, you will need to process errors from the payment gateway and redirect customer to some page, like shopping cart or other page.Hopes, my explanation is clearly enough and will help you.