protected function getFormURL() { $this->callEbanxLib(); $params = array(); $params = $this->getFormFields(); $response = \Ebanx\Ebanx::doRequest($params); if ($response->status == 'SUCCESS') { $checkoutURL = $response->redirect_url; } else { \XLite\Core\TopMessage::addError('Erro processando pagamento! EBANX: ' . $response->status_code . ": " . $response->status_message); return; } return $checkoutURL; }
/** * Authorizes a transaction * @param Varien_Object $payment * @param float $amount * @return Mage_Payment_Model_Method_Abstract */ public function authorize(Varien_Object $payment, $amount) { parent::authorize($payment, $amount); $session = Mage::getSingleton('checkout/session'); $order = $payment->getOrder(); $ebanx = Mage::app()->getRequest()->getParam('ebanx'); Mage::log('Authorizing order [' . $order->getApiOrderId() . ']'); $birthDate = str_pad($ebanx['birth_day'], 2, '0', STR_PAD_LEFT) . '/' . str_pad($ebanx['birth_month'], 2, '0', STR_PAD_LEFT) . '/' . $ebanx['birth_year']; // Street number workaround $streetNumber = preg_replace('/[\\D]/', '', $order->getBillingAddress()->getData('street')); $streetNumber = $streetNumber > 0 ? $streetNumber : '1'; // Defines the order ID, if in test append time() to avoid errors $testMode = intval(Mage::getStoreConfig('payment/ebanx/testing')) == 1; $orderId = $order->getIncrementId() . ($testMode ? time() : ''); // Cut order ID in test mode if (strlen($orderId) > 20 && $testMode) { $orderId = substr($orderId, 0, 20); } // Gets the currency code and total // Backend/base currency if (Mage::getStoreConfig('payment/ebanx_express/paymentcurrency') == 'base') { $amountTotal = $order->getBaseGrandTotal(); $currencyCode = $order->getBaseCurrencyCode(); } else { $amountTotal = $order->getGrandTotal(); $currencyCode = $order->getOrderCurrency()->getCurrencyCode(); } // On guest checkout, get billing email address $email = $order->getCustomerEmail() ?: $order->getBillingAddress()->getEmail(); $state = $order->getBillingAddress()->getRegionCode(); if (strlen($state) > 2) { $state = 'PR'; } $ccExpiration = str_pad($ebanx['cc_expiration_month'], 2, '0', STR_PAD_LEFT) . '/' . $ebanx['cc_expiration_year']; $params = array('mode' => 'full', 'operation' => 'request', 'payment' => array('name' => $order->getCustomerFirstname() . ' ' . $order->getCustomerLastname(), 'document' => $ebanx['cpf'], 'birth_date' => $birthDate, 'email' => $email, 'phone_number' => $order->getBillingAddress()->getTelephone(), 'currency_code' => $currencyCode, 'amount_total' => $amountTotal, 'payment_type_code' => $ebanx['cc_type'], 'merchant_payment_code' => $orderId, 'order_number' => $order->getIncrementId(), 'zipcode' => $order->getBillingAddress()->getData('postcode'), 'address' => $order->getBillingAddress()->getData('street'), 'street_number' => $streetNumber, 'city' => $order->getBillingAddress()->getData('city'), 'state' => $state, 'country' => 'br', 'creditcard' => array('card_name' => $ebanx['cc_name'], 'card_number' => $ebanx['cc_number'], 'card_cvv' => $ebanx['cc_cvv'], 'card_due_date' => $ccExpiration))); // If has installments, adjust total if (isset($ebanx['installments'])) { if (intval($ebanx['installments']) > 1) { $interestRate = floatval(Mage::getStoreConfig('payment/ebanx_express/interest_installments')); $interestMode = Mage::getStoreConfig('payment/ebanx_express/installments_mode'); $params['payment']['instalments'] = intval($ebanx['installments']); $params['payment']['amount_total'] = Ebanx_Express_Utils::calculateTotalWithInterest($interestMode, $interestRate, $amountTotal, intval($ebanx['installments'])); } } try { // Set DirectMode as true \Ebanx\Config::setDirectMode(true); $response = \Ebanx\Ebanx::doRequest($params); Mage::log('Authorizing order [' . $order->getIncrementId() . '] - calling EBANX'); if (!empty($response) && $response->status == 'SUCCESS') { $hash = $response->payment->hash; // Add the EBANX hash in the order data $order->getPayment()->setData('ebanx_hash', $hash)->save(); // Redirect to bank page if the client chose TEF if (isset($response->redirect_url)) { $_SESSION['ebxRedirectUrl'] = $response->redirect_url; } else { $_SESSION['ebxRedirectUrl'] = Mage::getUrl('checkout/onepage/success') . '?hash=' . $hash; } Mage::log('Authorizing order [' . $order->getIncrementId() . '] - success'); } else { Mage::log('Authorizing order [' . $order->getIncrementId() . '] - error: ' . $response->status_message); Mage::throwException($this->getEbanxErrorMessage($response->status_code)); } } catch (Exception $e) { Mage::throwException($e->getMessage()); } return $this; }
/** * Authorizes a transaction * @param Varien_Object $payment * @param float $amount * @return Mage_Payment_Model_Method_Abstract */ public function authorize(Varien_Object $payment, $amount) { parent::authorize($payment, $amount); $country = strtolower($payment->getOrder()->getBillingAddress()->getCountry()); $session = Mage::getSingleton('checkout/session'); $order = $payment->getOrder(); Mage::log('Authorizing order [' . $order->getApiOrderId() . ']'); // Street number workaround $streetNumber = preg_replace('/[\\D]/', '', $order->getBillingAddress()->getData('street')); $streetNumber = $streetNumber > 0 ? $streetNumber : '1'; // Defines the order ID, if in test append time() to avoid errors $testMode = intval(Mage::getStoreConfig('payment/ebanx/testing')) == 1; $orderId = $order->getIncrementId() . ($testMode ? time() : ''); // Cut order ID in test mode if (strlen($orderId) > 20 && $testMode) { $orderId = substr($orderId, 0, 20); } // Gets the currency code and total // Backend/base currency if (Mage::getStoreConfig('payment/ebanx_standard/paymentcurrency') == 'base') { $amountTotal = $order->getBaseGrandTotal(); $currencyCode = $order->getBaseCurrencyCode(); } else { $amountTotal = $order->getGrandTotal(); $currencyCode = $order->getOrderCurrency()->getCurrencyCode(); } $dueDate = date('d/m/Y', strtotime('+' . intval(Mage::getStoreConfig('payment/ebanx_standard/due_date')) . $dueDays . 'days')); // On guest checkout, get billing email address $email = $order->getCustomerEmail() ?: $order->getBillingAddress()->getEmail(); $params = array('name' => $order->getCustomerFirstname() . ' ' . $order->getCustomerLastname(), 'email' => $email, 'phone_number' => $order->getBillingAddress()->getTelephone(), 'currency_code' => $currencyCode, 'amount' => $amountTotal, 'payment_type_code' => '_all', 'merchant_payment_code' => $orderId, 'due_date' => $dueDate, 'order_number' => $order->getIncrementId(), 'zipcode' => $order->getBillingAddress()->getData('postcode'), 'address' => $order->getBillingAddress()->getData('street'), 'street_number' => $streetNumber, 'city' => $order->getBillingAddress()->getData('city'), 'state' => $order->getBillingAddress()->getRegionCode(), 'country' => strtolower($order->getBillingAddress()->getCountry())); try { $response = \Ebanx\Ebanx::doRequest($params); Mage::log('Authorizing order [' . $order->getIncrementId() . '] - calling EBANX'); if (!empty($response) && $response->status == 'SUCCESS') { $hash = $response->payment->hash; // Add the EBANX hash in the order data $order->getPayment()->setData('ebanx_hash', $hash)->save(); // Redirect to bank page if the client chose TEF if (isset($response->redirect_url)) { $_SESSION['ebxRedirectUrl'] = $response->redirect_url; } else { $_SESSION['ebxRedirectUrl'] = Mage::getUrl('checkout/onepage/success') . '?hash=' . $hash; } Mage::log('Authorizing order [' . $order->getIncrementId() . '] - success'); } else { Mage::log('Authorizing order [' . $order->getIncrementId() . '] - error: ' . $response->status_message); Mage::throwException($this->getEbanxErrorMessage($response->status_code)); } } catch (Exception $e) { Mage::throwException($e->getMessage()); } return $this; }
$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); // echo "Connected successfully 2\n"; $sql = "SELECT * FROM ebanx_token WHERE DATE_FORMAT(data,'%d') = {$data}"; //$sql = "SELECT * FROM ebanx_token"; $result = $conn->query($sql); //$result = $result->fetch (PDO::FETCH_ASSOC); while ($data = $result->fetch(PDO::FETCH_ASSOC)) { $args = array('status' => '', 'customer_id' => $data['customer_id'], 'customer_note' => '', 'created_via' => 'EBANX Recurring cron', 'order_id' => 0); $order = new WC_Order($data['order_id']); $streetNumber = isset($order->billing_number) ? $order->billing_number : '1'; $newOrder = wc_create_order($args); update_post_meta($newOrder->id, '_order_total', $order->order_total); $params = array('mode' => 'full', 'operation' => 'request', 'payment' => array('merchant_payment_code' => $newOrder->id, 'order_number' => $newOrder->id, 'amount_total' => $order->order_total, 'currency_code' => $data['currency_code'], 'name' => $order->billing_first_name . ' ' . $order->billing_last_name, 'email' => $order->billing_email, 'birth_date' => $data['birth_date'], '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' => $data['payment_type_code'], 'document' => $order->billing_cpf, 'creditcard' => array('token' => $data['token']))); $response = \Ebanx\Ebanx::doRequest($params); if (isset($response->status) && $response->status == 'SUCCESS') { if ($response->payment->status == 'CA') { $newOrder->add_order_note('Payment failed.'); $newOrder->cancel_order(); echo "OK: Payment {$response->hash} was cancelled via IPN\n"; } if ($response->payment->status == 'CO') { $newOrder->add_order_note('Payment confirmed.'); $newOrder->update_status('completed'); echo "OK: Payment {$response->hash} was confirmed via IPN\n"; } } } // var_dump($result); $conn = null;
/** * 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; }