From 3da9a112b3a88e94d467adaa7f32cab016a2daa9 Mon Sep 17 00:00:00 2001 From: "dimaspolohov@yandex.ru" Date: Fri, 1 Dec 2023 14:39:35 +0300 Subject: [PATCH 01/15] Process payment refactor --- includes/Api.php | 6 +- includes/Gateways/ReepayGateway.php | 197 ++++++++++++++++------------ 2 files changed, 115 insertions(+), 88 deletions(-) diff --git a/includes/Api.php b/includes/Api.php index 96817230..1a9209e0 100644 --- a/includes/Api.php +++ b/includes/Api.php @@ -561,7 +561,7 @@ public function capture_payment( $order, $amount = null ) { * @return array|mixed|object|WP_Error * @see ReepayGateway::payment_methods. */ - public function recurring( array $payment_methods, WC_Order $order, array $data, $token = false, $payment_text = '' ) { + public function recurring( array $payment_methods, WC_Order $order, array $data, $token = false ) { $params = array( 'locale' => $data['language'], 'create_customer' => array( @@ -582,8 +582,8 @@ public function recurring( array $payment_methods, WC_Order $order, array $data, 'cancel_url' => $order->get_cancel_order_url(), ); - if ( ! empty( $payment_text ) ) { - $params['button_text'] = $payment_text; + if ( ! empty( $data['button_text'] ) ) { + $params['button_text'] = $data['button_text']; } if ( ! empty( $token ) ) { diff --git a/includes/Gateways/ReepayGateway.php b/includes/Gateways/ReepayGateway.php index 57b467d9..e38a3ec7 100644 --- a/includes/Gateways/ReepayGateway.php +++ b/includes/Gateways/ReepayGateway.php @@ -725,14 +725,14 @@ public function payment_fields() { } /** - * Process Payment + * Run some conditions for woo blocks using * - * @param int $order_id Order ID. + * @param object $order Order. * - * @return array|false + * @return false * @throws Exception If payment error. */ - public function process_payment( $order_id ) { + public function woo_blocks_conditions( $order ){ $is_woo_blocks_checkout_request = false; if ( isset( $_SERVER['CONTENT_TYPE'] ) && 'application/json' === $_SERVER['CONTENT_TYPE'] ) { @@ -747,14 +747,11 @@ public function process_payment( $order_id ) { } } } catch ( Exception $e ) { - return array( - 'messages' => __( 'Wrong Request. Try again', 'reepay-checkout-gateway' ), - 'result' => 'failure', - ); + throw new Exception( __('Wrong Request. Try again', 'reepay-checkout-gateway') ); } } - $order = wc_get_order( $order_id ); + if ( $is_woo_blocks_checkout_request ) { /** @@ -763,7 +760,10 @@ public function process_payment( $order_id ) { $order->calculate_totals(); } - $token_id = isset( $_POST[ 'wc-' . $this->id . '-payment-token' ] ) ? wc_clean( $_POST[ 'wc-' . $this->id . '-payment-token' ] ) : 'new'; + return false; + } + + public function get_params( $order, $token_id ){ if ( 'yes' === $this->save_cc && 'new' === $token_id @@ -775,28 +775,13 @@ public function process_payment( $order_id ) { $maybe_save_card = wcs_cart_have_subscription(); } + $customer_handle = reepay()->api( $this )->get_customer_handle_by_order( $order->get_id() ); + $country = WC()->countries->country_exists( $order->get_billing_country() ) ? $order->get_billing_country() : ''; if ( $order->needs_shipping_address() ) { $country = WC()->countries->country_exists( $order->get_shipping_country() ) ? $order->get_shipping_country() : ''; } - if ( wcs_is_payment_change() ) { - return $this->wcs_change_payment_method( $order, $token_id ); - } - - $customer_handle = reepay()->api( $this )->get_customer_handle_by_order( $order_id ); - - $data = array( - 'country' => $country, - 'customer_handle' => $customer_handle, - 'test_mode' => $this->test_mode, - 'return_url' => $this->get_return_url( $order ), - 'language' => $this->get_language(), - ); - - $order_handle = rp_get_order_handle( $order ); - - // Initialize Payment. $params = array( 'locale' => $this->get_language(), 'recurring' => apply_filters( @@ -805,7 +790,7 @@ public function process_payment( $order_id ) { $order ), 'order' => array( - 'handle' => $order_handle, + 'handle' => rp_get_order_handle( $order ), 'amount' => 'yes' === $this->skip_order_lines ? rp_prepare_amount( $order->get_total(), $order->get_currency() ) : null, 'order_lines' => 'no' === $this->skip_order_lines ? $this->get_order_items( $order ) : null, 'currency' => $order->get_currency(), @@ -815,6 +800,7 @@ public function process_payment( $order_id ) { 'email' => $order->get_billing_email(), 'address' => $order->get_billing_address_1(), 'address2' => $order->get_billing_address_2(), + 'country' => ! empty( $country ) ? $country : '', 'city' => $order->get_billing_city(), 'phone' => $order->get_billing_phone(), 'company' => $order->get_billing_company(), @@ -828,6 +814,7 @@ public function process_payment( $order_id ) { 'email' => $order->get_billing_email(), 'address' => $order->get_billing_address_1(), 'address2' => $order->get_billing_address_2(), + 'country' => ! empty( $country ) ? $country : '', 'city' => $order->get_billing_city(), 'phone' => $order->get_billing_phone(), 'company' => $order->get_billing_company(), @@ -846,11 +833,6 @@ public function process_payment( $order_id ) { $params['button_text'] = reepay()->get_setting( 'payment_button_text' ); } - if ( ! empty( $country ) ) { - $params['order']['customer']['country'] = $country; - $params['order']['billing_address']['country'] = $country; - } - if ( $this->payment_methods && count( $this->payment_methods ) > 0 ) { $params['payment_methods'] = $this->payment_methods; } @@ -862,6 +844,7 @@ public function process_payment( $order_id ) { 'email' => $order->get_billing_email(), 'address' => $order->get_shipping_address_1(), 'address2' => $order->get_shipping_address_2(), + 'country' => ! empty( $country ) ? $country : '', 'city' => $order->get_shipping_city(), 'phone' => $order->get_billing_phone(), 'company' => $order->get_shipping_company(), @@ -871,58 +854,86 @@ public function process_payment( $order_id ) { 'postal_code' => $order->get_shipping_postcode(), 'state_or_province' => $order->get_shipping_state(), ); - - if ( ! empty( $country ) ) { - $params['order']['shipping_address']['country'] = $country; - } } if ( 'reepay_mobilepay_subscriptions' === $order->get_payment_method() ) { $params['parameters']['mps_ttl'] = 'PT24H'; } + if ( absint( $token_id ) > 0 ) { + $token = new TokenReepay( $token_id ); + + $params['card_on_file'] = $token->get_token(); + $params['card_on_file_require_cvv'] = false; + $params['card_on_file_require_exp_date'] = false; + unset( $params['recurring'] ); + } + + // "Save Card" flag + $order->update_meta_data( '_reepay_maybe_save_card', $maybe_save_card ); + $order->save_meta_data(); + + return $params; + } + + /** + * Process Payment + * + * @param int $order_id Order ID. + * + * @return array|false + * @throws Exception If payment error. + */ + public function process_payment( $order_id ) { + $order = wc_get_order( $order_id ); + + try { + $this->woo_blocks_conditions( $order ); + }catch (Exception $e){ + wc_add_notice( $e->getMessage(), 'error' ); + return array( + 'messages' => __( $e->getMessage() ), + 'result' => 'failure', + ); + } + + $token_id = isset( $_POST[ 'wc-' . $this->id . '-payment-token' ] ) ? wc_clean( $_POST[ 'wc-' . $this->id . '-payment-token' ] ) : 'new'; + + if ( wcs_is_payment_change() ) { + return $this->wcs_change_payment_method( $order, $token_id ); + } + + $params = $this->get_params( $order, $token_id ); + + $data = array( + 'country' => $params['order']['customer']['country'], + 'customer_handle' => $params['order']['customer']['handle'], + 'test_mode' => $this->test_mode, + 'return_url' => $this->get_return_url( $order ), + 'language' => $this->get_language(), + 'button_text' => reepay()->get_setting( 'payment_button_text' ), + ); + // Try to charge with saved token. if ( absint( $token_id ) > 0 ) { $token = new TokenReepay( $token_id ); if ( ! $token->get_id() ) { wc_add_notice( __( 'Failed to load token.', 'reepay-checkout-gateway' ), 'error' ); - return false; } if ( $token->get_user_id() !== $order->get_user_id() ) { wc_add_notice( __( 'Access denied.', 'reepay-checkout-gateway' ), 'error' ); - return false; } - $params['card_on_file'] = $token->get_token(); - $params['card_on_file_require_cvv'] = false; - $params['card_on_file_require_exp_date'] = false; - unset( $params['recurring'] ); - // Don't charge payment if zero amount. if ( abs( $order->get_total() ) < 0.01 ) { - if ( wcs_cart_only_subscriptions() ) { - $result = reepay()->api( $this )->recurring( $this->payment_methods, $order, $data, $token->get_token(), $params['button_text'] ); - - if ( is_wp_error( $result ) ) { - throw new Exception( $result->get_error_message(), $result->get_error_code() ); - } - - if ( ! empty( $result['id'] ) ) { - update_post_meta( $order_id, 'reepay_session_id', $result['id'] ); - } - - if ( is_wp_error( $result ) ) { - throw new Exception( $result->get_error_message(), $result->get_error_code() ); - } - + if ( wcs_cart_only_subscriptions() ) { // Cart contain only WC or Reepay subscriptions try { - ReepayTokens::assign_payment_token( $order, $token ); - ReepayTokens::save_card_info_to_order( $order, $token->get_token() ); - } catch ( Exception $e ) { - $order->add_order_note( $e->getMessage() ); + $this->create_recurring( $order, $data, $token->get_token()); + }catch (Exception $e){ + wc_add_notice( $e->getMessage(), 'error' ); return array( 'result' => 'failure', @@ -938,7 +949,7 @@ public function process_payment( $order_id ) { ReepayTokens::assign_payment_token( $order, $token ); ReepayTokens::save_card_info_to_order( $order, $token->get_token() ); } catch ( Exception $e ) { - $order->add_order_note( $e->getMessage() ); + wc_add_notice( $e->getMessage(), 'error' ); return array( 'result' => 'failure', @@ -1001,18 +1012,6 @@ public function process_payment( $order_id ) { ); } - // "Save Card" flag - $order->update_meta_data( '_reepay_maybe_save_card', $maybe_save_card ); - $order->save_meta_data(); - - if ( ! empty( $customer_handle ) && 0 === $order->get_customer_id() ) { - reepay()->api( $this )->request( - 'PUT', - 'https://api.reepay.com/v1/customer/' . $customer_handle, - $params['order']['customer'] - ); - } - $have_sub = (class_exists( WC_Reepay_Renewals::class ) && WC_Reepay_Renewals::is_order_contain_subscription( $order )) || wcs_cart_have_subscription(); $only_items_lines = array(); @@ -1032,18 +1031,17 @@ public function process_payment( $order_id ) { // If here's Subscription or zero payment. if ( ( $have_sub ) && ( abs( $order->get_total() ) < 0.01 || empty( $only_items_lines ) ) ) { - $result = reepay()->api( $this )->recurring( $this->payment_methods, $order, $data, false, $params['button_text'] ); - - if ( is_wp_error( $result ) ) { - throw new Exception( $result->get_error_message(), $result->get_error_code() ); - } + try { + $this->create_recurring( $order, $data ); + }catch (Exception $e){ + wc_add_notice( $e->getMessage(), 'error' ); - if ( ! empty( $result['id'] ) ) { - update_post_meta( $order_id, 'reepay_session_id', $result['id'] ); + return array( + 'result' => 'failure', + 'message' => $e->getMessage(), + ); } - do_action( 'reepay_instant_settle', $order ); - $redirect = '#!reepay-checkout'; if ( ! empty( $result['url'] ) ) { @@ -1064,6 +1062,35 @@ public function process_payment( $order_id ) { return $this->process_session_charge( $params, $order ); } + public function create_recurring( $order, $data, $token = false ){ + $result = reepay()->api( $this )->recurring( $this->payment_methods, $order, $data, $token ); + + if ( is_wp_error( $result ) ) { + throw new Exception( $result->get_error_message(), $result->get_error_code() ); + } + + if ( ! empty( $result['id'] ) ) { + update_post_meta( $order->get_id(), 'reepay_session_id', $result['id'] ); + } + + if( $token ){ + try { + ReepayTokens::assign_payment_token( $order, $token ); + ReepayTokens::save_card_info_to_order( $order, $token->get_token() ); + } catch ( Exception $e ) { + $order->add_order_note( $e->getMessage() ); + + return array( + 'result' => 'failure', + 'message' => $e->getMessage(), + ); + } + } + + + do_action( 'reepay_instant_settle', $order ); + } + /** * Handle WooCommerce subscription changing or adding payment method. * From 85392eebfe2abb1d1d7dae2b14403ea8526de6dd Mon Sep 17 00:00:00 2001 From: "dimaspolohov@yandex.ru" Date: Fri, 15 Dec 2023 13:33:12 +0300 Subject: [PATCH 02/15] mobilepay logo fix --- assets/images/svg/mobilepay.logo.svg | 1 + 1 file changed, 1 insertion(+) create mode 100644 assets/images/svg/mobilepay.logo.svg diff --git a/assets/images/svg/mobilepay.logo.svg b/assets/images/svg/mobilepay.logo.svg new file mode 100644 index 00000000..23681fa3 --- /dev/null +++ b/assets/images/svg/mobilepay.logo.svg @@ -0,0 +1 @@ +Payment-Method-Logos \ No newline at end of file From 9d4a9f6826eb977b16971454dd9029bc4f2f1417 Mon Sep 17 00:00:00 2001 From: "dimaspolohov@yandex.ru" Date: Fri, 15 Dec 2023 16:36:06 +0300 Subject: [PATCH 03/15] Fix notice --- includes/Gateways/ReepayCheckout.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Gateways/ReepayCheckout.php b/includes/Gateways/ReepayCheckout.php index a3a4df94..3f09e10f 100644 --- a/includes/Gateways/ReepayCheckout.php +++ b/includes/Gateways/ReepayCheckout.php @@ -82,7 +82,7 @@ public function __construct() { $this->payment_methods = $this->settings['payment_methods'] ?: $this->payment_methods; $this->skip_order_lines = $this->settings['skip_order_lines'] ?: $this->skip_order_lines; $this->failed_webhooks_email = $this->settings['failed_webhooks_email'] ?: $this->failed_webhooks_email; - $this->handle_failover = $this->settings['handle_failover'] ?: $this->handle_failover; + $this->handle_failover = !empty($this->settings['handle_failover']) ?: $this->handle_failover; if ( 'yes' === $this->save_cc ) { $this->supports[] = 'add_payment_method'; From 4e005424a98dcbaa3e6752c00b5cebfbc2ee0498 Mon Sep 17 00:00:00 2001 From: "dimaspolohov@yandex.ru" Date: Wed, 20 Dec 2023 14:09:00 +0300 Subject: [PATCH 04/15] Fix MS instant settle --- includes/Actions/Subscriptions.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/includes/Actions/Subscriptions.php b/includes/Actions/Subscriptions.php index d279bd80..5b0a84b4 100644 --- a/includes/Actions/Subscriptions.php +++ b/includes/Actions/Subscriptions.php @@ -362,6 +362,12 @@ public function scheduled_subscription_payment( float $amount_to_charge, WC_Orde throw new Exception( $result->get_error_message(), $result->get_error_code() ); } + // Fix for subscriptions can create the invoice + sleep(5); + if($renewal_order->get_status() == 'pending'){ + sleep(5); + } + // Instant settle. do_action( 'reepay_instant_settle', $renewal_order ); } catch ( Exception $e ) { From 4ce4e450136698e06f01b334edec3df91a59a381 Mon Sep 17 00:00:00 2001 From: "dimaspolohov@yandex.ru" Date: Thu, 21 Dec 2023 14:08:54 +0300 Subject: [PATCH 05/15] Revert "Process payment refactor" This reverts commit 3da9a112b3a88e94d467adaa7f32cab016a2daa9. --- includes/Api.php | 6 +- includes/Gateways/ReepayGateway.php | 197 ++++++++++++---------------- 2 files changed, 88 insertions(+), 115 deletions(-) diff --git a/includes/Api.php b/includes/Api.php index 1a9209e0..96817230 100644 --- a/includes/Api.php +++ b/includes/Api.php @@ -561,7 +561,7 @@ public function capture_payment( $order, $amount = null ) { * @return array|mixed|object|WP_Error * @see ReepayGateway::payment_methods. */ - public function recurring( array $payment_methods, WC_Order $order, array $data, $token = false ) { + public function recurring( array $payment_methods, WC_Order $order, array $data, $token = false, $payment_text = '' ) { $params = array( 'locale' => $data['language'], 'create_customer' => array( @@ -582,8 +582,8 @@ public function recurring( array $payment_methods, WC_Order $order, array $data, 'cancel_url' => $order->get_cancel_order_url(), ); - if ( ! empty( $data['button_text'] ) ) { - $params['button_text'] = $data['button_text']; + if ( ! empty( $payment_text ) ) { + $params['button_text'] = $payment_text; } if ( ! empty( $token ) ) { diff --git a/includes/Gateways/ReepayGateway.php b/includes/Gateways/ReepayGateway.php index e38a3ec7..57b467d9 100644 --- a/includes/Gateways/ReepayGateway.php +++ b/includes/Gateways/ReepayGateway.php @@ -725,14 +725,14 @@ public function payment_fields() { } /** - * Run some conditions for woo blocks using + * Process Payment * - * @param object $order Order. + * @param int $order_id Order ID. * - * @return false + * @return array|false * @throws Exception If payment error. */ - public function woo_blocks_conditions( $order ){ + public function process_payment( $order_id ) { $is_woo_blocks_checkout_request = false; if ( isset( $_SERVER['CONTENT_TYPE'] ) && 'application/json' === $_SERVER['CONTENT_TYPE'] ) { @@ -747,11 +747,14 @@ public function woo_blocks_conditions( $order ){ } } } catch ( Exception $e ) { - throw new Exception( __('Wrong Request. Try again', 'reepay-checkout-gateway') ); + return array( + 'messages' => __( 'Wrong Request. Try again', 'reepay-checkout-gateway' ), + 'result' => 'failure', + ); } } - + $order = wc_get_order( $order_id ); if ( $is_woo_blocks_checkout_request ) { /** @@ -760,10 +763,7 @@ public function woo_blocks_conditions( $order ){ $order->calculate_totals(); } - return false; - } - - public function get_params( $order, $token_id ){ + $token_id = isset( $_POST[ 'wc-' . $this->id . '-payment-token' ] ) ? wc_clean( $_POST[ 'wc-' . $this->id . '-payment-token' ] ) : 'new'; if ( 'yes' === $this->save_cc && 'new' === $token_id @@ -775,13 +775,28 @@ public function get_params( $order, $token_id ){ $maybe_save_card = wcs_cart_have_subscription(); } - $customer_handle = reepay()->api( $this )->get_customer_handle_by_order( $order->get_id() ); - $country = WC()->countries->country_exists( $order->get_billing_country() ) ? $order->get_billing_country() : ''; if ( $order->needs_shipping_address() ) { $country = WC()->countries->country_exists( $order->get_shipping_country() ) ? $order->get_shipping_country() : ''; } + if ( wcs_is_payment_change() ) { + return $this->wcs_change_payment_method( $order, $token_id ); + } + + $customer_handle = reepay()->api( $this )->get_customer_handle_by_order( $order_id ); + + $data = array( + 'country' => $country, + 'customer_handle' => $customer_handle, + 'test_mode' => $this->test_mode, + 'return_url' => $this->get_return_url( $order ), + 'language' => $this->get_language(), + ); + + $order_handle = rp_get_order_handle( $order ); + + // Initialize Payment. $params = array( 'locale' => $this->get_language(), 'recurring' => apply_filters( @@ -790,7 +805,7 @@ public function get_params( $order, $token_id ){ $order ), 'order' => array( - 'handle' => rp_get_order_handle( $order ), + 'handle' => $order_handle, 'amount' => 'yes' === $this->skip_order_lines ? rp_prepare_amount( $order->get_total(), $order->get_currency() ) : null, 'order_lines' => 'no' === $this->skip_order_lines ? $this->get_order_items( $order ) : null, 'currency' => $order->get_currency(), @@ -800,7 +815,6 @@ public function get_params( $order, $token_id ){ 'email' => $order->get_billing_email(), 'address' => $order->get_billing_address_1(), 'address2' => $order->get_billing_address_2(), - 'country' => ! empty( $country ) ? $country : '', 'city' => $order->get_billing_city(), 'phone' => $order->get_billing_phone(), 'company' => $order->get_billing_company(), @@ -814,7 +828,6 @@ public function get_params( $order, $token_id ){ 'email' => $order->get_billing_email(), 'address' => $order->get_billing_address_1(), 'address2' => $order->get_billing_address_2(), - 'country' => ! empty( $country ) ? $country : '', 'city' => $order->get_billing_city(), 'phone' => $order->get_billing_phone(), 'company' => $order->get_billing_company(), @@ -833,6 +846,11 @@ public function get_params( $order, $token_id ){ $params['button_text'] = reepay()->get_setting( 'payment_button_text' ); } + if ( ! empty( $country ) ) { + $params['order']['customer']['country'] = $country; + $params['order']['billing_address']['country'] = $country; + } + if ( $this->payment_methods && count( $this->payment_methods ) > 0 ) { $params['payment_methods'] = $this->payment_methods; } @@ -844,7 +862,6 @@ public function get_params( $order, $token_id ){ 'email' => $order->get_billing_email(), 'address' => $order->get_shipping_address_1(), 'address2' => $order->get_shipping_address_2(), - 'country' => ! empty( $country ) ? $country : '', 'city' => $order->get_shipping_city(), 'phone' => $order->get_billing_phone(), 'company' => $order->get_shipping_company(), @@ -854,86 +871,58 @@ public function get_params( $order, $token_id ){ 'postal_code' => $order->get_shipping_postcode(), 'state_or_province' => $order->get_shipping_state(), ); + + if ( ! empty( $country ) ) { + $params['order']['shipping_address']['country'] = $country; + } } if ( 'reepay_mobilepay_subscriptions' === $order->get_payment_method() ) { $params['parameters']['mps_ttl'] = 'PT24H'; } - if ( absint( $token_id ) > 0 ) { - $token = new TokenReepay( $token_id ); - - $params['card_on_file'] = $token->get_token(); - $params['card_on_file_require_cvv'] = false; - $params['card_on_file_require_exp_date'] = false; - unset( $params['recurring'] ); - } - - // "Save Card" flag - $order->update_meta_data( '_reepay_maybe_save_card', $maybe_save_card ); - $order->save_meta_data(); - - return $params; - } - - /** - * Process Payment - * - * @param int $order_id Order ID. - * - * @return array|false - * @throws Exception If payment error. - */ - public function process_payment( $order_id ) { - $order = wc_get_order( $order_id ); - - try { - $this->woo_blocks_conditions( $order ); - }catch (Exception $e){ - wc_add_notice( $e->getMessage(), 'error' ); - return array( - 'messages' => __( $e->getMessage() ), - 'result' => 'failure', - ); - } - - $token_id = isset( $_POST[ 'wc-' . $this->id . '-payment-token' ] ) ? wc_clean( $_POST[ 'wc-' . $this->id . '-payment-token' ] ) : 'new'; - - if ( wcs_is_payment_change() ) { - return $this->wcs_change_payment_method( $order, $token_id ); - } - - $params = $this->get_params( $order, $token_id ); - - $data = array( - 'country' => $params['order']['customer']['country'], - 'customer_handle' => $params['order']['customer']['handle'], - 'test_mode' => $this->test_mode, - 'return_url' => $this->get_return_url( $order ), - 'language' => $this->get_language(), - 'button_text' => reepay()->get_setting( 'payment_button_text' ), - ); - // Try to charge with saved token. if ( absint( $token_id ) > 0 ) { $token = new TokenReepay( $token_id ); if ( ! $token->get_id() ) { wc_add_notice( __( 'Failed to load token.', 'reepay-checkout-gateway' ), 'error' ); + return false; } if ( $token->get_user_id() !== $order->get_user_id() ) { wc_add_notice( __( 'Access denied.', 'reepay-checkout-gateway' ), 'error' ); + return false; } + $params['card_on_file'] = $token->get_token(); + $params['card_on_file_require_cvv'] = false; + $params['card_on_file_require_exp_date'] = false; + unset( $params['recurring'] ); + // Don't charge payment if zero amount. if ( abs( $order->get_total() ) < 0.01 ) { - if ( wcs_cart_only_subscriptions() ) { // Cart contain only WC or Reepay subscriptions + if ( wcs_cart_only_subscriptions() ) { + $result = reepay()->api( $this )->recurring( $this->payment_methods, $order, $data, $token->get_token(), $params['button_text'] ); + + if ( is_wp_error( $result ) ) { + throw new Exception( $result->get_error_message(), $result->get_error_code() ); + } + + if ( ! empty( $result['id'] ) ) { + update_post_meta( $order_id, 'reepay_session_id', $result['id'] ); + } + + if ( is_wp_error( $result ) ) { + throw new Exception( $result->get_error_message(), $result->get_error_code() ); + } + try { - $this->create_recurring( $order, $data, $token->get_token()); - }catch (Exception $e){ - wc_add_notice( $e->getMessage(), 'error' ); + ReepayTokens::assign_payment_token( $order, $token ); + ReepayTokens::save_card_info_to_order( $order, $token->get_token() ); + } catch ( Exception $e ) { + $order->add_order_note( $e->getMessage() ); return array( 'result' => 'failure', @@ -949,7 +938,7 @@ public function process_payment( $order_id ) { ReepayTokens::assign_payment_token( $order, $token ); ReepayTokens::save_card_info_to_order( $order, $token->get_token() ); } catch ( Exception $e ) { - wc_add_notice( $e->getMessage(), 'error' ); + $order->add_order_note( $e->getMessage() ); return array( 'result' => 'failure', @@ -1012,6 +1001,18 @@ public function process_payment( $order_id ) { ); } + // "Save Card" flag + $order->update_meta_data( '_reepay_maybe_save_card', $maybe_save_card ); + $order->save_meta_data(); + + if ( ! empty( $customer_handle ) && 0 === $order->get_customer_id() ) { + reepay()->api( $this )->request( + 'PUT', + 'https://api.reepay.com/v1/customer/' . $customer_handle, + $params['order']['customer'] + ); + } + $have_sub = (class_exists( WC_Reepay_Renewals::class ) && WC_Reepay_Renewals::is_order_contain_subscription( $order )) || wcs_cart_have_subscription(); $only_items_lines = array(); @@ -1031,17 +1032,18 @@ public function process_payment( $order_id ) { // If here's Subscription or zero payment. if ( ( $have_sub ) && ( abs( $order->get_total() ) < 0.01 || empty( $only_items_lines ) ) ) { - try { - $this->create_recurring( $order, $data ); - }catch (Exception $e){ - wc_add_notice( $e->getMessage(), 'error' ); + $result = reepay()->api( $this )->recurring( $this->payment_methods, $order, $data, false, $params['button_text'] ); - return array( - 'result' => 'failure', - 'message' => $e->getMessage(), - ); + if ( is_wp_error( $result ) ) { + throw new Exception( $result->get_error_message(), $result->get_error_code() ); + } + + if ( ! empty( $result['id'] ) ) { + update_post_meta( $order_id, 'reepay_session_id', $result['id'] ); } + do_action( 'reepay_instant_settle', $order ); + $redirect = '#!reepay-checkout'; if ( ! empty( $result['url'] ) ) { @@ -1062,35 +1064,6 @@ public function process_payment( $order_id ) { return $this->process_session_charge( $params, $order ); } - public function create_recurring( $order, $data, $token = false ){ - $result = reepay()->api( $this )->recurring( $this->payment_methods, $order, $data, $token ); - - if ( is_wp_error( $result ) ) { - throw new Exception( $result->get_error_message(), $result->get_error_code() ); - } - - if ( ! empty( $result['id'] ) ) { - update_post_meta( $order->get_id(), 'reepay_session_id', $result['id'] ); - } - - if( $token ){ - try { - ReepayTokens::assign_payment_token( $order, $token ); - ReepayTokens::save_card_info_to_order( $order, $token->get_token() ); - } catch ( Exception $e ) { - $order->add_order_note( $e->getMessage() ); - - return array( - 'result' => 'failure', - 'message' => $e->getMessage(), - ); - } - } - - - do_action( 'reepay_instant_settle', $order ); - } - /** * Handle WooCommerce subscription changing or adding payment method. * From 46699db12a5a64496c1104887456b0109283c22d Mon Sep 17 00:00:00 2001 From: "dimaspolohov@yandex.ru" Date: Thu, 21 Dec 2023 18:05:22 +0300 Subject: [PATCH 06/15] Metaboxes HPOS support --- includes/Admin/MetaBoxes.php | 70 ++++++++++++++---------------------- 1 file changed, 27 insertions(+), 43 deletions(-) diff --git a/includes/Admin/MetaBoxes.php b/includes/Admin/MetaBoxes.php index 656e3c2d..1b8c9e0d 100644 --- a/includes/Admin/MetaBoxes.php +++ b/includes/Admin/MetaBoxes.php @@ -40,21 +40,10 @@ public function __construct() { */ public function add_meta_boxes() { $screen = get_current_screen(); - $post_types = array( 'shop_order', 'shop_subscription' ); - if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { - $post_types[] = 'woocommerce_page_wc-orders'; + $order = wc_get_order( $_GET['id'] ); + $order_data = $order->get_data(); - $post = get_post( wc_clean( $_GET['id'] ?? 0 ) ); - } else { - global $post; - } - - if ( empty( $post ) || ! in_array( $screen->id, $post_types, true ) || ! in_array( $post->post_type, $post_types, true ) ) { - return; - } - - $order = wc_get_order( $post->ID ); if ( empty( $order ) || ! rp_is_order_paid_via_reepay( $order ) ) { return; @@ -66,12 +55,13 @@ public function add_meta_boxes() { return; } - if ( ! empty( get_post_meta( $post->ID, '_reepay_order', true ) ) && 0 !== $post->post_parent ) { - $subscription = get_post_meta( $post->post_parent, '_reepay_subscription_handle', true ); - } elseif ( ! empty( get_post_meta( $post->ID, '_reepay_subscription_handle_parent', true ) ) ) { - $subscription = get_post_meta( $post->ID, '_reepay_subscription_handle_parent', true ); + if ( ! empty( $order->get_meta( '_reepay_order' ) ) && 0 !== $order_data['parent_id'] ) { + $parent_order = wc_get_order( $order_data['parent_id'] ); + $subscription = $parent_order->get_meta( '_reepay_subscription_handle' ); + } elseif ( ! empty( $order->get_meta( '_reepay_subscription_handle_parent' ) ) ) { + $subscription = $order->get_meta( '_reepay_subscription_handle_parent' ); } else { - $subscription = get_post_meta( $post->ID, '_reepay_subscription_handle', true ); + $subscription = $order->get_meta( '_reepay_subscription_handle' ); } add_meta_box( @@ -84,11 +74,10 @@ public function add_meta_boxes() { array( 'order' => $order, 'gateway' => $gateway, - 'post' => $post, ) ); - if ( ! empty( get_post_meta( $post->ID, '_transaction_id', true ) ) ) { + if ( ! empty( $order->get_meta( '_transaction_id' ) ) ) { add_meta_box( 'reepay_checkout_invoice', __( 'Invoice', 'reepay-checkout-gateway' ), @@ -99,7 +88,6 @@ public function add_meta_boxes() { array( 'order' => $order, 'gateway' => $gateway, - 'post' => $post, ) ); } @@ -115,7 +103,6 @@ public function add_meta_boxes() { array( 'order' => $order, 'gateway' => $gateway, - 'post' => $post, ) ); } @@ -133,16 +120,15 @@ public function generate_meta_box_content_customer( $x, array $meta ) { * * @var WC_Order $order * @var ReepayCheckout $gateway - * @var WP_Post $post */ $order = $meta['args']['order']; - $gateway = $meta['args']['gateway']; - $post = $meta['args']['post']; + $order_data = $order->get_data(); - if ( ! empty( get_post_meta( $post->ID, '_reepay_order', true ) ) && 0 !== $post->post_parent ) { - $handle = get_post_meta( $post->post_parent, '_reepay_customer', true ); + if ( ! empty( $order->get_meta( '_reepay_order' ) ) && 0 !== $order_data['parent_id'] ) { + $parent_order = wc_get_order( $order_data['parent_id'] ); + $handle = $parent_order->get_meta('_reepay_customer'); } else { - $handle = get_post_meta( $post->ID, '_reepay_customer', true ); + $handle = $order->get_meta( '_reepay_customer' ); } if ( empty( $handle ) ) { @@ -150,7 +136,7 @@ public function generate_meta_box_content_customer( $x, array $meta ) { } $template_args = array( - 'email' => get_post_meta( $post->ID, '_billing_email', true ), + 'email' => $order->get_meta( '_billing_email' ), 'handle' => $handle, 'link' => $this->dashboard_url . 'customers/customers/customer/' . $handle, ); @@ -173,11 +159,9 @@ public function generate_meta_box_content_invoice( $x, array $meta ) { * * @var WC_Order $order * @var ReepayCheckout $gateway - * @var WP_Post $post */ $order = $meta['args']['order']; $gateway = $meta['args']['gateway']; - $post = $meta['args']['post']; $order_data = reepay()->api( $gateway )->get_invoice_data( $order ); @@ -218,21 +202,21 @@ public function generate_meta_box_content_subscription( $x, array $meta ) { * * @var WC_Order $order * @var ReepayCheckout $gateway - * @var WP_Post $post */ $order = $meta['args']['order']; $gateway = $meta['args']['gateway']; - $post = $meta['args']['post']; - - if ( ! empty( get_post_meta( $post->ID, '_reepay_order', true ) ) && 0 !== $post->post_parent ) { - $handle = get_post_meta( $post->post_parent, '_reepay_subscription_handle', true ); - $plan = get_post_meta( $post->post_parent, '_reepay_subscription_plan', true ); - } elseif ( get_post_meta( $post->ID, '_reepay_renewal', true ) ) { - $handle = get_post_meta( $post->ID, '_reepay_subscription_handle_parent', true ); - $plan = get_post_meta( $post->ID, '_reepay_subscription_plan', true ); + $order_data = $order->get_data(); + + if ( ! empty( $order->get_meta( '_reepay_order' ) ) && 0 !== $order_data['parent_id'] ) { + $parent_order = wc_get_order( $order_data['parent_id'] ); + $handle = $parent_order->get_meta( '_reepay_subscription_handle' ); + $plan = $parent_order->get_meta( '_reepay_subscription_plan' ); + } elseif ( $order->get_meta( '_reepay_renewal' ) ) { + $handle = $order->get_meta( '_reepay_subscription_handle_parent' ); + $plan = $order->get_meta( '_reepay_subscription_plan' ); } else { - $handle = get_post_meta( $post->ID, '_reepay_subscription_handle', true ); - $plan = get_post_meta( $post->ID, '_reepay_subscription_plan', true ); + $handle = $order->get_meta( '_reepay_subscription_handle' ); + $plan = $order->get_meta( '_reepay_subscription_plan' ); } $template_args = array( @@ -244,7 +228,7 @@ public function generate_meta_box_content_subscription( $x, array $meta ) { try { $subscription = reepay_s()->api()->request( "subscription/{$template_args['handle']}" ); $template_args['plan'] = $subscription['plan']; - update_post_meta( $post->ID, '_reepay_subscription_plan', $subscription['plan'] ); + $order->update_meta_data( '_reepay_subscription_plan', $subscription['plan'] ); } catch ( Exception $e ) { //phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch // Do not show subscription plan name if api error. } From d97f36cb9ba9bfdc7036fd819c70e69287f91439 Mon Sep 17 00:00:00 2001 From: "dimaspolohov@yandex.ru" Date: Thu, 21 Dec 2023 18:12:24 +0300 Subject: [PATCH 07/15] Get post meta HPOS --- includes/Actions/Subscriptions.php | 2 +- includes/OrderFlow/Webhook.php | 7 ++++--- templates/checkout/thankyou.php | 2 +- updates/update-1.1.0.php | 6 +++--- updates/update-1.2.0.php | 2 +- updates/update-1.2.3.php | 6 +++--- 6 files changed, 13 insertions(+), 12 deletions(-) diff --git a/includes/Actions/Subscriptions.php b/includes/Actions/Subscriptions.php index 5b0a84b4..418b82f9 100644 --- a/includes/Actions/Subscriptions.php +++ b/includes/Actions/Subscriptions.php @@ -473,7 +473,7 @@ public function save_new_payment_method_option_html( string $html, WC_Payment_Ga */ public function create_sub_invoice( int $order_id, string $this_status_transition_from, string $this_status_transition_to, WC_Order $instance ) { $renewal_order = wc_get_order( $order_id ); - $renewal_sub = get_post_meta( $order_id, '_subscription_renewal', true ); + $renewal_sub = $renewal_order->get_meta( '_subscription_renewal' ); $gateway = rp_get_payment_method( $renewal_order ); if ( ! empty( $renewal_sub ) && ! empty( $gateway ) ) { $order_data = reepay()->api( $gateway )->get_invoice_data( $renewal_order ); diff --git a/includes/OrderFlow/Webhook.php b/includes/OrderFlow/Webhook.php index 98b10d86..930e7805 100644 --- a/includes/OrderFlow/Webhook.php +++ b/includes/OrderFlow/Webhook.php @@ -329,7 +329,7 @@ public function process( array $data ) { return; } - $sub_order = get_post_meta( $order->get_id(), '_reepay_subscription_handle', true ); + $sub_order = $order->get_meta( '_reepay_subscription_handle' ); if ( ! empty( $sub_order ) ) { return; } @@ -509,7 +509,8 @@ private static function unlock_order( $order_id ) { private static function wait_for_unlock( int $order_id ): bool { set_time_limit( 0 ); - $is_locked = (bool) get_post_meta( $order_id, '_reepay_locked', true ); + $order = wc_get_order( $order_id ); + $is_locked = (bool) $order->get_meta( '_reepay_locked' ); $needs_reload = false; $attempts = 0; while ( $is_locked ) { @@ -520,7 +521,7 @@ private static function wait_for_unlock( int $order_id ): bool { } wp_cache_delete( $order_id, 'post_meta' ); - $is_locked = (bool) get_post_meta( $order_id, '_reepay_locked', true ); + $is_locked = (bool) $order->get_meta( '_reepay_locked' ); if ( $is_locked ) { $needs_reload = true; clean_post_cache( $order_id ); diff --git a/templates/checkout/thankyou.php b/templates/checkout/thankyou.php index b3ef34a6..97d3ae8e 100644 --- a/templates/checkout/thankyou.php +++ b/templates/checkout/thankyou.php @@ -31,7 +31,7 @@ get_id(), '_reepay_another_orders', true ) ?: array(); + $another_orders = $order->get_meta( '_reepay_another_orders' ) ?: array(); do_action( 'woocommerce_before_thankyou', $order->get_id() ); ?> diff --git a/updates/update-1.1.0.php b/updates/update-1.1.0.php index 5a63e1af..ad8c9ab5 100755 --- a/updates/update-1.1.0.php +++ b/updates/update-1.1.0.php @@ -44,7 +44,7 @@ } $log->add( $handler, sprintf( 'Processing subscription #%s', $subscription->get_id() ) ); - $token_id = get_post_meta( $subscription->get_parent()->get_id(), '_reepay_token_id', true ); + $token_id = $subscription->get_parent()->get_meta( '_reepay_token_id' ); if ( empty( $token_id ) ) { $log->add( $handler, sprintf( '[INFO] Subscription #%s doesn\'t have token id.', $subscription->get_id() ) ); continue; @@ -63,7 +63,7 @@ } // Check subscription's token - $subscriptions_tokens = get_post_meta( $subscription->get_id(), '_payment_tokens', true ); + $subscriptions_tokens = $subscription->get_meta( '_payment_tokens' ); if ( empty( $subscriptions_tokens ) ) { try { ReepayCheckout::assign_payment_token( $subscription, $token ); @@ -77,7 +77,7 @@ $orders = $subscription->get_related_orders( 'all', $order_types = array( 'renewal' ) ); foreach ( $orders as $order ) { /** WC_Order $order */ - $order_tokens = get_post_meta( $order->get_id(), '_payment_tokens', true ); + $order_tokens = $order->get_meta( '_payment_tokens' ); if ( empty( $order_tokens ) ) { try { ReepayCheckout::assign_payment_token( $order, $token ); diff --git a/updates/update-1.2.0.php b/updates/update-1.2.0.php index 65cbb1bb..4372447d 100755 --- a/updates/update-1.2.0.php +++ b/updates/update-1.2.0.php @@ -41,7 +41,7 @@ continue; } - $handle = get_post_meta( $order->get_id(), '_reepay_order', true ); + $handle = $order->get_meta( '_reepay_order' ); if ( ! empty( $handle ) ) { // Check if reepay handler is different that expected $check_id = str_replace( 'order-', '', $handle ); diff --git a/updates/update-1.2.3.php b/updates/update-1.2.3.php index 97e193e6..fcac00d3 100755 --- a/updates/update-1.2.3.php +++ b/updates/update-1.2.3.php @@ -47,7 +47,7 @@ } // Check Token ID - $token_id = get_post_meta( $subscription->get_id(), '_reepay_token_id', true ); + $token_id = $subscription->get_meta( '_reepay_token_id' ); if ( empty( $token_id ) ) { update_post_meta( $subscription->get_id(), '_reepay_token_id', $token->get_id() ); $processed[ $subscription->get_id() ] = $subscription; @@ -55,7 +55,7 @@ } // Check Token - $reepay_token = get_post_meta( $subscription->get_id(), '_reepay_token', true ); + $reepay_token = $subscription->get_meta( '_reepay_token' ); if ( empty( $reepay_token ) ) { update_post_meta( $subscription->get_id(), '_reepay_token', $token->get_token() ); update_post_meta( $subscription->get_id(), 'reepay_token', $token->get_token() ); @@ -95,7 +95,7 @@ } // Update _reepay_order meta - $meta = get_post_meta( $order_id, '_reepay_order', true ); + $meta = $order->get_meta( '_reepay_order' ); update_post_meta( $order_id, '_reepay_order', 'order-' . $order->get_id() ); $log->add( $handler, sprintf( '[INFO] Meta of order #%s changed. Meta that was before: %s', $order->get_id(), $meta ) ); From d27a53fc23cc81596145e0073528c37f059d5719 Mon Sep 17 00:00:00 2001 From: "dimaspolohov@yandex.ru" Date: Thu, 21 Dec 2023 18:16:32 +0300 Subject: [PATCH 08/15] Delete post meta HPOS --- includes/Actions/Subscriptions.php | 6 +++--- includes/OrderFlow/Webhook.php | 3 ++- includes/Tokens/ReepayTokens.php | 6 +++--- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/includes/Actions/Subscriptions.php b/includes/Actions/Subscriptions.php index 418b82f9..ea154047 100644 --- a/includes/Actions/Subscriptions.php +++ b/includes/Actions/Subscriptions.php @@ -176,9 +176,9 @@ public function update_failing_payment_method( WC_Subscription $subscription, WC public function delete_resubscribe_meta( WC_Order $resubscribe_order ) { if ( in_array( $resubscribe_order->get_payment_method(), self::PAYMENT_METHODS, true ) ) { // Delete tokens. - delete_post_meta( $resubscribe_order->get_id(), '_payment_tokens' ); - delete_post_meta( $resubscribe_order->get_id(), '_reepay_token' ); - delete_post_meta( $resubscribe_order->get_id(), '_reepay_token_id' ); + $resubscribe_order->delete_meta_data( '_payment_tokens' ); + $resubscribe_order->delete_meta_data( '_reepay_token' ); + $resubscribe_order->delete_meta_data( '_reepay_token_id' ); } } diff --git a/includes/OrderFlow/Webhook.php b/includes/OrderFlow/Webhook.php index 930e7805..008ab20e 100644 --- a/includes/OrderFlow/Webhook.php +++ b/includes/OrderFlow/Webhook.php @@ -496,7 +496,8 @@ private static function lock_order( int $order_id ) { * @see wait_for_unlock() */ private static function unlock_order( $order_id ) { - delete_post_meta( $order_id, '_reepay_locked' ); + $order = wc_get_order( $order_id ); + $order->delete_meta_data( '_reepay_locked' ); } /** diff --git a/includes/Tokens/ReepayTokens.php b/includes/Tokens/ReepayTokens.php index b1ae0bc6..af4e3298 100644 --- a/includes/Tokens/ReepayTokens.php +++ b/includes/Tokens/ReepayTokens.php @@ -44,12 +44,12 @@ public static function assign_payment_token( WC_Order $order, $token ) { } if ( $token->get_id() ) { - // Delete tokens if exist. - delete_post_meta( $order->get_id(), '_payment_tokens' ); + // Reload order. $order = wc_get_order( $order->get_id() ); - + // Delete tokens if exist. + $order->delete_meta_data( '_payment_tokens' ); // Add payment token. $order->add_payment_token( $token ); From 39417c5defee1e8e1b448d2f6e8884f25d052d4a Mon Sep 17 00:00:00 2001 From: "dimaspolohov@yandex.ru" Date: Thu, 21 Dec 2023 18:24:13 +0300 Subject: [PATCH 09/15] Update post meta HPOS --- includes/Actions/Subscriptions.php | 5 +++-- includes/Api.php | 5 ++--- includes/Gateways/ReepayGateway.php | 8 ++++---- includes/OrderFlow/Webhook.php | 5 +++-- updates/update-1.2.0.php | 2 +- updates/update-1.2.3.php | 8 ++++---- 6 files changed, 17 insertions(+), 16 deletions(-) diff --git a/includes/Actions/Subscriptions.php b/includes/Actions/Subscriptions.php index ea154047..b956da61 100644 --- a/includes/Actions/Subscriptions.php +++ b/includes/Actions/Subscriptions.php @@ -256,7 +256,7 @@ public function save_subscription_payment_meta( WC_Subscription $subscription, s // Add tokens. foreach ( explode( ',', $meta_value ) as $reepay_token ) { ReepayTokens::assign_payment_token( $subscription, $reepay_token ); - update_post_meta( $subscription->get_id(), 'reepay_token', $reepay_token ); + $subscription->update_meta_data( 'reepay_token', $reepay_token ); } } } @@ -504,7 +504,8 @@ public function create_sub_invoice( int $order_id, string $this_status_transitio */ public function sync_reepay_token_meta( int $meta_id, int $post_id, string $meta_key, $meta_value ) { if ( 'reepay_token' === $meta_key ) { - update_post_meta( $post_id, '_reepay_token', $meta_value ); + $order = wc_get_order( $post_id ); + $order->update_meta_data( '_reepay_token', $meta_value ); } } } diff --git a/includes/Api.php b/includes/Api.php index 96817230..06174289 100644 --- a/includes/Api.php +++ b/includes/Api.php @@ -972,8 +972,7 @@ private function process_charge_result( WC_Order $order, array $result ) { break; case 'settled': - update_post_meta( $order->get_id(), '_reepay_capture_transaction', $result['transaction'] ); - + $order->update_meta_data( '_reepay_capture_transaction', $result['transaction'] ); OrderStatuses::set_settled_status( $order, sprintf( @@ -987,7 +986,7 @@ private function process_charge_result( WC_Order $order, array $result ) { break; case 'cancelled': - update_post_meta( $order->get_id(), '_reepay_cancel_transaction', $result['transaction'] ); + $order->update_meta_data( '_reepay_cancel_transaction', $result['transaction'] ); if ( ! $order->has_status( 'cancelled' ) ) { $order->update_status( diff --git a/includes/Gateways/ReepayGateway.php b/includes/Gateways/ReepayGateway.php index 57b467d9..48f0fe54 100644 --- a/includes/Gateways/ReepayGateway.php +++ b/includes/Gateways/ReepayGateway.php @@ -911,7 +911,7 @@ public function process_payment( $order_id ) { } if ( ! empty( $result['id'] ) ) { - update_post_meta( $order_id, 'reepay_session_id', $result['id'] ); + $order->update_meta_data( 'reepay_session_id', $result['id'] ); } if ( is_wp_error( $result ) ) { @@ -1039,7 +1039,7 @@ public function process_payment( $order_id ) { } if ( ! empty( $result['id'] ) ) { - update_post_meta( $order_id, 'reepay_session_id', $result['id'] ); + $order->update_meta_data( 'reepay_session_id', $result['id'] ); } do_action( 'reepay_instant_settle', $order ); @@ -1208,7 +1208,7 @@ public function process_session_charge( array $params, WC_Order $order ): array $handle = rp_get_order_handle( $order, true ); $params['order']['handle'] = $handle; - update_post_meta( $order->get_id(), '_reepay_order', $handle ); + $order->update_meta_data( '_reepay_order', $handle ); $result = reepay()->api( $this )->request( 'POST', @@ -1242,7 +1242,7 @@ public function process_session_charge( array $params, WC_Order $order ): array } if ( ! empty( $result['id'] ) ) { - update_post_meta( $order->get_id(), 'reepay_session_id', $result['id'] ); + $order->update_meta_data( 'reepay_session_id', $result['id'] ); } if ( is_checkout_pay_page() ) { diff --git a/includes/OrderFlow/Webhook.php b/includes/OrderFlow/Webhook.php index 008ab20e..f555eb83 100644 --- a/includes/OrderFlow/Webhook.php +++ b/includes/OrderFlow/Webhook.php @@ -263,7 +263,7 @@ public function process( array $data ) { ); } - update_post_meta( $order->get_id(), '_reepay_capture_transaction', $data['transaction'] ); + $order->update_meta_data( '_reepay_capture_transaction', $data['transaction'] ); self::unlock_order( $order->get_id() ); @@ -484,7 +484,8 @@ public function process( array $data ) { * @see wait_for_unlock() */ private static function lock_order( int $order_id ) { - update_post_meta( $order_id, '_reepay_locked', '1' ); + $order = wc_get_order( $order_id ); + $order->update_meta_data( '_reepay_locked', '1' ); } /** diff --git a/updates/update-1.2.0.php b/updates/update-1.2.0.php index 4372447d..b27b3b0c 100755 --- a/updates/update-1.2.0.php +++ b/updates/update-1.2.0.php @@ -48,7 +48,7 @@ if ($check_id != $order->get_id()) { // Update handler $handle = 'order-' . $order->get_id(); - update_post_meta( $order->get_id(), '_reepay_order', $handle ); + $order->update_meta_data( '_reepay_order', $handle ); $log->add( $handler, sprintf( '[SUCCESS] Updated reepay handler for order #%s. Handler before: %s', $order->get_id(), $handle ) ); } } diff --git a/updates/update-1.2.3.php b/updates/update-1.2.3.php index fcac00d3..e6df4b92 100755 --- a/updates/update-1.2.3.php +++ b/updates/update-1.2.3.php @@ -49,7 +49,7 @@ // Check Token ID $token_id = $subscription->get_meta( '_reepay_token_id' ); if ( empty( $token_id ) ) { - update_post_meta( $subscription->get_id(), '_reepay_token_id', $token->get_id() ); + $subscription->update_meta_data( '_reepay_token_id', $token->get_id() ); $processed[ $subscription->get_id() ] = $subscription; $log->add( $handler, sprintf( '[SUCCESS] Subscription #%s. Token ID was filled.', $subscription->get_id() ) ); } @@ -57,8 +57,8 @@ // Check Token $reepay_token = $subscription->get_meta( '_reepay_token' ); if ( empty( $reepay_token ) ) { - update_post_meta( $subscription->get_id(), '_reepay_token', $token->get_token() ); - update_post_meta( $subscription->get_id(), 'reepay_token', $token->get_token() ); + $subscription->update_meta_data( '_reepay_token', $token->get_token() ); + $subscription->update_meta_data( 'reepay_token', $token->get_token() ); $processed[ $subscription->get_id() ] = $subscription; $log->add( $handler, sprintf( '[SUCCESS] Subscription #%s. Token was filled.', $subscription->get_id() ) ); } @@ -96,7 +96,7 @@ // Update _reepay_order meta $meta = $order->get_meta( '_reepay_order' ); - update_post_meta( $order_id, '_reepay_order', 'order-' . $order->get_id() ); + $order->update_meta_data( '_reepay_order', 'order-' . $order->get_id() ); $log->add( $handler, sprintf( '[INFO] Meta of order #%s changed. Meta that was before: %s', $order->get_id(), $meta ) ); // Charge From ce2e50a7c15bf0c4d35e6400c9113a1b201c8d62 Mon Sep 17 00:00:00 2001 From: "dimaspolohov@yandex.ru" Date: Thu, 21 Dec 2023 18:32:35 +0300 Subject: [PATCH 10/15] save_meta_data HPOS --- includes/Actions/Subscriptions.php | 3 +++ includes/Admin/MetaBoxes.php | 1 + includes/Api.php | 3 ++- includes/Gateways/ReepayGateway.php | 4 ++++ includes/OrderFlow/Webhook.php | 3 +++ updates/update-1.2.0.php | 1 + updates/update-1.2.3.php | 3 +++ 7 files changed, 17 insertions(+), 1 deletion(-) diff --git a/includes/Actions/Subscriptions.php b/includes/Actions/Subscriptions.php index b956da61..08bf1f9e 100644 --- a/includes/Actions/Subscriptions.php +++ b/includes/Actions/Subscriptions.php @@ -179,6 +179,7 @@ public function delete_resubscribe_meta( WC_Order $resubscribe_order ) { $resubscribe_order->delete_meta_data( '_payment_tokens' ); $resubscribe_order->delete_meta_data( '_reepay_token' ); $resubscribe_order->delete_meta_data( '_reepay_token_id' ); + $resubscribe_order->save_meta_data(); } } @@ -257,6 +258,7 @@ public function save_subscription_payment_meta( WC_Subscription $subscription, s foreach ( explode( ',', $meta_value ) as $reepay_token ) { ReepayTokens::assign_payment_token( $subscription, $reepay_token ); $subscription->update_meta_data( 'reepay_token', $reepay_token ); + $subscription->save_meta_data(); } } } @@ -506,6 +508,7 @@ public function sync_reepay_token_meta( int $meta_id, int $post_id, string $meta if ( 'reepay_token' === $meta_key ) { $order = wc_get_order( $post_id ); $order->update_meta_data( '_reepay_token', $meta_value ); + $order->save_meta_data(); } } } diff --git a/includes/Admin/MetaBoxes.php b/includes/Admin/MetaBoxes.php index 1b8c9e0d..1fcd9d01 100644 --- a/includes/Admin/MetaBoxes.php +++ b/includes/Admin/MetaBoxes.php @@ -229,6 +229,7 @@ public function generate_meta_box_content_subscription( $x, array $meta ) { $subscription = reepay_s()->api()->request( "subscription/{$template_args['handle']}" ); $template_args['plan'] = $subscription['plan']; $order->update_meta_data( '_reepay_subscription_plan', $subscription['plan'] ); + $order->save_meta_data(); } catch ( Exception $e ) { //phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch // Do not show subscription plan name if api error. } diff --git a/includes/Api.php b/includes/Api.php index 06174289..1495f5a9 100644 --- a/includes/Api.php +++ b/includes/Api.php @@ -973,6 +973,7 @@ private function process_charge_result( WC_Order $order, array $result ) { break; case 'settled': $order->update_meta_data( '_reepay_capture_transaction', $result['transaction'] ); + $order->save_meta_data(); OrderStatuses::set_settled_status( $order, sprintf( @@ -987,7 +988,7 @@ private function process_charge_result( WC_Order $order, array $result ) { break; case 'cancelled': $order->update_meta_data( '_reepay_cancel_transaction', $result['transaction'] ); - + $order->save_meta_data(); if ( ! $order->has_status( 'cancelled' ) ) { $order->update_status( 'cancelled', diff --git a/includes/Gateways/ReepayGateway.php b/includes/Gateways/ReepayGateway.php index 48f0fe54..4097ab07 100644 --- a/includes/Gateways/ReepayGateway.php +++ b/includes/Gateways/ReepayGateway.php @@ -912,6 +912,7 @@ public function process_payment( $order_id ) { if ( ! empty( $result['id'] ) ) { $order->update_meta_data( 'reepay_session_id', $result['id'] ); + $order->save_meta_data(); } if ( is_wp_error( $result ) ) { @@ -1040,6 +1041,7 @@ public function process_payment( $order_id ) { if ( ! empty( $result['id'] ) ) { $order->update_meta_data( 'reepay_session_id', $result['id'] ); + $order->save_meta_data(); } do_action( 'reepay_instant_settle', $order ); @@ -1209,6 +1211,7 @@ public function process_session_charge( array $params, WC_Order $order ): array $params['order']['handle'] = $handle; $order->update_meta_data( '_reepay_order', $handle ); + $order->save_meta_data(); $result = reepay()->api( $this )->request( 'POST', @@ -1243,6 +1246,7 @@ public function process_session_charge( array $params, WC_Order $order ): array if ( ! empty( $result['id'] ) ) { $order->update_meta_data( 'reepay_session_id', $result['id'] ); + $order->save_meta_data(); } if ( is_checkout_pay_page() ) { diff --git a/includes/OrderFlow/Webhook.php b/includes/OrderFlow/Webhook.php index f555eb83..fc02403c 100644 --- a/includes/OrderFlow/Webhook.php +++ b/includes/OrderFlow/Webhook.php @@ -264,6 +264,7 @@ public function process( array $data ) { } $order->update_meta_data( '_reepay_capture_transaction', $data['transaction'] ); + $order->save_meta_data(); self::unlock_order( $order->get_id() ); @@ -486,6 +487,7 @@ public function process( array $data ) { private static function lock_order( int $order_id ) { $order = wc_get_order( $order_id ); $order->update_meta_data( '_reepay_locked', '1' ); + $order->save_meta_data(); } /** @@ -499,6 +501,7 @@ private static function lock_order( int $order_id ) { private static function unlock_order( $order_id ) { $order = wc_get_order( $order_id ); $order->delete_meta_data( '_reepay_locked' ); + $order->save_meta_data(); } /** diff --git a/updates/update-1.2.0.php b/updates/update-1.2.0.php index b27b3b0c..c162ef84 100755 --- a/updates/update-1.2.0.php +++ b/updates/update-1.2.0.php @@ -49,6 +49,7 @@ // Update handler $handle = 'order-' . $order->get_id(); $order->update_meta_data( '_reepay_order', $handle ); + $order->save_meta_data(); $log->add( $handler, sprintf( '[SUCCESS] Updated reepay handler for order #%s. Handler before: %s', $order->get_id(), $handle ) ); } } diff --git a/updates/update-1.2.3.php b/updates/update-1.2.3.php index e6df4b92..887a6512 100755 --- a/updates/update-1.2.3.php +++ b/updates/update-1.2.3.php @@ -50,6 +50,7 @@ $token_id = $subscription->get_meta( '_reepay_token_id' ); if ( empty( $token_id ) ) { $subscription->update_meta_data( '_reepay_token_id', $token->get_id() ); + $subscription->save_meta_data(); $processed[ $subscription->get_id() ] = $subscription; $log->add( $handler, sprintf( '[SUCCESS] Subscription #%s. Token ID was filled.', $subscription->get_id() ) ); } @@ -59,6 +60,7 @@ if ( empty( $reepay_token ) ) { $subscription->update_meta_data( '_reepay_token', $token->get_token() ); $subscription->update_meta_data( 'reepay_token', $token->get_token() ); + $subscription->save_meta_data(); $processed[ $subscription->get_id() ] = $subscription; $log->add( $handler, sprintf( '[SUCCESS] Subscription #%s. Token was filled.', $subscription->get_id() ) ); } @@ -97,6 +99,7 @@ // Update _reepay_order meta $meta = $order->get_meta( '_reepay_order' ); $order->update_meta_data( '_reepay_order', 'order-' . $order->get_id() ); + $order->save_meta_data(); $log->add( $handler, sprintf( '[INFO] Meta of order #%s changed. Meta that was before: %s', $order->get_id(), $meta ) ); // Charge From 9abdd24b4742d35231cf5fcfefd980a2165fc796 Mon Sep 17 00:00:00 2001 From: "dimaspolohov@yandex.ru" Date: Thu, 21 Dec 2023 18:46:53 +0300 Subject: [PATCH 11/15] meta boxes HPOS --- includes/Admin/MetaBoxes.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/includes/Admin/MetaBoxes.php b/includes/Admin/MetaBoxes.php index 1fcd9d01..e0e34554 100644 --- a/includes/Admin/MetaBoxes.php +++ b/includes/Admin/MetaBoxes.php @@ -8,6 +8,7 @@ namespace Reepay\Checkout\Admin; use Automattic\WooCommerce\Utilities\OrderUtil; +use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController; use Exception; use Reepay\Checkout\Gateways\ReepayCheckout; use WC_Order; @@ -39,7 +40,9 @@ public function __construct() { * Register meta boxes on order page */ public function add_meta_boxes() { - $screen = get_current_screen(); + $screen = wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled() + ? wc_get_page_screen_id( 'shop-order' ) + : 'shop_order'; $order = wc_get_order( $_GET['id'] ); $order_data = $order->get_data(); @@ -68,7 +71,7 @@ public function add_meta_boxes() { 'reepay_checkout_customer', __( 'Customer', 'reepay-checkout-gateway' ), array( $this, 'generate_meta_box_content_customer' ), - $screen->id, + $screen, 'side', 'high', array( @@ -82,7 +85,7 @@ public function add_meta_boxes() { 'reepay_checkout_invoice', __( 'Invoice', 'reepay-checkout-gateway' ), array( $this, 'generate_meta_box_content_invoice' ), - $screen->id, + $screen, 'side', 'high', array( @@ -97,7 +100,7 @@ public function add_meta_boxes() { 'reepay_checkout_subscription', __( 'Subscription', 'reepay-checkout-gateway' ), array( $this, 'generate_meta_box_content_subscription' ), - $screen->id, + $screen, 'side', 'high', array( From 7438f8d64a4cd6052c8b87ec4c1856529d36a11c Mon Sep 17 00:00:00 2001 From: "dimaspolohov@yandex.ru" Date: Thu, 21 Dec 2023 19:16:01 +0300 Subject: [PATCH 12/15] Fix call methods HPOS --- includes/Admin/MetaBoxes.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/Admin/MetaBoxes.php b/includes/Admin/MetaBoxes.php index e0e34554..0986d20a 100644 --- a/includes/Admin/MetaBoxes.php +++ b/includes/Admin/MetaBoxes.php @@ -80,7 +80,7 @@ public function add_meta_boxes() { ) ); - if ( ! empty( $order->get_meta( '_transaction_id' ) ) ) { + if ( ! empty( $order->get_transaction_id() ) ) { add_meta_box( 'reepay_checkout_invoice', __( 'Invoice', 'reepay-checkout-gateway' ), @@ -139,7 +139,7 @@ public function generate_meta_box_content_customer( $x, array $meta ) { } $template_args = array( - 'email' => $order->get_meta( '_billing_email' ), + 'email' => $order->get_billing_email(), 'handle' => $handle, 'link' => $this->dashboard_url . 'customers/customers/customer/' . $handle, ); From 7acf3e4ca2157c7eb17b712b3a79678c3a8869d7 Mon Sep 17 00:00:00 2001 From: "dimaspolohov@yandex.ru" Date: Fri, 22 Dec 2023 12:47:33 +0300 Subject: [PATCH 13/15] Fix meta boxes HPOS --- includes/Admin/MetaBoxes.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/includes/Admin/MetaBoxes.php b/includes/Admin/MetaBoxes.php index 0986d20a..c0ed379f 100644 --- a/includes/Admin/MetaBoxes.php +++ b/includes/Admin/MetaBoxes.php @@ -44,7 +44,8 @@ public function add_meta_boxes() { ? wc_get_page_screen_id( 'shop-order' ) : 'shop_order'; - $order = wc_get_order( $_GET['id'] ); + $order_id = !empty($_GET['id']) ? $_GET['id'] : $_GET['post']; + $order = wc_get_order( $order_id ); $order_data = $order->get_data(); From 9ed82dfaf9d56ec564e76224944a63c175ef658b Mon Sep 17 00:00:00 2001 From: "dimaspolohov@yandex.ru" Date: Wed, 27 Dec 2023 22:21:22 +0300 Subject: [PATCH 14/15] Fix meta boxes HPOS --- includes/Admin/MetaBoxes.php | 96 ++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 47 deletions(-) diff --git a/includes/Admin/MetaBoxes.php b/includes/Admin/MetaBoxes.php index c0ed379f..83c76086 100644 --- a/includes/Admin/MetaBoxes.php +++ b/includes/Admin/MetaBoxes.php @@ -46,46 +46,33 @@ public function add_meta_boxes() { $order_id = !empty($_GET['id']) ? $_GET['id'] : $_GET['post']; $order = wc_get_order( $order_id ); - $order_data = $order->get_data(); - + if($order){ + $order_data = $order->get_data(); - if ( empty( $order ) || ! rp_is_order_paid_via_reepay( $order ) ) { - return; - } - $gateway = rp_get_payment_method( $order ); + if ( empty( $order ) || ! rp_is_order_paid_via_reepay( $order ) ) { + return; + } - if ( empty( $gateway ) ) { - return; - } + $gateway = rp_get_payment_method( $order ); - if ( ! empty( $order->get_meta( '_reepay_order' ) ) && 0 !== $order_data['parent_id'] ) { - $parent_order = wc_get_order( $order_data['parent_id'] ); - $subscription = $parent_order->get_meta( '_reepay_subscription_handle' ); - } elseif ( ! empty( $order->get_meta( '_reepay_subscription_handle_parent' ) ) ) { - $subscription = $order->get_meta( '_reepay_subscription_handle_parent' ); - } else { - $subscription = $order->get_meta( '_reepay_subscription_handle' ); - } + if ( empty( $gateway ) ) { + return; + } - add_meta_box( - 'reepay_checkout_customer', - __( 'Customer', 'reepay-checkout-gateway' ), - array( $this, 'generate_meta_box_content_customer' ), - $screen, - 'side', - 'high', - array( - 'order' => $order, - 'gateway' => $gateway, - ) - ); + if ( ! empty( $order->get_meta( '_reepay_order' ) ) && 0 !== $order_data['parent_id'] ) { + $parent_order = wc_get_order( $order_data['parent_id'] ); + $subscription = $parent_order->get_meta( '_reepay_subscription_handle' ); + } elseif ( ! empty( $order->get_meta( '_reepay_subscription_handle_parent' ) ) ) { + $subscription = $order->get_meta( '_reepay_subscription_handle_parent' ); + } else { + $subscription = $order->get_meta( '_reepay_subscription_handle' ); + } - if ( ! empty( $order->get_transaction_id() ) ) { add_meta_box( - 'reepay_checkout_invoice', - __( 'Invoice', 'reepay-checkout-gateway' ), - array( $this, 'generate_meta_box_content_invoice' ), + 'reepay_checkout_customer', + __( 'Customer', 'reepay-checkout-gateway' ), + array( $this, 'generate_meta_box_content_customer' ), $screen, 'side', 'high', @@ -94,21 +81,36 @@ public function add_meta_boxes() { 'gateway' => $gateway, ) ); - } - if ( ! empty( $subscription ) ) { - add_meta_box( - 'reepay_checkout_subscription', - __( 'Subscription', 'reepay-checkout-gateway' ), - array( $this, 'generate_meta_box_content_subscription' ), - $screen, - 'side', - 'high', - array( - 'order' => $order, - 'gateway' => $gateway, - ) - ); + if ( ! empty( $order->get_transaction_id() ) ) { + add_meta_box( + 'reepay_checkout_invoice', + __( 'Invoice', 'reepay-checkout-gateway' ), + array( $this, 'generate_meta_box_content_invoice' ), + $screen, + 'side', + 'high', + array( + 'order' => $order, + 'gateway' => $gateway, + ) + ); + } + + if ( ! empty( $subscription ) ) { + add_meta_box( + 'reepay_checkout_subscription', + __( 'Subscription', 'reepay-checkout-gateway' ), + array( $this, 'generate_meta_box_content_subscription' ), + $screen, + 'side', + 'high', + array( + 'order' => $order, + 'gateway' => $gateway, + ) + ); + } } } From a12e64e800189c34cf5fcd6ae4aad9d60410ae25 Mon Sep 17 00:00:00 2001 From: "dimaspolohov@yandex.ru" Date: Wed, 27 Dec 2023 22:39:40 +0300 Subject: [PATCH 15/15] Fix change status notify and settle --- templates/meta-boxes/invoice.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/templates/meta-boxes/invoice.php b/templates/meta-boxes/invoice.php index d2de0fc7..d5bd1b95 100644 --- a/templates/meta-boxes/invoice.php +++ b/templates/meta-boxes/invoice.php @@ -23,6 +23,10 @@ ?>
    + + + +