Exemple #1
0
 /**
  * Build the checkout form
  * @return void
  */
 protected function _prepareForm()
 {
     $ebanxConfig = Mage::getStoreConfig('payment/ebanx_express');
     $installmentsActive = (bool) $ebanxConfig['active_installments'];
     $currencyCode = strtoupper(Mage::app()->getStore()->getCurrentCurrencyCode());
     // Enforces minimum installment value (R$20)
     $maxInstallments = explode(',', $ebanxConfig['maximum_installments']);
     $interestRate = $ebanxConfig['interest_installments'];
     $interestMode = $ebanxConfig['installments_mode'];
     $total = $this->getFinalValue();
     $installmentsOptions = array();
     // Setup installment options
     foreach ($maxInstallments as $i) {
         if ($i == 1) {
             continue;
         }
         // Minimum amount per installment is R$20
         if ($total / $i >= 20) {
             $installmentsOptions[$i] = Ebanx_Express_Utils::calculateTotalWithInterest($interestMode, $interestRate, $total, $i);
         }
     }
     $installmentCards = array('Visa', 'Mastercard');
     $currencySymbol = Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol();
     $customer = Mage::getSingleton('customer/session')->getCustomer();
     $cpf = $customer->getEbanxCpf();
     $birthDate = $customer->getEbanxBirthdate();
     $birthDay = 0;
     $birthMonth = 0;
     $birthYear = 0;
     if (isset($birthDate)) {
         $birthDate = explode('/', $birthDate);
         $birthDay = $birthDate[0];
         $birthMonth = $birthDate[1];
         $birthYear = $birthDate[2];
     }
     $this->addData(array('installments_active' => $installmentsActive, 'max_installments' => $maxInstallments, 'installment_cards' => $installmentCards, 'installments' => $installmentsOptions, 'price_upfront' => $this->getFinalValue(), 'currency_symbol' => $currencySymbol, 'cpf' => $cpf, 'birth_day' => $birthDay, 'birth_month' => $birthMonth, 'birth_year' => $birthYear, 'has_interest' => intval($interestRate) > 0));
 }
 /**
  * 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;
 }