From 3c55c64ffaaccb04042b41b47310f6aa649392ca Mon Sep 17 00:00:00 2001 From: Max Frederiksen Date: Tue, 6 Feb 2024 16:39:39 +0100 Subject: [PATCH 01/11] followed guide to refactor up until declaring extension compatibility --- .../class-wc-gateway-mondido-abstract.php | 41 +++++++++--- includes/class-wc-gateway-mondido-hw.php | 62 ++++++++++++++++--- includes/class-wc-mondido-admin-actions.php | 30 ++++++--- includes/class-wc-mondido-subscriptions.php | 15 ++++- .../class-wc-order-compatibility-mondido.php | 19 +++++- 5 files changed, 138 insertions(+), 29 deletions(-) diff --git a/includes/abstracts/class-wc-gateway-mondido-abstract.php b/includes/abstracts/class-wc-gateway-mondido-abstract.php index 32c3018..daf5a6e 100644 --- a/includes/abstracts/class-wc-gateway-mondido-abstract.php +++ b/includes/abstracts/class-wc-gateway-mondido-abstract.php @@ -4,6 +4,8 @@ exit; } // Exit if accessed directly +use Automattic\WooCommerce\Utilities\OrderUtil; + abstract class WC_Gateway_Mondido_Abstract extends WC_Payment_Gateway { protected $api; protected $transaction; @@ -336,15 +338,32 @@ public function handle_transaction( $order, $transaction_data ) { throw new \Exception( "Transaction already applied. Order ID: {$order_id}. Transaction ID: {$transaction_id}. Transaction status: {$status}" ); } - // Save Transaction - delete_post_meta( $order_id, '_transaction_id' ); - update_post_meta( $order_id, '_transaction_id', $transaction_id ); - delete_post_meta( $order_id, '_mondido_transaction_status' ); - update_post_meta( $order_id, '_mondido_transaction_status', $status ); + if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { + // HPOS usage is enabled. + $order->delete_meta_data( '_transaction_id' ); + $order->update_meta_data( '_transaction_id', $transaction_id ); + + $order->delete_meta_data( '_mondido_transaction_status' ); + $order->update_meta_data( '_mondido_transaction_status', $status ); - delete_post_meta( $order_id, '_mondido_transaction_data' ); - update_post_meta( $order_id, '_mondido_transaction_data', $transaction_data ); + $order->delete_meta_data( '_mondido_transaction_data' ); + $order->update_meta_data( '_mondido_transaction_data', $transaction_data ); + + $order->save(); + } else { + // Traditional CPT-based orders are in use. + // Save Transaction + delete_post_meta( $order_id, '_transaction_id' ); + update_post_meta( $order_id, '_transaction_id', $transaction_id ); + + delete_post_meta( $order_id, '_mondido_transaction_status' ); + update_post_meta( $order_id, '_mondido_transaction_status', $status ); + + delete_post_meta( $order_id, '_mondido_transaction_data' ); + update_post_meta( $order_id, '_mondido_transaction_data', $transaction_data ); + } + switch ( $status ) { case 'pending': @@ -388,7 +407,13 @@ public function handle_transaction( $order, $transaction_data ) { 'postcode' => $details['zip'], 'country' => $this->get_country_alpha2( $details['country_code'] ), ); - update_post_meta( $order_id, '_mondido_invoice_address', $address ); + if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { + // HPOS usage is enabled. + $order->update_meta_data( '_mondido_invoice_address', $address ); + } else { + // Traditional CPT-based orders are in use. + update_post_meta( $order_id, '_mondido_invoice_address', $address ); + } } // Define address for Mondido Checkout diff --git a/includes/class-wc-gateway-mondido-hw.php b/includes/class-wc-gateway-mondido-hw.php index 2a63032..2970bd3 100644 --- a/includes/class-wc-gateway-mondido-hw.php +++ b/includes/class-wc-gateway-mondido-hw.php @@ -3,6 +3,8 @@ exit; } // Exit if accessed directly +use Automattic\WooCommerce\Utilities\OrderUtil; + class WC_Gateway_Mondido_HW extends WC_Gateway_Mondido_Abstract { protected $preselected_method = null; @@ -238,9 +240,18 @@ public function process_payment( $order_id ) { $new_card_key = "wc-{$this->id}-new-payment-method"; $token_id = isset( $_POST[$token_key] ) ? wc_clean( $_POST['token_key'] ) : 'new'; + - delete_post_meta( $order_id, '_mondido_use_store_card'); - delete_post_meta( $order_id, '_mondido_store_card'); + if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { + // HPOS usage is enabled. + $order->delete_meta_data('_mondido_use_store_card'); + $order->delete_post_meta('_mondido_store_card'); + $order->save(); + } else { + // Traditional CPT-based orders are in use. + delete_post_meta( $order_id, '_mondido_use_store_card' ); + delete_post_meta( $order_id, '_mondido_store_card' ); + } // Try to load saved token if ( $token_id !== 'new' ) { @@ -258,9 +269,23 @@ public function process_payment( $order_id ) { return false; } - update_post_meta( $order_id, '_mondido_use_store_card', $token->get_id() ); + if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { + // HPOS usage is enabled. + $order->update_meta_data('_mondido_use_store_card', $token->get_id()); + $order->save(); + } else { + // Traditional CPT-based orders are in use. + update_post_meta( $order_id, '_mondido_use_store_card', $token->get_id() ); + } } elseif ( isset( $_POST[$new_card_key] ) && $_POST[$new_card_key] === 'true' ) { - update_post_meta( $order_id, '_mondido_store_card', 1 ); + if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { + // HPOS usage is enabled. + $order->update_meta_data('_mondido_store_card', 1); + $order->save(); + } else { + // Traditional CPT-based orders are in use. + update_post_meta( $order_id, '_mondido_store_card', 1 ); + } } } @@ -299,7 +324,14 @@ public function process_payment( $order_id ) { ); if (!is_wp_error($transaction)) { - update_post_meta( $order_id, '_transaction_id', $transaction->id ); + if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { + // HPOS usage is enabled. + $order->update_meta_data('_transaction_id', $transaction->id); + $order->save(); + } else { + // Traditional CPT-based orders are in use. + update_post_meta( $order_id, '_transaction_id', $transaction->id ); + } } } @@ -523,11 +555,21 @@ public function notification_callback() { 'total' => $transaction_data['amount'], 'created_via' => 'mondido', ) ); - add_post_meta( $order->get_id(), '_payment_method', $this->id ); - update_post_meta( $order->get_id(), '_transaction_id', $transaction_data['id'] ); - update_post_meta( $order->get_id(), '_mondido_transaction_status', $transaction_data['status'] ); - update_post_meta( $order->get_id(), '_mondido_transaction_data', $transaction_data ); - update_post_meta( $order->get_id(), '_mondido_subscription_id', $transaction_data['subscription']['id'] ); + if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { + // HPOS usage is enabled. + $order->add_meta_data('_payment_method', $this->id); + $order->update_meta_data('_transaction_id', $transaction_data['id']); + $order->update_meta_data('_mondido_transaction_status', $transaction_data['status']); + $order->update_meta_data('_mondido_transaction_data', $transaction_data); + $order->update_meta_data('_mondido_subscription_id', $transaction_data['subscription']['id']); + } else { + // Traditional CPT-based orders are in use. + add_post_meta( $order->get_id(), '_payment_method', $this->id ); + update_post_meta( $order->get_id(), '_transaction_id', $transaction_data['id'] ); + update_post_meta( $order->get_id(), '_mondido_transaction_status', $transaction_data['status'] ); + update_post_meta( $order->get_id(), '_mondido_transaction_data', $transaction_data ); + update_post_meta( $order->get_id(), '_mondido_subscription_id', $transaction_data['subscription']['id'] ); + } // Add address $order->set_address( $transaction_data['metadata']['customer'], 'billing' ); diff --git a/includes/class-wc-mondido-admin-actions.php b/includes/class-wc-mondido-admin-actions.php index 00a045b..fbdf0a4 100644 --- a/includes/class-wc-mondido-admin-actions.php +++ b/includes/class-wc-mondido-admin-actions.php @@ -3,6 +3,9 @@ exit; } // Exit if accessed directly +use Automattic\WooCommerce\Utilities\OrderUtil; +use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController; + class WC_Mondido_Admin_Actions { /** * Constructor @@ -30,11 +33,15 @@ public static function add_meta_boxes() { if ( $order && strpos( $order->get_payment_method(), 'mondido' ) !== false ) { $transaction = get_post_meta( $order->get_id(), '_mondido_transaction_data', TRUE ); if ( ! empty( $transaction ) ) { + $screen = class_exists( '\Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController' ) && wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled() + ? wc_get_page_screen_id( 'shop-order' ) + : 'shop_order'; + add_meta_box( 'mondido_payment_actions', __( 'Mondido Payments', 'woocommerce-gateway-mondido' ), __CLASS__ . '::order_meta_box_payment_actions', - 'shop_order', + $screen, 'side', 'default' ); @@ -46,9 +53,9 @@ public static function add_meta_boxes() { * MetaBox for Payment Actions * @return void */ - public static function order_meta_box_payment_actions() { + public static function order_meta_box_payment_actions( $post_or_order_object ) { global $post_id; - $order = wc_get_order( $post_id ); + $order = ( $post_or_order_object instanceof WP_Post ) ? wc_get_order( $post_or_order_object->ID ) : $post_or_order_object; $transaction = get_post_meta( $order->get_id(), '_mondido_transaction_data', TRUE ); wc_get_template( @@ -111,10 +118,19 @@ public function ajax_mondido_capture() { } if ( $transaction['status'] === 'approved' ) { - // Save Transaction - update_post_meta( $order->get_id(), '_transaction_id', $transaction['id'] ); - update_post_meta( $order->get_id(), '_mondido_transaction_status', $transaction['status'] ); - update_post_meta( $order->get_id(), '_mondido_transaction_data', $transaction ); + if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { + // HPOS usage is enabled. + $order->update_meta_data( '_transaction_id', $transaction['id'] ); + $order->update_meta_data( '_mondido_transaction_status', $transaction['status'] ); + $order->update_meta_data( '_mondido_transaction_data', $transaction ); + $order->save(); + } else { + // Traditional CPT-based orders are in use. + // Save Transaction + update_post_meta( $order->get_id(), '_transaction_id', $transaction['id'] ); + update_post_meta( $order->get_id(), '_mondido_transaction_status', $transaction['status'] ); + update_post_meta( $order->get_id(), '_mondido_transaction_data', $transaction ); + } $order->add_order_note( sprintf( __( 'Payment captured. Transaction Id: %s', 'woocommerce-gateway-mondido' ), $transaction['id'] ) ); $order->payment_complete( $transaction['id'] ); diff --git a/includes/class-wc-mondido-subscriptions.php b/includes/class-wc-mondido-subscriptions.php index 7315e7b..d25bef8 100644 --- a/includes/class-wc-mondido-subscriptions.php +++ b/includes/class-wc-mondido-subscriptions.php @@ -4,6 +4,8 @@ exit; } // Exit if accessed directly +use Automattic\WooCommerce\Utilities\OrderUtil; + class WC_Mondido_Subscriptions { private $api; @@ -86,14 +88,23 @@ public function subscription_options_product_tab_content() { */ public function save_subscription_field() { global $post_id; + $order = wc_get_order( $post_id ); if ( empty( $post_id ) ) { return; } if ( isset( $_POST['_mondido_plan_id'] ) ) { - update_post_meta( $post_id, '_mondido_plan_id', $_POST['_mondido_plan_id'] ); - update_post_meta( $post_id, '_mondido_plan_include', $_POST['_mondido_plan_include'] ); + if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { + // HPOS usage is enabled. + $order->update_meta_data( '_mondido_plan_id', $_POST['_mondido_plan_id'] ); + $order->update_meta_data( '_mondido_plan_include', $_POST['_mondido_plan_include'] ); + $order->save(); + } else { + // Traditional CPT-based orders are in use. + update_post_meta( $post_id, '_mondido_plan_id', $_POST['_mondido_plan_id'] ); + update_post_meta( $post_id, '_mondido_plan_include', $_POST['_mondido_plan_include'] ); + } } } diff --git a/includes/deprecated/class-wc-order-compatibility-mondido.php b/includes/deprecated/class-wc-order-compatibility-mondido.php index a96d127..bb5842d 100644 --- a/includes/deprecated/class-wc-order-compatibility-mondido.php +++ b/includes/deprecated/class-wc-order-compatibility-mondido.php @@ -4,6 +4,8 @@ exit; } // Exit if accessed directly +use Automattic\WooCommerce\Utilities\OrderUtil; + /** * Compatibility Layer for WC_Order on WooCommerce < 3.0 * @see https://woocommerce.wordpress.com/2017/04/04/say-hello-to-woocommerce-3-0-bionic-butterfly/ @@ -52,12 +54,25 @@ class WC_Order_Compatibility_Mondido { */ public function __construct( $the_order ) { global $post; + global $post_id; if ( FALSE === $the_order ) { $the_order = $post; } elseif ( is_numeric( $the_order ) ) { - $the_order = get_post( $the_order ); + if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { + // HPOS usage is enabled. + $the_order = wc_get_order( $post_id ); + } else { + // Traditional CPT-based orders are in use. + $the_order = get_post( $the_order ); + } } elseif ( $the_order instanceof WC_Order ) { - $the_order = get_post( $the_order->id ); + if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { + // HPOS usage is enabled. + $the_order = wc_get_order( $post_id ); + } else { + // Traditional CPT-based orders are in use. + $the_order = get_post( $the_order->id ); + } } if ( ! $the_order || ! is_object( $the_order ) ) { From a6970c246ea1945c4773b02150316b9860835b0f Mon Sep 17 00:00:00 2001 From: Max Frederiksen Date: Tue, 9 Apr 2024 14:41:07 +0200 Subject: [PATCH 02/11] refactoring --- .../class-wc-gateway-mondido-abstract.php | 53 ++++-------- includes/class-wc-gateway-mondido-hw.php | 84 ++++++------------- includes/class-wc-mondido-admin-actions.php | 27 +++--- .../class-wc-mondido-hpos-compatibility.php | 68 +++++++++++++++ includes/class-wc-mondido-subscriptions.php | 15 +--- .../class-wc-order-compatibility-mondido.php | 18 +--- woocommerce-gateway-mondido.php | 1 + 7 files changed, 128 insertions(+), 138 deletions(-) create mode 100644 includes/class-wc-mondido-hpos-compatibility.php diff --git a/includes/abstracts/class-wc-gateway-mondido-abstract.php b/includes/abstracts/class-wc-gateway-mondido-abstract.php index daf5a6e..90a3746 100644 --- a/includes/abstracts/class-wc-gateway-mondido-abstract.php +++ b/includes/abstracts/class-wc-gateway-mondido-abstract.php @@ -10,10 +10,12 @@ abstract class WC_Gateway_Mondido_Abstract extends WC_Payment_Gateway { protected $api; protected $transaction; protected $logger; + protected $orderStorage; public function add_dependencies(WC_Mondido_Api $api, WC_Mondido_Transaction $transaction) { $this->api = $api; $this->transaction = $transaction; + $this->orderStorage = OrderStorageTechnology::current(); } /** @@ -333,37 +335,22 @@ public function handle_transaction( $order, $transaction_data ) { // Check transaction was processed $current_transaction_id = $order->get_transaction_id(); - $current_status = get_post_meta( $order_id, '_mondido_transaction_status', true ); + $current_status = $this->orderStorage->get_meta( $order, '_mondido_transaction_status', true ); + if ( $current_transaction_id === $transaction_id && $current_status === $status ) { throw new \Exception( "Transaction already applied. Order ID: {$order_id}. Transaction ID: {$transaction_id}. Transaction status: {$status}" ); } + $this->orderStorage->delete_meta_data( $order, '_transaction_id' ); + $this->orderStorage->update_meta_data( $order, '_transaction_id', $transaction_id ); + + $this->orderStorage->delete_meta_data( $order, '_mondido_transaction_status' ); + $this->orderStorage->update_meta_data( $order, '_mondido_transaction_status', $status ); - if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { - // HPOS usage is enabled. - $order->delete_meta_data( '_transaction_id' ); - $order->update_meta_data( '_transaction_id', $transaction_id ); - - $order->delete_meta_data( '_mondido_transaction_status' ); - $order->update_meta_data( '_mondido_transaction_status', $status ); - - $order->delete_meta_data( '_mondido_transaction_data' ); - $order->update_meta_data( '_mondido_transaction_data', $transaction_data ); + $this->orderStorage->delete_meta_data( $order, '_mondido_transaction_data' ); + $this->orderStorage->update_meta_data( $order, '_mondido_transaction_data', $transaction_data ); - $order->save(); - } else { - // Traditional CPT-based orders are in use. - // Save Transaction - delete_post_meta( $order_id, '_transaction_id' ); - update_post_meta( $order_id, '_transaction_id', $transaction_id ); - - delete_post_meta( $order_id, '_mondido_transaction_status' ); - update_post_meta( $order_id, '_mondido_transaction_status', $status ); - - delete_post_meta( $order_id, '_mondido_transaction_data' ); - update_post_meta( $order_id, '_mondido_transaction_data', $transaction_data ); - } - + $this->orderStorage->save( $order ); switch ( $status ) { case 'pending': @@ -407,17 +394,13 @@ public function handle_transaction( $order, $transaction_data ) { 'postcode' => $details['zip'], 'country' => $this->get_country_alpha2( $details['country_code'] ), ); - if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { - // HPOS usage is enabled. - $order->update_meta_data( '_mondido_invoice_address', $address ); - } else { - // Traditional CPT-based orders are in use. - update_post_meta( $order_id, '_mondido_invoice_address', $address ); - } - } + } + + $this->orderStorage->update_meta_data( $order, '_mondido_invoice_address', $address ); + $this->orderStorage->save( $order ); // Define address for Mondido Checkout - if ( (bool) get_post_meta( $order_id, '_mondido_checkout', TRUE ) ) { + if ( (bool) $this->orderStorage->get_meta( $order, '_mondido_checkout', TRUE ) ) { $order->set_address( $address, 'billing' ); if ( $order->needs_shipping_address() ) { @@ -581,7 +564,7 @@ public function getMondidoCustomerId( $customer_reference ) { public function get_payment_method_name($value, $order, $default_value) { - $transaction = get_post_meta( $order->get_id(), '_mondido_transaction_data', TRUE ); + $transaction = $this->orderStorage->get_meta( $order, '_mondido_transaction_data', TRUE ); if (!$transaction) { if ($order->get_transaction_id()) { diff --git a/includes/class-wc-gateway-mondido-hw.php b/includes/class-wc-gateway-mondido-hw.php index 2970bd3..b498b47 100644 --- a/includes/class-wc-gateway-mondido-hw.php +++ b/includes/class-wc-gateway-mondido-hw.php @@ -3,8 +3,6 @@ exit; } // Exit if accessed directly -use Automattic\WooCommerce\Utilities\OrderUtil; - class WC_Gateway_Mondido_HW extends WC_Gateway_Mondido_Abstract { protected $preselected_method = null; @@ -234,24 +232,17 @@ public function payment_fields() { */ public function process_payment( $order_id ) { $order = wc_get_order( $order_id ); + $orderStorage = OrderStorageTechnology::current(); if ( $this->store_cards === 'yes' ) { $token_key = "wc-{$this->id}-payment-token"; $new_card_key = "wc-{$this->id}-new-payment-method"; $token_id = isset( $_POST[$token_key] ) ? wc_clean( $_POST['token_key'] ) : 'new'; - - if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { - // HPOS usage is enabled. - $order->delete_meta_data('_mondido_use_store_card'); - $order->delete_post_meta('_mondido_store_card'); - $order->save(); - } else { - // Traditional CPT-based orders are in use. - delete_post_meta( $order_id, '_mondido_use_store_card' ); - delete_post_meta( $order_id, '_mondido_store_card' ); - } + $orderStorage->delete_meta_data($order, '_mondido_use_store_card'); + $order->delete_meta_data($order, '_mondido_store_card'); + $orderStorage->save( $order ); // Try to load saved token if ( $token_id !== 'new' ) { @@ -268,29 +259,16 @@ public function process_payment( $order_id ) { return false; } - - if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { - // HPOS usage is enabled. - $order->update_meta_data('_mondido_use_store_card', $token->get_id()); - $order->save(); - } else { - // Traditional CPT-based orders are in use. - update_post_meta( $order_id, '_mondido_use_store_card', $token->get_id() ); - } + $orderStorage->update_meta_data($order, '_mondido_use_store_card', $token->get_id()); + $orderStorage->save( $order ); } elseif ( isset( $_POST[$new_card_key] ) && $_POST[$new_card_key] === 'true' ) { - if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { - // HPOS usage is enabled. - $order->update_meta_data('_mondido_store_card', 1); - $order->save(); - } else { - // Traditional CPT-based orders are in use. - update_post_meta( $order_id, '_mondido_store_card', 1 ); - } + $orderStorage->update_meta_data($order, '_mondido_store_card', 1); + $orderStorage->save( $order ); } } $transaction_id = $order->get_transaction_id(); - $store_card = (bool) get_post_meta($order_id, '_mondido_store_card', true); + $store_card = (bool) $orderStorage->get_meta($order, '_mondido_store_card', true); if ($transaction_id) { $transaction = $this->transaction->get($transaction_id); @@ -324,14 +302,8 @@ public function process_payment( $order_id ) { ); if (!is_wp_error($transaction)) { - if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { - // HPOS usage is enabled. - $order->update_meta_data('_transaction_id', $transaction->id); - $order->save(); - } else { - // Traditional CPT-based orders are in use. - update_post_meta( $order_id, '_transaction_id', $transaction->id ); - } + $orderStorage->update_meta_data($order, '_transaction_id', $transaction->id); + $orderStorage->save( $order ); } } @@ -474,7 +446,7 @@ public function notification_callback() { if ( count( $tokens ) > 0 ) { $message = 'This Credit Card already stored: ' . $card_number; header( sprintf( '%s %s %s', 'HTTP/1.1', '200', 'OK' ), TRUE, '200' ); - $this->logger->notice( $this->id, sprintf( '[%s] IPN: %s', 'SUCCESS', $message ) ); + $this->logger->notice( $this->id, array('status' => '[SUCCESS] IPN', 'message' => $message)); echo sprintf( 'IPN: %s', $message ); } @@ -500,19 +472,19 @@ public function notification_callback() { // Success $message = 'Stored Credit Card: ' . $card_number; header( sprintf( '%s %s %s', 'HTTP/1.1', '200', 'OK' ), TRUE, '200' ); - $this->logger->notice( $this->id, sprintf( '[%s] IPN: %s', 'SUCCESS', $message ) ); + $this->logger->notice($this->id, array('status' => '[SUCCESS] IPN', 'message' => $message)); echo sprintf( 'IPN: %s', $message ); return; } - $this->logger->notice( $this->id, var_export($data, true) ); + $this->logger->notice( $this->id, array( 'data' => $data) ); if ( empty( $data['id'] ) ) { throw new \Exception( 'Invalid transaction ID' ); } // Log transaction details - $this->logger->notice( $this->id, 'Incoming Transaction: ' . var_export( json_encode( $data, true ), true) ); + $this->logger->notice( $this->id, array('message' => 'Incoming Transaction', 'data' => $data) ); // Wait for unlock $times = 0; @@ -555,21 +527,15 @@ public function notification_callback() { 'total' => $transaction_data['amount'], 'created_via' => 'mondido', ) ); - if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { - // HPOS usage is enabled. - $order->add_meta_data('_payment_method', $this->id); - $order->update_meta_data('_transaction_id', $transaction_data['id']); - $order->update_meta_data('_mondido_transaction_status', $transaction_data['status']); - $order->update_meta_data('_mondido_transaction_data', $transaction_data); - $order->update_meta_data('_mondido_subscription_id', $transaction_data['subscription']['id']); - } else { - // Traditional CPT-based orders are in use. - add_post_meta( $order->get_id(), '_payment_method', $this->id ); - update_post_meta( $order->get_id(), '_transaction_id', $transaction_data['id'] ); - update_post_meta( $order->get_id(), '_mondido_transaction_status', $transaction_data['status'] ); - update_post_meta( $order->get_id(), '_mondido_transaction_data', $transaction_data ); - update_post_meta( $order->get_id(), '_mondido_subscription_id', $transaction_data['subscription']['id'] ); - } + $orderStorage = OrderStorageTechnology::current(); + + $orderStorage->add_meta_data($order, '_payment_method', $this->id); + $orderStorage->update_meta_data($order, '_transaction_id', $transaction_data['id']); + $orderStorage->update_meta_data($order, '_mondido_transaction_status', $transaction_data['status']); + $orderStorage->update_meta_data($order, '_mondido_transaction_data', $transaction_data); + $orderStorage->update_meta_data($order, '_mondido_subscription_id', $transaction_data['subscription']['id']); + + wc_get_logger()->info('test 5'); // Add address $order->set_address( $transaction_data['metadata']['customer'], 'billing' ); @@ -672,7 +638,7 @@ public function notification_callback() { // Success header( sprintf( '%s %s %s', 'HTTP/1.1', '200', 'OK' ), TRUE, '200' ); - $this->logger->notice( $this->id, sprintf( '[%s] IPN: %s', 'SUCCESS', $message ) ); + $this->logger->notice($this->id, array('status' => '[SUCCESS] IPN', 'message' => $message)); echo sprintf( 'IPN: %s', $message ); exit(); } diff --git a/includes/class-wc-mondido-admin-actions.php b/includes/class-wc-mondido-admin-actions.php index fbdf0a4..c03eedb 100644 --- a/includes/class-wc-mondido-admin-actions.php +++ b/includes/class-wc-mondido-admin-actions.php @@ -3,7 +3,6 @@ exit; } // Exit if accessed directly -use Automattic\WooCommerce\Utilities\OrderUtil; use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController; class WC_Mondido_Admin_Actions { @@ -31,7 +30,8 @@ public static function add_meta_boxes() { global $post_id; $order = wc_get_order( $post_id ); if ( $order && strpos( $order->get_payment_method(), 'mondido' ) !== false ) { - $transaction = get_post_meta( $order->get_id(), '_mondido_transaction_data', TRUE ); + $orderStorage = OrderStorageTechnology::current(); + $transaction = $orderStorage->get_meta( $order, '_mondido_transaction_data', TRUE ); if ( ! empty( $transaction ) ) { $screen = class_exists( '\Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController' ) && wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled() ? wc_get_page_screen_id( 'shop-order' ) @@ -56,7 +56,9 @@ public static function add_meta_boxes() { public static function order_meta_box_payment_actions( $post_or_order_object ) { global $post_id; $order = ( $post_or_order_object instanceof WP_Post ) ? wc_get_order( $post_or_order_object->ID ) : $post_or_order_object; - $transaction = get_post_meta( $order->get_id(), '_mondido_transaction_data', TRUE ); + + $orderStorage = OrderStorageTechnology::current(); + $transaction = $orderStorage->get_meta( $order, '_mondido_transaction_data', TRUE ); wc_get_template( 'admin/payment-actions.php', @@ -118,19 +120,12 @@ public function ajax_mondido_capture() { } if ( $transaction['status'] === 'approved' ) { - if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { - // HPOS usage is enabled. - $order->update_meta_data( '_transaction_id', $transaction['id'] ); - $order->update_meta_data( '_mondido_transaction_status', $transaction['status'] ); - $order->update_meta_data( '_mondido_transaction_data', $transaction ); - $order->save(); - } else { - // Traditional CPT-based orders are in use. - // Save Transaction - update_post_meta( $order->get_id(), '_transaction_id', $transaction['id'] ); - update_post_meta( $order->get_id(), '_mondido_transaction_status', $transaction['status'] ); - update_post_meta( $order->get_id(), '_mondido_transaction_data', $transaction ); - } + $orderStorage = OrderStorageTechnology::current(); + + $orderStorage->update_meta_data( $order, '_transaction_id', $transaction['id'] ); + $orderStorage->update_meta_data( $order, '_mondido_transaction_status', $transaction['status'] ); + $orderStorage->update_meta_data( $order, '_mondido_transaction_data', $transaction ); + $orderStorage->save( $order ); $order->add_order_note( sprintf( __( 'Payment captured. Transaction Id: %s', 'woocommerce-gateway-mondido' ), $transaction['id'] ) ); $order->payment_complete( $transaction['id'] ); diff --git a/includes/class-wc-mondido-hpos-compatibility.php b/includes/class-wc-mondido-hpos-compatibility.php new file mode 100644 index 0000000..874f589 --- /dev/null +++ b/includes/class-wc-mondido-hpos-compatibility.php @@ -0,0 +1,68 @@ +get_meta($meta_key, $meta_value); + } + + public function add_meta_data($order, $meta_key, $meta_value) { + $order->add_meta_data($meta_key, $meta_value); + } + + public function update_meta_data($order, $meta_key, $meta_value) { + $order->update_meta_data($meta_key, $meta_value); + } + + public function delete_meta_data($order, $meta_key) { + $order->delete_meta_data($meta_key); + } + + public function save($order) { + $order->save(); + } +} + +class CPTOrderStorage implements OrderStorageTechnologyFactory { + public function get_meta($order, $meta_key, $meta_value) { + return get_post_meta($order->get_id(), $meta_key, $meta_value); + } + + public function add_meta_data($order, $meta_key, $meta_value) { + add_post_meta($order->get_id(), $meta_key, $meta_value); + } + + public function update_meta_data($order, $meta_key, $meta_value) { + update_post_meta($order->get_id(), $meta_key, $meta_value); + } + + public function delete_meta_data($order, $meta_key) { + delete_post_meta($order->get_id(), $meta_key); + } + + public function save($order) { + return; // Do nothing, CPT doesn't use save + } +} + +class OrderStorageTechnology { + public static function current() { + if (OrderUtil::custom_orders_table_usage_is_enabled()) { + return new HPOSOrderStorage(); + } else { + return new CPTOrderStorage(); + } + } +} \ No newline at end of file diff --git a/includes/class-wc-mondido-subscriptions.php b/includes/class-wc-mondido-subscriptions.php index d25bef8..7315e7b 100644 --- a/includes/class-wc-mondido-subscriptions.php +++ b/includes/class-wc-mondido-subscriptions.php @@ -4,8 +4,6 @@ exit; } // Exit if accessed directly -use Automattic\WooCommerce\Utilities\OrderUtil; - class WC_Mondido_Subscriptions { private $api; @@ -88,23 +86,14 @@ public function subscription_options_product_tab_content() { */ public function save_subscription_field() { global $post_id; - $order = wc_get_order( $post_id ); if ( empty( $post_id ) ) { return; } if ( isset( $_POST['_mondido_plan_id'] ) ) { - if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { - // HPOS usage is enabled. - $order->update_meta_data( '_mondido_plan_id', $_POST['_mondido_plan_id'] ); - $order->update_meta_data( '_mondido_plan_include', $_POST['_mondido_plan_include'] ); - $order->save(); - } else { - // Traditional CPT-based orders are in use. - update_post_meta( $post_id, '_mondido_plan_id', $_POST['_mondido_plan_id'] ); - update_post_meta( $post_id, '_mondido_plan_include', $_POST['_mondido_plan_include'] ); - } + update_post_meta( $post_id, '_mondido_plan_id', $_POST['_mondido_plan_id'] ); + update_post_meta( $post_id, '_mondido_plan_include', $_POST['_mondido_plan_include'] ); } } diff --git a/includes/deprecated/class-wc-order-compatibility-mondido.php b/includes/deprecated/class-wc-order-compatibility-mondido.php index bb5842d..d528188 100644 --- a/includes/deprecated/class-wc-order-compatibility-mondido.php +++ b/includes/deprecated/class-wc-order-compatibility-mondido.php @@ -4,7 +4,7 @@ exit; } // Exit if accessed directly -use Automattic\WooCommerce\Utilities\OrderUtil; +// FILE SHOULD BE DELETED AFTER UPGRADE OF WOOCOMMERCE 8.7 /** * Compatibility Layer for WC_Order on WooCommerce < 3.0 @@ -58,21 +58,9 @@ public function __construct( $the_order ) { if ( FALSE === $the_order ) { $the_order = $post; } elseif ( is_numeric( $the_order ) ) { - if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { - // HPOS usage is enabled. - $the_order = wc_get_order( $post_id ); - } else { - // Traditional CPT-based orders are in use. - $the_order = get_post( $the_order ); - } + $the_order = get_post( $the_order ); } elseif ( $the_order instanceof WC_Order ) { - if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { - // HPOS usage is enabled. - $the_order = wc_get_order( $post_id ); - } else { - // Traditional CPT-based orders are in use. - $the_order = get_post( $the_order->id ); - } + $the_order = get_post( $the_order->id ); } if ( ! $the_order || ! is_object( $the_order ) ) { diff --git a/woocommerce-gateway-mondido.php b/woocommerce-gateway-mondido.php index 5bba401..9e2e883 100644 --- a/woocommerce-gateway-mondido.php +++ b/woocommerce-gateway-mondido.php @@ -124,6 +124,7 @@ public function woocommerce_loaded() { include_once( dirname( __FILE__ ) . '/includes/class-wc-gateway-mondido-card.php' ); include_once( dirname( __FILE__ ) . '/includes/class-wc-mondido-api.php' ); include_once( dirname( __FILE__ ) . '/includes/class-wc-mondido-transaction.php' ); + include_once( dirname( __FILE__ ) . '/includes/class-wc-mondido-hpos-compatibility.php' ); include_once( dirname( __FILE__ ) . '/includes/class-wc-mondido-admin-actions.php' ); include_once( dirname( __FILE__ ) . '/includes/class-wc-mondido-subscriptions.php' ); From 6c381aabf0600bb2071e4ef1a6874ec70deab1e1 Mon Sep 17 00:00:00 2001 From: Max Frederiksen Date: Tue, 9 Apr 2024 14:44:48 +0200 Subject: [PATCH 03/11] remove unused var --- includes/deprecated/class-wc-order-compatibility-mondido.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/deprecated/class-wc-order-compatibility-mondido.php b/includes/deprecated/class-wc-order-compatibility-mondido.php index d528188..c3c5767 100644 --- a/includes/deprecated/class-wc-order-compatibility-mondido.php +++ b/includes/deprecated/class-wc-order-compatibility-mondido.php @@ -54,7 +54,7 @@ class WC_Order_Compatibility_Mondido { */ public function __construct( $the_order ) { global $post; - global $post_id; + if ( FALSE === $the_order ) { $the_order = $post; } elseif ( is_numeric( $the_order ) ) { From 5204b5ce82aedeb8451dd9b060c6b23ae0971ec9 Mon Sep 17 00:00:00 2001 From: Max Frederiksen Date: Tue, 9 Apr 2024 14:45:14 +0200 Subject: [PATCH 04/11] remvove whitespace --- includes/deprecated/class-wc-order-compatibility-mondido.php | 1 - 1 file changed, 1 deletion(-) diff --git a/includes/deprecated/class-wc-order-compatibility-mondido.php b/includes/deprecated/class-wc-order-compatibility-mondido.php index c3c5767..8f887cd 100644 --- a/includes/deprecated/class-wc-order-compatibility-mondido.php +++ b/includes/deprecated/class-wc-order-compatibility-mondido.php @@ -54,7 +54,6 @@ class WC_Order_Compatibility_Mondido { */ public function __construct( $the_order ) { global $post; - if ( FALSE === $the_order ) { $the_order = $post; } elseif ( is_numeric( $the_order ) ) { From 8ae20670e5ce0f3296416066817c2cd275b83430 Mon Sep 17 00:00:00 2001 From: Max Frederiksen Date: Tue, 9 Apr 2024 14:57:42 +0200 Subject: [PATCH 05/11] declare compatibility + refactor --- includes/class-wc-gateway-mondido-hw.php | 34 +++++++++++------------- woocommerce-gateway-mondido.php | 5 ++++ 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/includes/class-wc-gateway-mondido-hw.php b/includes/class-wc-gateway-mondido-hw.php index b498b47..0ceda09 100644 --- a/includes/class-wc-gateway-mondido-hw.php +++ b/includes/class-wc-gateway-mondido-hw.php @@ -6,6 +6,7 @@ class WC_Gateway_Mondido_HW extends WC_Gateway_Mondido_Abstract { protected $preselected_method = null; + protected $orderStorage; /** * Init @@ -16,6 +17,7 @@ public function __construct() { $this->has_fields = true; $this->method_title = __( 'Mondido', 'woocommerce-gateway-mondido' ); $this->method_description = ''; + $this->orderStorage = OrderStorageTechnology::current(); $this->icon = apply_filters( 'woocommerce_mondido_hw_icon', plugins_url( '/assets/images/mondido.png', dirname( __FILE__ ) ) ); $this->supports = array( @@ -232,7 +234,6 @@ public function payment_fields() { */ public function process_payment( $order_id ) { $order = wc_get_order( $order_id ); - $orderStorage = OrderStorageTechnology::current(); if ( $this->store_cards === 'yes' ) { $token_key = "wc-{$this->id}-payment-token"; @@ -240,9 +241,9 @@ public function process_payment( $order_id ) { $token_id = isset( $_POST[$token_key] ) ? wc_clean( $_POST['token_key'] ) : 'new'; - $orderStorage->delete_meta_data($order, '_mondido_use_store_card'); + $this->orderStorage->delete_meta_data($order, '_mondido_use_store_card'); $order->delete_meta_data($order, '_mondido_store_card'); - $orderStorage->save( $order ); + $this->orderStorage->save( $order ); // Try to load saved token if ( $token_id !== 'new' ) { @@ -259,16 +260,16 @@ public function process_payment( $order_id ) { return false; } - $orderStorage->update_meta_data($order, '_mondido_use_store_card', $token->get_id()); - $orderStorage->save( $order ); + $this->orderStorage->update_meta_data($order, '_mondido_use_store_card', $token->get_id()); + $this->orderStorage->save( $order ); } elseif ( isset( $_POST[$new_card_key] ) && $_POST[$new_card_key] === 'true' ) { - $orderStorage->update_meta_data($order, '_mondido_store_card', 1); - $orderStorage->save( $order ); + $this->orderStorage->update_meta_data($order, '_mondido_store_card', 1); + $this->orderStorage->save( $order ); } } $transaction_id = $order->get_transaction_id(); - $store_card = (bool) $orderStorage->get_meta($order, '_mondido_store_card', true); + $store_card = (bool) $this->orderStorage->get_meta($order, '_mondido_store_card', true); if ($transaction_id) { $transaction = $this->transaction->get($transaction_id); @@ -302,8 +303,8 @@ public function process_payment( $order_id ) { ); if (!is_wp_error($transaction)) { - $orderStorage->update_meta_data($order, '_transaction_id', $transaction->id); - $orderStorage->save( $order ); + $this->orderStorage->update_meta_data($order, '_transaction_id', $transaction->id); + $this->orderStorage->save( $order ); } } @@ -527,15 +528,12 @@ public function notification_callback() { 'total' => $transaction_data['amount'], 'created_via' => 'mondido', ) ); - $orderStorage = OrderStorageTechnology::current(); - $orderStorage->add_meta_data($order, '_payment_method', $this->id); - $orderStorage->update_meta_data($order, '_transaction_id', $transaction_data['id']); - $orderStorage->update_meta_data($order, '_mondido_transaction_status', $transaction_data['status']); - $orderStorage->update_meta_data($order, '_mondido_transaction_data', $transaction_data); - $orderStorage->update_meta_data($order, '_mondido_subscription_id', $transaction_data['subscription']['id']); - - wc_get_logger()->info('test 5'); + $this->orderStorage->add_meta_data($order, '_payment_method', $this->id); + $this->orderStorage->update_meta_data($order, '_transaction_id', $transaction_data['id']); + $this->orderStorage->update_meta_data($order, '_mondido_transaction_status', $transaction_data['status']); + $this->orderStorage->update_meta_data($order, '_mondido_transaction_data', $transaction_data); + $this->orderStorage->update_meta_data($order, '_mondido_subscription_id', $transaction_data['subscription']['id']); // Add address $order->set_address( $transaction_data['metadata']['customer'], 'billing' ); diff --git a/woocommerce-gateway-mondido.php b/woocommerce-gateway-mondido.php index 9e2e883..6a71b35 100644 --- a/woocommerce-gateway-mondido.php +++ b/woocommerce-gateway-mondido.php @@ -31,6 +31,11 @@ public function __construct() { register_deactivation_hook( __FILE__, array( $this, 'flush_rewrite_rules' ) ); // Actions + add_action( 'before_woocommerce_init', function() { + if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) { + \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true ); + } + } ); add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_action_links' From da3bf912a533b2f312ec342a04179f27d468966f Mon Sep 17 00:00:00 2001 From: Max Frederiksen Date: Wed, 10 Apr 2024 17:44:57 +0200 Subject: [PATCH 06/11] Comments, fixes and refactorings --- .../class-wc-gateway-mondido-abstract.php | 13 ++++++------- includes/class-wc-gateway-mondido-hw.php | 6 +++--- includes/class-wc-mondido-admin-actions.php | 17 ++++++++--------- .../class-wc-mondido-hpos-compatibility.php | 14 +++++++------- 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/includes/abstracts/class-wc-gateway-mondido-abstract.php b/includes/abstracts/class-wc-gateway-mondido-abstract.php index 90a3746..aaafd44 100644 --- a/includes/abstracts/class-wc-gateway-mondido-abstract.php +++ b/includes/abstracts/class-wc-gateway-mondido-abstract.php @@ -15,7 +15,7 @@ abstract class WC_Gateway_Mondido_Abstract extends WC_Payment_Gateway { public function add_dependencies(WC_Mondido_Api $api, WC_Mondido_Transaction $transaction) { $this->api = $api; $this->transaction = $transaction; - $this->orderStorage = OrderStorageTechnology::current(); + $this->orderStorage = OrderStorage::current(); } /** @@ -335,7 +335,7 @@ public function handle_transaction( $order, $transaction_data ) { // Check transaction was processed $current_transaction_id = $order->get_transaction_id(); - $current_status = $this->orderStorage->get_meta( $order, '_mondido_transaction_status', true ); + $current_status = $this->orderStorage->get_meta_data( $order, '_mondido_transaction_status', true ); if ( $current_transaction_id === $transaction_id && $current_status === $status ) { throw new \Exception( "Transaction already applied. Order ID: {$order_id}. Transaction ID: {$transaction_id}. Transaction status: {$status}" ); @@ -394,13 +394,12 @@ public function handle_transaction( $order, $transaction_data ) { 'postcode' => $details['zip'], 'country' => $this->get_country_alpha2( $details['country_code'] ), ); + $this->orderStorage->update_meta_data( $order, '_mondido_invoice_address', $address ); + $this->orderStorage->save( $order ); } - $this->orderStorage->update_meta_data( $order, '_mondido_invoice_address', $address ); - $this->orderStorage->save( $order ); - // Define address for Mondido Checkout - if ( (bool) $this->orderStorage->get_meta( $order, '_mondido_checkout', TRUE ) ) { + if ( (bool) $this->orderStorage->get_meta_data( $order, '_mondido_checkout', TRUE ) ) { $order->set_address( $address, 'billing' ); if ( $order->needs_shipping_address() ) { @@ -564,7 +563,7 @@ public function getMondidoCustomerId( $customer_reference ) { public function get_payment_method_name($value, $order, $default_value) { - $transaction = $this->orderStorage->get_meta( $order, '_mondido_transaction_data', TRUE ); + $transaction = $this->orderStorage->get_meta_data( $order, '_mondido_transaction_data', TRUE ); if (!$transaction) { if ($order->get_transaction_id()) { diff --git a/includes/class-wc-gateway-mondido-hw.php b/includes/class-wc-gateway-mondido-hw.php index 0ceda09..1910cc7 100644 --- a/includes/class-wc-gateway-mondido-hw.php +++ b/includes/class-wc-gateway-mondido-hw.php @@ -17,7 +17,7 @@ public function __construct() { $this->has_fields = true; $this->method_title = __( 'Mondido', 'woocommerce-gateway-mondido' ); $this->method_description = ''; - $this->orderStorage = OrderStorageTechnology::current(); + $this->orderStorage = OrderStorage::current(); $this->icon = apply_filters( 'woocommerce_mondido_hw_icon', plugins_url( '/assets/images/mondido.png', dirname( __FILE__ ) ) ); $this->supports = array( @@ -242,7 +242,7 @@ public function process_payment( $order_id ) { $token_id = isset( $_POST[$token_key] ) ? wc_clean( $_POST['token_key'] ) : 'new'; $this->orderStorage->delete_meta_data($order, '_mondido_use_store_card'); - $order->delete_meta_data($order, '_mondido_store_card'); + $this->orderStorage->delete_meta_data($order, '_mondido_store_card'); $this->orderStorage->save( $order ); // Try to load saved token @@ -269,7 +269,7 @@ public function process_payment( $order_id ) { } $transaction_id = $order->get_transaction_id(); - $store_card = (bool) $this->orderStorage->get_meta($order, '_mondido_store_card', true); + $store_card = (bool) $this->orderStorage->get_meta_data($order, '_mondido_store_card', true); if ($transaction_id) { $transaction = $this->transaction->get($transaction_id); diff --git a/includes/class-wc-mondido-admin-actions.php b/includes/class-wc-mondido-admin-actions.php index c03eedb..605b117 100644 --- a/includes/class-wc-mondido-admin-actions.php +++ b/includes/class-wc-mondido-admin-actions.php @@ -20,6 +20,8 @@ public function __construct() { $this, 'ajax_mondido_capture' ) ); + + $this->orderStorage = OrderStorage::current(); } /** @@ -30,8 +32,7 @@ public static function add_meta_boxes() { global $post_id; $order = wc_get_order( $post_id ); if ( $order && strpos( $order->get_payment_method(), 'mondido' ) !== false ) { - $orderStorage = OrderStorageTechnology::current(); - $transaction = $orderStorage->get_meta( $order, '_mondido_transaction_data', TRUE ); + $transaction = $this->orderStorage->get_meta_data( $order, '_mondido_transaction_data', TRUE ); if ( ! empty( $transaction ) ) { $screen = class_exists( '\Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController' ) && wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled() ? wc_get_page_screen_id( 'shop-order' ) @@ -57,8 +58,7 @@ public static function order_meta_box_payment_actions( $post_or_order_object ) { global $post_id; $order = ( $post_or_order_object instanceof WP_Post ) ? wc_get_order( $post_or_order_object->ID ) : $post_or_order_object; - $orderStorage = OrderStorageTechnology::current(); - $transaction = $orderStorage->get_meta( $order, '_mondido_transaction_data', TRUE ); + $transaction = $this->orderStorage->get_meta_data( $order, '_mondido_transaction_data', TRUE ); wc_get_template( 'admin/payment-actions.php', @@ -120,12 +120,11 @@ public function ajax_mondido_capture() { } if ( $transaction['status'] === 'approved' ) { - $orderStorage = OrderStorageTechnology::current(); - $orderStorage->update_meta_data( $order, '_transaction_id', $transaction['id'] ); - $orderStorage->update_meta_data( $order, '_mondido_transaction_status', $transaction['status'] ); - $orderStorage->update_meta_data( $order, '_mondido_transaction_data', $transaction ); - $orderStorage->save( $order ); + $this->orderStorage->update_meta_data( $order, '_transaction_id', $transaction['id'] ); + $this->orderStorage->update_meta_data( $order, '_mondido_transaction_status', $transaction['status'] ); + $this->orderStorage->update_meta_data( $order, '_mondido_transaction_data', $transaction ); + $this->orderStorage->save( $order ); $order->add_order_note( sprintf( __( 'Payment captured. Transaction Id: %s', 'woocommerce-gateway-mondido' ), $transaction['id'] ) ); $order->payment_complete( $transaction['id'] ); diff --git a/includes/class-wc-mondido-hpos-compatibility.php b/includes/class-wc-mondido-hpos-compatibility.php index 874f589..37e2214 100644 --- a/includes/class-wc-mondido-hpos-compatibility.php +++ b/includes/class-wc-mondido-hpos-compatibility.php @@ -5,16 +5,16 @@ use Automattic\WooCommerce\Utilities\OrderUtil; -interface OrderStorageTechnologyFactory { - public function get_meta($order, $meta_key, $meta_value); +interface OrderStorageInterface { + public function get_meta_data($order, $meta_key, $meta_value); public function add_meta_data($order, $meta_key, $meta_value); public function update_meta_data($order, $meta_key, $meta_value); public function delete_meta_data($order, $meta_key); public function save($order); } -class HPOSOrderStorage implements OrderStorageTechnologyFactory { - public function get_meta($order, $meta_key, $meta_value) { +class HPOSOrderStorage implements OrderStorageInterface { + public function get_meta_data($order, $meta_key, $meta_value) { return $order->get_meta($meta_key, $meta_value); } @@ -35,8 +35,8 @@ public function save($order) { } } -class CPTOrderStorage implements OrderStorageTechnologyFactory { - public function get_meta($order, $meta_key, $meta_value) { +class CPTOrderStorage implements OrderStorageInterface { + public function get_meta_data($order, $meta_key, $meta_value) { return get_post_meta($order->get_id(), $meta_key, $meta_value); } @@ -57,7 +57,7 @@ public function save($order) { } } -class OrderStorageTechnology { +class OrderStorage { public static function current() { if (OrderUtil::custom_orders_table_usage_is_enabled()) { return new HPOSOrderStorage(); From 0b9d8dd933fc0c846da7cc88a862034d9cb1dadd Mon Sep 17 00:00:00 2001 From: Max Frederiksen Date: Fri, 12 Apr 2024 09:57:43 +0200 Subject: [PATCH 07/11] refactor hpos class structure --- .../class-wc-gateway-mondido-abstract.php | 1 + .../class-wc-mondido-hpos-compatibility.php | 34 +++++++++---------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/includes/abstracts/class-wc-gateway-mondido-abstract.php b/includes/abstracts/class-wc-gateway-mondido-abstract.php index aaafd44..c8eae13 100644 --- a/includes/abstracts/class-wc-gateway-mondido-abstract.php +++ b/includes/abstracts/class-wc-gateway-mondido-abstract.php @@ -394,6 +394,7 @@ public function handle_transaction( $order, $transaction_data ) { 'postcode' => $details['zip'], 'country' => $this->get_country_alpha2( $details['country_code'] ), ); + $this->orderStorage->update_meta_data( $order, '_mondido_invoice_address', $address ); $this->orderStorage->save( $order ); } diff --git a/includes/class-wc-mondido-hpos-compatibility.php b/includes/class-wc-mondido-hpos-compatibility.php index 37e2214..f40eb8d 100644 --- a/includes/class-wc-mondido-hpos-compatibility.php +++ b/includes/class-wc-mondido-hpos-compatibility.php @@ -5,15 +5,23 @@ use Automattic\WooCommerce\Utilities\OrderUtil; -interface OrderStorageInterface { - public function get_meta_data($order, $meta_key, $meta_value); - public function add_meta_data($order, $meta_key, $meta_value); - public function update_meta_data($order, $meta_key, $meta_value); - public function delete_meta_data($order, $meta_key); - public function save($order); +abstract class OrderStorage { + public static function current(): OrderStorage { + if (OrderUtil::custom_orders_table_usage_is_enabled()) { + return new HPOSOrderStorage(); + } else { + return new CPTOrderStorage(); + } + } + + public abstract function get_meta_data($order, $meta_key, $meta_value); + public abstract function add_meta_data($order, $meta_key, $meta_value); + public abstract function update_meta_data($order, $meta_key, $meta_value); + public abstract function delete_meta_data($order, $meta_key); + public abstract function save($order); } -class HPOSOrderStorage implements OrderStorageInterface { +class HPOSOrderStorage implements OrderStorage { public function get_meta_data($order, $meta_key, $meta_value) { return $order->get_meta($meta_key, $meta_value); } @@ -35,7 +43,7 @@ public function save($order) { } } -class CPTOrderStorage implements OrderStorageInterface { +class CPTOrderStorage implements OrderStorage { public function get_meta_data($order, $meta_key, $meta_value) { return get_post_meta($order->get_id(), $meta_key, $meta_value); } @@ -55,14 +63,4 @@ public function delete_meta_data($order, $meta_key) { public function save($order) { return; // Do nothing, CPT doesn't use save } -} - -class OrderStorage { - public static function current() { - if (OrderUtil::custom_orders_table_usage_is_enabled()) { - return new HPOSOrderStorage(); - } else { - return new CPTOrderStorage(); - } - } } \ No newline at end of file From 9f6480eba399e2d8f66e9c6ff1de53be6dde3a39 Mon Sep 17 00:00:00 2001 From: Max Frederiksen Date: Fri, 12 Apr 2024 09:59:42 +0200 Subject: [PATCH 08/11] github sync bug + indentation --- includes/abstracts/class-wc-gateway-mondido-abstract.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/includes/abstracts/class-wc-gateway-mondido-abstract.php b/includes/abstracts/class-wc-gateway-mondido-abstract.php index c8eae13..322293e 100644 --- a/includes/abstracts/class-wc-gateway-mondido-abstract.php +++ b/includes/abstracts/class-wc-gateway-mondido-abstract.php @@ -394,9 +394,8 @@ public function handle_transaction( $order, $transaction_data ) { 'postcode' => $details['zip'], 'country' => $this->get_country_alpha2( $details['country_code'] ), ); - - $this->orderStorage->update_meta_data( $order, '_mondido_invoice_address', $address ); - $this->orderStorage->save( $order ); + $this->orderStorage->update_meta_data( $order, '_mondido_invoice_address', $address ); + $this->orderStorage->save( $order ); } // Define address for Mondido Checkout From d350fd4cb5cb763fef982ad6da496e2bce730554 Mon Sep 17 00:00:00 2001 From: Max Frederiksen Date: Fri, 12 Apr 2024 10:01:34 +0200 Subject: [PATCH 09/11] fix whitespace --- includes/abstracts/class-wc-gateway-mondido-abstract.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/abstracts/class-wc-gateway-mondido-abstract.php b/includes/abstracts/class-wc-gateway-mondido-abstract.php index 322293e..e363b1c 100644 --- a/includes/abstracts/class-wc-gateway-mondido-abstract.php +++ b/includes/abstracts/class-wc-gateway-mondido-abstract.php @@ -394,7 +394,7 @@ public function handle_transaction( $order, $transaction_data ) { 'postcode' => $details['zip'], 'country' => $this->get_country_alpha2( $details['country_code'] ), ); - $this->orderStorage->update_meta_data( $order, '_mondido_invoice_address', $address ); + $this->orderStorage->update_meta_data( $order, '_mondido_invoice_address', $address ); $this->orderStorage->save( $order ); } From 5a90eb9a546ec3bf5b4b7e66eda0eedc2128ef3a Mon Sep 17 00:00:00 2001 From: Max Frederiksen Date: Fri, 12 Apr 2024 10:02:17 +0200 Subject: [PATCH 10/11] indent bracket --- includes/abstracts/class-wc-gateway-mondido-abstract.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/abstracts/class-wc-gateway-mondido-abstract.php b/includes/abstracts/class-wc-gateway-mondido-abstract.php index e363b1c..e77e166 100644 --- a/includes/abstracts/class-wc-gateway-mondido-abstract.php +++ b/includes/abstracts/class-wc-gateway-mondido-abstract.php @@ -396,7 +396,7 @@ public function handle_transaction( $order, $transaction_data ) { ); $this->orderStorage->update_meta_data( $order, '_mondido_invoice_address', $address ); $this->orderStorage->save( $order ); - } + } // Define address for Mondido Checkout if ( (bool) $this->orderStorage->get_meta_data( $order, '_mondido_checkout', TRUE ) ) { From 2c25fe7b5747c0cd51ea69f0f53b2ac7b5a435c8 Mon Sep 17 00:00:00 2001 From: Max Frederiksen Date: Mon, 29 Apr 2024 18:02:40 +0200 Subject: [PATCH 11/11] add version check for using new hpos file --- includes/class-wc-mondido-hpos-compatibility.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/includes/class-wc-mondido-hpos-compatibility.php b/includes/class-wc-mondido-hpos-compatibility.php index f40eb8d..4a805d4 100644 --- a/includes/class-wc-mondido-hpos-compatibility.php +++ b/includes/class-wc-mondido-hpos-compatibility.php @@ -3,6 +3,11 @@ exit; } // Exit if accessed directly + +if ( ! function_exists( 'WC' ) || version_compare( WC()->version, '8.2', '<' ) ) { + exit; +} // Exit if WooCommerce is not activated or version is less than 8.2 + use Automattic\WooCommerce\Utilities\OrderUtil; abstract class OrderStorage {