Skip to content

Commit

Permalink
Merge pull request #70 from LinkNacional/master
Browse files Browse the repository at this point in the history
ajustes conforme issues
  • Loading branch information
gblmarquez authored Jan 26, 2021
2 parents e7cbf51 + c11cf7c commit 00b95a3
Show file tree
Hide file tree
Showing 39 changed files with 2,756 additions and 4,595 deletions.
1,194 changes: 580 additions & 614 deletions includes/admin/class-admin.php

Large diffs are not rendered by default.

1,635 changes: 824 additions & 811 deletions includes/admin/class-api.php

Large diffs are not rendered by default.

702 changes: 347 additions & 355 deletions includes/admin/class-settings.php

Large diffs are not rendered by default.

258 changes: 128 additions & 130 deletions includes/admin/class-webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,136 +11,134 @@
* @version 1.0.4
*/
class WC_NFe_Webhook_Handler {

/**
* WC_Logger Logger instance.
*
* @var bool
*/
public static $logger = false;

/**
* Base Construct.
*/
public function __construct() {
add_action( 'woocommerce_api_' . WC_API_CALLBACK, [ $this, 'handle' ] );
}

/**
* Handling incoming webhooks.
*
* @throws Exception Exception.
*
* @return void
*/
public function handle() {
$raw_body = file_get_contents( 'php://input' );
$body = json_decode( $raw_body );

// translators: Fired when a new webhook is called.
$this->logger( sprintf( __( 'New webhook called. %s', 'woo-nfe' ), $raw_body ) );

try {
$this->process_event( $body );
} catch ( Exception $e ) {
// translators: Error message from event.
$this->logger( sprintf( __( 'Error: %s.', 'woo-nfe' ), $e->getMessage() ) );

if ( 2 === $e->getCode() ) {
http_response_code( 422 );
die( $e->getMessage() );
}
}
}

/**
* Read json entity received and proccess the webhook.
*
* @throws Exception Throws an exception.
* @param string $body Event body.
*
* @return void
*/
protected function process_event( $body ) {

$this->logger( __( 'Starting to proccess new webhook event.', 'woo-nfe' ) );

if ( is_null( $body ) ) {
$this->logger( __( 'Error while checking webhook JSON.', 'woo-nfe' ) );
$this->logger( sprintf( __( 'Error: %s.', 'woo-nfe' ), $body ) );

throw new Exception( __( 'Error while checking webhook JSON.', 'woo-nfe' ) );
}

$this->logger( __( 'Getting Order ID of the webhook.', 'woo-nfe' ) );
$order = $this->get_order_by_nota_id( $body->id );

$this->logger( __( 'Updating Order with NFe info.', 'woo-nfe' ) );

$meta = update_post_meta( $order->get_id(), 'nfe_issued', [
'id' => $body->id,
'status' => $body->flowStatus,
'issuedOn' => $body->issuedOn,
'amountNet' => $body->amountNet,
'checkCode' => $body->checkCode,
'number' => $body->number,
] );

if ( ! $meta ) {
$this->logger( sprintf( __( 'There was a problem while updating the Order #%d with the NFe information.', 'woo-nfe' ), $order->get_id() ) );
return;
}

// translators: Order updated with its status.
$msg = sprintf( __( 'Order updated. Order: #%d NFe status: %s.', 'woo-nfe' ), $order->get_id(), nfe_status_label( $body->flowStatus ) );
$this->logger( $msg );
$order->add_order_note( $msg );
}

/**
* Find orders by NFe.io ID.
*
* @throws Exception Throws an exception.
* @param string $id NFE.io Receipt ID.
*
* @return WC_Order Order ID.
*/
protected function get_order_by_nota_id( $id ) {
$query = nfe_get_order_by_nota_value( $id );

if ( ! $query->have_posts() || is_wp_error( $query ) ) {
// translators: Order with receipt number.
$this->logger( sprintf( __( 'Order with receipt number #%d not found.', 'woo-nfe' ), $id ) );

// translators: Order with receipt number.
throw new Exception( sprintf( __( 'Order with receipt number #%d not found.', 'woo-nfe' ), $id ), 2 );
}

return nfe_wc_get_order( $query->post->ID );
}

/**
* Logging method.
*
* @param string $message Log message.
*
* @return void
*/
public static function logger( $message ) {
$debug = nfe_get_field( 'debug' );

if ( empty( $debug ) ) {
return;
}

if ( 'yes' === $debug ) {
if ( empty( self::$logger ) ) {
self::$logger = wc_get_logger();
}

self::$logger->debug( $message, [ 'source' => 'nfe_webhook' ] );
}
}
/**
* WC_Logger Logger instance.
*
* @var bool
*/
public static $logger = false;

/**
* Base Construct.
*/
public function __construct() {
add_action( 'woocommerce_api_' . WC_API_CALLBACK, [$this, 'handle'] );
}

/**
* Handling incoming webhooks.
*
* @throws Exception Exception.
*
* @return void
*/
public function handle() {
$raw_body = file_get_contents( 'php://input' );
$body = json_decode( $raw_body );

// translators: Fired when a new webhook is called.
$this->logger( sprintf( __( 'New webhook called. %s', 'woo-nfe' ), $raw_body ) );

try {
$this->process_event( $body );
} catch ( Exception $e ) {
// translators: Error message from event.
$this->logger( sprintf( __( 'Error: %s.', 'woo-nfe' ), $e->getMessage() ) );

if ($e->getCode() === 2) {
http_response_code( 422 );
die( $e->getMessage() );
}
}
}

/**
* Read json entity received and proccess the webhook.
*
* @throws Exception Throws an exception.
* @param string $body Event body.
*
* @return void
*/
protected function process_event( $body ) {
$this->logger( __( 'Starting to proccess new webhook event.', 'woo-nfe' ) );

if ( is_null( $body ) ) {
$this->logger( __( 'Error while checking webhook JSON.', 'woo-nfe' ) );
$this->logger( sprintf( __( 'Error: %s.', 'woo-nfe' ), $body ) );

throw new Exception( __( 'Error while checking webhook JSON.', 'woo-nfe' ) );
}

$this->logger( __( 'Getting Order ID of the webhook.', 'woo-nfe' ) );
$order = $this->get_order_by_nota_id( $body->id );

$this->logger( __( 'Updating Order with NFe info.', 'woo-nfe' ) );

$meta = update_post_meta( $order->get_id(), 'nfe_issued', [
'id' => $body->id,
'status' => $body->flowStatus,
'issuedOn' => $body->issuedOn,
'amountNet' => $body->amountNet,
'checkCode' => $body->checkCode,
'number' => $body->number,
] );

if ( !$meta ) {
$this->logger( sprintf( __( 'There was a problem while updating the Order #%d with the NFe information.', 'woo-nfe' ), $order->get_id() ) );
return;
}

// translators: Order updated with its status.
$msg = sprintf( __( 'Order updated. Order: #%d NFe status: %s.', 'woo-nfe' ), $order->get_id(), nfe_status_label( $body->flowStatus ) );
$this->logger( $msg );
$order->add_order_note( $msg );
}

/**
* Find orders by NFe.io ID.
*
* @throws Exception Throws an exception.
* @param string $id NFE.io Receipt ID.
*
* @return WC_Order Order ID.
*/
protected function get_order_by_nota_id( $id ) {
$query = nfe_get_order_by_nota_value( $id );

if ( !$query->have_posts() || is_wp_error( $query ) ) {
// translators: Order with receipt number.
$this->logger( sprintf( __( 'Order with receipt number #%d not found.', 'woo-nfe' ), $id ) );

// translators: Order with receipt number.
throw new Exception( sprintf( __( 'Order with receipt number #%d not found.', 'woo-nfe' ), $id ), 2 );
}

return nfe_wc_get_order( $query->post->ID );
}

/**
* Logging method.
*
* @param string $message Log message.
*
* @return void
*/
public static function logger( $message ) {
$debug = nfe_get_field( 'debug' );

if ( empty( $debug ) ) {
return;
}

if ($debug === 'yes') {
if ( empty( self::$logger ) ) {
self::$logger = wc_get_logger();
}

self::$logger->debug( $message, ['source' => 'nfe_webhook'] );
}
}
}

return new WC_NFe_Webhook_Handler();

This file was deleted.

Loading

0 comments on commit 00b95a3

Please sign in to comment.