Esempio n. 1
0
<?php

require_once "../includes/init.inc.php";
$metodo = $_POST['metodo'];
$cliente = $_POST['cliente'];
$enderecoEntrega = $_POST['enderecoEntrega'];
$tokenPagamento = $_POST['tokenPagamento'];
$xmlData = array("itens" => array(), "cliente" => array("nome" => $cliente['nome'], "cpf" => $cliente['cpf'], "email" => $cliente['email'], "nascimento" => $cliente['nascimento'], "celular" => $cliente['celular']), "enderecoEntrega" => array("logradouro" => $enderecoEntrega['logradouro'], "numero" => $enderecoEntrega['numero'], "bairro" => $enderecoEntrega['bairro'], "cidade" => $enderecoEntrega['cidade'], "cep" => $enderecoEntrega['cep'], "estado" => $enderecoEntrega['estado']));
if ($metodo == 'cartao-credito') {
    $formaPagamento = $_POST['formaPagamento']['cartao'];
    $xmlData["formaPagamento"] = array("cartao" => array("parcelas" => $formaPagamento['parcelas'], "enderecoCobranca" => array("logradouro" => $formaPagamento['enderecoCobranca']['logradouro'], "numero" => $formaPagamento['enderecoCobranca']['numero'], "bairro" => $formaPagamento['enderecoCobranca']['bairro'], "cidade" => $formaPagamento['enderecoCobranca']['cidade'], "cep" => $formaPagamento['enderecoCobranca']['cep'], "estado" => $formaPagamento['enderecoCobranca']['estado'])));
} else {
    $date = date('Y-m-d', strtotime("+3 days"));
    $xmlData["formaPagamento"] = array("boleto" => array('vencimento' => $date));
}
if (isset($_POST['outros'])) {
    $outros = $_POST['outros'];
    $xmlData["frete"] = $outros['frete'];
}
foreach (Cart::getItens() as $item) {
    $xmlData['itens'][] = array('itemDescricao' => $item["prod"]->description, 'itemValor' => $item["prod"]->price * 100, 'itemQuantidade' => $item["qty"]);
}
$integrationToken = "seu_token_aqui";
$paymentToken = $tokenPagamento;
$resp = ApiCheckoutGerencianet::pagar($xmlData, $integrationToken, $paymentToken);
$respXml = simplexml_load_string($resp);
$_SESSION["checkout_response"] = json_encode($respXml);
echo json_encode($respXml);
 function __construct($payment_url)
 {
     self::$_URL = $payment_url;
 }
 /**
  * After the payment method and validation data from transparent-checkout.php this method is called
  * to send the real payment information to gerencianet
  * 
  * Also this method handle with notifications of any status of the payment
  *
  * @param array $posted gerencianet post data.
  *
  * @return void
  */
 public function successful_request($posted)
 {
     //If the IPN request is about notification
     if (!empty($posted['notificacao'])) {
         try {
             include_once 'gerencianet-api/ApiCheckoutGerencianet.php';
             $url = 'https://go.gerencianet.com.br/api/notificacao/xml';
             $apiGerenciaNet = new ApiCheckoutGerencianet($url);
             $token = $this->token;
             $notification_code = $posted['notificacao'];
             $parametros = array($notification_code => 'notificacao');
             $result = $apiGerenciaNet->consultarStatusPagamento($parametros, $token);
             $result = simplexml_load_string($result);
             $order = new WC_Order($result->resposta->identificador);
             if (!empty($order)) {
                 $status = $result->resposta->status;
                 switch ($status) {
                     case 'aguardando':
                         $order->update_status('pending', __('Waiting Payment nº ') . $result->resposta->transacao);
                         break;
                     case 'pago':
                         $order->update_status('completed', __('All payment process completed transaction nº ') . $result->resposta->transacao);
                         break;
                     case 'cancelado':
                         $order->update_status('cancelled', __('Order cancelled transaction nº ') . $result->resposta->transacao);
                         break;
                     case 'vencido':
                         $order->update_status('failed', __('Order failed transaction nº ') . $result->resposta->transacao);
                         break;
                     default:
                         //no action
                         break;
                 }
                 if ('yes' == $this->debug) {
                     $this->log->add('gerencianet', 'changing status order ' . $order->get_order_number());
                 }
             } else {
                 if ('yes' == $this->debug) {
                     $this->log->add('gerencianet', 'changing status order error ' . $e->getMessage());
                 }
             }
         } catch (Exception $e) {
             if ('yes' == $this->debug) {
                 $this->log->add('gerencianet', 'changing status order error ' . $e->getMessage());
             }
         }
         exit;
     }
     //if the IPN request is about payment
     if (!empty($posted['tokenPagamento'])) {
         try {
             include_once 'gerencianet-api/ApiCheckoutGerencianet.php';
             $order = new WC_Order($posted['outros']['wc_order_id']);
             // Payment URL or Sandbox URL.
             $payment_url = 'https://go.gerencianet.com.br/api/checkout/pagar/xml';
             if ('yes' == $this->sandbox) {
                 $payment_url = 'https://go.gerencianet.com.br/teste/api/checkout/pagar/xml';
             }
             $apiGerenciaNet = new ApiCheckoutGerencianet($payment_url);
             $token = $this->token;
             $tokenPagamento = $posted['tokenPagamento'];
             $order_items = $order->get_items();
             $items = array();
             foreach ($order_items as $items_key => $item) {
                 $items[] = array("itemDescricao" => $item['name'], "itemValor" => $this->price_cents_format($item['line_total'] / $item['qty']), "itemQuantidade" => $item['qty']);
             }
             $retorno = array('identificador' => $posted['outros']['wc_order_id'], 'urlNotificacao' => home_url('/?wc-api=WC_Gerencianet'));
             $client = $posted['cliente'];
             $enderecoEntrega = $posted['enderecoEntrega'];
             //forma de entrega
             $formaEntrega = array();
             if ($posted['metodo'] == 'boleto') {
                 $date = $order->order_date;
                 $date = date_create($date);
                 date_add($date, date_interval_create_from_date_string($this->billet_number_days . ' days'));
                 $formaEntrega['boleto'] = array('vencimento' => date_format($date, 'Y-m-d'));
             } else {
                 if ($posted['metodo'] == 'cartao-credito') {
                     $parcelas = $posted['formaPagamento']['cartao']['parcelas'];
                     $formaEntrega['cartao'] = array('parcelas' => $parcelas, 'enderecoCobranca' => $enderecoEntrega);
                 }
             }
             $parametros = array('itens' => $items, 'retorno' => $retorno, 'desconto' => $this->price_cents_format($order->get_total_discount()), 'frete' => $this->price_cents_format($order->get_total_shipping()), 'tipo' => 'produto', 'cliente' => $client, 'enderecoEntrega' => $enderecoEntrega, 'formaPagamento' => $formaEntrega);
             $result = $apiGerenciaNet->pagar($parametros, $token, $tokenPagamento);
             $result = simplexml_load_string($result);
             $result->redirect = $posted['outros']['wc_redirect'];
             $result->tokenPagamento = $tokenPagamento;
             echo json_encode($result);
             if ('yes' == $this->debug) {
                 $this->log->add('gerencianet', 'Sending payment ' . print_r($parametros, true) . ' token =>' . $token . ' tokenPagamento => ' . $tokenPagamento);
             }
         } catch (Exception $e) {
             if ('yes' == $this->debug) {
                 $this->log->add('gerencianet', 'Sending payment error ' . $e->getMessage());
             }
         }
         exit;
     }
 }