コード例 #1
0
 /**
  * Generates the EBANX button link
  * @return string
  */
 public function generate_ebanx_form($order_id)
 {
     global $woocommerce;
     $servername = DB_HOST;
     $username = DB_USER;
     $password = DB_PASSWORD;
     $database = DB_NAME;
     // Set EBANX configs
     \Ebanx\Config::set(array('integrationKey' => $this->merchant_key, 'testMode' => $this->test_mode, 'directMode' => true));
     // Loads the current order
     $order = new WC_Order($order_id);
     // If is GET, do nothing, otherwise process the request
     if ($_SERVER['REQUEST_METHOD'] === 'GET') {
         $this->_renderCheckout($order_id);
         return;
     }
     $order = new WC_Order($order_id);
     $streetNumber = isset($order->billing_number) ? $order->billing_number : '1';
     $paymentMethod = isset($_POST['ebanx']['method']) ? $_POST['ebanx']['method'] : '';
     $countryCode = $order->billing_country;
     // Append timestamp on test mode
     $orderId = $this->test_mode ? $order_id . time() : $order_id;
     $params = array('mode' => 'full', 'operation' => 'request', 'payment' => array('merchant_payment_code' => $orderId, 'order_number' => $order_id, 'amount_total' => $order->order_total, 'currency_code' => get_woocommerce_currency(), 'name' => $order->billing_first_name . ' ' . $order->billing_last_name, 'email' => $order->billing_email, 'birth_date' => $this->getBirthdateFromRequest(true), 'address' => $order->billing_address_1, 'street_number' => $streetNumber, 'city' => $order->billing_city, 'state' => $order->billing_state, 'zipcode' => $order->billing_postcode, 'country' => $order->billing_country, 'phone_number' => $order->billing_phone, 'payment_type_code' => $_POST['ebanx']['cc_type'], 'document' => $order->billing_cpf));
     $ccExpiration = str_pad($_POST['ebanx']['cc_expiration_month'], 2, '0', STR_PAD_LEFT) . '/' . $_POST['ebanx']['cc_expiration_year'];
     try {
         $token = \Ebanx\Ebanx::doToken(['payment_type_code' => $_POST['ebanx']['cc_type'], 'creditcard' => ['card_number' => $_POST['ebanx']['cc_number'], 'card_name' => $_POST['ebanx']['cc_name'], 'card_due_date' => $ccExpiration, 'card_cvv' => $_POST['ebanx']['cc_cvv']]]);
     } catch (Exception $e) {
         $_SESSION['ebanxError'] = $e->getMessage();
         $this->_renderCheckout($order_id);
         return;
     }
     if ($token->status == "ERROR") {
         $_SESSION['ebanxError'] = "Erro ao processar pagamento: " . $token->status_message;
         $this->_renderCheckout($order_id);
         return;
     }
     $customer = wp_get_current_user();
     $customer_id = $customer->data->ID;
     $order_id_from_object = $order->id;
     $currency_code = $params['payment']['currency_code'];
     $payment_type_code = $params['payment']['payment_type_code'];
     $birth_date = $params['payment']['birth_date'];
     try {
         $conn = new PDO("mysql:host={$servername};dbname={$database}", $username, $password);
         // set the PDO error mode to exception
         $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     } catch (PDOException $e) {
         echo "Connection failed: " . $e->getMessage();
     }
     try {
         $params['payment']['creditcard'] = array('token' => $token->token);
         $response = \Ebanx\Ebanx::doRequest($params);
         if ($response->status == 'SUCCESS') {
             $sql = "CREATE TABLE IF NOT EXISTS `ebanx_token` (\n                `id` INT AUTO_INCREMENT NOT NULL,\n                `data` datetime NOT NULL,\n                `token` varchar(200),\n                `customer_id` varchar(200),\n                `order_id` varchar(200),\n                `currency_code` varchar(200),\n                `birth_date` varchar(200),\n                `payment_type_code` varchar(200),\n                PRIMARY KEY (`id`)) ";
             $conn->query($sql);
             date_default_timezone_set('America/Sao_Paulo');
             $month = date('m');
             $year = date('Y');
             $day = date('d');
             if ($day > '28' && $month == '02') {
                 $day = '28';
             } else {
                 if ($day == '31') {
                     $day = '01';
                 }
             }
             $date = $year . '-' . $month . '-' . $day;
             $sql = "INSERT INTO ebanx_token (data, token, customer_id, order_id, currency_code, birth_date, payment_type_code)\n                VALUES ('{$date}', '{$token->token}', '{$customer_id}', '{$order_id_from_object}', '{$currency_code}', '{$birth_date}', '{$payment_type_code}')";
             $conn->query($sql);
             // Clear cart
             $woocommerce->cart->empty_cart();
             if ($paymentMethod == 'boleto') {
                 $boletoUrl = $response->payment->boleto_url;
                 $orderUrl = $order->get_checkout_order_received_url($order);
                 $tplDir = dirname(__FILE__) . '/view/';
                 $template = file_get_contents($tplDir . 'success/boleto.php');
                 echo eval(' ?>' . $template . '<?php ');
             } else {
                 if ($paymentMethod == 'pagoefectivo') {
                     $cipUrl = $response->payment->cip_url;
                     $cipCode = $response->payment->cip_code;
                     $orderUrl = $order->get_checkout_order_received_url($order);
                     $tplDir = dirname(__FILE__) . '/view/';
                     $template = file_get_contents($tplDir . 'success/pagoefectivo.php');
                     echo eval(' ?>' . $template . '<?php ');
                 } else {
                     if ($paymentMethod == 'tef') {
                         wp_redirect($response->redirect_url);
                     } else {
                         wp_redirect($this->get_return_url($order));
                     }
                 }
             }
         } else {
             $_SESSION['ebanxError'] = $this->getEbanxErrorMessage($response->status_code, $countryCode);
             $this->_renderCheckout($order_id);
         }
     } catch (Exception $e) {
         $_SESSION['ebanxError'] = $e->getMessage();
         $this->_renderCheckout($order_id);
     }
     $conn = null;
 }