CodeIgniter + PagSeguro

11

I'm having a hard time integrating a system with pagseguro using codeigniter.

I've already downloaded the official pagseguro library. link

I have already created a controller with all the data (in this case for a test);

function pagseguro(){

// $data['dados_pessoa'] = $this->cadastro_model->listar($this->session->userdata('uid'));
// $data['dados_pedido'] = $this->cadastro_model->listarpedidos($this->session->userdata('uid'));


 require_once "PagSeguroLibrary/PagSeguroLibrary.php";

 $paymentRequest = new PagSeguroPaymentRequest();
 $paymentRequest->addItem('12345', 'Software', 1, 50.00);

 $sedexCode = PagSeguroShippingType::getCodeByType('SEDEX');
 $paymentRequest->setShippingType($sedexCode);
 $paymentRequest->setShippingAddress(
    '01452002',  
    'Av. x',  
    '1384',  
    'apto. x',  
    'Jardim x',  
    'São Paulo',  
    'SP',  
    'BRA'
    );
 $paymentRequest->setCurrency("BRL");

 $paymentRequest->setReference('12345');
 $paymentRequest->setRedirectUrl("http://meusite.com.br/pedidos");

 $paymentRequest->addParameter('notificationURL', 'http://www.meusite.com.br/minhapagina');

 try {
    $credentials = PagseguroConfig::getAccountCredentials();
    $checkoutUrl = $paymentRequest->register($credentials);
}
catch (PagseguroServiceException $e) {
    die($e->getMessage());
}

}

It is the first time that I move with the pagseguro in framework and do not understand very well. The controller loads normally and no error appears! I would like to know how do I post to be able to send the data to the pagseguro!

    
asked by anonymous 24.07.2015 / 16:38

2 answers

1

To send the post there are 2 methods:

The function file_get_contents (yes it is strange, I know) , and the function curl_ .

The function file_get_contents sends a POST since the second parameter is false and the third one the data you want to send, below example:

$postdata = http_build_query(
    array(
        'var1' => 'some content',
        'var2' => 'doh'
    )
);
$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);
$context  = stream_context_create($opts);
$result = file_get_contents('http://example.com/submit.php', false, $context);

Using curl_ :

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('postvar1' => 'value1')));

// recebe a resposta do servidor ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);

curl_close ($ch);

// processa o resultado ....
if ($server_output == "OK") { ... } else { ... }
    
29.03.2017 / 15:04
0

Use this example that PagSeguro created in PHP for transparent Checkout. This example is intended to illustrate the functionality of transparent Checkout and the use of JS features.

The file is available [at this link].

In the example, your credentials should be set up in the 'example \\ app \\ PagSeguroData.class.php' file. The example processes the transaction by default, but you can change the test for production by passing the false parameter on the creation of the checkout () object, for example in the file example \\ checkout.php.

<?php
   require "app/Checkout.class.php";
   $checkout = new Checkout(false);
   $checkout->showTemplate();               
?>

The password for the file is uol123.

Remembering that it's a good idea to create an account in the pagseguro sandbox to test your application, Sandbox Pagseguro

    
27.07.2015 / 22:59