Skip to content

Commit

Permalink
Calculation/Transaction Sync Status Meta Box (#214)
Browse files Browse the repository at this point in the history
* Save Calculation Results to Cart and Order (#207)

* Persist calculation results on cart and order

* Ensure correct tax results get set on subscription

* Admin Order Meta Box (#208)

* Add meta box to orders

* Change copied notification from alert to tool tip

* INT 2155 Transaction Sync Data in Metabox (#211)

* Add meta box to orders

* Change copied notification from alert to tool tip

* Add transaction sync data to order metabox

* Refactor sync queue link method for better readability.

* Remove usage of deprecated function

* Remove storing of raw request and response and section for them in the order meta box (#213)

* Version 4.1.0
  • Loading branch information
dallendalton authored Jan 19, 2022
1 parent 465a81f commit b6a1bc9
Show file tree
Hide file tree
Showing 22 changed files with 893 additions and 69 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 4.1.0 (2022-01-19)
* Add order meta box to give details of calculation and sync statuses
* Remove usage of deprecated function is_ajax
* WooCommerce tested up to 6.1
* Update minimum WordPress version to 5.6

# 4.0.2 (2021-12-20)
* Filter invalid PTCs before creating transactions in TaxJar.
* Fix floating point precision issue causing transactions to be rejected by the TaxJar API.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Cart Tax Calculation Result Data Store
*
* Persists tax calculation result to cart.
*
* @package TaxJar
*/

namespace TaxJar;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

/**
* Class Cart_Tax_Calculation_Result_Data_Store
*/
class Cart_Tax_Calculation_Result_Data_Store implements Tax_Calculation_Result_Data_Store {

/**
* Cart to persist result on
*
* @var \WC_Cart
*/
private $cart;

/**
* Cart_Tax_Calculation_Result_Data_Store Constructor
*
* @param \WC_Cart $cart Cart to persist results on.
*/
public function __construct( \WC_Cart $cart ) {
$this->cart = $cart;
}

/**
* Persist results on the cart
*
* @param Tax_Calculation_Result $calculation_result Result of tax calculation.
*/
public function update( Tax_Calculation_Result $calculation_result ) {
$calculation_result->set_raw_request('');
$calculation_result->set_raw_response('');
$this->cart->tax_calculation_results = $calculation_result->to_json();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Order Tax Calculation Result Data Store
*
* Persists tax calculation result to order.
*
* @package TaxJar
*/

namespace TaxJar;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

/**
* Class Order_Tax_Calculation_Result_Data_Store
*/
class Order_Tax_Calculation_Result_Data_Store implements Tax_Calculation_Result_Data_Store {

/**
* Order to persist result on
*
* @var \WC_Order
*/
private $order;

/**
* Order_Tax_Calculation_Result_Data_Store Constructor
*
* @param \WC_Order $order Order to persist results on.
*/
public function __construct( \WC_Order $order ) {
$this->order = $order;
}

/**
* Persist results on the order
*
* @param Tax_Calculation_Result $calculation_result Result of tax calculation.
*/
public function update( Tax_Calculation_Result $calculation_result ) {
$calculation_result->set_raw_request('');
$calculation_result->set_raw_response('');
$this->order->update_meta_data( '_taxjar_tax_result', $calculation_result->to_json() );
}

}
27 changes: 27 additions & 0 deletions includes/TaxCalculation/class-tax-calculator-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace TaxJar;

use WC_Cart;
use WC_Order;
use WC_Taxjar_Nexus;
use TaxJar_Settings;

Expand Down Expand Up @@ -101,6 +102,7 @@ private function setup_api_tax_calculator( $order ) {
$this->set_order_applicator( $order );
$this->set_order_validator( $order );
$this->set_context( 'api_order' );
$this->set_order_tax_result_data_store( $order );
}

/**
Expand All @@ -114,6 +116,7 @@ private function setup_order_calculator( $order ) {
$this->set_order_applicator( $order );
$this->set_order_validator( $order );
$this->set_context( 'order' );
$this->set_order_tax_result_data_store( $order );
}

/**
Expand Down Expand Up @@ -270,6 +273,7 @@ private function setup_admin_order_calculator( $order ) {
$this->set_order_applicator( $order );
$this->set_order_validator( $order );
$this->set_context( 'admin_order' );
$this->set_order_tax_result_data_store( $order );
}

/**
Expand All @@ -294,6 +298,7 @@ public function build_subscription_order_calculator( $subscription ) {
$this->set_order_applicator( $subscription );
$this->set_order_validator( $subscription );
$this->set_context( 'subscription_order' );
$this->set_order_tax_result_data_store( $subscription );
return $this->calculator;
}

Expand All @@ -310,9 +315,20 @@ public function build_renewal_order_calculator( $renewal ): Tax_Calculator {
$this->set_order_applicator( $renewal );
$this->set_order_validator( $renewal );
$this->set_context( 'renewal_order' );
$this->set_order_tax_result_data_store( $renewal );
return $this->calculator;
}

/**
* Set the tax result data store when building an order calculator.
*
* @param WC_Order $order Order whose tax is being calculated.
*/
private function set_order_tax_result_data_store( WC_Order $order ) {
$result_data_store = new Order_Tax_Calculation_Result_Data_Store( $order );
$this->calculator->set_result_data_store( $result_data_store );
}

/**
* Build cart tax calculator.
*
Expand All @@ -326,9 +342,20 @@ public function build_cart_calculator( WC_Cart $cart ): Tax_Calculator {
$this->set_cart_tax_applicator( $cart );
$this->set_cart_validator( $cart );
$this->set_context( 'cart' );
$this->set_cart_tax_result_data_store( $cart );
return $this->calculator;
}

/**
* Set the tax result data store when building a cart calculator.
*
* @param WC_Cart $cart Cart whose tax is being calculated.
*/
private function set_cart_tax_result_data_store( WC_Cart $cart ) {
$result_data_store = new Cart_Tax_Calculation_Result_Data_Store( $cart );
$this->calculator->set_result_data_store( $result_data_store );
}

/**
* Sets the logger for cart tax calculation.
*/
Expand Down
51 changes: 38 additions & 13 deletions includes/TaxCalculation/class-tax-calculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ class Tax_Calculator {
*/
private $tax_details;

/**
* Persists the tax calculation results on the object having tax calculated.
*
* @var Tax_Calculation_Result_Data_Store
*/
private $result_data_store;

/**
* Result of tax calculation.
*
* @var Tax_Calculation_Result
*/
private $result;

/**
* Sets the logger.
*
Expand Down Expand Up @@ -154,6 +168,15 @@ public function get_context() {
return $this->context;
}

/**
* Set the result data store.
*
* @param Tax_Calculation_Result_Data_Store $data_store Result data store.
*/
public function set_result_data_store( Tax_Calculation_Result_Data_Store $data_store ) {
$this->result_data_store = $data_store;
}

/**
* Calculates and applies tax if possible and necessary.
*/
Expand All @@ -166,6 +189,8 @@ public function maybe_calculate_and_apply_tax() {
$this->success();
} catch ( Exception $exception ) {
$this->failure( $exception );
} finally {
$this->result_data_store->update( $this->result );
}
}

Expand Down Expand Up @@ -250,12 +275,12 @@ public function apply_tax() {
* Logs success details.
*/
private function success() {
$result = new Tax_Calculation_Result();
$result->set_success( true );
$result->set_context( $this->get_context() );
$result->set_raw_request( $this->request_body->to_json() );
$result->set_raw_response( wp_json_encode( $this->tax_details->get_raw_response() ) );
$this->logger->log_success( $result );
$this->result = new Tax_Calculation_Result();
$this->result->set_success( true );
$this->result->set_context( $this->get_context() );
$this->result->set_raw_request( $this->request_body->to_json() );
$this->result->set_raw_response( wp_json_encode( $this->tax_details->get_raw_response() ) );
$this->logger->log_success( $this->result );
}

/**
Expand All @@ -264,16 +289,16 @@ private function success() {
* @param Exception $exception Exception that occurred during tax calculation.
*/
private function failure( Exception $exception ) {
$result = new Tax_Calculation_Result();
$result->set_success( false );
$result->set_context( $this->get_context() );
$result->set_raw_request( $this->request_body->to_json() );
$this->result = new Tax_Calculation_Result();
$this->result->set_success( false );
$this->result->set_context( $this->get_context() );
$this->result->set_raw_request( $this->request_body->to_json() );

if ( $this->tax_details ) {
$result->set_raw_response( wp_json_encode( $this->tax_details->get_raw_response() ) );
$this->result->set_raw_response( wp_json_encode( $this->tax_details->get_raw_response() ) );
}

$result->set_error_message( $exception->getMessage() );
$this->logger->log_failure( $result, $exception );
$this->result->set_error_message( $exception->getMessage() );
$this->logger->log_failure( $this->result, $exception );
}
}
12 changes: 10 additions & 2 deletions includes/abstract-class-taxjar-record.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,13 @@ public function sync_success() {
$this->save();
}

public function add_object_sync_metadata() {
public function update_object_sync_success_meta_data() {
$data = $this->get_data();
$data_hash = hash( 'md5', serialize( $data ) );
$sync_datetime = $this->get_processed_datetime();
$this->object->update_meta_data( '_taxjar_last_sync', $sync_datetime );
$this->object->update_meta_data( '_taxjar_hash', $data_hash );
$this->object->delete_meta_data( '_taxjar_sync_last_error' );
$this->object->save();
}

Expand Down Expand Up @@ -331,6 +332,13 @@ public function sync_failure( $error_message ) {
$this->save();
}

public function update_object_sync_failure_meta_data( $error_message ) {
if ( $this->object ) {
$this->object->update_meta_data( '_taxjar_sync_last_error', $error_message );
$this->object->save();
}
}

abstract function create_in_taxjar();
abstract function update_in_taxjar();
abstract function delete_in_taxjar();
Expand Down Expand Up @@ -555,4 +563,4 @@ public function set_last_error( $last_error ) {
public function get_plugin_parameter() {
return 'woo';
}
}
}
43 changes: 43 additions & 0 deletions includes/admin/class-admin-meta-boxes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Admin Meta Boxes
*
* Adds meta box to order type posts.
*
* @package TaxJar
*/

namespace TaxJar;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

/**
* Class Admin_Meta_Boxes
*/
class Admin_Meta_Boxes {

/**
* Admin_Meta_Boxes Constructor.
*/
public function __construct() {
add_action( 'add_meta_boxes', array( $this, 'add_order_meta_box' ), 30 );
}

/**
* Add meta box to order post types.
*/
public function add_order_meta_box() {
foreach ( wc_get_order_types( 'order-meta-boxes' ) as $type ) {
add_meta_box(
'taxjar',
__( 'TaxJar', 'taxjar' ),
'\TaxJar\Order_Meta_Box::output',
$type,
'normal',
'low'
);
}
}
}
Loading

0 comments on commit b6a1bc9

Please sign in to comment.