Solving the problem of Internet-Acquiring for an online store on Magento 2 and the LiqPay payment system

13 May 2024

next article
Oleksandr Kavyuk

Backend Developer

Oleksandr Kavyuk
Solving the problem of Internet-Acquiring for an online store on Magento 2 and the LiqPay payment system

About the author: Oleksandr Kavyuk, Magento 2 developer with more than 10 years of experience in creating online stores. Worked with various platforms using PHP programming language. He has experience in solving difficult tasks, about which others said "it's impossible".


The online store should be convenient and at the same time simple for the buyer. Only then online shopping will bring the most benefit to both the client and the owner of the online store. During the pandemic, the culture of online shopping has undergone many changes, and the requirements for online stores have increased significantly. That is why your site today should have all the possibilities for a convenient purchase in order to withstand the competition and increase the number of customers.

Why is Internet-Acquiring important ?

Internet acquiring is one of the important attributes of any modern online store. Thanks to it, buyers can pay for goods or services directly on the site, using their bank card or electronic wallet. This service allows you to accept bank cards of international and national payment systems. Like any ready-made service, Internet acquiring involves the presence of several participants:

  • acquirer — a legal entity (in particular, a bank) that accepts transactions from merchants and sends them to the corresponding payment system;

  • the issuing bank is the bank that issued the payer's card;

  • service provider is an online service that integrates the website of the online merchant with the acquirer.

In practice, the buyer goes to the secure page of the processing center to pay for the order, enters his card details and pays for the order. Today, many names of different services are known, so our clients choose a system according to their own wishes and preferences, such as localization, commissions, and others. For developers, an important issue is the availability of an API for integration with an online store.

Briefly about LiqPay

In the specific case of Avivi's clients, LiqPay was chosen as an internet acquiring service — one of the common payment systems: it is very convenient to manage and integrate into any platform. The developers of the service provided an API for integration with websites on Magento 2, so there were no difficulties at the time of project planning.

scrnli_5_8_2024_11-22-04 AM.png

Among the advantages of this service, we can note many options of different payment methods available to end customers. Also, LiqPay is quite loyal in terms of commissions, which makes the service attractive for online store owners and contributes to its popularity in the world of eCommerce. However, despite all the advantages, one cannot rule out the hidden difficulties that we encountered.

Problems

LiqPay service has a well-described API and many modules for integration. An extension from LiqPay was installed on our client's website, all necessary accesses were created and settings were made. The payment interaction worked as planned, except for the last step.

Magento vs liqupay ісруьф.png

The problem was with receiving the Callback method with information about the payment status, so the site system did not know whether the buyer had paid for his order. This led to critical inconveniences:

  • buyers have worries about their funds;

  • automation of the process suffers, since the site managers do not have information about the payment status.

With such an error, the use of Internet acquiring is absolutely impossible, so this problem had to be solved custom.

Research and resolution

the Callback correctly. The next step is to add logging in the place where we need to get the result. In the payment system office, in the payment logs, we can see that the data is received correctly and the request is made. But from the site we get a status of 400 (bad request).

room 400.png

This situation prompted the idea to analyze requests from LiqPay. And it was the right decision, because it became clear that the content type "application /x-www-form-urlencoded" is transferred in the header. In Magento 2 out of the box, WEBAPI_REST recognizes the following types:

●     application / json;

●     application / xml;

●     application / xhtml+xml;

●      text / xml.

Any other types will result in an error message.

Since we cannot influence requests from LiqPay, it was decided to custom expand the capabilities of the regular deserilizer.

A module with the corresponding method has been created:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
namespace Vendor\ModuleName\Webapi\Rest\Request\Deserializer;

use InvalidArgumentException;
use Magento\Framework\App\State;
use Magento\Framework\Phrase;
use Magento\Framework\Stdlib\Parameters;
use Magento\Framework\Webapi\Rest\Request\DeserializerInterface;

/**
* Class FormUrlencoded
*/
class FormUrlencoded implements DeserializerInterface
{
   /**
    * @var Parameters
    */
   private $parameters;
   /**
    * @var State
    */
   protected $_appState;
   /**
    * FormUrlencoded constructor.
    * @param Parameters $parameters
    * @param State $appState
    */
   public function __construct(
       Parameters $parameters,
       State $appState
   ) {
       $this->parameters = $parameters;
       $this->_appState = $appState;
   }
   /**
    * Parse Request body into array of params.
    *
    * @param string $encodedBody Posted content from request.
    * @return array|null Return NULL if content is invalid.
    * @throws InvalidArgumentException
    * @throws \Magento\Framework\Webapi\Exception If decoding error was encountered.
    */
   public function deserialize($encodedBody)
   {
      
       if (!is_string($encodedBody)) {
           throw new \InvalidArgumentException(
               sprintf('"%s" data type is invalid. String is expected.', gettype($encodedBody))
           );
       }
       try {
           $decodedBody = urldecode($encodedBody);
           $this->parameters->fromString($decodedBody);
           return $this->parameters->toArray();
       } catch (\InvalidArgumentException $e) {
           if ($this->_appState->getMode() !== State::MODE_DEVELOPER) {
               throw new \Magento\Framework\Webapi\Exception(new Phrase('Decoding error.'));
           } else {
               throw new \Magento\Framework\Webapi\Exception(
                   new Phrase(
                       'Decoding error: %1%2%3%4',
                       [PHP_EOL, $e->getMessage(), PHP_EOL, $e->getTraceAsString()]
                   )
               );
           }
       }
   }
}

And ask for its connection in / app / etc / di.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<type name="Magento\Framework\Webapi\Rest\Request\DeserializerFactory">
   <arguments>
       <argument name="deserializers" xsi:type="array">
            …
           <item name="application_x_www_form_urlencoded" xsi:type="array">
               <item name="type" xsi:type="string">application/x-www-form-urlencoded</item>
               <item name="model" xsi:type="string">Vendor\ModuleName\Webapi\Rest\Request\Deserializer\FormUrlencoded</item>
           </item>
       </argument>
   </arguments>
</type>

After installing the module, do not forget about the need to recompile the entire project. And now you can check the result:

room 500.png

As you can see, the request works normally in the LiqPay office.

Similarly, in the administrative part of the site, the order changed its status and an invoice was created.

magento invoice.png

So, by creating a custom module for Magento 2, I allowed the site to process the request from LiqPay and update the payment status of the order. The working out of the business process has been restored, which means that now customers can calmly use Internet-Acquiring and engage in online shopping without problems.

Conclusion

Magento 2 platform is a powerful and perfect tool for the development of eCommerce projects. However, it is impossible to predict all cases where certain errors may occur. The good news is that the flexibility of Magento 2 allows you to fix almost any problem by custom developing add-ons for the platform. The Avivi team has a lot of experience in this work, so we will be happy to help you create the best and most efficient online store.

Baner articles 2023 eng.png


Similar articles
Apply for a consultation

We will reach out to within 10 minutes