Skip to content

Commit

Permalink
Merge pull request #462 from ebanx/hotfix/1.11.1
Browse files Browse the repository at this point in the history
Hotfix/1.11.1
  • Loading branch information
Cezar Luiz authored Apr 8, 2017
2 parents ec88348 + c5ead5c commit e293bf2
Show file tree
Hide file tree
Showing 17 changed files with 1,326 additions and 392 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

# 1.11.1
* Fix - Fixed translation paths [#462](https://github.com/ebanx/woocommerce-gateway-ebanx/pull/462)
* Fix - Fixed converted value message when instalments is changed [#462](https://github.com/ebanx/woocommerce-gateway-ebanx/pull/462)
* Fix - Fixed problems with newer version of WooCommerce [#462](https://github.com/ebanx/woocommerce-gateway-ebanx/pull/462)

# 1.11.0
* Feature - Showing the prices with IOF for Brazil before on gateways [#441](https://github.com/ebanx/woocommerce-gateway-ebanx/pull/441)
* Feature - Alert the merchants when HTTPS isn't present [#427](https://github.com/ebanx/woocommerce-gateway-ebanx/pull/427)
Expand Down
41 changes: 41 additions & 0 deletions woocommerce-gateway-ebanx/assets/js/credit-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,45 @@ jQuery( function($) {
wc_ebanx_form.init( $( "form.checkout, form#order_review, form#add_payment_method, form.woocommerce-checkout" ) );

wc_ebanx_form.toggleCardUse();

// Update IOF value when instalments is changed
var update_converted = function (self) {
var instalments = self.val();
var country = self.attr('data-country');
var amount = self.attr('data-amount');
var currency = self.attr('data-currency');
var text = self.parents('.payment_box').find('.ebanx-payment-converted-amount p');
var spinner = self.parents('.payment_box').find('.ebanx-spinner');

spinner.fadeIn();

$.ajax({
url: wc_ebanx_params.ajaxurl,
type: 'POST',
data: {
action: 'ebanx_update_converted_value',
instalments: instalments,
country: country,
amount: amount,
currency: currency
}
})
.done(function (data) {
text.html(data);
})
.always(function () {
spinner.fadeOut();
});
};

$(document).on('change', 'select.ebanx-instalments', function (){
update_converted($(this));
});

$(document).on('change', 'input[name="ebanx-credit-card-use"]', function () {
update_converted($(this)
.parents('.ebanx-credit-card-option')
.find('select.ebanx-instalments'));
});

} );
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class WC_EBANX_Credit_Card_BR_Gateway extends WC_EBANX_Credit_Card_Gateway {
/**
* Constructor
*/
public function __construct() {
public function __construct()
{
$this->id = 'ebanx-credit-card-br';
$this->method_title = __('EBANX - Credit Card Brazil', 'woocommerce-gateway-ebanx');

Expand Down Expand Up @@ -48,6 +49,11 @@ public function ebanx_process_merchant_currency($currency) {
public function payment_fields() {
parent::payment_fields();

parent::checkout_rate_conversion(WC_EBANX_Constants::CURRENCY_CODE_BRL);
parent::checkout_rate_conversion(
WC_EBANX_Constants::CURRENCY_CODE_BRL,
true,
null,
1
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function checkout_assets()
// Using // to avoid conflicts between http and https protocols
wp_enqueue_script('ebanx', '//js.ebanx.com/ebanx-1.5.min.js', '', null, true);
wp_enqueue_script('woocommerce_ebanx_jquery_mask', plugins_url('assets/js/jquery-mask.js', WC_EBANX::DIR), array('jquery'), WC_EBANX::get_plugin_version(), true);
wp_enqueue_script('woocommerce_ebanx', plugins_url('assets/js/credit-card.js', WC_EBANX::DIR), array('jquery-payment', 'ebanx'), WC_EBANX::get_plugin_version(), true);
wp_enqueue_script('woocommerce_ebanx_credit_card', plugins_url('assets/js/credit-card.js', WC_EBANX::DIR), array('jquery-payment', 'ebanx'), WC_EBANX::get_plugin_version(), true);

// If we're on the checkout page we need to pass ebanx.js the address of the order.
if (is_checkout_pay_page() && isset($_GET['order']) && isset($_GET['order_id'])) {
Expand Down Expand Up @@ -295,16 +295,22 @@ protected function save_user_meta_fields($order)
*/
public function process_payment($order_id)
{
if ( WC_EBANX_Request::has('ebanx_billing_instalments') ) {
$has_instalments = (WC_EBANX_Request::has('ebanx_billing_instalments') || WC_EBANX_Request::has('ebanx-credit-card-installments'));

if ( $has_instalments ) {

$order = wc_get_order( $order_id );
$total_price = $order->get_total();

$total_price = get_post_meta($order_id, '_order_total', true);
$tax_rate = 0;
$instalments = $_POST['ebanx_billing_instalments'];
$instalments = WC_EBANX_Request::has('ebanx_billing_instalments') ? WC_EBANX_Request::read('ebanx_billing_instalments') : WC_EBANX_Request::read('ebanx-credit-card-installments');

if ( array_key_exists( $instalments, $this->instalment_rates ) ) {
$tax_rate = $this->instalment_rates[$instalments];
}

$total_price += $total_price * $tax_rate;
$order->set_total($total_price);
update_post_meta($order_id, '_order_total', $total_price);
}

return parent::process_payment($order_id);
Expand Down Expand Up @@ -421,9 +427,13 @@ public function payment_fields() {

$country = $this->getTransactionAddress('country');

$currency = $country === WC_EBANX_Constants::COUNTRY_BRAZIL ? WC_EBANX_Constants::CURRENCY_CODE_BRL : WC_EBANX_Constants::CURRENCY_CODE_MXN;

wc_get_template(
$this->id . '/payment-form.php',
array(
'currency' => $currency,
'country' => $country,
'instalments_terms' => $instalments_terms,
'cards' => (array) $cards,
'cart_total' => $cart_total,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public function ebanx_process_merchant_currency($currency) {
public function payment_fields() {
parent::payment_fields();

parent::checkout_rate_conversion(WC_EBANX_Constants::CURRENCY_CODE_MXN);
parent::checkout_rate_conversion(
WC_EBANX_Constants::CURRENCY_CODE_MXN,
true,
null,
1
);
}
}
85 changes: 70 additions & 15 deletions woocommerce-gateway-ebanx/gateways/class-wc-ebanx-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,25 @@
exit;
}

abstract class WC_EBANX_Gateway extends WC_Payment_Gateway
// Update converted value via ajax
add_action('wp_ajax_nopriv_ebanx_update_converted_value', 'ebanx_update_converted_value');
add_action('wp_ajax_ebanx_update_converted_value', 'ebanx_update_converted_value');

/**
* It's a just a method to call `ebanx_update_converted_value`
* to avoid WordPress hooks problem
*
* @return void
*/
function ebanx_update_converted_value () {
$gateway = new WC_EBANX_Gateway();

echo $gateway->ebanx_update_converted_value();

wp_die();
}

class WC_EBANX_Gateway extends WC_Payment_Gateway
{
protected static $ebanx_params = array();
protected static $initializedGateways = 0;
Expand Down Expand Up @@ -51,6 +69,27 @@ public function __construct()
$this->merchant_currency = strtoupper(get_woocommerce_currency());
}

/**
* Receives values from instalments and show an updated message with new values
*
* @return void
*/
public function ebanx_update_converted_value () {
try {
$message = $this->checkout_rate_conversion(
WC_EBANX_Request::read('currency'),
false,
WC_EBANX_Request::read('country'),
WC_EBANX_Request::read('instalments')
);

return $message;
}
catch (Exception $e) {
echo $e->getMessage();
}
}

/**
* Sets up the pay api to be called during the plugin lifecycle
*
Expand Down Expand Up @@ -79,7 +118,7 @@ public function is_available()
&& !empty($this->public_key)
&& !empty($this->private_key)
&& ($this->currency_is_usd_eur($currency)
|| $this->ebanx_process_merchant_currency($currency)
|| $this->ebanx_process_merchant_currency($currency)
);
}

Expand All @@ -88,7 +127,9 @@ public function is_available()
* @param string $currency Possible currencies: BRL, USD, EUR, PEN, CLP, COP, MXN
* @return boolean Return true if EBANX process the currency
*/
abstract public function ebanx_process_merchant_currency($currency);
public function ebanx_process_merchant_currency($currency) {
return $currency;
}

/**
* General method to check if the currency is USD or EUR. These currencies are accepted by all payment methods.
Expand Down Expand Up @@ -303,12 +344,13 @@ public function checkout_assets()
static::$ebanx_params = array(
'key' => $this->public_key,
'mode' => $this->is_sandbox_mode ? 'test' : 'production',
'ajaxurl' => admin_url('admin-ajax.php', null)
);

self::$initializedGateways++;

if (self::$initializedGateways === self::$totalGateways) {
wp_localize_script('woocommerce_ebanx', 'wc_ebanx_params', apply_filters('wc_ebanx_params', static::$ebanx_params));
wp_localize_script('woocommerce_ebanx_credit_card', 'wc_ebanx_params', apply_filters('wc_ebanx_params', static::$ebanx_params));
}
}
}
Expand Down Expand Up @@ -447,7 +489,6 @@ public function get_currency_rate($local_currency_code) {
*/
protected function request_data($order)
{

$home_url = esc_url( home_url() );

$has_cpf = false;
Expand Down Expand Up @@ -971,7 +1012,7 @@ final public function process_hook(array $codes, $notificationType)
* @param string $currency Possible currencies: BRL, USD, EUR, PEN, CLP, COP, MXN
* @return void
*/
public function checkout_rate_conversion($currency) {
public function checkout_rate_conversion($currency, $template = true, $country = null, $instalments = null) {
if ( ! in_array($this->merchant_currency, array(
WC_EBANX_Constants::CURRENCY_CODE_USD,
WC_EBANX_Constants::CURRENCY_CODE_EUR,
Expand All @@ -980,7 +1021,10 @@ public function checkout_rate_conversion($currency) {
}

$amount = WC()->cart->cart_contents_total;
$country = $this->getTransactionAddress('country');

if ($country === null) {
$country = $this->getTransactionAddress('country');
}

if ( in_array($this->merchant_currency, array(
WC_EBANX_Constants::CURRENCY_CODE_USD,
Expand All @@ -994,21 +1038,32 @@ public function checkout_rate_conversion($currency) {
$amount *= $rate;
}

// Applies instalments taxes
if ( $instalments !== null ) {
$interest_rate = $this->configs->settings['interest_rates_' . sprintf("%02d", $instalments)];

$amount += ($amount * $interest_rate / 100);
}

// Applies IOF for Brazil payments only
if ( $country === WC_EBANX_Constants::COUNTRY_BRAZIL ) {
$amount += ($amount * WC_EBANX_Constants::BRAZIL_TAX);
}

$message = $this->get_checkout_message($amount, $currency, $country);

wc_get_template(
'checkout-conversion-rate.php',
array(
'message' => $message
),
'woocommerce/ebanx/',
WC_EBANX::get_templates_path()
);
if ($template) {
wc_get_template(
'checkout-conversion-rate.php',
array(
'message' => $message
),
'woocommerce/ebanx/',
WC_EBANX::get_templates_path()
);
}

return $message;
}

/**
Expand Down
Binary file not shown.
Loading

0 comments on commit e293bf2

Please sign in to comment.