Handling the Customer's Return

Handle the customer's return from a 3rd party site

After the customer has entered the payment details on the 3rd party page, they are returned to the e-commerce store. Often, there is additional logic required in this step. For example, you might have to fetch the transaction result to display it to the user.

In our case, we need to call the mollieProcessTransaction GraphQL mutation to make sure the transaction is processed, and to retrieve the order result (success/failure, etc.). Finally, we need to display this result to the user, in the form of a success or failure message.

Setting the Return URL

First, we need to ensure that the correct return URL is used. Since all Scandi checkout steps start with checkout, we're going to use checkout/mollie_result for the final order processing and displaying the result.

Mollie allows us to do this really easily by configuring some database values:

<?php
namespace ScandiTutorials\\MollieScandiConfig\\Setup;

use Magento\\Framework\\App\\Config\\ConfigResource\\ConfigInterface;
use Magento\\Framework\\Setup\\InstallDataInterface;
use Magento\\Framework\\Setup\\ModuleContextInterface;
use Magento\\Framework\\Setup\\ModuleDataSetupInterface;
use Mollie\\Payment\\Config;

class InstallData implements InstallDataInterface
{
    /** @var ConfigInterface */
    protected $config;

    public function __construct(ConfigInterface $config) {
        $this->config = $config;
    }

    public function install(
			ModuleDataSetupInterface $setup, ModuleContextInterface $context
		) {
       $setup->startSetup();
       $this->config->saveConfig(Config::GENERAL_USE_CUSTOM_REDIRECT_URL, true);
       $this->config->saveConfig(
           Config::GENERAL_CUSTOM_REDIRECT_URL,
           '{{secure_base_url}}checkout/mollie_result?order_id={{increment_id}}&mollie_payment_token={{payment_token}}&mollie_order_hash={{order_hash}}'
       );
        $setup->endSetup();
    }
}

Now, we need to make sure that the correct business logic takes place after this redirect.

Processing the Order

When the user is redirected back, we need to use the mollieProcessTransaction GraphQL mutation to process the order and get the transaction result.

First, let's take a look at some relevant methods in the CheckoutContainer, which renders all checkout routes, and thus will be involved in checkout/mollie_result as well:

We'll need to change the behavior of __construct, because otherwise it would cause problems by setting an incorrect initial state – such as BILLING_STEP or SHIPPING_STEP – but we actually want a custom MOLLIE_PROCESSING_STEP to be set.

For this, we can write a plugin. The function is very similar to the original one, but sets the correct initial step if applicable:

We'll also plug into componentDidMount, since that's a good place to put business logic that needs to run once when the component is initialized. The original function has some logic that we want to suppress for the Mollie processing step, so we'll check before calling it.

The above plugin makes use of the MollieQuery class. That's a query creator utility for mollieProcessTransaction which we also have to define:

Finally, don't forget to update the .container plugin configuration:

Displaying the Result

After the order has been processed, we need to inform the user of the order status.

Now, we can add plugins for the Checkout .component, which is responsible for the presentation logic of the checkout flow.

Conclusion

At this point, we've covered the main steps in integrating a payment method extension in Scandi. Usually, you need to do some debugging to make sure everything is working properly – payment integration can be tricky! You should be able to test your extension using the test mode of the payment provider.

When you're done with the extension, consider publishing it on the ScandiPWA Marketplace so others can benefit from your creation!

Last updated

Was this helpful?