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
32 changes: 20 additions & 12 deletions includes/abstracts/class-wc-gateway-mondido-abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
exit;
} // Exit if accessed directly

use Automattic\WooCommerce\Utilities\OrderUtil;

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();
}

/**
Expand Down Expand Up @@ -331,20 +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}" );
}

// Save Transaction
delete_post_meta( $order_id, '_transaction_id' );
update_post_meta( $order_id, '_transaction_id', $transaction_id );
$this->orderStorage->delete_meta_data( $order, '_transaction_id' );

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pair of lines with delete + update is repeating itself in multiple places. In order to reduce duplication we could introduce a replace_meta_data method on the order storage.

$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 );

delete_post_meta( $order_id, '_mondido_transaction_status' );
update_post_meta( $order_id, '_mondido_transaction_status', $status );
$this->orderStorage->delete_meta_data( $order, '_mondido_transaction_data' );
$this->orderStorage->update_meta_data( $order, '_mondido_transaction_data', $transaction_data );

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':
Expand Down Expand Up @@ -388,11 +394,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 );
}
}
eric-thelin marked this conversation as resolved.
Show resolved Hide resolved

$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() ) {
Expand Down Expand Up @@ -556,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()) {
Expand Down
42 changes: 25 additions & 17 deletions includes/class-wc-gateway-mondido-hw.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +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';

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');
maxfrederiksen-mondido marked this conversation as resolved.
Show resolved Hide resolved
$orderStorage->save( $order );

// Try to load saved token
if ( $token_id !== 'new' ) {
Expand All @@ -257,15 +259,16 @@ public function process_payment( $order_id ) {

return false;
}

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' ) {
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);
Expand Down Expand Up @@ -299,7 +302,8 @@ public function process_payment( $order_id ) {
);

if (!is_wp_error($transaction)) {
update_post_meta( $order_id, '_transaction_id', $transaction->id );
$orderStorage->update_meta_data($order, '_transaction_id', $transaction->id);
$orderStorage->save( $order );
}

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

Expand All @@ -468,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;
Expand Down Expand Up @@ -523,11 +527,15 @@ 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'] );
$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');
maxfrederiksen-mondido marked this conversation as resolved.
Show resolved Hide resolved

// Add address
$order->set_address( $transaction_data['metadata']['customer'], 'billing' );
Expand Down Expand Up @@ -630,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();
}
Expand Down
29 changes: 20 additions & 9 deletions includes/class-wc-mondido-admin-actions.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\Internal\DataStores\Orders\CustomOrdersTableController;

class WC_Mondido_Admin_Actions {
/**
* Constructor
Expand All @@ -28,13 +30,18 @@ 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()
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,10 +53,12 @@ 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 );
$transaction = get_post_meta( $order->get_id(), '_mondido_transaction_data', TRUE );
$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 );

wc_get_template(
'admin/payment-actions.php',
Expand Down Expand Up @@ -111,10 +120,12 @@ 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 );
$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'] );
Expand Down
68 changes: 68 additions & 0 deletions includes/class-wc-mondido-hpos-compatibility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly

use Automattic\WooCommerce\Utilities\OrderUtil;

interface OrderStorageTechnologyFactory {
eric-thelin marked this conversation as resolved.
Show resolved Hide resolved
public function get_meta($order, $meta_key, $meta_value);
eric-thelin marked this conversation as resolved.
Show resolved Hide resolved
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) {
return $order->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() {
eric-thelin marked this conversation as resolved.
Show resolved Hide resolved
if (OrderUtil::custom_orders_table_usage_is_enabled()) {
return new HPOSOrderStorage();
} else {
return new CPTOrderStorage();
}
}
}
3 changes: 3 additions & 0 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

// FILE SHOULD BE DELETED AFTER UPGRADE OF WOOCOMMERCE 8.7

/**
* 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,6 +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 ) ) {
Expand Down
1 change: 1 addition & 0 deletions woocommerce-gateway-mondido.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down