Skip to content

Commit 2c0f1d6

Browse files
Add HPOS compatibility and fixes
1 parent 10a6932 commit 2c0f1d6

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

includes/core/class-rfw-payment-gateway.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
return;
66
}
77

8+
use Automattic\WooCommerce\Utilities\OrderUtil;
9+
810
/**
911
* RFW_Payment_Gateway class
1012
*/
@@ -312,7 +314,7 @@ public function display_capture_button( $order ) {
312314
* @return void
313315
*/
314316
public function process_capture( $post_id ) {
315-
if ( get_post_type( $post_id ) !== 'shop_order' ) {
317+
if ( 'shop_order' !== OrderUtil::get_order_type( $post_id ) ) {
316318
return;
317319
}
318320

@@ -348,7 +350,7 @@ public function process_capture( $post_id ) {
348350
}
349351
}
350352

351-
$this->send_capture_request( $charge_id, $order );
353+
$this->send_capture_request( $charge_id, $order, true );
352354
}
353355

354356

@@ -357,15 +359,18 @@ public function process_capture( $post_id ) {
357359
*
358360
* @param string $charge_id ID of the order in Resolve system, used to capture funds.
359361
* @param WC_Order $order Object of the order in WooCommerce.
362+
* @param bool $manual Is the capture automatically initiated or manually by webshop administrator.
360363
*
361364
* @return void
362365
*/
363-
private function send_capture_request( $charge_id, $order ) {
366+
private function send_capture_request( $charge_id, $order, $manual = false ) {
364367
$url_format = 'https://%s:%s@%s.resolvepay.com/api/charges/%s/capture';
365368

366369
$merchant_id = RFW_Data::get_settings( 'webshop-merchant-id', true );
367370
$api_key = RFW_Data::get_settings( 'webshop-api-key', true );
368371

372+
$captured_order_status = RFW_Data::get_captured_status();
373+
369374
$mode = RFW_Data::test_mode() ? 'app-sandbox' : 'app';
370375
$url = sprintf( $url_format, $merchant_id, $api_key, $mode, $charge_id );
371376
$args = [
@@ -394,7 +399,9 @@ private function send_capture_request( $charge_id, $order ) {
394399
} else {
395400
do_action( 'rfw_order_payment_captured', $order );
396401
// translators: charge ID.
397-
$order->set_status( apply_filters( 'rfw_payment_captured_order_status', 'processing' ), sprintf( __( 'The payment was successfully captured! Resolve ID: %s.', 'resolve' ), '<b>' . $body['number'] . '</b>' ) );
402+
$order->set_status( $captured_order_status, sprintf( __( 'The payment was successfully captured! Resolve ID: %s.', 'resolve' ), '<b>' . $body['number'] . '</b>' ), $manual );
403+
// Prevent post status update overriding order status with value submited in form.
404+
$_POST['order_status'] = 'wc-' . $captured_order_status;
398405

399406
$order->payment_complete( $body['number'] );
400407
$order->add_meta_data( 'rfw_payment_captured', 'yes', true );

includes/utilities/class-rfw-data.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,25 @@ public static function get_customer_data( $order ) {
243243
public static function display_modal_link( $label = null ) {
244244
return '<a href="#" id="rfw-apply">' . ( $label ?: __( 'Learn more', 'resolve' ) ) . '</a>';
245245
}
246+
247+
/**
248+
* Allows changes to default capture status but double check if they are valid.
249+
*
250+
* @param bool $prefix To return status with WC prefix or without.
251+
*
252+
* @return string Captured order status.
253+
*/
254+
public static function get_captured_status( $prefix = false ) {
255+
$captured_order_status = apply_filters( 'rfw_payment_captured_order_status', 'processing' );
256+
if ( 'processing' !== $captured_order_status ) {
257+
$order_statuses = wc_get_order_statuses();
258+
259+
if ( ! isset( $order_statuses[ 'wc-' . $captured_order_status ] ) ) {
260+
RFW_Logger::log( 'Custom order status: "' . $captured_order_status . '" is not a valid WC Order status, default capture status will be used (Processing).', 'warning' );
261+
$captured_order_status = 'processing';
262+
}
263+
}
264+
265+
return $prefix ? 'wc-' . $captured_order_status : $captured_order_status;
266+
}
246267
}

resolve-for-woocommerce.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ function rfw_admin_notice_missing_woocommerce() {
5151
return;
5252
}
5353

54+
/**
55+
* Declare Resolve plugin compatible with HPOS.
56+
*
57+
* @return void
58+
*/
59+
function rfw_hpos_compatible() {
60+
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
61+
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
62+
}
63+
}
64+
add_action( 'before_woocommerce_init', 'rfw_hpos_compatible' );
65+
66+
/**
67+
* RFW_Main class
68+
*/
5469
if ( ! class_exists( 'RFW_Main' ) ) {
5570
class RFW_Main {
5671

0 commit comments

Comments
 (0)