/**
  * 
  * @param \Controller $controller
  * @throws RuntimeException
  * @since 2.1
  */
 public function startup(\Controller $controller)
 {
     $this->config = Configure::read('PagSeguro');
     if (empty($this->config)) {
         throw new RuntimeException('Você precisa definir as configurações básicas do plugin "PagSeguro", leia o manual.');
     }
     if (isset($this->config['isSandbox']) && true === $this->config['isSandbox']) {
         PagSeguroConfig::setEnvironment('sandbox');
     }
     $this->credenciais = new PagSeguroAccountCredentials($this->config['email'], $this->config['token']);
 }
 public function pay($id)
 {
     \PagSeguroLibrary::init();
     \PagSeguroConfig::setEnvironment('production');
     $this->plan = Plans::find($id);
     $this->client = User::find(Auth::user()->id);
     $this->payment = Payments::create(['plan_name' => $this->plan->name, 'plan_value' => $this->plan->value, 'plan_id' => $this->plan->id, 'user_id' => $this->client->id, 'confirmed' => 0]);
     // Instantiate a new payment request
     $paymentRequest = new \PagSeguroPaymentRequest();
     // Set the currency
     $paymentRequest->setCurrency("BRL");
     /* // Add an item for this payment request
        $paymentRequest->addItem('0001', 'Sempre da Negócio - Plano '.$this->plan->name, 1, $this->plan->value);*/
     $paymentRequest->addItem($this->plan->id, 'Sempre da Negócio - Plano ' . $this->plan->name, 1, $this->plan->value);
     // Set a reference code for this payment request. It is useful to identify this payment
     // in future notifications.
     $paymentRequest->setReference($this->payment->id);
     //Create object PagSeguroShipping
     $shipping = new \PagSeguroShipping();
     //Set Type Shipping
     $type = new \PagSeguroShippingType(3);
     $shipping->setType($type);
     //Set address of client
     $data = array('postalCode' => $this->client->zipcode, 'street' => $this->client->address, 'number' => $this->client->number, 'city' => $this->client->city, 'state' => $this->client->state);
     $address = new \PagSeguroAddress($data);
     $shipping->setAddress($address);
     //Add Shipping to Payment Request
     $paymentRequest->setShipping($shipping);
     // Set your customer information.
     $phone = str_replace(['(', ')', ' ', '-'], ['', '', '', ''], $this->client->phone);
     $paymentRequest->setSender($this->client->name, $this->client->email_responsible, substr($phone, 0, 2), substr($phone, 2));
     try {
         /*
                      * #### Credentials #####
                      * Replace the parameters below with your credentials (e-mail and token)
                      * You can also get your credentials from a config file. See an example:
                      * $credentials = PagSeguroConfig::getAccountCredentials();
                     //  */
         $credentials = new \PagSeguroAccountCredentials($this->email, $this->token);
         // Register this payment request in PagSeguro to obtain the payment URL to redirect your customer.
         $onlyCheckoutCode = true;
         $code = $paymentRequest->register($credentials, $onlyCheckoutCode);
         return view('site.pages.confirma_pagamento', compact('code'));
     } catch (\PagSeguroServiceException $e) {
         die($e->getMessage());
     }
 }
 public function enviar($test = false)
 {
     $email = Configure::read('Pagseguro.email');
     $token = Configure::read('Pagseguro.token');
     $environment = $test ? 'sandbox' : 'production';
     $config = new \PagSeguroAccountCredentials($email, $token);
     \PagSeguroConfig::setEnvironment($environment);
     $response = $this->request_pg->register($config);
     if ($response) {
         $this->sucesso = true;
         $this->erro = false;
         $this->link = $response;
         $this->msg = '';
         $exp = explode('=', $response);
         $this->codigo = $exp[1];
     } else {
         $this->sucesso = false;
         $this->erro = true;
         $this->link = '';
         $this->msg = $response;
         $this->codigo = '';
     }
     return $response;
 }
/**
 * Processa o formulário, 
 */
function process_form()
{
    // var_dump($_POST);
    // die();
    require_once "PagSeguroLibrary/PagSeguroLibrary.php";
    $id = $_POST['item_id'];
    $descricao = $_POST['item_descricao'];
    $qtd = $_POST['item_qtd'];
    $valor = $_POST['item_valor'];
    $valor = str_replace(",", ".", $valor);
    //Instancia uma requisicao de pagamento
    $paymentRequest = new PagSeguroPaymentRequest();
    //Seta a moeda
    $paymentRequest->setCurrency("BRL");
    //Adiciona os itens para gerar a url
    $paymentRequest->addItem($id, $descricao, $qtd, $valor);
    //Seta o ambiente de producao, se é Sandbox, ou production
    $is_sandbox = get_option('ps_config_enable_sandbox');
    if (strlen($is_sandbox) > 0) {
        PagSeguroConfig::setEnvironment('sandbox');
    } else {
        PagSeguroConfig::setEnvironment('production');
    }
    /* Infos seguintes apenas para referencia*/
    // Add another item for this payment request
    //$paymentRequest->addItem('0002', 'Notebook rosa', 2, 1.00);
    // Sets a reference code for this payment request, it is useful to identify this payment in future notifications.
    //$paymentRequest->setReference($dadosComprador["codReference"]);
    // Sets shipping information for this payment request
    // 		$CODIGO_SEDEX = PagSeguroShippingType::getCodeByType('SEDEX');
    // 		$paymentRequest->setShippingType($CODIGO_SEDEX);
    // 		$paymentRequest->setShippingAddress($dadosComprador["cep"], $dadosComprador["logradouro"], $dadosComprador["numero"], $dadosComprador["complemento"], $dadosComprador["bairro"], $dadosComprador["cidade"], $dadosComprador["estado"], 'BRA');
    // Sets your customer information.
    //$paymentRequest->setSender($dadosComprador["nome"] . ' ' . $dadosComprador["sobrenome"], $dadosComprador["email"]);
    //Seta a URL de retorno
    $paymentRequest->setRedirectUrl(get_option('ps_config_return_url'));
    try {
        //Inicializa as credenciais
        $credentials = new PagSeguroAccountCredentials(get_option('ps_config_auth_email'), get_option('ps_config_auth_token'));
        //obtem-se a URL da compra
        $url = $paymentRequest->register($credentials);
        //faz o redirect para a url do pagseguro
        wp_redirect($url);
        exit;
    } catch (PagSeguroServiceException $e) {
        die($e->getMessage());
    }
}
/**
 * Processa o formulário, 
 */
function process_form()
{
    //var_dump($_POST);
    //die();
    require_once "PagSeguroLibrary/PagSeguroLibrary.php";
    $id = $_POST['item_id'];
    $descricao = $_POST['item_descricao'];
    $qtd = $_POST['item_qtd'];
    $valor = $_POST['item_valor'];
    $peso = $_POST['item_peso'];
    $frete = $_POST['frete'];
    $valor = str_replace(",", ".", $valor);
    //Instancia uma requisicao de pagamento
    $paymentRequest = new PagSeguroPaymentRequest();
    //Seta a moeda
    $paymentRequest->setCurrency("BRL");
    //Adiciona os itens para gerar a url
    $paymentRequest->addItem($id, $descricao, $qtd, $valor, $peso);
    //Seta o ambiente de producao, se é Sandbox, ou production
    $is_sandbox = get_option('ps_config_enable_sandbox');
    if (strlen($is_sandbox) > 0) {
        PagSeguroConfig::setEnvironment('sandbox');
        $PagSeguroConfig['log']['active'] = TRUE;
    } else {
        PagSeguroConfig::setEnvironment('production');
    }
    /* Infos seguintes apenas para referencia*/
    // Add another item for this payment request
    //$paymentRequest->addItem('0002', 'Notebook rosa', 2, 1.00);
    // Sets a reference code for this payment request, it is useful to identify this payment in future notifications.
    //$paymentRequest->setReference($dadosComprador["codReference"]);
    // Sets shipping information for this payment request
    //$CODIGO_SEDEX = PagSeguroShippingType::getCodeByType('SEDEX');
    //$paymentRequest->setShippingType($CODIGO_SEDEX);
    //$paymentRequest->setShippingAddress('30315230', 'Rua Zito Soares', '179', '', 'Mangabeiras', 'Belo Horizonte', 'MG', 'BRA');
    /* Criando o tipo de frete */
    $shippingType = new PagSeguroShippingType();
    /* Definindo tipo de frete 'PAC' */
    $shippingType->setByType('PAC');
    $shipping = new PagSeguroShipping();
    $shipping->setType($shippingType);
    $data = array('postalCode' => '01452002', 'street' => 'Av. Brig. Faria Lima', 'number' => '1384', 'complement' => 'apto. 114', 'district' => 'Jardim Paulistano', 'city' => 'São Paulo', 'state' => 'SP', 'country' => 'BRA');
    $address = new PagSeguroAddress($data);
    // objeto PagSeguroAddress
    $shipping->setAddress($address);
    $type = $shipping->getType();
    // Objeto PagSeguroShippingType
    $address = $shipping->getAddress();
    // objeto PagSeguroAddress
    $cost = $shipping->getCost();
    // Float
    $shipping->setCost($cost);
    /* $paymentRequest deve ser um objeto do tipo PagSeguroPaymentRequest */
    $paymentRequest->setShipping($shipping);
    var_dump($shipping->setCost);
    die;
    // Sets your customer information.
    //$paymentRequest->setSender($dadosComprador["nome"] . ' ' . $dadosComprador["sobrenome"], $dadosComprador["email"]);
    //Seta a URL de retorno
    $paymentRequest->setRedirectUrl(get_option('ps_config_return_url'));
    try {
        //Inicializa as credenciais
        $credentials = new PagSeguroAccountCredentials(get_option('ps_config_auth_email'), get_option('ps_config_auth_token'));
        //obtem-se a URL da compra
        $url = $paymentRequest->register($credentials);
        //faz o redirect para a url do pagseguro
        wp_redirect($url);
        exit;
    } catch (PagSeguroServiceException $e) {
        die($e->getMessage());
    }
    var_dump($dadosComprador);
    die;
}