diff --git a/classes/clone-discount.php b/classes/clone-discount.php new file mode 100755 index 0000000..9e504b0 --- /dev/null +++ b/classes/clone-discount.php @@ -0,0 +1,90 @@ +provider = ClubBi::get_instance(); + $this->subtotal = $subtotal; + $this->currency = $currency; + $this->discount = $discount; + $this->benefit_code = $code; + $this->cbi_card = $card; + } + + /** + * Crea un nuevo DEscuento para ser validado + * + * @throws Exception Si la llamada a club BI falla + * @author Luis E. Mendoza + * @return string HTTP Response Code de la llamada + * @link https://codingtipi.com/project/club-bi + * @since 1.0.0 + */ + public function validate(){ + try{ + $url = 'https://aurora.codingtipi.com/discounts/v1/club-bi/benefits'; + + $curl = new Curl( + $this->provider->user, + $this->provider->password, + $this->provider->branch + );// Inicializar Curl + + + $discount = $this->get_api_model();//Obtiene objeto en formato JSON como lo requiere Club BI + + $response = $curl->execute_post($url, $discount); + print_r($response); + + $curl->terminate(); + + $this->code = $response['code']; + if($this->code == 200){ + + }else{ + return $response['body']->message; + } + + } catch (Exception $e) { + return new WP_Error('error', $e->getMessage()); + } + } + + /** + * Obtiene el modelo de un un descuento para poder interactuar con el API de Club BI + * + * @author Luis E. Mendoza + * @return Array Objeto para uso del API de Club BI + * @link https://codingtipi.com/project/club-bi + * @since 1.0.0 + */ + private function get_api_model(){ + return Array( + "discount" => $this->discount, + "total" => $this->subtotal, + "currency" => $this->currency, + "code" => $this->benefit_code, + "card" => $this->cbi_card + ); + } +} \ No newline at end of file diff --git a/classes/club-bi.php b/classes/club-bi.php old mode 100644 new mode 100755 index 7230db5..25cae24 --- a/classes/club-bi.php +++ b/classes/club-bi.php @@ -103,6 +103,7 @@ function register_settings() { $settings = ClubBiSettings::get_instance(); $settings->register_settings(); } + /** * Muestra un nuevo campo al área de cupones de WooCommerce @@ -157,43 +158,38 @@ public function init_checkout(){ */ public function process_card(){ $card = $_POST['cbi_card']; - if($card === ''){ echo $this->invalid_card(); }else{ - $coupon_keys = $this->validate_discount(); - if($coupon_keys['coupon'] != 0){ + $coupon_keys = $this->get_benefidecode(); + if($coupon_keys['coupon'] != false){ include_once dirname(__FILE__) . '/../utils/curl.php'; include_once 'discount.php'; - WC()->cart->apply_coupon( $coupon_keys['coupon'] ); global $woocommerce; + WC()->cart->apply_coupon( $coupon_keys['coupon'] ); $subtotal = WC()->cart->subtotal; $currency = 'GTQ'; $discount = WC()->cart->get_coupon_discount_amount( $coupon_keys['coupon'] ); - - $discount = new Discount($card, $coupon_keys['benefit'], $subtotal, $currency, $discount); //Inicia un descuento + $discount = new Discount($card, $coupon_keys['benefit'], $subtotal, $currency, $discount ); $discount_transaction = $discount->validate(); - - if ( is_wp_error( $discount_transaction ) ){ + if ( $discount_transaction['code'] != 200){ WC()->cart->remove_coupon( $coupon_keys['coupon'] ); wc_clear_notices(); - echo $discount_transaction; - }else{ - if ( $discount->code != 200 ){ + echo $this->invalid_card(); + } else { + if ( $discount->code == 200 ){ + echo json_encode($discount_transaction);die(); + } else{ WC()->cart->remove_coupon( $coupon_keys['coupon'] ); wc_clear_notices(); - echo $discount_transaction; - } else{ - return $coupon_keys; + echo json_encode(array('code' => 400, 'message' => $this->invalid_card())); } - } //Valida por error en la llamada del API - + } //Valida por error en la llamada del API }else{ - echo invalid_coupon(); + echo $this->invalid_card(); } } - wp_die(); } @@ -228,31 +224,38 @@ private function invalid_coupon(){ * @link https://codingtipi.com/project/club-bi * @since 1.0.0 */ - private function validate_discount(){ + + private function get_benefidecode(){ + $coupon_posts = get_posts(array( - 'posts_per_page' => -1, + 'posts_per_page' => 1, 'post_type' => 'shop_coupon', 'post_status' => 'publish', - 'meta_query' => array( - // meta query takes an array of arrays, watch out for this! - array( - 'key' => 'benefit_code' - ) - ) + 'meta_query' => array( + array( + 'key' => 'benefit_code', + 'value' => array(''), + 'compare' => 'NOT IN' + ) + ) )); - + $benefit_code = 0; $coupon_code = 0; + $coupon_data = array(); foreach ( $coupon_posts as $coupon_post ) { - $benefit_code = get_post_meta($coupon_post->ID, 'benefit_code', true ); - $coupon = new WC_Coupon($coupon_post->post_title); - if($coupon->is_valid()){ - $coupon_code = $coupon->get_code(); - break; - } + $benefit_code = get_post_meta($coupon_post->ID, 'benefit_code', true ); + $coupon_data = $coupon_post; } - + $coupon = new WC_Coupon($coupon_data->post_title); + if($coupon->is_valid()){ + $coupon_code = $coupon->get_code(); + } $response = array("benefit"=>$benefit_code, "coupon"=>$coupon_code); + return $response; } -} \ No newline at end of file + + +} + diff --git a/classes/discount.php b/classes/discount.php old mode 100644 new mode 100755 index ffa20b2..b9dcdd9 --- a/classes/discount.php +++ b/classes/discount.php @@ -43,24 +43,29 @@ function __construct($card, $code, $subtotal, $currency, $discount) { public function validate(){ try{ $url = 'https://aurora.codingtipi.com/discounts/v1/club-bi/benefits'; + $curl = new Curl( $this->provider->user, $this->provider->password, $this->provider->branch );// Inicializar Curl - + $discount = $this->get_api_model();//Obtiene objeto en formato JSON como lo requiere Club BI - + // print_r($discount);die(); + $response = $curl->execute_post($url, $discount); + $curl->terminate(); - $this->code = $response['code']; if($this->code == 200){ - + $data['code'] = $response['code']; + $data['authorization'] = $response['body']->authorization; + $data['confirmation'] = $response['body']->confirmation; }else{ - return $response['body']->message; + $data['code'] = $response['code']; + $data['message'] = $response['body']->message; } - + return $data; } catch (Exception $e) { return new WP_Error('error', $e->getMessage()); } diff --git a/club-bi-woocommerce.php b/club-bi-woocommerce.php old mode 100644 new mode 100755 index a638cf1..5bc1988 --- a/club-bi-woocommerce.php +++ b/club-bi-woocommerce.php @@ -17,6 +17,7 @@ if ( ! defined( 'ABSPATH' ) ) { exit; // No permitir acceder el plugin directamente } +include_once dirname(__FILE__) . '/classes/club-bi.php'; /** * Añade funcionalidad para compatibilidad con HPO de WooCommerce @@ -40,17 +41,16 @@ function club_bi_hpo(){ * @since 1.0.0 */ function club_bi_init() { + if ( ! class_exists( 'WC_Payment_Gateway' ) ) return; include_once ('classes/club-bi.php') ; ClubBi::get_instance(); - + } add_action( 'plugins_loaded', 'club_bi_init', 0 ); - - /** * Registra los archivos de Javascript para poder manejar el UI * @@ -67,5 +67,143 @@ function club_bi_script_enqueuer() { } add_action( 'init', 'club_bi_script_enqueuer' ); + +// Add custom Field +add_action('woocommerce_after_order_notes', 'custom_checkout_field'); + +function custom_checkout_field($checkout) +{ + echo ''; + ?> + + get_used_coupons() as $coupon_code ){ + $coupon = new WC_Coupon($coupon_code); + $coupon_id = $coupon->code; + $couponstatus = 200; + + if (!empty($coupon_id) && !empty($couponstatus)) { + $order->update_meta_data( '_custom_value', $_POST['_custom_value'] ); + $order->update_meta_data( '_coupon_status', $couponstatus ); + $order->update_meta_data( '_authorozacion_club_bi', $_POST['_authorozacion_club_bi'] ); + $order->update_meta_data( '_confirmacion_club_bi', $_POST['_confirmacion_club_bi'] ); + $order->save(); + } + } +} + + +// Get the Custom Field value +add_action( 'woocommerce_admin_order_data_after_order_details', 'misha_editable_order_meta_general' ); + +function misha_editable_order_meta_general( $order ){ + ?> +
+ get_meta( '_custom_value' ); + $gift_message = $order->get_meta( '_coupon_status' ); + $gift_auth = $order->get_meta( '_authorozacion_club_bi' ); + $gift_conf = $order->get_meta( '_confirmacion_club_bi' ); + ?> +
+ '_custom_value', + 'label' => 'Coupon Code', + 'value' => $gift_name, + 'wrapper_class' => 'form-field-wide' + ) ); + woocommerce_wp_textarea_input( array( + 'id' => '_coupon_status', + 'label' => 'Coupon Success Code', + 'value' => $gift_message, + 'wrapper_class' => 'form-field-wide' + ) ); + woocommerce_wp_textarea_input( array( + 'id' => '_authorozacion_club_bi', + 'label' => 'Authorisation Code', + 'value' => $gift_auth, + 'wrapper_class' => 'form-field-wide' + ) ); + woocommerce_wp_textarea_input( array( + 'id' => '_confirmacion_club_bi', + 'label' => 'Confirmation Code', + 'value' => $gift_conf, + 'wrapper_class' => 'form-field-wide' + ) ); + ?> +
+ .club_bi { background-color: #003865; @@ -61,22 +63,39 @@ public static function get_checkout_component(){ border-top-right-radius: 0px !important; border-top-left-radius: 1.8rem !important; font-weight: 600; + } + .club_bi.disabled { + pointer-events: none; + opacity: 0.5; + } + .woocommerce-checkout-review-order-table.disabled { + pointer-events: none; + opacity: 0.5; + } + .loader { + display: none; + } + .loader.show { + display: block; } "; - $template = $styles."
+ $template = $styles."

¿Cuentas con tarjeta Club BI?

+ + +

-
"; +
"; return $template; } -} \ No newline at end of file +} diff --git a/includes/club-bi-settings.php b/includes/club-bi-settings.php old mode 100644 new mode 100755 diff --git a/js/club_bi.js b/js/club_bi.js old mode 100644 new mode 100755 index 6d55713..fcd09a7 --- a/js/club_bi.js +++ b/js/club_bi.js @@ -1,23 +1,39 @@ jQuery(document).ready(function () { jQuery(".validate").click(function (e) { - e.preventDefault(); - jQuery(document.body).trigger("update_checkout"); - jQuery.ajax({ - type: "post", - dataType: "json", - url: club_bi_ajax.ajaxurl, - data: { action: "club_bi_redeem", - cbi_card: jQuery('#cbi_card').val() }, - success: function (response) { - console.log('Success'); - console.log(response); - jQuery(document.body).trigger("update_checkout"); - }, - error: function(error) { - console.log('Error'); - console.log(error); - jQuery('.woocommerce-notices-wrapper').first().empty().append(''); - } - }); + e.preventDefault(); + jQuery('.club_bi, .woocommerce-checkout-review-order-table').addClass('disabled'); + jQuery('.loader').addClass('show'); + jQuery.ajax({ + type: "post", + dataType: "json", + url: club_bi_ajax.ajaxurl, + data: { + action: "club_bi_redeem", + cbi_card: jQuery('#cbi_card').val() + }, + success: function (response) { + if(response.code = 200){ + jQuery('#coupon_status').val(200); + jQuery('#authorozacion_club_bi').val(response.authorization); + jQuery('#confirmacion_club_bi').val(response.confirmation); + jQuery( document.body ).trigger( 'update_checkout' ); + } + + jQuery('.club_bi, .woocommerce-checkout-review-order-table').removeClass('disabled'); + jQuery('.loader').removeClass('show'); + }, + error: function(error) { + console.log('Error'); + console.log(error.responseText); + jQuery('.club_bi, .woocommerce-checkout-review-order-table').removeClass('disabled'); + jQuery('.loader').removeClass('show'); + jQuery('.woocommerce-notices-wrapper').first().empty().append(''); + jQuery('html, body').animate({ + scrollTop: 0 + }, 1000); + + } + }); }); -}); \ No newline at end of file + }); + \ No newline at end of file diff --git a/utils/curl.php b/utils/curl.php old mode 100644 new mode 100755