Generate Signature Pagseguro API

4

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

I've 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);

You are returning me:

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

What's up?

    
asked by anonymous 28.07.2016 / 22:42

2 answers

2

Resolved, follows functional full code to generate signature via API with the 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']);
    
13.09.2016 / 18:49
0

The error 11101 refers to preApproval data is required as stated in your error response. In the Portuguese translation of 'data is requered.' means 'data is required' or 'data is required.' As I understand it, you did not fill out all the fields as required according to the documentation of the Integration Guide of PagSeguro .

PagSeguro Subscription Service URL:

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

The Content-Type header should be reported as in the example below in the documentation:

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

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

See below structured XML example to represent the data of a payment:

<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 successful, PagSeguro will return a XML with the redirect code. The example below shows a successful response to a call to the subscription service:

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

If the request returns in error:

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

A field that deserves prominence is redirectURL , where it is ONLY   IT WILL WORK IF YOUR ACCOUNT IS ACCOUNTED "I want to receive   only payments via API ";

Details

Good luck!

    
31.08.2016 / 15:29