Search code examples
phpsymfonypaymentpaybox

Lexik paybox installation: Warning: DOMDocument::loadHTML(): Empty string supplied as input


Context: I try to set up the LexikPaybox bundle. I followed the installation guide in the readme.

Problem: The $paybox->getUrl() method raises

Warning: DOMDocument::loadHTML(): Empty string supplied as input

Adress returned by:

dump($this->getWebPage(sprintf(
    '%s://%s%s',
    $server['protocol'],
    $server['host'],
    $server['test_path']
)));
>>> "https://preprod-tpeweb.paybox.com/load.html"

Then, getWebPage() (line 163) returns an empty string that is raising the error in $doc->loadHTML().

My research:

  • There is exactly the same issue here. However, there is no clear answer.

    If I run curl https://tpeweb.paybox.com/load.html , I get the expected html output.

  • There are quite a lot of similar post dealing with the above error. However, I don't think this a code error from the module but more something I'm missing.

Configuration:

Config.yml:

# Lexik Paybox Bundle
lexik_paybox:
    parameters:
        production: false        # Switches between Paybox test and production servers (preprod-tpe <> tpe)
        # Site number provided by the bank
        site:  '1999888'
        # Rank number provided by the bank
        rank:  '32'
        rang: '32'
        # Customer's login provided by Paybox
        login: '2'
        hmac:
            # Key used to compute the hmac hash, provided by Paybox
            key: '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF'
            algorithm:      sha512 # signature algorithm
            signature_name: Sign   # customize the signature parameter name

        currencies:                # Optionnal parameters, this is the default value
            - '978'                # EUR

routing.yml:

# Lexik Paybox Bundle
lexik_paybox:
    resource: '@LexikPayboxBundle/Resources/config/routing.yml'


lexik_paybox_sample_return:
    path:     /payment/return/{status}
    defaults: { _controller: LexikPayboxBundle:Sample:return, status: error }
    requirements:
        status: success|canceled|denied

PaymentController.php:

<?php

namespace Modules\ReservationBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class PaymentController extends Controller {
    /**
     * Sample action to call a payment.
     * It create the form to submit with all parameters.
     * @Route("/paiemant", name="paiemant")
     */
    public function callAction()
    {
        $paybox = $this->get('lexik_paybox.request_handler');
        $paybox->setParameters(array(
            'PBX_CMD'          => 'CMD'.time(),
            'PBX_DEVISE'       => '978',
            'PBX_SITE'          => '1999888',
            'PBX_IDENTIFIANT'   => '107904482',
            'PBX_RANG'          => '32',
            'PBX_PORTEUR'      => 'test@paybox.com',
            'PBX_RETOUR'       => 'Mt:M;Ref:R;Auto:A;Erreur:E',
            'PBX_TOTAL'        => '1000',
            'PBX_TYPEPAIEMENT' => 'CARTE',
            'PBX_TYPECARTE'    => 'CB',
            'PBX_EFFECTUE'     => $this->generateUrl('lexik_paybox_sample_return', array('status' => 'success'), UrlGeneratorInterface::ABSOLUTE_URL),
            'PBX_REFUSE'       => $this->generateUrl('lexik_paybox_sample_return', array('status' => 'denied'), UrlGeneratorInterface::ABSOLUTE_URL),
            'PBX_ANNULE'       => $this->generateUrl('lexik_paybox_sample_return', array('status' => 'canceled'), UrlGeneratorInterface::ABSOLUTE_URL),
            'PBX_RUF1'         => 'POST',
            'PBX_REPONDRE_A'   => $this->generateUrl('lexik_paybox_ipn', array('time' => time()), UrlGeneratorInterface::ABSOLUTE_URL),

//            'PBX_TOTAL'         => '1000',
//            'PBX_DEVISE'        => '978',
//            'PBX_CMD'           => 'CMD'.time(),
//            'PBX_PORTEUR'       => 'test@paybox.com',
//            'PBX_RETOUR'        => 'Mt:M;Ref:R;Auto:A;Erreur:E',
        ));

        return $this->render(
            'LexikPayboxBundle:Sample:index.html.twig',
            array(
                'url'  => $paybox->getUrl(),
                'form' => $paybox->getForm()->createView(),
            )
        );
    }

    /**
     * Sample action of a confirmation payment page on witch the user is sent
     * after he seizes his payment informations on the Paybox's platform.
     * This action must only containts presentation logic.
     */
    public function responseAction($status)
    {
        return $this->render(
            'LexikPayboxBundle:Sample:return.html.twig',
            array(
                'status'     => $status,
                'parameters' => $this->getRequest()->query,
            )
        );
    }
}

Solution

  • As the following solves my issue, I share it. This is not really an answer but might help others people..

    The problem is there is no server found in the request. As results, loadHTML fails on empty string.

    Since this error is raised while looking at the servers and checking if they are correct, I just remove the check and directly pass the URL.

    So the controller becomes:

    public function callAction(){
    
            // Server URL
            $PAYBOX_DOMAIN_SERVER = "preprod-tpeweb.paybox.com";
            $paybox_url = "https://".$PAYBOX_DOMAIN_SERVER."/cgi/MYchoix_pagepaiement.cgi";
    
            $paybox = $this->get('lexik_paybox.request_handler');
            $paybox->setParameters(array(
                'PBX_SITE'          => "1999888",
                'PBX_RANG'          => "32",
                'PBX_IDENTIFIANT'   => "110647233",
                'PBX_TOTAL'         => "999",
                'PBX_DEVISE'        => "978",
                'PBX_CMD'           => "TEST Paybox".mktime(),
                'PBX_PORTEUR'       => "test@paybox.com",
                'PBX_RETOUR'        => "Mt:M;Ref:R;Auto:A;Erreur:E",
                'PBX_HASH'          => "SHA512",
    
    //            'PBX_TYPEPAIEMENT' => 'CARTE',
    //            'PBX_TYPECARTE'    => 'CB',
    
                'PBX_EFFECTUE'     => $this->generateUrl('lexik_paybox_sample_return', array('status' => 'success'), UrlGeneratorInterface::ABSOLUTE_URL),
                'PBX_REFUSE'       => $this->generateUrl('lexik_paybox_sample_return', array('status' => 'denied'), UrlGeneratorInterface::ABSOLUTE_URL),
                'PBX_ANNULE'       => $this->generateUrl('lexik_paybox_sample_return', array('status' => 'canceled'), UrlGeneratorInterface::ABSOLUTE_URL),
                'PBX_RUF1'         => 'POST',
                'PBX_REPONDRE_A'   => $this->generateUrl('lexik_paybox_ipn', array('time' => time()), UrlGeneratorInterface::ABSOLUTE_URL),
            ));
    
            return $this->render(
                'LexikPayboxBundle:Sample:index.html.twig',
                array(
                    'url'  => $paybox_url, //$paybox->getUrl(),
                    'form' => $paybox->getForm()->createView(),
                )
            );
    }
    

    You can still check if the server url is valid by:

    public function checkServer($url){
        $doc = new \DOMDocument();
        $doc->loadHTML($url);
        $element = $doc->getElementById('server_status');
    
        if (!$element || 'OK' != $element->textContent) {
            throw new RuntimeException('No server available.');
        }
    }