Skip to content

Commit

Permalink
Merge pull request #153 from SwedbankPay/feature/hpos
Browse files Browse the repository at this point in the history
Add High-Performance Order Storage
  • Loading branch information
aait authored Mar 25, 2023
2 parents 15ba514 + aa996d0 commit 7704713
Show file tree
Hide file tree
Showing 15 changed files with 156 additions and 108 deletions.
5 changes: 5 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Version 7.1.0
* High-Performance Order Storage support
* Fixed critical error when a product have been deleted in the admin backend
* Session fixes

Version 7.0.1
* Fixed PaymentOrder.Payer.ConsumerProfileRef: Both ConsumerProfileRef and PayerReference cannot be set at the same time.

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"php": ">=7.0",
"adci/full-name-parser": "^0.2.4",
"ramsey/uuid": "^3.7",
"swedbank-pay/swedbank-pay-woocommerce-core": "~6.0.1"
"swedbank-pay/swedbank-pay-woocommerce-core": "~6.1.0"
},
"require-dev": {
"yoast/phpunit-polyfills": "^1.0.2"
Expand Down
22 changes: 1 addition & 21 deletions includes/class-wc-background-swedbank-pay-queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ protected function task( $item ) {
// Get Order by Payment Id
$transaction_id = $data['transaction']['number'];
$payment_id = $data['payment']['id'];
$order_id = $this->get_post_id_by_meta( '_payex_payment_id', $payment_id );
$order_id = sb_get_post_id_by_meta( '_payex_payment_id', $payment_id );
if ( ! $order_id ) {
throw new \Exception( sprintf( 'Error: Failed to get order Id by Payment Id %s', $payment_id ) );
}
Expand Down Expand Up @@ -235,24 +235,4 @@ public function dispatch_queue() {
$this->save()->dispatch();
}
}

/**
* Get Post Id by Meta
*
* @param $key
* @param $value
*
* @return null|string
*/
private function get_post_id_by_meta( $key, $value ) {
global $wpdb;

return $wpdb->get_var(
$wpdb->prepare(
"SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_key = %s AND meta_value = %s;",
$key,
$value
)
);
}
}
27 changes: 4 additions & 23 deletions includes/class-wc-gateway-swedbank-pay-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ public function process_payment( $order_id ) {
}

// Replace token
delete_post_meta( $order->get_id(), '_payment_tokens' );
$order->delete_meta_data( '_payment_tokens' );
$order->add_payment_token( $token );

if ( self::wcs_is_payment_change() ) {
Expand Down Expand Up @@ -1322,7 +1322,7 @@ public function return_handler() {
$paymentorder_id = $data['paymentOrder']['id'];

// Get Order by Order Payment Id
$order_id = $this->get_post_id_by_meta( '_payex_paymentorder_id', $paymentorder_id );
$order_id = sb_get_post_id_by_meta( '_payex_paymentorder_id', $paymentorder_id );

// Get Order ID from payeeInfo if is not exist
if ( empty( $order_id ) ) {
Expand Down Expand Up @@ -1542,7 +1542,8 @@ public function delete_token( $token ) {
* @throws Exception
*/
public function update_address( $order_id ) {
$paymentorder_id = get_post_meta( $order_id, '_payex_paymentorder_id', true );
$order = wc_get_order( $order_id );
$paymentorder_id = $order->get_meta( '_payex_paymentorder_id' );
if ( ! empty( $paymentorder_id ) ) {
$result = $this->core->request( 'GET', $paymentorder_id . '/payers' );

Expand Down Expand Up @@ -1763,26 +1764,6 @@ public function update_consumer_address( $user_id, $type, $address ) {
}
}

/**
* Get Post Id by Meta
*
* @param $key
* @param $value
*
* @return null|string
*/
private function get_post_id_by_meta( $key, $value ) {
global $wpdb;

return $wpdb->get_var(
$wpdb->prepare(
"SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_key = %s AND meta_value = %s;",
$key,
$value
)
);
}

/**
* Checks an order to see if it contains a subscription.
* @see wcs_order_contains_subscription()
Expand Down
33 changes: 16 additions & 17 deletions includes/class-wc-swedbank-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct() {
);

// Add meta boxes
add_action( 'add_meta_boxes', __CLASS__ . '::add_meta_boxes' );
add_action( 'add_meta_boxes', __CLASS__ . '::add_meta_boxes', 10, 2 );

// Add action buttons
add_action( 'woocommerce_order_item_add_action_buttons', __CLASS__ . '::add_action_buttons', 10, 1 );
Expand Down Expand Up @@ -60,25 +60,25 @@ public function add_valid_order_statuses( $statuses, $order ) {

/**
* Add meta boxes in admin
* @param $screen_id
* @param WC_Order $order
* @return void
*/
public static function add_meta_boxes() {
global $post_id;

$order = wc_get_order( $post_id );

public static function add_meta_boxes( $screen_id, $order ) {
if ( $order ) {
$payment_method = $order->get_payment_method();
if ( in_array( $payment_method, WC_Swedbank_Plugin::PAYMENT_METHODS, true ) ) {
$payment_id = get_post_meta( $post_id, '_payex_payment_id', true );
$payment_id = $order->get_meta( '_payex_payment_id' );
if ( ! empty( $payment_id ) ) {
$screen = sb_is_hpos_enabled() ? wc_get_page_screen_id( 'shop-order' ) : 'shop_order';

add_meta_box(
'swedbank_payment_actions',
__( 'Swedbank Pay Payments Actions', 'swedbank-pay-woocommerce-checkout' ),
__CLASS__ . '::order_meta_box_payment_actions',
'shop_order',
$screen,
'side',
'default'
'high'
);
}
}
Expand All @@ -87,13 +87,11 @@ public static function add_meta_boxes() {

/**
* MetaBox for Payment Actions
* @param WC_Order $order
* @return void
*/
public static function order_meta_box_payment_actions() {
global $post_id;

$order = wc_get_order( $post_id );
$payment_id = get_post_meta( $post_id, '_payex_payment_id', true );
public static function order_meta_box_payment_actions( $order ) {
$payment_id = $order->get_meta( '_payex_payment_id' );
if ( empty( $payment_id ) ) {
return;
}
Expand All @@ -118,7 +116,7 @@ public static function order_meta_box_payment_actions() {
array(
'gateway' => $gateway,
'order' => $order,
'order_id' => $post_id,
'order_id' => $order->get_id(),
'payment_id' => $payment_id,
'info' => $result,
),
Expand All @@ -133,7 +131,7 @@ public static function order_meta_box_payment_actions() {
* @param WC_Order $order
*/
public static function add_action_buttons( $order ) {
if (function_exists('wcs_is_subscription') && wcs_is_subscription($order)) {
if ( function_exists( 'wcs_is_subscription' ) && wcs_is_subscription( $order ) ) {
// Buttons are available for orders only
return;
}
Expand Down Expand Up @@ -168,7 +166,8 @@ public static function add_action_buttons( $order ) {
* @return void
*/
public static function admin_enqueue_scripts( $hook ) {
if ( 'post.php' === $hook ) {
$hook_to_check = sb_is_hpos_enabled() ? wc_get_page_screen_id( 'shop-order' ) : 'post.php';
if ( $hook_to_check === $hook ) {
// Scripts
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
wp_register_script(
Expand Down
7 changes: 1 addition & 6 deletions includes/class-wc-swedbank-pay-instant-capture.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,7 @@ private function get_instant_capture_items( $order ) {
}

// Get Product Class
$product_class = get_post_meta(
$order_item->get_product()->get_id(),
'_sb_product_class',
true
);

$product_class = $product->get_meta( '_sb_product_class' );
if ( empty( $product_class ) ) {
$product_class = 'ProductGroup1';
}
Expand Down
14 changes: 11 additions & 3 deletions includes/class-wc-swedbank-pay-payment-url.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public function __construct() {

$this->payment_menu_style = isset( $this->settings['paymentMenuStyle'] ) ? $this->settings['paymentMenuStyle'] : $this->payment_menu_style;

add_action( 'init', array( $this, 'override_checkout_shortcode' ), 20 );
if ( isset( $_GET['payment_url'] ) ) { // WPCS: input var ok, CSRF ok.
add_action( 'init', array( $this, 'override_checkout_shortcode' ), 100 );
}
}

/**
Expand All @@ -63,16 +65,22 @@ public function override_checkout_shortcode()
*/
public function shortcode_woocommerce_checkout( $atts )
{
// Check WC sessions
if ( ! WC()->session ) {
WC()->initialize_session();
}

$payment_url = WC()->session->get( 'sb_payment_url' );

if ( empty( $payment_url ) ) {
$order_id = absint( WC()->session->get( 'order_awaiting_payment' ) );
if ( $order_id > 0 ) {
$payment_url = get_post_meta( $order_id, '_sb_view_paymentorder', true );
$order = wc_get_order( $order_id );
$payment_url = $order->get_meta( '_sb_view_paymentorder' );
}
}

if ( isset( $_GET['payment_url'] ) && ! empty( $payment_url ) ) { // WPCS: input var ok, CSRF ok.
if ( ! empty( $payment_url ) ) {
wp_dequeue_script( 'featherlight' );
wp_dequeue_script( 'wc-sb-seamless-checkout' );
wp_dequeue_script( 'wc-sb-checkout' );
Expand Down
60 changes: 31 additions & 29 deletions includes/class-wc-swedbank-pay-refund.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,53 +202,55 @@ public static function refund( $gateway, $order, $amount, $reason ) {
case 'line_item':
/** @var WC_Order_Item_Product $item */

$image = null;
$product_class = 'ProductGroup1';

/**
* @var WC_Product $product
*/
$product = $item->get_product();
if ( $product ) {
// Get Product Sku
$reference = trim(
str_replace(
array( ' ', '.', ',' ),
'-',
$product->get_sku()
)
);

// Get Product Sku
$reference = trim(
str_replace(
array( ' ', '.', ',' ),
'-',
$product->get_sku()
)
);
// Get Product image
$image = wp_get_attachment_image_src( $product->get_image_id(), 'full' );
if ( $image ) {
$image = array_shift( $image );
}

// Get Product Class
$product_class = $product->get_meta( '_sb_product_class' );

if ( empty( $product_class ) ) {
$product_class = apply_filters(
'sb_product_class',
'ProductGroup1',
$product
);
}
}

if ( empty( $reference ) ) {
$reference = wp_generate_password( 12, false );
}

// Get Product image
$image = wp_get_attachment_image_src( $product->get_image_id(), 'full' );
if ( $image ) {
$image = array_shift( $image );
} else {
if ( empty( $image ) ) {
$image = wc_placeholder_img_src( 'full' );
}

if ( null === parse_url( $image, PHP_URL_SCHEME ) &&
mb_substr( $image, 0, mb_strlen(WP_CONTENT_URL), 'UTF-8' ) === WP_CONTENT_URL
mb_substr( $image, 0, mb_strlen( WP_CONTENT_URL ), 'UTF-8' ) === WP_CONTENT_URL
) {
$image = wp_guess_url() . $image;
}

// Get Product Class
$product_class = get_post_meta(
$product->get_id(),
'_sb_product_class',
true
);

if ( empty( $product_class ) ) {
$product_class = apply_filters(
'sb_product_class',
'ProductGroup1',
$product
);
}

// The field Reference must match the regular expression '[\\w-]*'
$order_item[OrderItemInterface::FIELD_REFERENCE] = $reference;
$order_item[OrderItemInterface::FIELD_TYPE] = OrderItemInterface::TYPE_PRODUCT;
Expand Down
1 change: 1 addition & 0 deletions includes/class-wc-swedbank-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public function includes() {
}
}

require_once( dirname( __FILE__ ) . '/functions.php' );
require_once( dirname( __FILE__ ) . '/class-wc-swedbank-pay-transactions.php' );
require_once( dirname( __FILE__ ) . '/class-wc-swedbank-subscriptions.php' );
require_once( dirname( __FILE__ ) . '/class-wc-swedbank-pay-checkin.php' );
Expand Down
9 changes: 6 additions & 3 deletions includes/class-wc-swedbank-subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ public static function add_subscription_card_id( $order_id ) {
*/
public static function update_failing_payment_method( $subscription, $renewal_order ) {
// Delete tokens
//delete_post_meta( $subscription->get_id(), '_payment_tokens' );
//$subscription->delete_meta_data( '_payment_tokens' );
//$subscription->save();
}

/**
Expand All @@ -164,7 +165,8 @@ public static function update_failing_payment_method( $subscription, $renewal_or
*/
public static function delete_resubscribe_meta( $resubscribe_order ) {
// Delete tokens
delete_post_meta( $resubscribe_order->get_id(), '_payment_tokens' );
$resubscribe_order->delete_meta_data( '_payment_tokens' );
$resubscribe_order->save();
}

/**
Expand Down Expand Up @@ -270,7 +272,8 @@ public static function save_subscription_payment_meta( $subscription, $meta_tabl

if ( 'swedbankpay_meta' === $meta_table && 'token_id' === $meta_key ) {
// Delete tokens
delete_post_meta( $subscription->get_id(), '_payment_tokens' );
$subscription->delete_meta_data( '_payment_tokens' );
$subscription->save();

// Add tokens
$tokens = explode( ',', $meta_value );
Expand Down
Loading

0 comments on commit 7704713

Please sign in to comment.