0

I need to create payment module for opencart. The problem how to build xml output in php to post it to remote leasing server url: https://ecredit.leasing.com/eshop_test/UBLOnline.aspx?eshopdata=?

using post method. I trying to build this xml:

<?xml version="1.0" encoding="UTF-8"?>
<ContractRequest xmlns="http://e-credit.ubl.lt/eshop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://e-credit.ubl.lt/eshop http://e-credit.leasing.com/eshop/contractrequest.xsd">
    <SaleLogin>test</SaleLogin>
    <SaleConditionID>111</SaleConditionID>
    <Person>
        <FirstName>John</FirstName>
        <LastName>Jones</LastName>
    </Person>
    <Communication>
        <StreetOrVillage>First</StreetOrVillage>
        <City>New York</City>
        <PostCode>21212</PostCode>
        <Phone>+100000000</Phone>
        <Email>john@mymail.com</Email>
    </Communication>
    <ContractDetails>
        <CreditAmount>617.60</CreditAmount>
        <CreditCurrency>USD</CreditCurrency>
    </ContractDetails>
    <OrderedItemsDetail>
        <Item>
            <Name>1 x HP 250 G5 UMA Celeron N3060 15.6 HD SVA, 1 x HP 15-r101na 15.6 HD Brightview flat (B), </Name>
            <Amount>2pcs.</Amount>
            <Price>617.60</Price>
            <Currency>USD</Currency>
        </Item>
    </OrderedItemsDetail>

</ContractRequest>

I have tried create xml like this:

$XmlString =  '<?xml version="1.0" encoding="UTF-8" ?>'; 
                  $XmlString .= '<ContractRequest 
    xmlns="http://e-credit.ubl.lt/eshop"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://e-credit.ubl.com/eshop http://e-credit.leasing.com/eshop/contractrequest.xsd">'; 


    $XmlString .= '<SaleLogin>' . $data['SaleLogin'] . '</SaleLogin>';
    $XmlString .= '<SaleConditionID>' . $data['SaleConditionID'] . '</SaleConditionID>'; 

    $XmlString .= '<Person>';  
        $XmlString .= '<FirstName>' . $data['first_name'] . '</FirstName>'; 
        $XmlString .= '<LastName>' . $data['last_name'] . '</LastName>'; 

    $XmlString .= '</Person>'; 

    $XmlString .= '<Communication>';
        $XmlString .= '<StreetOrVillage>' . $data['street'] . '</StreetOrVillage>'; 
        $XmlString .= '<City>' . $data['city'] . '</City>'; 
        $XmlString .= '<PostCode>' . $data['postcode'] . '</PostCode>'; 
        $XmlString .= '<Phone>' . $data['telephone'] . '</Phone>'; 
        $XmlString .= '<Email>' . $data['email'] . '</Email>'; 
    $XmlString .= '</Communication>'; 

    $XmlString .= '<ContractDetails>';

        $XmlString .= '<CreditAmount>' . $data['amount'] . '</CreditAmount>'; 
        $XmlString .= '<CreditCurrency>' . $data['currency'] . '</CreditCurrency>'; 
    $XmlString .= '</ContractDetails>'; 

    $XmlString .= '<OrderedItemsDetail>';
        $XmlString .= '<Item>';
            $XmlString .= '<Name>' . $data['description'] . '</Name>'; 
            $XmlString .= '<Amount>' . $data['products_amount'] . '</Amount>'; 
            $XmlString .= '<Price>' . $data['amount'] . '</Price>'; 
            $XmlString .= '<Currency>' .$data['currency'] . '</Currency>'; 
        $XmlString .= '</Item>'; 
    $XmlString .= '</OrderedItemsDetail>'; 


$XmlString .= '</ContractRequest>'; 

and after :

$url = https://ecredit.leasing.com/eshop_test/UBLOnline.aspx?eshopdata=?;
$header = array();

                        $header[] = 'Content-Type: text/xml';
                        $header[] = 'Content-Length: ' . strlen($XmlString);

                        $ch = curl_init();

                        curl_setopt($ch, CURLOPT_URL, &url);
                        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                        //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
                        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
                        curl_setopt($ch, CURLOPT_POSTFIELDS, $XmlString);
                        ///////////////////////
                        $response = curl_exec($ch);

                curl_close($ch);

    $XmlString = simplexml_load_string($response);

            //create object to use as json
            $json = array();



                $json['redirect'] = $this->url->link('checkout/success', '', true);


            $this->response->addHeader('Content-Type: application/json');
            $this->response->setOutput(json_encode($json));

but this does not work... sad.... what i miss?

how to create properly xml request and post it to url?

K. B.
  • 1,388
  • 2
  • 13
  • 25
  • What is the url? ```curl_setopt($ch, CURLOPT_URL, &url);``` – CUGreen Dec 20 '16 at 11:08
  • Did you mean to do ```curl_setopt($ch, CURLOPT_URL, $url);```? – CUGreen Dec 20 '16 at 11:10
  • `&url = https://ecredit.leasing.com/eshop_test/UBLOnline.aspx?eshopdata=?` – K. B. Dec 20 '16 at 11:35
  • Do you mean to write ```$url = 'https://ecredit.leasing.com/eshop_test/UBLOnline.aspx?eshopd‌​ata=?';```? – CUGreen Dec 20 '16 at 11:38
  • Yes this `$url` not `&url`... I need to do request with xml data usingthis `$url` using post method and I need to be forwarded to leasing server if xml data are correct. XML data I have checked with schema... all strings are good. – K. B. Dec 20 '16 at 11:50
  • I'm not sure what the problem is. Can you do ```var_dump($response)``` or ```curl_error($ch)``` before closing the connection and see what comes back? – CUGreen Dec 20 '16 at 12:05
  • Have you tried using urlencode()? `curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($XmlString));` – Bernard van der Velden Dec 20 '16 at 12:32
  • Oh yes I forget to say I getting this errors: `Warning: simplexml_load_string(): Entity: line 60: parser error : xmlParseEntityRef: no name in /mounted-storage/home132/sub005/sc75372-HHAX/xxxx.eu/test/catalog/controller/payment/leasing.php on line 296Warning: simplexml_load_string(): return this.optional(element) || (val >= param[0] && va in /mounted-storage/home132/sub005/sc75372-HHAX/xxxx.eu/test/catalog/controller/payment/leasing.php on line 296` – K. B. Dec 20 '16 at 12:35
  • similar errors for all strings of xml. – K. B. Dec 20 '16 at 12:36
  • I just tried `curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($XmlString));` but result the same. – K. B. Dec 20 '16 at 12:39

1 Answers1

2

You already has some tips (and maybe your answer) in the comments, but here is some thoughts on what you should do to improve your code and also help others to help you.

PHP gives you some good resources to create XML, you should use them. I recommend DOMDocument. Below is a starter code to your example.

$dom = new DOMDocument('1.0', 'utf-8');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;

$namespace = 'http://e-credit.ubl.lt/eshop';
$contractRequest = $dom->createElement('ContractRequest');
$contractRequest->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', $namespace);
$saleLogin = $dom->createElement('SaleLogin', 'test');
$saleConditionID = $dom->createElement('SaleConditionID', '111');

$contractRequest->appendChild($saleLogin);
$contractRequest->appendChild($saleConditionID);
$dom->appendChild($contractRequest);
//you got the idea...

For the curl part, first change the first setopt to curl_setopt($ch, CURLOPT_URL, $url);. Now, before close your curl handle, insert the following code:

if($response === false)
{
    echo 'Curl error: ' . curl_error($ch);
}

If it not work, you can debug your curl request. This another question may help you.

UPDATE

I think that the use of curl for your request is the right option, but, as you requested in the comment, here is an option if your curl request continues to fail.

$payload = array();
$payload['header'] = 'Content-Type: text/xml';
$payload['header'] .= 'Content-Length: ' . strlen($XmlString);
$payload['content'] = is_array($XmlString) ? http_build_query($XmlString) : $XmlString;
$payload['method'] = 'POST';
$payloadContext =  stream_context_create(array('http' => $payload));
$result = file_get_contents($url, false, $payloadContext);
if ($result === false) {
  print_r ("Error in your request. Check logs for more information");
}

As I said, its just an option, but curl remains a good one, you just need to correct it.

miken32
  • 42,008
  • 16
  • 111
  • 154
James
  • 1,653
  • 2
  • 31
  • 60
  • 1
    Ok I'll try in this way DOMDocument. And maybe can you suggest some method how to properly POST this XML. The remote server where I submitting XML working asynchronously. Only After I submit xml I can do request from remote server using GET method. – K. B. Dec 22 '16 at 08:34
  • 1
    @K.B. If your request keeps returning error, I've posted an option for you, but keep in mind that using curl is a good option, you just need to fix your request. – James Dec 22 '16 at 11:56