Codeigniter + Pagseguro

Asked

Viewed 2,549 times

12

I’m having some difficulty to integrate a system with pagseguro using codeigniter.

I’ve already downloaded the official pagseguro library. https://pagseguro.uol.com.br/v2/guia-de-integracao/tutorial-da-biblioteca-pagseguro-em-php.html

I’ve already created a controller with all the data (in 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 deal with framework pagseguro and do not understand very well. The controller loads normally and no error appears! I would like to know how I do the post to send the data to the pagseguro!

  • It would be good for you to post the full controller here. Another point, have you tried debugging line by line? The controller can even load normally, but that doesn’t mean there’s no error.Try adding more information and if you have an xdebugger try to debug and make sure everything is really working as it should..

2 answers

2


To submit the post there are 2 methods:

the function file_get_contents (Yes it’s weird, I know), and the function curl_.

The function file_get_contents send a POST as long as the second parameter is false and the third 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 { ... }

0

Use this example that Pagseguro created in PHP from Transparent Checkout. This example is intended to illustrate the features of transparent Checkout and the use of JS features.

The file is available [in this link].

In the example, your credentials should be configured in the ?example app Pagsegurodata.class.php' file. The example processes the transaction by sandbox by default but you can change the test to production by passing the false parameter in the creation of the checkout() object, for example in the example file example checkout.php.

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

The file password is uol123.

Remembering that it is ideal you create an account in the pagseguro sandbox to test your application, Sandbox Pagseguro

  • I believe you forgot to put the link in the text, exactly in this part "The file is available [in this link]."

Browser other questions tagged

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