Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions classes/clone-discount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* Clase para interactuar con el descuento de Club BI
*
* Objeto principal para interactuar con un descuento de Club BI
*
* @copyright 2024 - tipi(code)
* @since 1.0.0
*/
class cloneDiscount {
private $provider;
private $subtotal;
private $currency;
private $discount;
private $cbi_card;
private $benefit_code;
public $code;

/**
* Constructor
*
* @param WC_Order $customer_order Orden de WooCommerce para procesar los datos del producto.
*
*/
function __construct($card, $code, $subtotal, $currency, $discount) {
$this->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 <lmendoza@codingtipi.com>
* @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 <lmendoza@codingtipi.com>
* @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
);
}
}
73 changes: 38 additions & 35 deletions classes/club-bi.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ function register_settings() {
$settings = ClubBiSettings::get_instance();
$settings->register_settings();
}


/**
* Muestra un nuevo campo al área de cupones de WooCommerce
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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;
}
}


}

17 changes: 11 additions & 6 deletions classes/discount.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Loading