Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to support HPOS #71

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
41 changes: 33 additions & 8 deletions includes/abstracts/class-wc-gateway-mondido-abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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
Expand Down
62 changes: 52 additions & 10 deletions includes/class-wc-gateway-mondido-hw.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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' ) {
Expand All @@ -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 );
}
}
}

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

}
Expand Down Expand Up @@ -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' );
Expand Down
30 changes: 23 additions & 7 deletions includes/class-wc-mondido-admin-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
eric-thelin marked this conversation as resolved.
Show resolved Hide resolved
? 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'
);
Expand All @@ -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(
Expand Down Expand Up @@ -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'] );
Expand Down
15 changes: 13 additions & 2 deletions includes/class-wc-mondido-subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
exit;
} // Exit if accessed directly

use Automattic\WooCommerce\Utilities\OrderUtil;

class WC_Mondido_Subscriptions {

private $api;
Expand Down Expand Up @@ -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'] );
}
}
}

Expand Down
19 changes: 17 additions & 2 deletions includes/deprecated/class-wc-order-compatibility-mondido.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down Expand Up @@ -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 ) ) {
Expand Down