Generate Secure API Subscription

Asked

Viewed 1,829 times

4

Well, I need to generate subscription via pagseguro API, user should be redirected to the pagseguro page and when finished return to my page, this should be via API.

I tried through XML, with the codes:

$url = "https://ws.sandbox.pagseguro.uol.com.br/v2/pre-approvals/[email protected]&token=********************************";

$xml = '<?xml version="1.0" encoding="charset=ISO-8859-1"?>
<preApprovalRequest>
    <reviewURL>http://www.site.com.br/dashboard/revisao.php</reviewURL>
    <redirectURL>http://www.site.com.br/painel/sucesso.php</redirectURL>
    <reference>EVX01</reference>
    <sender>
        <name>Jose de Solza</name>
        <email>[email protected]</email>
       <phone>
         <areaCode>11</areaCode>
         <number>56273440</number>
       </phone>
       <address>
       <street>Avenida Brigadeiro Faria Lima</street>
         <number>1384</number>
         <complement>1 Andar</complement>
         <district>Jardim Paulistano</district>
         <postalCode>01452002</postalCode>
         <city>São Paulo</city>
         <state>SP</state>
         <country>BRA</country>
       </address>
    </sender>
    <preApproval>
        <charge>auto</charge>
        <name>testes Ultimate</name>
            <details>
                Todos os dias 26 de cada mes
            </details>
        <amountPerPayment>19.90</amountPerPayment>
        <period>Monthly</period>
        <finalDate>2018-01-21T00:00:000-03:00</finalDate>
        <maxTotalAmount>2500.00</maxTotalAmount>
        <date>2016-08-25T20:33:00.sTZD</date>
    </preApproval> 

 </preApprovalRequest>
';




    $xml = str_replace("\n", '', $xml);
    $xml = str_replace("\r",'',$xml);
    $xml = str_replace("\t",'',$xml);

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/x-www-form-urlencoded; charset=ISO-8859-1"));

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    $data = array('entrada' => $xml);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
    $resposta = curl_exec($ch)or die(curl_error($ch));
    curl_close($ch);

        //echo $url;
        //exit;

    $retorno = simplexml_load_string($resposta);
    $xml    = json_encode($retorno);
    $array  = json_decode($xml,TRUE);


    print_r($array);

It’s returning to me:

Array ( [error] => Array ( [code] => 11101 [message] => preApproval data is required. ) )

What’s?

  • You’re running the tags twice, have you tried removing them? <receiver> and <Reference> and <redirectURL> at the end, and at the beginning, vc has already set them.

  • Are you using HTTPS or HTTP? Is there a firewall on your server?

  • Opa @Gustavotinoco removed duplicate tags, thanks nor had seen. But, still the error in xml. The connection is HTTP

  • I just don’t know. But I remind you that it is necessary to register the redirect in the configuration area of the https://pagseguro.uol.com.br/v2/guia-integracao/finalizaca-payment.html#v2-item-redirecting-the-buyer-to-a-url-fixed

2 answers

2


Resolvi, follows full functional code to generate signature via API with Pagseguro:

$url = "https://ws.pagseguro.uol.com.br/v2/pre-approvals/[email protected]&token=************************************";
$xml = '<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
    <preApprovalRequest>
     <redirectURL>http://www.site.com.br/retorno.php</redirectURL>
     <reviewURL>http://www.site.com.br/revisao.php</reviewURL>
     <reference>REF1234</reference>
     <sender>
       <name>Nome do Cliente</name>
       <email>[email protected]</email>
       <phone>
         <areaCode>82</areaCode>
         <number>99999999</number>
       </phone>
       <address>
       <street>Avenida Brigadeiro Faria Lima</street>
         <number>1384</number>
         <complement>1 Andar</complement>
         <district>Jardim Paulistano</district>
         <postalCode>01452002</postalCode>
         <city>São Paulo</city>
         <state>SP</state>
         <country>BRA</country>
       </address>
     </sender>
     <preApproval>
       <charge>auto</charge>
       <name>Seguro contra roubo do Notebook</name>
       <details>Todo dia 28 será cobrado o valor de R$100,00 referente ao seguro contra
                roubo de Notebook
        </details>
       <amountPerPayment>19.90</amountPerPayment>
       <period>Monthly</period>
       <finalDate>2018-01-21T00:00:000-03:00</finalDate>
       <maxTotalAmount>2000.00</maxTotalAmount>
       </preApproval>
     </preApprovalRequest>
';


$xml = str_replace("\n", '', $xml);
$xml = str_replace("\r",'',$xml);
$xml = str_replace("\t",'',$xml);


$curl = curl_init($url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, Array('Content-Type: application/xml; charset=ISO-8859-1'));
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
$xml= curl_exec($curl);


if($xml == 'Unauthorized'){
   // header('Location: paginaDeErro.php');
    echo 'Unauthorized';
    exit;
}

curl_close($curl);

$xml_retorno= simplexml_load_string($xml);

if(count($xml_retorno -> error) > 0)
{
    print_r($xml_retorno -> error).'<br>';
    //header('Location: paginaDeErro.php');
    exit;
}

$xml    = json_encode($xml_retorno);
$array  = json_decode($xml,TRUE);
//print_r($array);

header('Location: https://pagseguro.uol.com.br/pre-approvals/request.html?code='.$array['code']);

0

The mistake 11101 refers to the preApproval data is required as stated in your error reply. In direct English translation of 'data is requered. 'means 'data is required' or 'data is required. As I understand it, you have not filled in all fields as required according to the documentation of the Pagseguro Integration Guide.

URL of Pagseguro Subscription service:

POST https://ws.pagseguro.uol.com.br/v2/pre-approvals/request

The header Content-Type must be informed as in the example below described in the documentation:

Content-Type: application/xml; charset=ISO-8859-1

Note: If your app or store does not use the set of ISO-8859-1 characters, e.g. (UTF-8), it is necessary to replace the charset parameter of the above example.

See below for an example of structured XML to represent payment data:

<preApprovalRequest>
 <redirectURL>http://www.seusite.com.br/retorno.php</redirectURL>
 <reviewURL>http://www.seusite.com.br/revisao.php</reviewURL>
 <reference>REF1234</reference>
 <sender>
   <name>Nome do Cliente</name>
   <email>[email protected]</email>
   <phone>
     <areaCode>11</areaCode>
     <number>56273440</number>
   </phone>
   <address>
   <street>Avenida Brigadeiro Faria Lima</street>
     <number>1384</number>
     <complement>1 Andar</complement>
     <district>Jardim Paulistano</district>
     <postalCode>01452002</postalCode>
     <city>São Paulo</city>
     <state>SP</state>
     <country>BRA</country>
   </address>
 </sender>
 <preApproval>
   <charge>auto</charge>
   <name>Seguro contra roubo do Notebook</name>
   <details>Todo dia 28 será cobrado o valor de R$100,00 referente ao seguro contra
roubo de Notebook</details>
   <amountPerPayment>100.00</amountPerPayment>
   <period>Monthly</period>
   <finalDate>2014-01-21T00:00:000-03:00</finalDate>
   <maxTotalAmount>2400.00</maxTotalAmount>
   </preApproval>
 </preApprovalRequest>

Server Response

If the call is successfully made, Pagseguro will return one XML with the redirect code. The example below shows a successful response to a call to the service:

<preApprovalRequest>
  <code>DC2DAC98FBFBDD1554493F94E85FAE05</code>
  <date>2014-01-21T00:00:00.000-03:00</date>
</preApprovalRequest>

In case the request returns with error:

<errors>
  <error>
    <code>11101</code>
    <message>preApproval data is required.</message>
  </error>
</errors>

A field worth highlighting is the redirectURL, where the same ONLY WILL WORK IF YOUR ACCOUNT IS MARKED "I WANT TO RECEIVE API payments only";

Details

Good Luck!

  • Good morning buddy, I checked the xml you sent, in mine was missing only the <address> and the <phone>, I reviewed the url, I changed the header, it’s okay, but even so continue the error, I will edit the code of the post for the code already changed.

  • @sNniffer You also checked the issue of Content-Type? If it is not in the correct configuration, it will not work. In the documentation it is explicit like this:to use application/x-www-form-urlencoded

  • Yes, I’m setting on Curl curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/x-www-form-urlencoded; charset=ISO-8859-1"));&#xA;

  • @sNniffer sorry man, I made a mistake there but already corrected. The question of Content-type, you’re sending using XML., there has to be Content-Type: application/xml; charset=ISO-8859-1... had forgotten these details.

  • Fuck buddy, I changed the content-type, and changed the error, I get: Array ( [error] => Array ( [code] => Malformed request XML: {0}. [message] => Malformed request XML: Content is not allowed in prolog.. ) ), what is wrong with xml, I could not identify

  • @sNniffer as I told you, let the Content-Type with application/xml same. Because you are sending the parameters in format .xml. This error is giving because . xml is badly formatted.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.