Redirecting to the Payment Provider
Redirect to a 3rd party site after the order is placed
Very often, after the shipping details are entered and the payment method is chosen on your website, the user needs to be redirected to a third-party site to complete the payment. You can easily do this in Scandi by writing a few plugins.
Note: In this example, we're implementing a specific payment method provider, Mollie. If you are making an extension for a different payment service, the steps outlined below might be slightly different. However, the general workflow of integrating a payment method will be similar: inspect the checkout source code and customize it through plugins.
Getting the Redirection URL
First, we need to get the URL that the user should be redirected to. For many payment extensions, this URL is included in the placeOrder
GraphQL mutation response. Indeed, our example extension, Mollie, includes a mollie_redirect_url
in the order
field of the response.
However, as you may know, in GraphQL, only the fields that have been requested are found in the response. And of course, since mollie_redirect_url
is an extension-specific field, it is not requested by default. Therefore, we need to write a plugin that will request this additional field as part of the placeOrder
mutation.
After inspecting the Scandi source code, you'll find that the file responsible for creating the placeOrder
mutation is scandipwa/src/query/Checkout.query.js
.
All Scandi query creators live in the query
folder. If you need to find the file responsible for a specific query, you can search for this query's name there.
First, let's take a look at the relevant parts of the originalCheckout.query
file, to get an idea of what we're plugging in to:
As you see, the _getOrderField
function is responsible for creating the order
field, and populating it with the order_id
subfield. We want to plug into this function to add an additional subfield β mollie_redirect_url
.
So let's create a new plugin file! In the plugin
directory of your extension, create a new Javascript file that ends in .plugin.js
. It's a good idea to name it after the query it plugs into, so we'll name ours Checkout.query.plugin.js
.
In Scandi, plugins are functions that wrap around the original function (read the documentation for more details of how they work). In this case, it's really simple:
If all goes smoothly, you should see a mollie_redirect_url
value returned in the GraphQL response when completing an order with a Mollie payment method.
You can check the "Network" tab in your browser's developer tools to see the GraphQL responses. This can be useful for debugging requests!
Now, we have the redirect URL in the response, but we're not doing anything with it yet. The next step is to implement the actual redirection functionality.
Redirecting the User
First we need to find out which code is responsible for taking the user through the checkout steps. As the React Developer Tools extension will tell you, it's the Checkout
route. We also know that the business logic (which is what we're interested in right now) most likely lives in the .container
. So let's look at scandipwa/src/route/Checkout/Checkout.container.js
, and the savePaymentMethodAndPlaceOrder
function in particular:
The function sets the payment method for the order, and places the order. After the order is placed, it simply transitions to the order success step (also known as the "details step").
We want to change this functionality β instead of transitioning to the details step, we need to redirect the user to the final payment step, on the 3rd party website. We already made sure that CheckoutQuery.getPlaceOrderMutation
will return the redirection URL in the response, so we just need to use that.
Let's write a plugin for savePaymentMethodAndPlaceOrder
that will redirect the user if necessary. We can add it to our existing Checkout.container.plugin.js
file:
As you might have noticed, we used a couple of custom utility functions, namely redirectToUrl
and setPaymentToken
. Of course we need to define them as well.
It's common for extensions to need new utility functions. They just need to implement them in the util
directory, just like you would in a theme.
The redirectToUrl
in Redirect.js
is a really simple function that I found on StackOverflow (don't tell anyone). Still, it's nice to have it as a reusable function so the rest of the code is more readable.
The setPaymentToken
and getPaymentToken
functions (we haven't used the latter yet) are also very simple. They use the BrowserDatabase
utility to save (and load) the payment token. This token will still be accessible after redirecting the user to the payment provider's page and back.
And with that, the redirection step is complete! If you wish, you can complete an order using Mollie's payment methods to verify that you are indeed redirected.
Last updated