From 70f6f62d3cf87c0c9e6180dfdb36d9f8c605960f Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 12 Dec 2024 14:17:02 -0300 Subject: [PATCH 01/24] Order wrapper class proposal --- .../abstract-wc-stripe-payment-gateway.php | 12 ++--- includes/class-wc-stripe-helper.php | 42 +++++++++++++--- includes/class-wc-stripe-order-handler.php | 12 ++--- includes/class-wc-stripe-order.php | 48 +++++++++++++++++++ includes/class-wc-stripe-webhook-handler.php | 30 ++++++------ .../phpunit/helpers/class-wc-helper-order.php | 2 +- ...st-class-wc-stripe-upe-payment-gateway.php | 8 ++-- .../test-wc-stripe-webhook-handler.php | 2 +- 8 files changed, 116 insertions(+), 40 deletions(-) create mode 100644 includes/class-wc-stripe-order.php diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index 6663eb0b23..ff427970e9 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -536,7 +536,7 @@ public function process_response( $response, $order ) { $captured = ( isset( $response->captured ) && $response->captured ) ? 'yes' : 'no'; // Store charge data. - $order->update_meta_data( '_stripe_charge_captured', $captured ); + $order->set_charge_captured( $captured ); if ( isset( $response->balance_transaction ) ) { $this->update_fees( $order, is_string( $response->balance_transaction ) ? $response->balance_transaction : $response->balance_transaction->id ); @@ -1079,7 +1079,7 @@ public function update_fees( $order, $balance_transaction_id ) { * @throws Exception Throws exception when charge wasn't captured. */ public function process_refund( $order_id, $amount = null, $reason = '' ) { - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); if ( ! $order ) { return false; @@ -1088,7 +1088,7 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) { $request = []; $order_currency = $order->get_currency(); - $captured = $order->get_meta( '_stripe_charge_captured', true ); + $captured = $order->charge_captured(); $charge_id = $order->get_transaction_id(); if ( ! $charge_id ) { @@ -1100,7 +1100,7 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) { } // If order is only authorized, don't pass amount. - if ( 'yes' !== $captured ) { + if ( ! $captured ) { unset( $request['amount'] ); } @@ -1149,7 +1149,7 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) { } } - if ( ! $intent_cancelled && 'yes' === $captured ) { + if ( ! $intent_cancelled && $captured ) { $response = WC_Stripe_API::request( $request, 'refunds' ); } } catch ( WC_Stripe_Exception $e ) { @@ -1184,7 +1184,7 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) { } // If charge wasn't captured, skip creating a refund and cancel order. - if ( 'yes' !== $captured ) { + if ( ! $captured ) { /* translators: amount (including currency symbol) */ $order->add_order_note( sprintf( __( 'Pre-Authorization for %s voided.', 'woocommerce-gateway-stripe' ), $formatted_amount ) ); $order->update_status( 'cancelled' ); diff --git a/includes/class-wc-stripe-helper.php b/includes/class-wc-stripe-helper.php index 651cc8e92a..21a0dd7dcf 100644 --- a/includes/class-wc-stripe-helper.php +++ b/includes/class-wc-stripe-helper.php @@ -735,7 +735,7 @@ public static function filter_payment_methods_with_capabilities( $payment_method $payment_method_ids_with_capability = []; foreach ( $payment_method_ids as $payment_method_id ) { - $key = $payment_method_id . '_payments'; + $key = $payment_method_id . '_payments'; // Check if the payment method has capabilities set in the account data. // Generally the key is the payment method id appended with '_payments' (i.e. 'card_payments', 'sepa_debit_payments', 'klarna_payments'). // In some cases, the Stripe account might have the legacy key set. For example, for Klarna, the legacy key is 'klarna'. @@ -884,7 +884,7 @@ public static function get_order_by_source_id( $source_id ) { } if ( ! empty( $order_id ) ) { - return wc_get_order( $order_id ); + return self::get_order( $order_id ); } return false; @@ -917,7 +917,7 @@ public static function get_order_by_charge_id( $charge_id ) { } if ( ! empty( $order_id ) ) { - return wc_get_order( $order_id ); + return self::get_order( $order_id ); } return false; @@ -950,7 +950,7 @@ public static function get_order_by_refund_id( $refund_id ) { } if ( ! empty( $order_id ) ) { - return wc_get_order( $order_id ); + return self::get_order( $order_id ); } return false; @@ -984,7 +984,7 @@ public static function get_order_by_intent_id( $intent_id ) { } if ( ! empty( $order_id ) ) { - $order = wc_get_order( $order_id ); + $order = self::get_order( $order_id ); } if ( ! empty( $order ) && $order->get_status() !== 'trash' ) { @@ -1022,7 +1022,7 @@ public static function get_order_by_setup_intent_id( $intent_id ) { } if ( ! empty( $order_id ) ) { - return wc_get_order( $order_id ); + return self::get_order( $order_id ); } return false; @@ -1605,4 +1605,34 @@ public static function get_klarna_preferred_locale( $store_locale, $billing_coun return $target_locale; } + + /** + * Wrapper to return an order using the extension's custom WC_Stripe_Order class. + * + * @param $order_id int Order ID. + * @return bool|WC_Order + */ + public static function get_order( $order_id ) { + $order = wc_get_order( $order_id ); + if ( ! $order ) { + return false; + } + + return new WC_Stripe_Order( $order ); + } + + /** + * Wrapper to create an order using the extension's custom WC_Stripe_Order class. + * + * @param $order_data array Order data. + * @return bool|WC_Order + */ + public static function create_order( $order_data ) { + $order = wc_create_order( $order_data ); + if ( ! $order ) { + return false; + } + + return new WC_Stripe_Order( $order ); + } } diff --git a/includes/class-wc-stripe-order-handler.php b/includes/class-wc-stripe-order-handler.php index 6332d1b69a..8d074ab61f 100644 --- a/includes/class-wc-stripe-order-handler.php +++ b/includes/class-wc-stripe-order-handler.php @@ -248,11 +248,11 @@ private function maybe_process_legacy_redirect() { */ public function capture_payment( $order_id ) { $result = new stdClass(); - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); if ( WC_Stripe_Helper::payment_method_allows_manual_capture( $order->get_payment_method() ) ) { $charge = $order->get_transaction_id(); - $captured = $order->get_meta( '_stripe_charge_captured', true ); + $captured = $order->charge_captured(); $is_stripe_captured = false; if ( $charge && 'no' === $captured ) { @@ -325,7 +325,7 @@ public function capture_payment( $order_id ) { if ( $is_stripe_captured ) { /* translators: transaction id */ $order->add_order_note( sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $result->id ) ); - $order->update_meta_data( '_stripe_charge_captured', 'yes' ); + $order->set_charge_captured( 'yes' ); // Store other data such as fees $order->set_transaction_id( $result->id ); @@ -356,12 +356,10 @@ public function capture_payment( $order_id ) { * @param int $order_id */ public function cancel_payment( $order_id ) { - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); if ( WC_Stripe_Helper::payment_method_allows_manual_capture( $order->get_payment_method() ) ) { - $captured = $order->get_meta( '_stripe_charge_captured', true ); - - if ( 'no' === $captured ) { + if ( ! $order->charge_captured() ) { // To cancel a pre-auth, we need to refund the charge. $this->process_refund( $order_id ); } diff --git a/includes/class-wc-stripe-order.php b/includes/class-wc-stripe-order.php new file mode 100644 index 0000000000..aa399eef4c --- /dev/null +++ b/includes/class-wc-stripe-order.php @@ -0,0 +1,48 @@ +update_meta_data( '_stripe_charge_captured', $value ); + } + /** + * Whether the charge has been captured. + * + * @return bool + */ + public function charge_captured() { + return $this->get_meta( '_stripe_charge_captured' ) === 'yes'; + } + + /** + * Set the status final value. + * + * @param $value bool The value to set. + * @return void + */ + public function set_status_final( $value ) { + $this->update_meta_data( '_stripe_status_final', $value ); + } + + /** + * Whether the current order status is final. + * + * @return bool + */ + public function status_final() { + return (bool) $this->get_meta( '_stripe_status_final' ); + } +} diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index 667a6c7f6d..18944c935d 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -351,7 +351,7 @@ public function process_webhook_dispute( $notification ) { '' ); - if ( ! $order->get_meta( '_stripe_status_final', false ) ) { + if ( ! $order->status_final() ) { $order->update_status( 'on-hold', $message ); } else { $order->add_order_note( $message ); @@ -391,7 +391,7 @@ public function process_webhook_dispute_closed( $notification ) { if ( apply_filters( 'wc_stripe_webhook_dispute_change_order_status', true, $order, $notification ) ) { // Mark final so that order status is not overridden by out-of-sequence events. - $order->update_meta_data( '_stripe_status_final', true ); + $order->set_status_final( true ); // Fail order if dispute is lost, or else revert to pre-dispute status. $order_status = 'lost' === $status ? 'failed' : $this->get_stripe_order_status_before_hold( $order ); @@ -419,10 +419,10 @@ public function process_webhook_capture( $notification ) { if ( WC_Stripe_Helper::payment_method_allows_manual_capture( $order->get_payment_method() ) ) { $charge = $order->get_transaction_id(); - $captured = $order->get_meta( '_stripe_charge_captured', true ); + $captured = $order->charge_captured(); - if ( $charge && 'no' === $captured ) { - $order->update_meta_data( '_stripe_charge_captured', 'yes' ); + if ( $charge && ! $captured ) { + $order->set_charge_captured( 'yes' ); // Store other data such as fees $order->set_transaction_id( $notification->data->object->id ); @@ -542,7 +542,7 @@ public function process_webhook_charge_failed( $notification ) { } else { $message = __( 'This payment failed to clear.', 'woocommerce-gateway-stripe' ); } - if ( ! $order->get_meta( '_stripe_status_final', false ) ) { + if ( ! $order->status_final() ) { $order->update_status( 'failed', $message ); } else { $order->add_order_note( $message ); @@ -579,7 +579,7 @@ public function process_webhook_source_canceled( $notification ) { } $message = __( 'This payment was cancelled.', 'woocommerce-gateway-stripe' ); - if ( ! $order->has_status( 'cancelled' ) && ! $order->get_meta( '_stripe_status_final', false ) ) { + if ( ! $order->has_status( 'cancelled' ) && ! $order->status_final() ) { $order->update_status( 'cancelled', $message ); } else { $order->add_order_note( $message ); @@ -613,7 +613,7 @@ public function process_webhook_refund( $notification ) { if ( 'stripe' === substr( (string) $order->get_payment_method(), 0, 6 ) ) { $charge = $order->get_transaction_id(); - $captured = $order->get_meta( '_stripe_charge_captured' ); + $captured = $order->charge_captured(); $refund_id = $order->get_meta( '_stripe_refund_id' ); $currency = $order->get_currency(); $raw_amount = $refund_object->amount; @@ -625,7 +625,7 @@ public function process_webhook_refund( $notification ) { $amount = wc_price( $raw_amount, [ 'currency' => $currency ] ); // If charge wasn't captured, skip creating a refund. - if ( 'yes' !== $captured ) { + if ( ! $captured ) { // If the process was initiated from wp-admin, // the order was already cancelled, so we don't need a new note. if ( 'cancelled' !== $order->get_status() ) { @@ -771,7 +771,7 @@ public function process_review_opened( $notification ) { esc_html( $notification->data->object->reason ) ); - if ( apply_filters( 'wc_stripe_webhook_review_change_order_status', true, $order, $notification ) && ! $order->get_meta( '_stripe_status_final', false ) ) { + if ( apply_filters( 'wc_stripe_webhook_review_change_order_status', true, $order, $notification ) && ! $order->status_final() ) { $order->update_status( 'on-hold', $message ); } else { $order->add_order_note( $message ); @@ -809,12 +809,12 @@ public function process_review_closed( $notification ) { ( ! empty( $notification->data->object->closed_reason ) && 'approved' === $notification->data->object->closed_reason ) && $order->has_status( 'on-hold' ) && apply_filters( 'wc_stripe_webhook_review_change_order_status', true, $order, $notification ) && - ! $order->get_meta( '_stripe_status_final', false ) + ! $order->status_final() ) { $status_after_review = $this->get_stripe_order_status_before_hold( $order ); // If the review was approved, the charge has been captured and the status we stored before hold is an incomplete status, restore the status to processing/completed instead. - if ( 'yes' === $order->get_meta( '_stripe_charge_captured' ) && in_array( $status_after_review, apply_filters( 'woocommerce_valid_order_statuses_for_payment_complete', [ 'on-hold', 'pending', 'failed', 'cancelled' ], $order ) ) ) { + if ( $order->charge_captured() && in_array( $status_after_review, apply_filters( 'woocommerce_valid_order_statuses_for_payment_complete', [ 'on-hold', 'pending', 'failed', 'cancelled' ], $order ), true ) ) { $status_after_review = apply_filters( 'woocommerce_payment_complete_order_status', $order->needs_processing() ? 'processing' : 'completed', $order->get_id(), $order ); } @@ -982,7 +982,7 @@ public function process_payment_intent_success( $notification ) { $message = sprintf( __( 'Stripe SCA authentication failed. Reason: %s', 'woocommerce-gateway-stripe' ), $error_message ); $status_update = []; - if ( ! $order->get_meta( '_stripe_status_final', false ) ) { + if ( ! $order->status_final() ) { $status_update['from'] = $order->get_status(); $status_update['to'] = 'failed'; $order->update_status( 'failed', $message ); @@ -1036,7 +1036,7 @@ public function process_setup_intent( $notification ) { $message = sprintf( __( 'Stripe SCA authentication failed. Reason: %s', 'woocommerce-gateway-stripe' ), $error_message ); $status_update = []; - if ( ! $order->get_meta( '_stripe_status_final', false ) ) { + if ( ! $order->status_final() ) { $status_update['from'] = $order->get_status(); $status_update['to'] = 'failed'; $order->update_status( 'failed', $message ); @@ -1224,7 +1224,7 @@ private function get_order_from_intent( $intent ) { $data = explode( ':', $signature ); // Verify we received the order ID and signature (hash). - $order = isset( $data[0], $data[1] ) ? wc_get_order( absint( $data[0] ) ) : false; + $order = isset( $data[0], $data[1] ) ? WC_Stripe_Helper::get_order( absint( $data[0] ) ) : false; if ( $order ) { $intent_id = WC_Stripe_Helper::get_intent_id_from_order( $order ); diff --git a/tests/phpunit/helpers/class-wc-helper-order.php b/tests/phpunit/helpers/class-wc-helper-order.php index ffd9674392..c9cdada4de 100644 --- a/tests/phpunit/helpers/class-wc-helper-order.php +++ b/tests/phpunit/helpers/class-wc-helper-order.php @@ -60,7 +60,7 @@ public static function create_order( $customer_id = 1, $product = null, $order_p ]; $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; // Required, else wc_create_order throws an exception. - $order = wc_create_order( $order_data ); + $order = WC_Stripe_Helper::create_order( $order_data ); // Add order products. $item = new WC_Order_Item_Product(); diff --git a/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php b/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php index 2d84c3978c..508aebfcbc 100644 --- a/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php +++ b/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php @@ -1175,9 +1175,9 @@ public function test_process_response_updates_order_by_charge_status() { $charge_mock['captured'] = false; $charge_mock['id'] = 'ch_mock_1'; $this->mock_gateway->process_response( $this->array_to_object( $charge_mock ), wc_get_order( $order_id ) ); - $test_order = wc_get_order( $order_id ); + $test_order = WC_Stripe_Helper::get_order( $order_id ); - $this->assertEquals( 'no', $test_order->get_meta( '_stripe_charge_captured', true ) ); + $this->assertFalse( $test_order->charge_captured() ); $this->assertEquals( $charge_mock['id'], $test_order->get_transaction_id() ); $this->assertEquals( 'on-hold', $test_order->get_status() ); @@ -1187,7 +1187,7 @@ public function test_process_response_updates_order_by_charge_status() { $this->mock_gateway->process_response( $this->array_to_object( $charge_mock ), wc_get_order( $order_id ) ); $test_order = wc_get_order( $order_id ); - $this->assertEquals( 'yes', $test_order->get_meta( '_stripe_charge_captured', true ) ); + $this->assertTrue( $test_order->charge_captured() ); $this->assertEquals( 'processing', $test_order->get_status() ); // Test charge pending. @@ -1196,7 +1196,7 @@ public function test_process_response_updates_order_by_charge_status() { $this->mock_gateway->process_response( $this->array_to_object( $charge_mock ), wc_get_order( $order_id ) ); $test_order = wc_get_order( $order_id ); - $this->assertEquals( 'yes', $test_order->get_meta( '_stripe_charge_captured', true ) ); + $this->assertTrue( $test_order->charge_captured() ); $this->assertEquals( $charge_mock['id'], $test_order->get_transaction_id() ); $this->assertEquals( 'on-hold', $test_order->get_status() ); diff --git a/tests/phpunit/test-wc-stripe-webhook-handler.php b/tests/phpunit/test-wc-stripe-webhook-handler.php index 0ac99bf776..99cc70f03d 100644 --- a/tests/phpunit/test-wc-stripe-webhook-handler.php +++ b/tests/phpunit/test-wc-stripe-webhook-handler.php @@ -223,7 +223,7 @@ public function test_process_webhook_charge_failed( $order->set_status( $order_status ); $order->set_transaction_id( $charge_id ); if ( $order_status_final ) { - $order->update_meta_data( '_stripe_status_final', true ); + $order->set_status_final( true ); } $order->save(); From bf23a9158a5c7c5e18ba14db9aa5c2812da4e5b4 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 12 Dec 2024 14:55:25 -0300 Subject: [PATCH 02/24] Including more fields --- ...ract-wc-stripe-payment-gateway-voucher.php | 6 +- .../abstract-wc-stripe-payment-gateway.php | 19 +++-- ...class-wc-rest-stripe-orders-controller.php | 6 +- includes/admin/class-wc-stripe-privacy.php | 10 +-- includes/class-wc-stripe-helper.php | 19 ++--- .../class-wc-stripe-intent-controller.php | 6 +- includes/class-wc-stripe-order.php | 76 +++++++++++++++++++ includes/class-wc-stripe-webhook-handler.php | 4 +- .../compat/trait-wc-stripe-subscriptions.php | 8 +- .../class-wc-gateway-stripe-alipay.php | 2 +- .../class-wc-gateway-stripe-bancontact.php | 4 +- .../class-wc-gateway-stripe-boleto.php | 4 +- .../class-wc-gateway-stripe-eps.php | 2 +- .../class-wc-gateway-stripe-giropay.php | 4 +- .../class-wc-gateway-stripe-ideal.php | 2 +- .../class-wc-gateway-stripe-multibanco.php | 2 +- .../class-wc-gateway-stripe-oxxo.php | 2 +- .../class-wc-gateway-stripe-p24.php | 2 +- .../class-wc-gateway-stripe-sofort.php | 2 +- .../class-wc-stripe-upe-payment-gateway.php | 26 +++---- ...ss-wc-stripe-upe-payment-method-boleto.php | 4 +- ...c-stripe-upe-payment-method-multibanco.php | 4 +- ...lass-wc-stripe-upe-payment-method-oxxo.php | 4 +- ...class-wc-rest-stripe-orders-controller.php | 4 +- .../phpunit/helpers/class-wc-helper-order.php | 2 +- ...st-class-wc-stripe-upe-payment-gateway.php | 60 +++++++-------- tests/phpunit/test-wc-stripe-sub-initial.php | 2 +- tests/phpunit/test-wc-stripe-sub-renewal.php | 4 +- 28 files changed, 181 insertions(+), 109 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php b/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php index 8cf964bfaa..1766887b7d 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php @@ -253,7 +253,7 @@ public function init_form_fields() { */ public function process_payment( $order_id, $retry = true, $force_save_save = false ) { try { - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); if ( ! in_array( $order->get_billing_country(), $this->supported_countries ) ) { throw new \Exception( __( 'This payment method is not available in the selected country', 'woocommerce-gateway-stripe' ) ); @@ -261,7 +261,7 @@ public function process_payment( $order_id, $retry = true, $force_save_save = fa $intent = $this->create_or_update_payment_intent( $order ); - $order->update_meta_data( '_stripe_upe_payment_type', $this->stripe_id ); + $order->set_upe_payment_type( $this->stripe_id ); $order->update_status( 'pending', __( 'Awaiting payment.', 'woocommerce-gateway-stripe' ) ); $order->save(); @@ -383,7 +383,7 @@ public function update_payment_intent_ajax() { $intent = $this->create_or_update_payment_intent( $order ); $order->update_status( 'pending', __( 'Awaiting payment.', 'woocommerce-gateway-stripe' ) ); - $order->update_meta_data( '_stripe_upe_payment_type', $this->stripe_id ); + $order->set_upe_payment_type( $this->stripe_id ); $order->save(); wp_send_json( diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index ff427970e9..3e7ab9b5e2 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -392,8 +392,7 @@ public function get_transaction_url( $order ) { */ public function get_stripe_customer_id( $order ) { // Try to get it via the order first. - $customer = $order->get_meta( '_stripe_customer_id', true ); - + $customer = $order->get_stripe_customer_id(); if ( empty( $customer ) ) { $customer = get_user_option( '_stripe_customer_id', $order->get_customer_id() ); } @@ -951,14 +950,14 @@ public function prepare_order_source( $order = null ) { $stripe_customer->set_id( $stripe_customer_id ); } - $source_id = $order->get_meta( '_stripe_source_id', true ); + $source_id = $order->get_source_id(); // Since 4.0.0, we changed card to source so we need to account for that. if ( empty( $source_id ) ) { $source_id = $order->get_meta( '_stripe_card_id', true ); // Take this opportunity to update the key name. - $order->update_meta_data( '_stripe_source_id', $source_id ); + $order->set_source_id( $source_id ); if ( is_callable( [ $order, 'save' ] ) ) { $order->save(); @@ -1005,17 +1004,17 @@ public function check_source( $prepared_source ) { * * @since 3.1.0 * @version 4.0.0 - * @param WC_Order $order For to which the source applies. + * @param WC_Stripe_Order $order For to which the source applies. * @param stdClass $source Source information. */ public function save_source_to_order( $order, $source ) { // Store source in the order. if ( $source->customer ) { - $order->update_meta_data( '_stripe_customer_id', $source->customer ); + $order->set_stripe_customer_id( $source->customer ); } if ( $source->source ) { - $order->update_meta_data( '_stripe_source_id', $source->source ); + $order->set_source_id( $source->source ); } if ( is_callable( [ $order, 'save' ] ) ) { @@ -1619,11 +1618,11 @@ public function save_intent_to_order( $order, $intent ) { * Retrieves the payment intent, associated with an order. * * @since 4.2 - * @param WC_Order $order The order to retrieve an intent for. - * @return obect|bool Either the intent object or `false`. + * @param WC_Stripe_Order $order The order to retrieve an intent for. + * @return obect|bool Either the intent object or `false`. */ public function get_intent_from_order( $order ) { - $intent_id = $order->get_meta( '_stripe_intent_id' ); + $intent_id = $order->get_intent_id(); if ( $intent_id ) { return $this->get_intent( 'payment_intents', $intent_id ); diff --git a/includes/admin/class-wc-rest-stripe-orders-controller.php b/includes/admin/class-wc-rest-stripe-orders-controller.php index 7c1b45d384..20ee5c2b27 100644 --- a/includes/admin/class-wc-rest-stripe-orders-controller.php +++ b/includes/admin/class-wc-rest-stripe-orders-controller.php @@ -72,7 +72,7 @@ public function create_customer( $request ) { $order_id = $request['order_id']; // Ensure order exists. - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); if ( false === $order || ! ( $order instanceof WC_Order ) ) { return new WP_Error( 'wc_stripe', __( 'Order not found', 'woocommerce-gateway-stripe' ), [ 'status' => 404 ] ); } @@ -91,7 +91,7 @@ public function create_customer( $request ) { $customer = new WC_Stripe_Customer( $order_user->ID ); // Set the customer ID if known but not already set. - $customer_id = $order->get_meta( '_stripe_customer_id', true ); + $customer_id = $order->get_stripe_customer_id(); if ( ! $customer->get_id() && $customer_id ) { $customer->set_id( $customer_id ); } @@ -108,7 +108,7 @@ public function create_customer( $request ) { return new WP_Error( 'stripe_error', $e->getMessage() ); } - $order->update_meta_data( '_stripe_customer_id', $customer_id ); + $order->set_stripe_customer_id( $customer_id ); $order->save(); return rest_ensure_response( [ 'id' => $customer_id ] ); diff --git a/includes/admin/class-wc-stripe-privacy.php b/includes/admin/class-wc-stripe-privacy.php index de01186b96..b4c0e7711f 100644 --- a/includes/admin/class-wc-stripe-privacy.php +++ b/includes/admin/class-wc-stripe-privacy.php @@ -133,11 +133,11 @@ public function order_data_exporter( $email_address, $page = 1 ) { 'data' => [ [ 'name' => __( 'Stripe payment id', 'woocommerce-gateway-stripe' ), - 'value' => $order->get_meta( '_stripe_source_id', true ), + 'value' => $order->get_source_id(), ], [ 'name' => __( 'Stripe customer id', 'woocommerce-gateway-stripe' ), - 'value' => $order->get_meta( '_stripe_customer_id', true ), + 'value' => $order->get_stripe_customer_id(), ], ], ]; @@ -384,13 +384,13 @@ protected function maybe_handle_subscription( $order ) { /** * Handle eraser of data tied to Orders * - * @param WC_Order $order + * @param WC_Stripe_Order $order * @return array */ protected function maybe_handle_order( $order ) { - $stripe_source_id = $order->get_meta( '_stripe_source_id', true ); + $stripe_source_id = $order->get_source_id(); $stripe_refund_id = $order->get_meta( '_stripe_refund_id', true ); - $stripe_customer_id = $order->get_meta( '_stripe_customer_id', true ); + $stripe_customer_id = $order->get_stripe_customer_id(); if ( ! $this->is_retention_expired( $order->get_date_created()->getTimestamp() ) ) { /* translators: %d Order ID */ diff --git a/includes/class-wc-stripe-helper.php b/includes/class-wc-stripe-helper.php index 21a0dd7dcf..04b4647d2a 100644 --- a/includes/class-wc-stripe-helper.php +++ b/includes/class-wc-stripe-helper.php @@ -1260,12 +1260,10 @@ private static function should_load_scripts_for_prb_location( $location ) { * Adds payment intent id and order note to order if payment intent is not already saved * * @param $payment_intent_id - * @param $order + * @param $order WC_Stripe_Order */ public static function add_payment_intent_to_order( $payment_intent_id, $order ) { - - $old_intent_id = $order->get_meta( '_stripe_intent_id' ); - + $old_intent_id = $order->get_intent_id(); if ( $old_intent_id === $payment_intent_id ) { return; } @@ -1278,7 +1276,7 @@ public static function add_payment_intent_to_order( $payment_intent_id, $order ) ) ); - $order->update_meta_data( '_stripe_intent_id', $payment_intent_id ); + $order->set_intent_id( $payment_intent_id ); $order->save(); } @@ -1373,13 +1371,12 @@ public static function get_payment_method_from_intent( $intent ) { /** * Returns the payment intent or setup intent ID from a given order object. * - * @param WC_Order $order The order to fetch the Stripe intent from. + * @param WC_Stripe_Order $order The order to fetch the Stripe intent from. * * @return string|bool The intent ID if found, false otherwise. */ public static function get_intent_id_from_order( $order ) { - $intent_id = $order->get_meta( '_stripe_intent_id' ); - + $intent_id = $order->get_intent_id(); if ( ! $intent_id ) { $intent_id = $order->get_meta( '_stripe_setup_intent' ); } @@ -1494,14 +1491,14 @@ public static function payment_method_allows_manual_capture( string $payment_met /** * Verifies if the provided order contains the identifier for a wallet method. * - * @param WC_Order $order The order. + * @param WC_Stripe_Order $order The order. * @return bool * * @deprecated 8.9.0 */ public static function is_wallet_payment_method( $order ) { wc_deprecated_function( __METHOD__, '8.9.0', 'in_array( $order->get_meta( \'_stripe_upe_payment_type\' ), WC_Stripe_Payment_Methods::WALLET_PAYMENT_METHODS, true )' ); - return in_array( $order->get_meta( '_stripe_upe_payment_type' ), WC_Stripe_Payment_Methods::WALLET_PAYMENT_METHODS, true ); + return in_array( $order->get_upe_payment_type(), WC_Stripe_Payment_Methods::WALLET_PAYMENT_METHODS, true ); } /** @@ -1625,7 +1622,7 @@ public static function get_order( $order_id ) { * Wrapper to create an order using the extension's custom WC_Stripe_Order class. * * @param $order_data array Order data. - * @return bool|WC_Order + * @return bool|WC_Stripe_Order */ public static function create_order( $order_data ) { $order = wc_create_order( $order_data ); diff --git a/includes/class-wc-stripe-intent-controller.php b/includes/class-wc-stripe-intent-controller.php index 262d38a999..d685742515 100644 --- a/includes/class-wc-stripe-intent-controller.php +++ b/includes/class-wc-stripe-intent-controller.php @@ -418,7 +418,7 @@ public function update_payment_intent_ajax() { * @return array|null An array with result of the update, or nothing */ public function update_payment_intent( $payment_intent_id = '', $order_id = null, $save_payment_method = false, $selected_upe_payment_type = '' ) { - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); if ( ! is_a( $order, 'WC_Order' ) ) { return; @@ -455,7 +455,7 @@ public function update_payment_intent( $payment_intent_id = '', $order_id = null WC_Stripe_UPE_Payment_Method_Link::STRIPE_ID, ]; } - $order->update_meta_data( '_stripe_upe_payment_type', $selected_upe_payment_type ); + $order->set_upe_payment_type( $selected_upe_payment_type ); } if ( ! empty( $customer ) && $customer->get_id() ) { $request['customer'] = $customer->get_id(); @@ -770,7 +770,7 @@ public function create_and_confirm_payment_intent( $payment_information ) { // Only update the payment_type if we have a reference to the payment type the customer selected. if ( '' !== $selected_payment_type ) { - $order->update_meta_data( '_stripe_upe_payment_type', $selected_payment_type ); + $order->set_upe_payment_type( $selected_payment_type ); } return $payment_intent; diff --git a/includes/class-wc-stripe-order.php b/includes/class-wc-stripe-order.php index aa399eef4c..8bd86163da 100644 --- a/includes/class-wc-stripe-order.php +++ b/includes/class-wc-stripe-order.php @@ -9,6 +9,82 @@ * Wrapper for the original WC_Order class to allow custom getters and setter with the extension's specific metadata. */ class WC_Stripe_Order extends WC_Order { + /** + * Set the Stripe intent ID. + * + * @param $intent_id string The intent ID to set. + * @return void + */ + public function set_intent_id( $intent_id ) { + $this->update_meta_data( '_stripe_intent_id', $intent_id ); + } + + /** + * Get the Stripe intent ID. + * + * @return string + */ + public function get_intent_id() { + return $this->get_meta( '_stripe_intent_id' ); + } + + /** + * Set the UPE payment type. + * + * @param $payment_type string The payment type to set. + * @return void + */ + public function set_upe_payment_type( $payment_type ) { + $this->update_meta_data( '_stripe_upe_payment_type', $payment_type ); + } + + /** + * Get the UPE payment type. + * + * @return string + */ + public function get_upe_payment_type() { + return $this->get_meta( '_stripe_upe_payment_type' ); + } + + /** + * Set the Stripe source ID. + * + * @param $source_id string The Stripe source ID. + * @return void + */ + public function set_source_id( $source_id ) { + $this->update_meta_data( '_stripe_source_id', $source_id ); + } + + /** + * Get the Stripe source ID. + * + * @return string + */ + public function get_source_id() { + return $this->get_meta( '_stripe_source_id' ); + } + + /** + * Set the Stripe customer ID. + * + * @param $customer_id string The Stripe customer ID. + * @return void + */ + public function set_stripe_customer_id( $customer_id ) { + $this->update_meta_data( '_stripe_customer_id', $customer_id ); + } + + /** + * Get the Stripe customer ID. + * + * @return string + */ + public function get_stripe_customer_id() { + return $this->get_meta( '_stripe_customer_id' ); + } + /** * Set the charge captured flag. * diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index 18944c935d..6a5d12909f 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -926,7 +926,7 @@ public function process_payment_intent_success( $notification ) { } $order_id = $order->get_id(); - $payment_type_meta = $order->get_meta( '_stripe_upe_payment_type' ); + $payment_type_meta = $order->get_upe_payment_type(); $is_voucher_payment = in_array( $payment_type_meta, WC_Stripe_Payment_Methods::VOUCHER_PAYMENT_METHODS, true ); $is_wallet_payment = in_array( $payment_type_meta, WC_Stripe_Payment_Methods::WALLET_PAYMENT_METHODS, true ); @@ -1215,7 +1215,7 @@ public function process_webhook( $request_body ) { * Fetches an order from a payment intent. * * @param stdClass $intent The Stripe PaymentIntent object. - * @return WC_Order|false The order object, or false if not found. + * @return WC_Stripe_Order|false The order object, or false if not found. */ private function get_order_from_intent( $intent ) { // Attempt to get the order from the intent metadata. diff --git a/includes/compat/trait-wc-stripe-subscriptions.php b/includes/compat/trait-wc-stripe-subscriptions.php index 91bc3bc54d..52ae594db6 100644 --- a/includes/compat/trait-wc-stripe-subscriptions.php +++ b/includes/compat/trait-wc-stripe-subscriptions.php @@ -646,7 +646,7 @@ public function delete_renewal_meta( $renewal_order ) { */ public function update_failing_payment_method( $subscription, $renewal_order ) { $subscription->update_meta_data( '_stripe_customer_id', $renewal_order->get_meta( '_stripe_customer_id', true ) ); - $subscription->update_meta_data( '_stripe_source_id', $renewal_order->get_meta( '_stripe_source_id', true ) ); + $subscription->update_meta_data( '_stripe_source_id', $renewal_order->get_source_id() ); $subscription->save(); } @@ -798,7 +798,7 @@ private function get_mandate_for_subscription( $order, $payment_method ) { } $mandate = $renewal_order->get_meta( '_stripe_mandate_id', true ); - $renewal_order_payment_method = $renewal_order->get_meta( '_stripe_source_id', true ); + $renewal_order_payment_method = $renewal_order->get_source_id(); // Return from the most recent renewal order with a valid mandate. Mandate is created against a payment method // in Stripe so the payment method should also match to reuse the mandate. @@ -938,14 +938,14 @@ public function maybe_render_subscription_payment_method( $payment_method_to_dis if ( ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) && false !== $subscription->get_parent() ) { $parent_order = wc_get_order( $subscription->get_parent_id() ); $stripe_customer_id = $parent_order->get_meta( '_stripe_customer_id', true ); - $stripe_source_id = $parent_order->get_meta( '_stripe_source_id', true ); + $stripe_source_id = $parent_order->get_source_id(); // For BW compat will remove in future. if ( empty( $stripe_source_id ) ) { $stripe_source_id = $parent_order->get_meta( '_stripe_card_id', true ); // Take this opportunity to update the key name. - $parent_order->update_meta_data( '_stripe_source_id', $stripe_source_id ); + $parent_order->set_source_id( $stripe_source_id ); $parent_order->save(); } } diff --git a/includes/payment-methods/class-wc-gateway-stripe-alipay.php b/includes/payment-methods/class-wc-gateway-stripe-alipay.php index f7a9bce217..ca497b1997 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-alipay.php +++ b/includes/payment-methods/class-wc-gateway-stripe-alipay.php @@ -274,7 +274,7 @@ public function process_payment( $order_id, $retry = true, $force_save_save = fa throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message ); } - $order->update_meta_data( '_stripe_source_id', $response->id ); + $order->set_source_id( $response->id ); $order->save(); WC_Stripe_Logger::log( 'Info: Redirecting to Alipay...' ); diff --git a/includes/payment-methods/class-wc-gateway-stripe-bancontact.php b/includes/payment-methods/class-wc-gateway-stripe-bancontact.php index ea35f75278..206a13d5f2 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-bancontact.php +++ b/includes/payment-methods/class-wc-gateway-stripe-bancontact.php @@ -240,7 +240,7 @@ public function create_source( $order ) { */ public function process_payment( $order_id, $retry = true, $force_save_source = false ) { try { - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); // This will throw exception if not valid. $this->validate_minimum_order_amount( $order ); @@ -262,7 +262,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message ); } - $order->update_meta_data( '_stripe_source_id', $response->id ); + $order->set_source_id( $response->id ); $order->save(); WC_Stripe_Logger::log( 'Info: Redirecting to Bancontact...' ); diff --git a/includes/payment-methods/class-wc-gateway-stripe-boleto.php b/includes/payment-methods/class-wc-gateway-stripe-boleto.php index b14b9c378b..b270f7d312 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-boleto.php +++ b/includes/payment-methods/class-wc-gateway-stripe-boleto.php @@ -105,12 +105,12 @@ public function update_unique_settings( WP_REST_Request $request ) { * Adds on-hold as accepted status during webhook handling on orders paid with voucher * * @param $allowed_statuses - * @param $order + * @param $order WC_Stripe_Order * * @return mixed */ public function add_allowed_payment_processing_statuses( $allowed_statuses, $order ) { - if ( $this->stripe_id === $order->get_meta( '_stripe_upe_payment_type' ) && ! in_array( 'on-hold', $allowed_statuses ) ) { + if ( $this->stripe_id === $order->get_upe_payment_type() && ! in_array( 'on-hold', $allowed_statuses, true ) ) { $allowed_statuses[] = 'on-hold'; } diff --git a/includes/payment-methods/class-wc-gateway-stripe-eps.php b/includes/payment-methods/class-wc-gateway-stripe-eps.php index b11a5cded9..a27976b2ef 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-eps.php +++ b/includes/payment-methods/class-wc-gateway-stripe-eps.php @@ -261,7 +261,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = throw new Exception( $response->error->message ); } - $order->update_meta_data( '_stripe_source_id', $response->id ); + $order->set_source_id( $response->id ); $order->save(); WC_Stripe_Logger::log( 'Info: Redirecting to EPS...' ); diff --git a/includes/payment-methods/class-wc-gateway-stripe-giropay.php b/includes/payment-methods/class-wc-gateway-stripe-giropay.php index 2e0dd24c8d..76923fa792 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-giropay.php +++ b/includes/payment-methods/class-wc-gateway-stripe-giropay.php @@ -235,7 +235,7 @@ public function create_source( $order ) { */ public function process_payment( $order_id, $retry = true, $force_save_source = false ) { try { - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); // This will throw exception if not valid. $this->validate_minimum_order_amount( $order ); @@ -257,7 +257,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message ); } - $order->update_meta_data( '_stripe_source_id', $response->id ); + $order->set_source_id( $response->id ); $order->save(); WC_Stripe_Logger::log( 'Info: Redirecting to giropay...' ); diff --git a/includes/payment-methods/class-wc-gateway-stripe-ideal.php b/includes/payment-methods/class-wc-gateway-stripe-ideal.php index 791e9527b9..092d791c81 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-ideal.php +++ b/includes/payment-methods/class-wc-gateway-stripe-ideal.php @@ -261,7 +261,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message ); } - $order->update_meta_data( '_stripe_source_id', $response->id ); + $order->set_source_id( $response->id ); $order->save(); WC_Stripe_Logger::log( 'Info: Redirecting to iDEAL...' ); diff --git a/includes/payment-methods/class-wc-gateway-stripe-multibanco.php b/includes/payment-methods/class-wc-gateway-stripe-multibanco.php index 14a499167e..92dc286444 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-multibanco.php +++ b/includes/payment-methods/class-wc-gateway-stripe-multibanco.php @@ -361,7 +361,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = throw new Exception( $response->error->message ); } - $order->update_meta_data( '_stripe_source_id', $response->id ); + $order->set_source_id( $response->id ); $order->save(); $this->save_instructions( $order, $response ); diff --git a/includes/payment-methods/class-wc-gateway-stripe-oxxo.php b/includes/payment-methods/class-wc-gateway-stripe-oxxo.php index 74f4b2edbd..2a2ed792c4 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-oxxo.php +++ b/includes/payment-methods/class-wc-gateway-stripe-oxxo.php @@ -64,7 +64,7 @@ public function __construct() { * @return mixed */ public function add_allowed_payment_processing_statuses( $allowed_statuses, $order ) { - if ( $this->stripe_id === $order->get_meta( '_stripe_upe_payment_type' ) && ! in_array( 'on-hold', $allowed_statuses ) ) { + if ( $this->stripe_id === $order->get_upe_payment_type() && ! in_array( 'on-hold', $allowed_statuses, true ) ) { $allowed_statuses[] = 'on-hold'; } diff --git a/includes/payment-methods/class-wc-gateway-stripe-p24.php b/includes/payment-methods/class-wc-gateway-stripe-p24.php index c637361478..0558c3072c 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-p24.php +++ b/includes/payment-methods/class-wc-gateway-stripe-p24.php @@ -258,7 +258,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message ); } - $order->update_meta_data( '_stripe_source_id', $response->id ); + $order->set_source_id( $response->id ); $order->save(); WC_Stripe_Logger::log( 'Info: Redirecting to P24...' ); diff --git a/includes/payment-methods/class-wc-gateway-stripe-sofort.php b/includes/payment-methods/class-wc-gateway-stripe-sofort.php index ee721cc7bb..05e5d4e21f 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-sofort.php +++ b/includes/payment-methods/class-wc-gateway-stripe-sofort.php @@ -274,7 +274,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message ); } - $order->update_meta_data( '_stripe_source_id', $response->id ); + $order->set_source_id( $response->id ); $order->save(); WC_Stripe_Logger::log( 'Info: Redirecting to Sofort...' ); diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index 2ff4851545..82acd73bcd 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -248,12 +248,12 @@ public function set_cookie_on_current_request( $cookie ) { /** * Hides refund through stripe when payment method does not allow refund * - * @param WC_Order $order + * @param WC_Stripe_Order $order * * @return array|bool */ public function can_refund_order( $order ) { - $upe_payment_type = $order->get_meta( '_stripe_upe_payment_type' ); + $upe_payment_type = $order->get_upe_payment_type(); if ( ! $upe_payment_type ) { return true; @@ -649,7 +649,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = } $payment_intent_id = isset( $_POST['wc_payment_intent_id'] ) ? wc_clean( wp_unslash( $_POST['wc_payment_intent_id'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); $payment_needed = $this->is_payment_needed( $order_id ); $save_payment_method = $this->has_subscription( $order_id ) || ! empty( $_POST[ 'wc-' . self::ID . '-new-payment-method' ] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing $selected_upe_payment_type = ! empty( $_POST['wc_stripe_selected_upe_payment_type'] ) ? wc_clean( wp_unslash( $_POST['wc_stripe_selected_upe_payment_type'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing @@ -725,7 +725,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = WC_Stripe_Helper::add_payment_intent_to_order( $payment_intent_id, $order ); $order->update_status( 'pending', __( 'Awaiting payment.', 'woocommerce-gateway-stripe' ) ); - $order->update_meta_data( '_stripe_upe_payment_type', $selected_upe_payment_type ); + $order->set_upe_payment_type( $selected_upe_payment_type ); // TODO: This is a stop-gap to fix a critical issue, see // https://github.com/woocommerce/woocommerce-gateway-stripe/issues/2536. It would @@ -1390,15 +1390,15 @@ public function prepare_payment_method( $payment_method ) { /** * Save payment method to order. * - * @param WC_Order $order For to which the source applies. + * @param WC_Stripe_Order $order For to which the source applies. * @param stdClass $payment_method Stripe Payment Method. */ public function save_payment_method_to_order( $order, $payment_method ) { if ( $payment_method->customer ) { - $order->update_meta_data( '_stripe_customer_id', $payment_method->customer ); + $order->set_stripe_customer_id( $payment_method->customer ); } // Save the payment method id as `source_id`, because we use both `sources` and `payment_methods` APIs. - $order->update_meta_data( '_stripe_source_id', $payment_method->payment_method ); + $order->set_source_id( $payment_method->payment_method ); if ( is_callable( [ $order, 'save' ] ) ) { $order->save(); @@ -2220,12 +2220,12 @@ protected function handle_saving_payment_method( WC_Order $order, $payment_metho /** * Set the payment metadata for payment method id. * - * @param WC_Order $order The order. + * @param WC_Stripe_Order $order The order. * @param string $payment_method_id The value to be set. */ public function set_payment_method_id_for_order( WC_Order $order, string $payment_method_id ) { // Save the payment method id as `source_id`, because we use both `sources` and `payment_methods` APIs. - $order->update_meta_data( '_stripe_source_id', $payment_method_id ); + $order->set_source_id( $payment_method_id ); $order->save_meta_data(); } @@ -2234,22 +2234,22 @@ public function set_payment_method_id_for_order( WC_Order $order, string $paymen * * Set to public so it can be called from confirm_change_payment_from_setup_intent_ajax() * - * @param WC_Order $order The order. + * @param WC_Stripe_Order $order The order. * @param string $customer_id The value to be set. */ public function set_customer_id_for_order( WC_Order $order, string $customer_id ) { - $order->update_meta_data( '_stripe_customer_id', $customer_id ); + $order->set_stripe_customer_id( $customer_id ); $order->save_meta_data(); } /** * Set the payment metadata for the selected payment type. * - * @param WC_Order $order The order for which we're setting the selected payment type. + * @param WC_Stripe_Order $order The order for which we're setting the selected payment type. * @param string $selected_payment_type The selected payment type. */ private function set_selected_payment_type_for_order( WC_Order $order, string $selected_payment_type ) { - $order->update_meta_data( '_stripe_upe_payment_type', $selected_payment_type ); + $order->set_upe_payment_type( $selected_payment_type ); $order->save_meta_data(); } /** diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-method-boleto.php b/includes/payment-methods/class-wc-stripe-upe-payment-method-boleto.php index 60bcf5d4ad..d3ec64816d 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-method-boleto.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-method-boleto.php @@ -39,12 +39,12 @@ public function __construct() { * Adds on-hold as accepted status during webhook handling on orders paid with Boleto * * @param $allowed_statuses - * @param $order + * @param $order WC_Stripe_Order * * @return mixed */ public function add_allowed_payment_processing_statuses( $allowed_statuses, $order ) { - if ( WC_Stripe_Payment_Methods::BOLETO === $order->get_meta( '_stripe_upe_payment_type' ) && ! in_array( 'on-hold', $allowed_statuses ) ) { + if ( WC_Stripe_Payment_Methods::BOLETO === $order->get_upe_payment_type() && ! in_array( 'on-hold', $allowed_statuses, true ) ) { $allowed_statuses[] = 'on-hold'; } diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-method-multibanco.php b/includes/payment-methods/class-wc-stripe-upe-payment-method-multibanco.php index eab0b68a72..8b8a6309c7 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-method-multibanco.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-method-multibanco.php @@ -35,12 +35,12 @@ public function __construct() { * Adds on-hold as accepted status during webhook handling on orders paid with Mukltibanco * * @param $allowed_statuses - * @param $order + * @param $order WC_Stripe_Order * * @return mixed */ public function add_allowed_payment_processing_statuses( $allowed_statuses, $order ) { - if ( WC_Stripe_Payment_Methods::MULTIBANCO === $order->get_meta( '_stripe_upe_payment_type' ) && ! in_array( 'on-hold', $allowed_statuses, true ) ) { + if ( WC_Stripe_Payment_Methods::MULTIBANCO === $order->get_upe_payment_type() && ! in_array( 'on-hold', $allowed_statuses, true ) ) { $allowed_statuses[] = 'on-hold'; } diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-method-oxxo.php b/includes/payment-methods/class-wc-stripe-upe-payment-method-oxxo.php index 48681bdd2a..71cff0a725 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-method-oxxo.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-method-oxxo.php @@ -39,12 +39,12 @@ public function __construct() { * Adds on-hold as accepted status during webhook handling on orders paid with OXXO * * @param $allowed_statuses - * @param $order + * @param $order WC_Stripe_Order * * @return mixed */ public function add_allowed_payment_processing_statuses( $allowed_statuses, $order ) { - if ( WC_Stripe_Payment_Methods::OXXO === $order->get_meta( '_stripe_upe_payment_type' ) && ! in_array( 'on-hold', $allowed_statuses ) ) { + if ( WC_Stripe_Payment_Methods::OXXO === $order->get_upe_payment_type() && ! in_array( 'on-hold', $allowed_statuses, true ) ) { $allowed_statuses[] = 'on-hold'; } diff --git a/tests/phpunit/admin/test-class-wc-rest-stripe-orders-controller.php b/tests/phpunit/admin/test-class-wc-rest-stripe-orders-controller.php index 89d9d92753..05930e8186 100644 --- a/tests/phpunit/admin/test-class-wc-rest-stripe-orders-controller.php +++ b/tests/phpunit/admin/test-class-wc-rest-stripe-orders-controller.php @@ -75,7 +75,7 @@ public function test_create_customer_with_existing_id() { $endpoint = '/' . strval( $order->get_id() ) . '/create_customer'; $order->add_meta_data( '_stripe_customer_id', 'cus_12345', true ); $order->save(); - $this->assertEquals( 'cus_12345', $order->get_meta( '_stripe_customer_id', true ) ); + $this->assertEquals( 'cus_12345', $order->get_stripe_customer_id() ); // Mock response from Stripe API using request arguments. $test_request = function ( $preempt, $parsed_args, $url ) { @@ -143,7 +143,7 @@ public function test_capture_payment_success() { $this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 'succeeded', $response->get_data()['status'] ); $this->assertEquals( 'ch_12345', $response->get_data()['id'] ); - $this->assertEquals( 'pi_12345', $order->get_meta( '_stripe_intent_id', true ) ); + $this->assertEquals( 'pi_12345', $order->get_intent_id() ); remove_filter( 'pre_http_request', $test_request, 10, 3 ); } diff --git a/tests/phpunit/helpers/class-wc-helper-order.php b/tests/phpunit/helpers/class-wc-helper-order.php index c9cdada4de..edab7a6bc2 100644 --- a/tests/phpunit/helpers/class-wc-helper-order.php +++ b/tests/phpunit/helpers/class-wc-helper-order.php @@ -42,7 +42,7 @@ public static function delete_order( $order_id ) { * @param WC_Product $product The product to add to the order. * @param array $order_props Order properties. * - * @return WC_Order + * @return WC_Stripe_Order */ public static function create_order( $customer_id = 1, $product = null, $order_props = [] ) { diff --git a/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php b/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php index 508aebfcbc..8e53aca76b 100644 --- a/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php +++ b/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php @@ -312,7 +312,7 @@ public function test_process_payment_returns_valid_response() { $order_id = $order->get_id(); $order->update_meta_data( '_stripe_intent_id', $payment_intent_id ); - $order->update_meta_data( '_stripe_upe_payment_type', '' ); + $order->set_upe_payment_type( '' ); $order->update_meta_data( '_stripe_upe_waiting_for_redirect', true ); $order->save(); @@ -786,7 +786,7 @@ public function test_process_redirect_payment_returns_valid_response() { $this->assertEquals( 'processing', $final_order->get_status() ); $this->assertEquals( 'Credit / Debit Card', $final_order->get_payment_method_title() ); - $this->assertEquals( $payment_intent_id, $final_order->get_meta( '_stripe_intent_id', true ) ); + $this->assertEquals( $payment_intent_id, $final_order->get_intent_id() ); $this->assertTrue( (bool) $final_order->get_meta( '_stripe_upe_redirect_processed', true ) ); $this->assertMatchesRegularExpression( '/Charge ID: ch_mock/', $note->content ); } @@ -851,7 +851,7 @@ public function test_process_redirect_payment_only_runs_once() { // assert successful order processing $this->assertEquals( 'processing', $success_order->get_status() ); $this->assertEquals( 'Credit / Debit Card', $success_order->get_payment_method_title() ); - $this->assertEquals( $payment_intent_id, $success_order->get_meta( '_stripe_intent_id', true ) ); + $this->assertEquals( $payment_intent_id, $success_order->get_intent_id() ); $this->assertTrue( (bool) $success_order->get_meta( '_stripe_upe_redirect_processed', true ) ); $this->assertMatchesRegularExpression( '/Charge ID: ch_mock/', $note->content ); @@ -911,8 +911,8 @@ public function test_checkout_without_payment_uses_setup_intents() { $final_order = wc_get_order( $order_id ); $this->assertEquals( 'processing', $final_order->get_status() ); - $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); - $this->assertEquals( $payment_method_id, $final_order->get_meta( '_stripe_source_id', true ) ); + $this->assertEquals( $customer_id, $final_order->get_customer_id() ); + $this->assertEquals( $payment_method_id, $final_order->get_source_id() ); $this->assertEquals( 'Credit / Debit Card', $final_order->get_payment_method_title() ); } @@ -973,9 +973,9 @@ public function test_checkout_saves_payment_method_to_order() { $final_order = wc_get_order( $order_id ); $this->assertEquals( 'processing', $final_order->get_status() ); - $this->assertEquals( $payment_intent_id, $final_order->get_meta( '_stripe_intent_id', true ) ); - $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); - $this->assertEquals( $payment_method_id, $final_order->get_meta( '_stripe_source_id', true ) ); + $this->assertEquals( $payment_intent_id, $final_order->get_intent_id() ); + $this->assertEquals( $customer_id, $final_order->get_stripe_customer_id() ); + $this->assertEquals( $payment_method_id, $final_order->get_source_id() ); } /** @@ -1041,9 +1041,9 @@ public function test_checkout_saves_sepa_generated_payment_method_to_order() { $final_order = wc_get_order( $order_id ); $this->assertEquals( 'processing', $final_order->get_status() ); - $this->assertEquals( $payment_intent_id, $final_order->get_meta( '_stripe_intent_id', true ) ); - $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); - $this->assertEquals( $generated_payment_method_id, $final_order->get_meta( '_stripe_source_id', true ) ); + $this->assertEquals( $payment_intent_id, $final_order->get_intent_id() ); + $this->assertEquals( $customer_id, $final_order->get_stripe_customer_id() ); + $this->assertEquals( $generated_payment_method_id, $final_order->get_source_id() ); } /** @@ -1101,7 +1101,7 @@ public function test_setup_intent_checkout_saves_sepa_generated_payment_method_t $this->assertEquals( 'processing', $final_order->get_status() ); $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); - $this->assertEquals( $generated_payment_method_id, $final_order->get_meta( '_stripe_source_id', true ) ); + $this->assertEquals( $generated_payment_method_id, $final_order->get_source_id() ); } /** @@ -1306,9 +1306,9 @@ function( $passed_order ) use ( $order ) { $this->assertEquals( 'success', $response['result'] ); $this->assertEquals( 'processing', $final_order->get_status() ); - $this->assertEquals( $payment_intent_id, $final_order->get_meta( '_stripe_intent_id', true ) ); - $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); - $this->assertEquals( $payment_method_id, $final_order->get_meta( '_stripe_source_id', true ) ); + $this->assertEquals( $payment_intent_id, $final_order->get_intent_id() ); + $this->assertEquals( $customer_id, $final_order->get_stripe_customer_id() ); + $this->assertEquals( $payment_method_id, $final_order->get_source_id() ); $this->assertMatchesRegularExpression( '/Charge ID: ch_mock/', $note->content ); } @@ -1389,9 +1389,9 @@ function( $passed_order ) use ( $order ) { $this->assertEquals( 'success', $response['result'] ); $this->assertEquals( 'pending', $final_order->get_status() ); // Order status should be pending until 3DS is completed. - $this->assertEquals( $payment_intent_id, $final_order->get_meta( '_stripe_intent_id', true ) ); - $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); - $this->assertEquals( $payment_method_id, $final_order->get_meta( '_stripe_source_id', true ) ); + $this->assertEquals( $payment_intent_id, $final_order->get_intent_id() ); + $this->assertEquals( $customer_id, $final_order->get_stripe_customer_id() ); + $this->assertEquals( $payment_method_id, $final_order->get_source_id() ); $this->assertMatchesRegularExpression( "/#wc-stripe-confirm-pi:$order_id:$client_secret/", $response['redirect'] ); } @@ -1451,8 +1451,8 @@ function( $passed_order ) use ( $order ) { $this->assertEquals( 'failure', $response['result'] ); $this->assertEquals( 'failed', $final_order->get_status() ); - $this->assertEquals( $payment_intent_id, $final_order->get_meta( '_stripe_intent_id', true ) ); - $this->assertEquals( $payment_method_id, $final_order->get_meta( '_stripe_source_id', true ) ); + $this->assertEquals( $payment_intent_id, $final_order->get_intent_id() ); + $this->assertEquals( $payment_method_id, $final_order->get_source_id() ); } /** @@ -1552,9 +1552,9 @@ function( $passed_order ) use ( $order ) { $this->assertEquals( 'success', $response['result'] ); $this->assertEquals( 'processing', $final_order->get_status() ); - $this->assertEquals( $payment_intent_id, $final_order->get_meta( '_stripe_intent_id', true ) ); - $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); - $this->assertEquals( $payment_method_id, $final_order->get_meta( '_stripe_source_id', true ) ); + $this->assertEquals( $payment_intent_id, $final_order->get_intent_id() ); + $this->assertEquals( $customer_id, $final_order->get_stripe_customer_id() ); + $this->assertEquals( $payment_method_id, $final_order->get_source_id() ); $this->assertMatchesRegularExpression( '/Charge ID: ch_mock/', $note->content ); } @@ -1658,8 +1658,8 @@ public function test_if_order_has_subscription_payment_method_will_be_saved() { $currency = $order->get_currency(); $order_id = $order->get_id(); - $order->update_meta_data( '_stripe_intent_id', $payment_intent_id ); - $order->update_meta_data( '_stripe_upe_payment_type', '' ); + $order->set_intent_id( $payment_intent_id ); + $order->set_upe_payment_type( '' ); $order->update_meta_data( '_stripe_upe_waiting_for_redirect', true ); $order->save(); @@ -2028,9 +2028,9 @@ public function test_pre_order_payment_is_successful() { $final_order = wc_get_order( $order_id ); $this->assertEquals( 'Credit / Debit Card', $final_order->get_payment_method_title() ); - $this->assertEquals( $payment_method_id, $final_order->get_meta( '_stripe_source_id', true ) ); - $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); - $this->assertEquals( $payment_intent_id, $final_order->get_meta( '_stripe_intent_id', true ) ); + $this->assertEquals( $payment_method_id, $final_order->get_source_id() ); + $this->assertEquals( $customer_id, $final_order->get_customer_id() ); + $this->assertEquals( $payment_intent_id, $final_order->get_intent_id() ); $this->assertTrue( (bool) $final_order->get_meta( '_stripe_upe_redirect_processed', true ) ); } @@ -2094,7 +2094,7 @@ public function test_pre_order_without_payment_uses_setup_intents() { $final_order = wc_get_order( $order_id ); - $this->assertEquals( $payment_method_id, $final_order->get_meta( '_stripe_source_id', true ) ); + $this->assertEquals( $payment_method_id, $final_order->get_source_id() ); $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); $this->assertTrue( (bool) $final_order->get_meta( '_stripe_upe_redirect_processed', true ) ); } @@ -2273,7 +2273,7 @@ function( $passed_order ) use ( $order ) { )[0]; $this->assertEquals( 'success', $response['result'] ); - $this->assertEquals( $payment_method_id, $final_order->get_meta( '_stripe_source_id', true ) ); + $this->assertEquals( $payment_method_id, $final_order->get_source_id() ); $this->assertMatchesRegularExpression( '/Charge ID: ch_mock/', $note->content ); } diff --git a/tests/phpunit/test-wc-stripe-sub-initial.php b/tests/phpunit/test-wc-stripe-sub-initial.php index 55db0ddde7..6e5df2e8ee 100644 --- a/tests/phpunit/test-wc-stripe-sub-initial.php +++ b/tests/phpunit/test-wc-stripe-sub-initial.php @@ -195,7 +195,7 @@ public function test_initial_intent_parameters() { $this->assertArrayHasKey( 'redirect', $result ); $order = wc_get_order( $order_id ); - $order_data = $order->get_meta( '_stripe_intent_id' ); + $order_data = $order->get_intent_id(); $this->assertEquals( $order_data, 'pi_123abc' ); diff --git a/tests/phpunit/test-wc-stripe-sub-renewal.php b/tests/phpunit/test-wc-stripe-sub-renewal.php index 7e408868d1..0014992edb 100644 --- a/tests/phpunit/test-wc-stripe-sub-renewal.php +++ b/tests/phpunit/test-wc-stripe-sub-renewal.php @@ -225,7 +225,7 @@ public function test_renewal_successful() { // Assert that we saved the payment intent to the order. $order_id = $renewal_order->get_id(); $order = wc_get_order( $order_id ); - $order_data = $order->get_meta( '_stripe_intent_id' ); + $order_data = $order->get_intent_id(); $this->assertEquals( $order_data, 'pi_123abc' ); @@ -341,7 +341,7 @@ public function test_renewal_authorization_required() { // Assert that we saved the payment intent to the order. $order_id = $renewal_order->get_id(); $order = wc_get_order( $order_id ); - $order_data = $order->get_meta( '_stripe_intent_id' ); + $order_data = $order->get_intent_id(); $order_transaction_id = $order->get_transaction_id(); // Intent was saved to order even though there was an error in the response body. From 9ac5a55729f03c5fec19ebd07c6e7b72f9cc7d22 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 26 Dec 2024 12:22:33 -0300 Subject: [PATCH 03/24] Additional getters and setters --- .../abstract-wc-stripe-payment-gateway.php | 14 ++-- includes/class-wc-stripe-order.php | 76 +++++++++++++++++++ includes/class-wc-stripe-webhook-handler.php | 2 +- .../class-wc-gateway-stripe-multibanco.php | 10 +-- ...c-stripe-upe-payment-method-multibanco.php | 8 +- ...st-class-wc-stripe-upe-payment-gateway.php | 6 +- .../test-wc-stripe-payment-gateway.php | 8 +- .../test-wc-stripe-webhook-handler.php | 2 +- 8 files changed, 101 insertions(+), 25 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index 0bdb4f216a..792d7b3ed9 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -519,6 +519,8 @@ public function generate_payment_request( $order, $prepared_payment_method ) { /** * Store extra meta data for an order from a Stripe Response. * + * @param object $response The Stripe response. + * @param WC_Stripe_Order $order The order object. * @throws WC_Stripe_Exception */ public function process_response( $response, $order ) { @@ -542,7 +544,7 @@ public function process_response( $response, $order ) { } if ( isset( $response->payment_method_details->card->mandate ) ) { - $order->update_meta_data( '_stripe_mandate_id', $response->payment_method_details->card->mandate ); + $order->set_mandate_id( $response->payment_method_details->card->mandate ); } if ( isset( $response->payment_method, $response->payment_method_details ) ) { @@ -1200,7 +1202,7 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) { } } - $order->update_meta_data( '_stripe_refund_id', $response->id ); + $order->set_refund_id( $response->id ); if ( isset( $response->balance_transaction ) ) { $this->update_fees( $order, $response->balance_transaction ); @@ -1592,7 +1594,7 @@ public function confirm_intent( $intent, $order, $prepared_source ) { * Saves intent to order. * * @since 3.2.0 - * @param WC_Order $order For to which the source applies. + * @param WC_Stripe_Order $order For to which the source applies. * @param stdClass $intent Payment intent information. */ public function save_intent_to_order( $order, $intent ) { @@ -1608,7 +1610,7 @@ public function save_intent_to_order( $order, $intent ) { $charge = $this->get_latest_charge_from_intent( $intent ); if ( isset( $charge->payment_method_details->card->mandate ) ) { - $order->update_meta_data( '_stripe_mandate_id', $charge->payment_method_details->card->mandate ); + $order->set_mandate_id( $charge->payment_method_details->card->mandate ); } } elseif ( 'setup_intent' === $intent->object ) { $order->update_meta_data( '_stripe_setup_intent', $intent->id ); @@ -1672,7 +1674,7 @@ private function get_intent( $intent_type, $intent_id ) { * Locks an order for payment intent processing for 5 minutes. * * @since 4.2 - * @param WC_Order $order The order that is being paid. + * @param WC_Stripe_Order $order The order that is being paid. * @param stdClass $intent The intent that is being processed. * @return bool A flag that indicates whether the order is already locked. */ @@ -1694,7 +1696,7 @@ public function lock_order_payment( $order, $intent = null ) { $new_lock = ( time() + 5 * MINUTE_IN_SECONDS ) . ( isset( $intent->id ) ? '|' . $intent->id : '' ); - $order->update_meta_data( '_stripe_lock_payment', $new_lock ); + $order->set_lock_payment( $new_lock ); $order->save_meta_data(); return false; diff --git a/includes/class-wc-stripe-order.php b/includes/class-wc-stripe-order.php index 8bd86163da..c721b4c15b 100644 --- a/includes/class-wc-stripe-order.php +++ b/includes/class-wc-stripe-order.php @@ -9,6 +9,82 @@ * Wrapper for the original WC_Order class to allow custom getters and setter with the extension's specific metadata. */ class WC_Stripe_Order extends WC_Order { + /** + * Set the mandate ID. + * + * @param $mandate_id string The mandate ID to set. + * @return void + */ + public function set_mandate_id( $mandate_id ) { + $this->update_meta_data( '_stripe_mandate_id', $mandate_id ); + } + + /** + * Get the mandate ID. + * + * @return string + */ + public function get_mandate_id() { + return $this->get_meta( '_stripe_mandate_id' ); + } + + /** + * Set the lock payment time. + * + * @param $time int The time to set. + * @return void + */ + public function set_lock_payment( $time ) { + $this->update_meta_data( '_stripe_lock_payment', $time ); + } + + /** + * Get the lock payment time. + * + * @return int + */ + public function get_lock_payment() { + return $this->get_meta( '_stripe_lock_payment' ); + } + + /** + * Set the refund ID. + * + * @param $refund_id string The refund ID to set. + * @return void + */ + public function set_refund_id( $refund_id ) { + $this->update_meta_data( '_stripe_refund_id', $refund_id ); + } + + /** + * Get the refund ID. + * + * @return string + */ + public function get_refund_id() { + return $this->get_meta( '_stripe_refund_id' ); + } + + /** + * Set the Multibanco data. + * + * @param $data array The Multibanco data to set. + * @return void + */ + public function set_multibanco_data( $data ) { + $this->update_meta_data( '_stripe_multibanco', $data ); + } + + /** + * Get the Multibanco data. + * + * @return array + */ + public function get_multibanco_data() { + return $this->get_meta( '_stripe_multibanco' ); + } + /** * Set the Stripe intent ID. * diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index 1577ad23ee..617bb8f486 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -680,7 +680,7 @@ public function process_webhook_refund( $notification ) { WC_Stripe_Logger::log( $refund->get_error_message() ); } - $order->update_meta_data( '_stripe_refund_id', $refund_object->id ); + $order->set_refund_id( $refund_object->id ); if ( isset( $refund_object->balance_transaction ) ) { $this->update_fees( $order, $refund_object->balance_transaction ); diff --git a/includes/payment-methods/class-wc-gateway-stripe-multibanco.php b/includes/payment-methods/class-wc-gateway-stripe-multibanco.php index 92dc286444..95955f5cbe 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-multibanco.php +++ b/includes/payment-methods/class-wc-gateway-stripe-multibanco.php @@ -238,14 +238,14 @@ public function email_instructions( $order, $sent_to_admin, $plain_text = false * * @since 4.1.0 * @version 4.1.0 - * @param int|WC_Order $order + * @param int|WC_Stripe_Order $order */ public function get_instructions( $order, $plain_text = false ) { if ( true === is_int( $order ) ) { $order = wc_get_order( $order ); } - $data = $order->get_meta( '_stripe_multibanco' ); + $data = $order->get_multibanco_data(); if ( $plain_text ) { esc_html_e( 'MULTIBANCO INFORMAÇÕES DE ENCOMENDA:', 'woocommerce-gateway-stripe' ) . "\n\n"; @@ -284,7 +284,7 @@ public function get_instructions( $order, $plain_text = false ) { * * @since 4.1.0 * @version 4.1.0 - * @param object $order + * @param WC_Stripe_Order $order * @param object $source_object */ public function save_instructions( $order, $source_object ) { @@ -294,9 +294,7 @@ public function save_instructions( $order, $source_object ) { 'reference' => $source_object->multibanco->reference, ]; - $order_id = $order->get_id(); - - $order->update_meta_data( '_stripe_multibanco', $data ); + $order->set_multibanco_data( $data ); } /** diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-method-multibanco.php b/includes/payment-methods/class-wc-stripe-upe-payment-method-multibanco.php index 4cdfd3bd13..76afa70e57 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-method-multibanco.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-method-multibanco.php @@ -66,11 +66,11 @@ public function email_instructions( $order, $sent_to_admin, $plain_text = false /** * Gets Multibanco payment instructions for the customer. * - * @param WC_Order $order + * @param WC_Stripe_Order $order * @param bool $plain_text */ public function get_instructions( $order, $plain_text = false ) { - $data = $order->get_meta( '_stripe_multibanco' ); + $data = $order->get_multibanco_data(); if ( ! $data ) { return; } @@ -111,7 +111,7 @@ public function get_instructions( $order, $plain_text = false ) { /** * Saves Multibanco information to the order meta for later use. * - * @param object $order + * @param WC_Stripe_Order $order * @param object $payment_intent. The PaymentIntent object. */ public function save_instructions( $order, $payment_intent ) { @@ -125,7 +125,7 @@ public function save_instructions( $order, $payment_intent ) { 'reference' => $payment_intent->next_action->multibanco_display_details->reference, ]; - $order->update_meta_data( '_stripe_multibanco', $data ); + $order->set_multibanco_data( $data ); } /** diff --git a/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php b/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php index 343f73b2e9..2626967850 100644 --- a/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php +++ b/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php @@ -311,7 +311,7 @@ public function test_process_payment_returns_valid_response() { $currency = $order->get_currency(); $order_id = $order->get_id(); - $order->update_meta_data( '_stripe_intent_id', $payment_intent_id ); + $order->set_intent_id( $payment_intent_id ); $order->set_upe_payment_type( '' ); $order->update_meta_data( '_stripe_upe_waiting_for_redirect', true ); $order->save(); @@ -1769,7 +1769,7 @@ public function test_subscription_renewal_is_successful() { list( $amount, $description, $metadata ) = $this->get_order_details( $order ); $order->set_payment_method( WC_Stripe_UPE_Payment_Gateway::ID ); - $order->update_meta_data( '_stripe_lock_payment', ( time() + MINUTE_IN_SECONDS ) ); // To assist with comparing expected order objects, set an existing lock. + $order->set_lock_payment( ( time() + MINUTE_IN_SECONDS ) ); // To assist with comparing expected order objects, set an existing lock. $order->save(); $order = wc_get_order( $order_id ); @@ -1863,7 +1863,7 @@ public function test_subscription_renewal_checks_payment_method_authorization() list( $amount, $description, $metadata ) = $this->get_order_details( $order ); $order->set_payment_method( WC_Stripe_UPE_Payment_Gateway::ID ); - $order->update_meta_data( '_stripe_lock_payment', ( time() + MINUTE_IN_SECONDS ) ); // To assist with comparing expected order objects, set an existing lock. + $order->set_lock_payment( ( time() + MINUTE_IN_SECONDS ) ); // To assist with comparing expected order objects, set an existing lock. $order->save(); $order = wc_get_order( $order_id ); diff --git a/tests/phpunit/test-wc-stripe-payment-gateway.php b/tests/phpunit/test-wc-stripe-payment-gateway.php index 86f4be5653..c8ca0bc70a 100644 --- a/tests/phpunit/test-wc-stripe-payment-gateway.php +++ b/tests/phpunit/test-wc-stripe-payment-gateway.php @@ -578,8 +578,8 @@ public function test_render_subscription_payment_method() { $mock_subscription = WC_Helper_Order::create_order(); // We can use an order as a subscription. $mock_subscription->set_payment_method( 'stripe' ); - $mock_subscription->update_meta_data( '_stripe_source_id', 'src_mock' ); - $mock_subscription->update_meta_data( '_stripe_customer_id', 'cus_mock' ); + $mock_subscription->set_source_id( 'src_mock' ); + $mock_subscription->set_stripe_customer_id( 'cus_mock' ); $mock_subscription->save(); // This is the key the customer's payment methods are stored under in the transient. @@ -655,11 +655,11 @@ public function test_lock_order_payment() { // test expired locks. $order_3 = WC_Helper_Order::create_order(); - $order_3->update_meta_data( '_stripe_lock_payment', time() - 1 ); + $order_3->set_lock_payment( time() - 1 ); $order_3->save_meta_data(); $locked = $this->gateway->lock_order_payment( $order_3, $intent_id ); - $current_lock = $order_3->get_meta( '_stripe_lock_payment' ); + $current_lock = $order_3->get_lock_payment(); $this->assertFalse( $locked ); $this->assertEqualsWithDelta( (int) $current_lock, ( time() + 5 * MINUTE_IN_SECONDS ), 3 ); diff --git a/tests/phpunit/test-wc-stripe-webhook-handler.php b/tests/phpunit/test-wc-stripe-webhook-handler.php index 16cdd6afeb..6b8c26942f 100644 --- a/tests/phpunit/test-wc-stripe-webhook-handler.php +++ b/tests/phpunit/test-wc-stripe-webhook-handler.php @@ -313,7 +313,7 @@ public function test_process_webhook_dispute( $order_status, $order_status_final $order->set_status( $order_status ); $order->set_transaction_id( $charge_id ); if ( $order_status_final ) { - $order->update_meta_data( '_stripe_status_final', true ); + $order->set_status_final( true ); } $order->save(); From 19820a5389d1d755f580fdc2cb84e168991b8aa7 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 26 Dec 2024 14:57:34 -0300 Subject: [PATCH 04/24] Additional getters and setters --- .../abstract-wc-stripe-payment-gateway.php | 55 ++------ includes/class-wc-stripe-helper.php | 34 ++++- includes/class-wc-stripe-order.php | 126 ++++++++++++++++++ includes/class-wc-stripe-webhook-handler.php | 4 +- .../class-wc-stripe-upe-payment-gateway.php | 20 +-- ...class-wc-rest-stripe-orders-controller.php | 2 +- ...st-class-wc-stripe-upe-payment-gateway.php | 30 ++--- 7 files changed, 189 insertions(+), 82 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index 792d7b3ed9..46403f6ef8 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -582,7 +582,7 @@ public function process_response( $response, $order ) { * to ensure the review.closed event handler will update the status to the proper status. */ if ( 'manual_review' === $this->get_risk_outcome( $response ) ) { - $this->set_stripe_order_status_before_hold( $order, 'default_payment_complete' ); + $order->set_status_before_hold( 'default_payment_complete' ); $order->set_transaction_id( $response->id ); // Save the transaction ID to link the order to the Stripe charge ID. This is to fix reviews that result in refund. } else { $order->payment_complete( $response->id ); @@ -1613,7 +1613,7 @@ public function save_intent_to_order( $order, $intent ) { $order->set_mandate_id( $charge->payment_method_details->card->mandate ); } } elseif ( 'setup_intent' === $intent->object ) { - $order->update_meta_data( '_stripe_setup_intent', $intent->id ); + $order->set_setup_intent( $intent->id ); } if ( is_callable( [ $order, 'save' ] ) ) { @@ -1636,7 +1636,7 @@ public function get_intent_from_order( $order ) { } // The order doesn't have a payment intent, but it may have a setup intent. - $intent_id = $order->get_meta( '_stripe_setup_intent' ); + $intent_id = $order->get_setup_intent(); if ( $intent_id ) { return $this->get_intent( 'setup_intents', $intent_id ); @@ -1717,13 +1717,13 @@ public function unlock_order_payment( $order ) { * Locks an order for refund processing for 5 minutes. * * @since 9.1.0 - * @param WC_Order $order The order that is being refunded. + * @param WC_Stripe_Order $order The order that is being refunded. * @return bool A flag that indicates whether the order is already locked. */ public function lock_order_refund( $order ) { $order->read_meta_data( true ); - $existing_lock = $order->get_meta( '_stripe_lock_refund', true ); + $existing_lock = $order->get_lock_refund(); if ( $existing_lock ) { $expiration = (int) $existing_lock; @@ -1736,7 +1736,7 @@ public function lock_order_refund( $order ) { $new_lock = time() + 5 * MINUTE_IN_SECONDS; - $order->update_meta_data( '_stripe_lock_refund', $new_lock ); + $order->set_lock_refund( $new_lock ); $order->save_meta_data(); return false; @@ -1768,7 +1768,7 @@ public function is_authentication_required_for_payment( $response ) { /** * Creates a SetupIntent for future payments, and saves it to the order. * - * @param WC_Order $order The ID of the (free/pre- order). + * @param WC_Stripe_Order $order The ID of the (free/pre- order). * @param object $prepared_source The source, entered/chosen by the customer. * @return string|null The client secret of the intent, used for confirmation in JS. */ @@ -1793,7 +1793,7 @@ public function setup_intent( $order, $prepared_source ) { if ( is_wp_error( $setup_intent ) ) { WC_Stripe_Logger::log( "Unable to create SetupIntent for Order #$order_id: " . print_r( $setup_intent, true ) ); } elseif ( WC_Stripe_Intent_Status::REQUIRES_ACTION === $setup_intent->status ) { - $order->update_meta_data( '_stripe_setup_intent', $setup_intent->id ); + $order->set_setup_intent( $setup_intent->id ); $order->save(); return $setup_intent->client_secret; @@ -2212,45 +2212,6 @@ private function needs_ssl_setup() { return ! $this->testmode && ! is_ssl(); // @phpstan-ignore-line (testmode is defined in the classes that use this class) } - /** - * Helper method to retrieve the status of the order before it was put on hold. - * - * @since 8.3.0 - * - * @param WC_Order $order The order. - * - * @return string The status of the order before it was put on hold. - */ - protected function get_stripe_order_status_before_hold( $order ) { - $before_hold_status = $order->get_meta( '_stripe_status_before_hold' ); - - if ( ! empty( $before_hold_status ) ) { - return $before_hold_status; - } - - $default_before_hold_status = $order->needs_processing() ? 'processing' : 'completed'; - return apply_filters( 'woocommerce_payment_complete_order_status', $default_before_hold_status, $order->get_id(), $order ); - } - - /** - * Stores the status of the order before being put on hold in metadata. - * - * @since 8.3.0 - * - * @param WC_Order $order The order. - * @param string $status The order status to store. Accepts 'default_payment_complete' which will fetch the default status for payment complete orders. - * - * @return void - */ - protected function set_stripe_order_status_before_hold( $order, $status ) { - if ( 'default_payment_complete' === $status ) { - $payment_complete_status = $order->needs_processing() ? 'processing' : 'completed'; - $status = apply_filters( 'woocommerce_payment_complete_order_status', $payment_complete_status, $order->get_id(), $order ); - } - - $order->update_meta_data( '_stripe_status_before_hold', $status ); - } - /** * Retrieves the risk/fraud outcome from the webhook payload. * diff --git a/includes/class-wc-stripe-helper.php b/includes/class-wc-stripe-helper.php index 9594f11174..06dcf00347 100644 --- a/includes/class-wc-stripe-helper.php +++ b/includes/class-wc-stripe-helper.php @@ -617,7 +617,7 @@ public static function get_legacy_individual_payment_method_settings() { */ public static function get_upe_individual_payment_method_settings( $gateway ) { $payment_method_settings = []; - $available_gateways = $gateway->get_upe_available_payment_methods(); + $available_gateways = $gateway->get_upe_available_payment_methods(); foreach ( $available_gateways as $gateway ) { $individual_gateway_settings = get_option( 'woocommerce_stripe_' . $gateway . '_settings', [] ); @@ -868,7 +868,7 @@ public static function get_order_by_source_id( $source_id ) { global $wpdb; if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { - $orders = wc_get_orders( + $orders = self::get_orders( [ 'limit' => 1, 'meta_query' => [ @@ -906,7 +906,7 @@ public static function get_order_by_charge_id( $charge_id ) { } if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { - $orders = wc_get_orders( + $orders = self::get_orders( [ 'transaction_id' => $charge_id, 'limit' => 1, @@ -934,7 +934,7 @@ public static function get_order_by_refund_id( $refund_id ) { global $wpdb; if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { - $orders = wc_get_orders( + $orders = self::get_orders( [ 'limit' => 1, 'meta_query' => [ @@ -968,7 +968,7 @@ public static function get_order_by_intent_id( $intent_id ) { global $wpdb; if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { - $orders = wc_get_orders( + $orders = self::get_orders( [ 'limit' => 1, 'meta_query' => [ @@ -1006,7 +1006,7 @@ public static function get_order_by_setup_intent_id( $intent_id ) { global $wpdb; if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { - $orders = wc_get_orders( + $orders = self::get_orders( [ 'limit' => 1, 'meta_query' => [ @@ -1379,7 +1379,7 @@ public static function get_payment_method_from_intent( $intent ) { public static function get_intent_id_from_order( $order ) { $intent_id = $order->get_intent_id(); if ( ! $intent_id ) { - $intent_id = $order->get_meta( '_stripe_setup_intent' ); + $intent_id = $order->get_setup_intent(); } return $intent_id ?? false; @@ -1619,6 +1619,26 @@ public static function get_order( $order_id ) { return new WC_Stripe_Order( $order ); } + /** + * Wrapper to get orders using the extension's custom WC_Stripe_Order class. + * + * @param $args array Arguments to pass to wc_get_orders. + * @return array|WC_Stripe_Order[] + */ + public static function get_orders( $args ) { + $orders = wc_get_orders( $args ); + if ( empty( $orders ) ) { + return []; + } + + return array_map( + function ( $order ) { + return new WC_Stripe_Order( $order ); + }, + $orders + ); + } + /** * Wrapper to create an order using the extension's custom WC_Stripe_Order class. * diff --git a/includes/class-wc-stripe-order.php b/includes/class-wc-stripe-order.php index c721b4c15b..02e611ef7a 100644 --- a/includes/class-wc-stripe-order.php +++ b/includes/class-wc-stripe-order.php @@ -9,6 +9,132 @@ * Wrapper for the original WC_Order class to allow custom getters and setter with the extension's specific metadata. */ class WC_Stripe_Order extends WC_Order { + /** + * Set the preferred card brand. + * + * @param $brand string The brand to set. + * @return void + */ + public function set_card_brand( $brand ) { + $this->update_meta_data( '_stripe_card_brand', $brand ); + } + + /** + * Get the preferred card brand. + * + * @return string + */ + public function get_card_brand() { + return $this->get_meta( '_stripe_card_brand' ); + } + + /** + * Set the lock refund time. + * + * @param $time int The time to set. + * @return void + */ + public function set_lock_refund( $time ) { + $this->update_meta_data( '_stripe_lock_refund', $time ); + } + + /** + * Get the lock refund time. + * + * @return int + */ + public function get_lock_refund() { + return $this->get_meta( '_stripe_lock_refund' ); + } + + /** + * Set the setup intent. + * + * @param $value string The value to set. + * @return void + */ + public function set_setup_intent( $value ) { + $this->update_meta_data( '_stripe_setup_intent', $value ); + } + + /** + * Get the setup intent. + * + * @return string + */ + public function get_setup_intent() { + return $this->get_meta( '_stripe_setup_intent' ); + } + + /** + * Set the UPE redirect processed flag. + * + * @param $value bool The value to set. + * @return void + */ + public function set_upe_redirect_processed( $value ) { + $this->update_meta_data( '_stripe_upe_redirect_processed', $value ); + } + + /** + * Whether the UPE redirect has been processed. + * + * @return bool The value of the flag. + */ + public function upe_redirect_processed() { + return (bool) $this->get_meta( '_stripe_upe_redirect_processed' ); + } + + /** + * Stores the status of the order before being put on hold in metadata. + * + * @param string $status The order status to store. Accepts 'default_payment_complete' which will fetch the default status for payment complete orders. + * @return void + */ + public function set_status_before_hold( $status ) { + if ( 'default_payment_complete' === $status ) { + $payment_complete_status = $this->needs_processing() ? 'processing' : 'completed'; + $status = apply_filters( 'woocommerce_payment_complete_order_status', $payment_complete_status, $this->get_id(), $this ); + } + + $this->update_meta_data( '_stripe_status_before_hold', $status ); + } + + /** + * Helper method to retrieve the status of the order before it was put on hold. + * + * @return string The status of the order before it was put on hold. + */ + public function get_status_before_hold() { + $before_hold_status = $this->get_meta( '_stripe_status_before_hold' ); + + if ( ! empty( $before_hold_status ) ) { + return $before_hold_status; + } + + $default_before_hold_status = $this->needs_processing() ? 'processing' : 'completed'; + return apply_filters( 'woocommerce_payment_complete_order_status', $default_before_hold_status, $this->get_id(), $this ); + } + + /** + * Set the UPE waiting for redirect flag. + * + * @param $value bool The value to set. + * @return void + */ + public function set_upe_waiting_for_redirect( $value ) { + $this->update_meta_data( '_stripe_upe_waiting_for_redirect', $value ); + } + + /** + * Whether the UPE payment is waiting for redirect. + * + * @return bool + */ + public function upe_waiting_for_redirect() { + return (bool) $this->get_meta( '_stripe_upe_waiting_for_redirect' ); + } + /** * Set the mandate ID. * diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index 617bb8f486..4b554e51b9 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -399,7 +399,7 @@ public function process_webhook_dispute_closed( $notification ) { $order->set_status_final( true ); // Fail order if dispute is lost, or else revert to pre-dispute status. - $order_status = 'lost' === $status ? 'failed' : $this->get_stripe_order_status_before_hold( $order ); + $order_status = 'lost' === $status ? 'failed' : $order->get_status_before_hold(); // Do not re-send "Processing Order" email to customer after a dispute win. if ( 'processing' === $order_status ) { @@ -837,7 +837,7 @@ public function process_review_closed( $notification ) { apply_filters( 'wc_stripe_webhook_review_change_order_status', true, $order, $notification ) ) { // If the status we stored before hold is an incomplete status, restore the status to processing/completed instead. - $status_after_review = $this->get_stripe_order_status_before_hold( $order ); + $status_after_review = $order->get_status_before_hold(); if ( in_array( $status_after_review, apply_filters( 'woocommerce_valid_order_statuses_for_payment_complete', [ 'on-hold', 'pending', 'failed', 'cancelled' ], $order ), true ) ) { $status_after_review = apply_filters( 'woocommerce_payment_complete_order_status', $order->needs_processing() ? 'processing' : 'completed', $order->get_id(), $order ); } diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index 47faac4b17..1ce40f8ac1 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -733,7 +733,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = // https://github.com/woocommerce/woocommerce-gateway-stripe/issues/2536. It would // be better if we removed the need for additional meta data in favor of refactoring // this part of the payment processing. - $order->update_meta_data( '_stripe_upe_waiting_for_redirect', true ); + $order->set_upe_waiting_for_redirect( true ); $order->save(); @@ -779,7 +779,7 @@ private function process_payment_with_deferred_intent( int $order_id ) { return $this->process_change_subscription_payment_with_deferred_intent( $order_id ); } - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); try { $payment_information = $this->prepare_payment_information_from_request( $order ); @@ -887,7 +887,7 @@ private function process_payment_with_deferred_intent( int $order_id ) { WC_Stripe_Helper::set_payment_awaiting_action( $order, false ); // Prevent processing the payment intent webhooks while also processing the redirect payment (also prevents duplicate Stripe meta stored on the order). - $order->update_meta_data( '_stripe_upe_waiting_for_redirect', true ); + $order->set_upe_waiting_for_redirect( true ); $order->save(); } else { $redirect = $return_url; @@ -1251,7 +1251,7 @@ private function is_order_associated_to_setup_intent( int $order_id, string $int * @version 5.5.0 */ public function process_upe_redirect_payment( $order_id, $intent_id, $save_payment_method ) { - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); if ( ! is_object( $order ) ) { return; @@ -1261,7 +1261,7 @@ public function process_upe_redirect_payment( $order_id, $intent_id, $save_payme return; } - if ( $order->get_meta( '_stripe_upe_redirect_processed', true ) ) { + if ( $order->upe_redirect_processed() ) { return; } @@ -1284,7 +1284,7 @@ public function process_upe_redirect_payment( $order_id, $intent_id, $save_payme /** * Update order and maybe save payment method for an order after an intent has been created and confirmed. * - * @param WC_Order $order Order being processed. + * @param WC_Stripe_Order $order Order being processed. * @param string $intent_id Stripe setup/payment ID. * @param bool $save_payment_method Boolean representing whether payment method for order should be saved. */ @@ -1354,7 +1354,7 @@ public function process_order_for_confirmed_intent( $order, $intent_id, $save_pa $this->save_intent_to_order( $order, $intent ); $this->set_payment_method_title_for_order( $order, $payment_method_type ); - $order->update_meta_data( '_stripe_upe_redirect_processed', true ); + $order->set_upe_redirect_processed( true ); // TODO: This is a stop-gap to fix a critical issue, see // https://github.com/woocommerce/woocommerce-gateway-stripe/issues/2536. It would @@ -2093,15 +2093,15 @@ protected function prepare_payment_information_from_request( WC_Order $order ) { /** * Conditionally stores the card brand to the order meta. * - * @param WC_Order $order The WC Order for which we're processing a payment. + * @param WC_Stripe_Order $order The WC Order for which we're processing a payment. * @param stdClass $payment_method The payment method object. */ - private function maybe_set_preferred_card_brand_for_order( WC_Order $order, $payment_method ) { + private function maybe_set_preferred_card_brand_for_order( WC_Stripe_Order $order, $payment_method ) { // Retrieve the preferred card brand for the payment method. $preferred_brand = $payment_method->card->networks->preferred ?? null; if ( WC_Stripe_Co_Branded_CC_Compatibility::is_wc_supported() && $preferred_brand ) { - $order->update_meta_data( '_stripe_card_brand', $preferred_brand ); + $order->set_card_brand( $preferred_brand ); $order->save_meta_data(); if ( function_exists( 'wc_admin_record_tracks_event' ) ) { diff --git a/tests/phpunit/admin/test-class-wc-rest-stripe-orders-controller.php b/tests/phpunit/admin/test-class-wc-rest-stripe-orders-controller.php index 7bced75bd0..a1ea086029 100644 --- a/tests/phpunit/admin/test-class-wc-rest-stripe-orders-controller.php +++ b/tests/phpunit/admin/test-class-wc-rest-stripe-orders-controller.php @@ -73,7 +73,7 @@ public function test_create_customer_with_existing_id() { $order = WC_Helper_Order::create_order(); $endpoint = '/' . strval( $order->get_id() ) . '/create_customer'; - $order->add_meta_data( '_stripe_customer_id', 'cus_12345', true ); + $order->set_stripe_customer_id( 'cus_12345' ); $order->save(); $this->assertEquals( 'cus_12345', $order->get_stripe_customer_id() ); diff --git a/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php b/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php index 2626967850..ff1d80b755 100644 --- a/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php +++ b/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php @@ -313,7 +313,7 @@ public function test_process_payment_returns_valid_response() { $order->set_intent_id( $payment_intent_id ); $order->set_upe_payment_type( '' ); - $order->update_meta_data( '_stripe_upe_waiting_for_redirect', true ); + $order->set_upe_waiting_for_redirect( true ); $order->save(); list( $amount, $description, $metadata ) = $this->get_order_details( $order ); @@ -776,7 +776,7 @@ public function test_process_redirect_payment_returns_valid_response() { $this->mock_gateway->process_upe_redirect_payment( $order_id, $payment_intent_id, false ); - $final_order = wc_get_order( $order_id ); + $final_order = WC_Stripe_Helper::get_order( $order_id ); $note = wc_get_order_notes( [ 'order_id' => $order_id, @@ -787,7 +787,7 @@ public function test_process_redirect_payment_returns_valid_response() { $this->assertEquals( 'processing', $final_order->get_status() ); $this->assertEquals( 'Credit / Debit Card', $final_order->get_payment_method_title() ); $this->assertEquals( $payment_intent_id, $final_order->get_intent_id() ); - $this->assertTrue( (bool) $final_order->get_meta( '_stripe_upe_redirect_processed', true ) ); + $this->assertTrue( $final_order->upe_redirect_processed() ); $this->assertMatchesRegularExpression( '/Charge ID: ch_mock/', $note->content ); } @@ -839,7 +839,7 @@ public function test_process_redirect_payment_only_runs_once() { $this->mock_gateway->process_upe_redirect_payment( $order_id, $payment_intent_id, false ); - $success_order = wc_get_order( $order_id ); + $success_order = WC_Stripe_Helper::get_order( $order_id ); $note = wc_get_order_notes( [ @@ -852,7 +852,7 @@ public function test_process_redirect_payment_only_runs_once() { $this->assertEquals( 'processing', $success_order->get_status() ); $this->assertEquals( 'Credit / Debit Card', $success_order->get_payment_method_title() ); $this->assertEquals( $payment_intent_id, $success_order->get_intent_id() ); - $this->assertTrue( (bool) $success_order->get_meta( '_stripe_upe_redirect_processed', true ) ); + $this->assertTrue( $success_order->upe_redirect_processed() ); $this->assertMatchesRegularExpression( '/Charge ID: ch_mock/', $note->content ); // simulate an order getting marked as failed as if from a webhook @@ -1097,10 +1097,10 @@ public function test_setup_intent_checkout_saves_sepa_generated_payment_method_t $this->mock_gateway->process_upe_redirect_payment( $order_id, $setup_intent_id, true ); - $final_order = wc_get_order( $order_id ); + $final_order = WC_Stripe_Helper::get_order( $order_id ); $this->assertEquals( 'processing', $final_order->get_status() ); - $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); + $this->assertEquals( $customer_id, $final_order->get_stripe_customer_id() ); $this->assertEquals( $generated_payment_method_id, $final_order->get_source_id() ); } @@ -1637,11 +1637,11 @@ function( $passed_order ) use ( $order ) { ); $response = $this->mock_gateway->process_payment( $order_id ); - $final_order = wc_get_order( $order_id ); + $final_order = WC_Stripe_Helper::get_order( $order_id ); $this->assertEquals( 'failure', $response['result'] ); $this->assertEquals( 'failed', $final_order->get_status() ); - $this->assertEquals( '', $final_order->get_meta( '_stripe_customer_id', true ) ); + $this->assertEquals( '', $final_order->get_stripe_customer_id() ); } /** @@ -1660,7 +1660,7 @@ public function test_if_order_has_subscription_payment_method_will_be_saved() { $order->set_intent_id( $payment_intent_id ); $order->set_upe_payment_type( '' ); - $order->update_meta_data( '_stripe_upe_waiting_for_redirect', true ); + $order->set_upe_waiting_for_redirect( true ); $order->save(); list( $amount, $description, $metadata ) = $this->get_order_details( $order ); @@ -2025,13 +2025,13 @@ public function test_pre_order_payment_is_successful() { $this->mock_gateway->process_upe_redirect_payment( $order_id, $payment_intent_id, false ); - $final_order = wc_get_order( $order_id ); + $final_order = WC_Stripe_Helper::get_order( $order_id ); $this->assertEquals( 'Credit / Debit Card', $final_order->get_payment_method_title() ); $this->assertEquals( $payment_method_id, $final_order->get_source_id() ); $this->assertEquals( $customer_id, $final_order->get_customer_id() ); $this->assertEquals( $payment_intent_id, $final_order->get_intent_id() ); - $this->assertTrue( (bool) $final_order->get_meta( '_stripe_upe_redirect_processed', true ) ); + $this->assertTrue( $final_order->upe_redirect_processed() ); } /** @@ -2092,11 +2092,11 @@ public function test_pre_order_without_payment_uses_setup_intents() { $this->mock_gateway->process_upe_redirect_payment( $order_id, $setup_intent_id, true ); - $final_order = wc_get_order( $order_id ); + $final_order = WC_Stripe_Helper::get_order( $order_id ); $this->assertEquals( $payment_method_id, $final_order->get_source_id() ); - $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); - $this->assertTrue( (bool) $final_order->get_meta( '_stripe_upe_redirect_processed', true ) ); + $this->assertEquals( $customer_id, $final_order->get_stripe_customer_id() ); + $this->assertTrue( $final_order->upe_redirect_processed() ); } /** From a0d3258cac1ee4cf2c239cdfcf52f48c41146d84 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 26 Dec 2024 15:15:03 -0300 Subject: [PATCH 05/24] Lock/unlock methods --- .../abstract-wc-stripe-payment-gateway.php | 31 ++++----------- includes/admin/class-wc-stripe-privacy.php | 2 +- includes/class-wc-gateway-stripe.php | 16 ++++---- includes/class-wc-stripe-order-handler.php | 10 ++--- includes/class-wc-stripe-order.php | 38 +++++++++++++++++++ includes/class-wc-stripe-webhook-handler.php | 20 +++++----- .../compat/trait-wc-stripe-subscriptions.php | 6 +-- .../class-wc-stripe-upe-payment-gateway.php | 4 +- .../test-wc-stripe-payment-gateway.php | 18 ++++----- 9 files changed, 83 insertions(+), 62 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index 46403f6ef8..515cf720de 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -1674,43 +1674,26 @@ private function get_intent( $intent_type, $intent_id ) { * Locks an order for payment intent processing for 5 minutes. * * @since 4.2 + * @deprecated 9.1.0 Use WC_Stripe_Order::lock_payment instead. * @param WC_Stripe_Order $order The order that is being paid. * @param stdClass $intent The intent that is being processed. * @return bool A flag that indicates whether the order is already locked. */ public function lock_order_payment( $order, $intent = null ) { - $order->read_meta_data( true ); - - $existing_lock = $order->get_meta( '_stripe_lock_payment', true ); - - if ( $existing_lock ) { - $parts = explode( '|', $existing_lock ); // Format is: "{expiry_timestamp}" or "{expiry_timestamp}|{pi_xxxx}" if an intent is passed. - $expiration = (int) $parts[0]; - $locked_intent = ! empty( $parts[1] ) ? $parts[1] : ''; - - // If the lock is still active, return true. - if ( time() <= $expiration && ( empty( $intent ) || empty( $locked_intent ) || ( $intent->id ?? '' ) === $locked_intent ) ) { - return true; - } - } - - $new_lock = ( time() + 5 * MINUTE_IN_SECONDS ) . ( isset( $intent->id ) ? '|' . $intent->id : '' ); - - $order->set_lock_payment( $new_lock ); - $order->save_meta_data(); - - return false; + wc_deprecated_function( __FUNCTION__, '9.1.0', 'WC_Stripe_Order::lock_payment' ); + return $order->lock_payment( $intent ); } /** * Unlocks an order for processing by payment intents. * * @since 4.2 - * @param WC_Order $order The order that is being unlocked. + * @deprecated 9.1.0 Use WC_Stripe_Order::unlock_payment instead. + * @param WC_Stripe_Order $order The order that is being unlocked. */ public function unlock_order_payment( $order ) { - $order->delete_meta_data( '_stripe_lock_payment' ); - $order->save_meta_data(); + wc_deprecated_function( __FUNCTION__, '9.1.0', 'WC_Stripe_Order::unlock_payment' ); + $order->unlock_payment(); } /** diff --git a/includes/admin/class-wc-stripe-privacy.php b/includes/admin/class-wc-stripe-privacy.php index b4c0e7711f..570137f876 100644 --- a/includes/admin/class-wc-stripe-privacy.php +++ b/includes/admin/class-wc-stripe-privacy.php @@ -389,7 +389,7 @@ protected function maybe_handle_subscription( $order ) { */ protected function maybe_handle_order( $order ) { $stripe_source_id = $order->get_source_id(); - $stripe_refund_id = $order->get_meta( '_stripe_refund_id', true ); + $stripe_refund_id = $order->get_refund_id(); $stripe_customer_id = $order->get_stripe_customer_id(); if ( ! $this->is_retention_expired( $order->get_date_created()->getTimestamp() ) ) { diff --git a/includes/class-wc-gateway-stripe.php b/includes/class-wc-gateway-stripe.php index 4b856783d4..79e516ff63 100644 --- a/includes/class-wc-gateway-stripe.php +++ b/includes/class-wc-gateway-stripe.php @@ -387,7 +387,7 @@ public function complete_free_order( $order, $prepared_source, $force_save_sourc */ public function process_payment( $order_id, $retry = true, $force_save_source = false, $previous_error = false, $use_order_source = false ) { try { - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); if ( $this->has_subscription( $order_id ) ) { $force_save_source = true; @@ -445,7 +445,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = // Confirm the intent after locking the order to make sure webhooks will not interfere. if ( empty( $intent->error ) ) { - $this->lock_order_payment( $order, $intent ); + $order->lock_payment( $intent ); $intent = $this->confirm_intent( $intent, $order, $prepared_source ); } @@ -459,7 +459,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = return $this->retry_after_error( $intent, $order, $retry, $force_save_source, $previous_error, $use_order_source ); } - $this->unlock_order_payment( $order ); + $order->unlock_payment(); $this->throw_localized_message( $intent, $order ); } @@ -473,7 +473,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = // If the intent requires a 3DS flow, redirect to it. if ( WC_Stripe_Intent_Status::REQUIRES_ACTION === $intent->status ) { - $this->unlock_order_payment( $order ); + $order->unlock_payment(); // If the order requires some action from the customer, add meta to the order to prevent it from being cancelled by WooCommerce's hold stock settings. WC_Stripe_Helper::set_payment_awaiting_action( $order ); @@ -511,7 +511,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = } // Unlock the order. - $this->unlock_order_payment( $order ); + $order->unlock_payment(); // Return thank you page redirect. return [ @@ -854,7 +854,7 @@ public function set_cookie_on_current_request( $cookie ) { * method updates orders based on the status of associated PaymentIntents. * * @since 4.2.0 - * @param WC_Order $order The order which is in a transitional state. + * @param WC_Stripe_Order $order The order which is in a transitional state. */ public function verify_intent_after_checkout( $order ) { $payment_method = $order->get_payment_method(); @@ -884,7 +884,7 @@ public function verify_intent_after_checkout( $order ) { return; } - if ( $this->lock_order_payment( $order, $intent ) ) { + if ( $order->lock_payment( $intent ) ) { return; } @@ -903,7 +903,7 @@ public function verify_intent_after_checkout( $order ) { $this->handle_intent_verification_failure( $order, $intent ); } - $this->unlock_order_payment( $order ); + $order->unlock_payment(); /** * This meta is to prevent stores with short hold stock settings from cancelling orders while waiting for payment to be finalised by Stripe or the customer (i.e. completing 3DS or payment redirects). diff --git a/includes/class-wc-stripe-order-handler.php b/includes/class-wc-stripe-order-handler.php index 130c99b802..93ee8911af 100644 --- a/includes/class-wc-stripe-order-handler.php +++ b/includes/class-wc-stripe-order-handler.php @@ -140,7 +140,7 @@ public function process_redirect_payment( $order_id, $retry = true, $previous_er return; } - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); if ( ! is_object( $order ) ) { return; @@ -159,7 +159,7 @@ public function process_redirect_payment( $order_id, $retry = true, $previous_er WC_Stripe_Logger::log( "Info: (Redirect) Begin processing payment for order $order_id for the amount of {$order->get_total()}" ); // Lock the order or return if the order is already locked. - if ( $this->lock_order_payment( $order ) ) { + if ( $order->lock_payment( $order ) ) { return; } @@ -224,7 +224,7 @@ public function process_redirect_payment( $order_id, $retry = true, $previous_er // We want to retry. if ( $this->is_retryable_error( $response->error ) ) { // Unlock the order before retrying. - $this->unlock_order_payment( $order ); + $order->unlock_payment(); if ( $retry ) { // Don't do anymore retries after this. @@ -272,7 +272,7 @@ public function process_redirect_payment( $order_id, $retry = true, $previous_er $order->update_status( 'failed', sprintf( __( 'Stripe payment failed: %s', 'woocommerce-gateway-stripe' ), $e->getLocalizedMessage() ) ); // Unlock the order. - $this->unlock_order_payment( $order ); + $order->unlock_payment(); wc_add_notice( $e->getLocalizedMessage(), 'error' ); wp_safe_redirect( wc_get_checkout_url() ); @@ -280,7 +280,7 @@ public function process_redirect_payment( $order_id, $retry = true, $previous_er } // Unlock the order. - $this->unlock_order_payment( $order ); + $order->unlock_payment(); } /** diff --git a/includes/class-wc-stripe-order.php b/includes/class-wc-stripe-order.php index 02e611ef7a..2d0ea71216 100644 --- a/includes/class-wc-stripe-order.php +++ b/includes/class-wc-stripe-order.php @@ -154,6 +154,44 @@ public function get_mandate_id() { return $this->get_meta( '_stripe_mandate_id' ); } + /** + * Locks an order for payment intent processing for 5 minutes. + * + * @param stdClass $intent The intent that is being processed. + * @return bool A flag that indicates whether the order is already locked. + */ + public function lock_payment( $intent = null ) { + $this->read_meta_data( true ); + + $existing_lock = $this->get_lock_payment(); + + if ( $existing_lock ) { + $parts = explode( '|', $existing_lock ); // Format is: "{expiry_timestamp}" or "{expiry_timestamp}|{pi_xxxx}" if an intent is passed. + $expiration = (int) $parts[0]; + $locked_intent = ! empty( $parts[1] ) ? $parts[1] : ''; + + // If the lock is still active, return true. + if ( time() <= $expiration && ( empty( $intent ) || empty( $locked_intent ) || ( $intent->id ?? '' ) === $locked_intent ) ) { + return true; + } + } + + $new_lock = ( time() + 5 * MINUTE_IN_SECONDS ) . ( isset( $intent->id ) ? '|' . $intent->id : '' ); + + $this->set_lock_payment( $new_lock ); + $this->save_meta_data(); + + return false; + } + + /** + * Unlocks an order for processing by payment intents. + */ + public function unlock_payment() { + $this->delete_meta_data( '_stripe_lock_payment' ); + $this->save_meta_data(); + } + /** * Set the lock payment time. * diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index 4b554e51b9..a0ebd30211 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -220,7 +220,7 @@ public function process_webhook_payment( $notification, $retry = true ) { $is_pending_receiver = ( 'receiver' === $notification->data->object->flow ); - if ( $this->lock_order_payment( $order ) ) { + if ( $order->lock_payment() ) { return; } @@ -269,7 +269,7 @@ public function process_webhook_payment( $notification, $retry = true ) { // We want to retry. if ( $this->is_retryable_error( $response->error ) ) { // Unlock the order before retrying. - $this->unlock_order_payment( $order ); + $order->unlock_payment(); if ( $retry ) { // Don't do anymore retries after this. @@ -323,7 +323,7 @@ public function process_webhook_payment( $notification, $retry = true ) { } } - $this->unlock_order_payment( $order ); + $order->unlock_payment(); } /** @@ -632,7 +632,7 @@ public function process_webhook_refund( $notification ) { if ( 'stripe' === substr( (string) $order->get_payment_method(), 0, 6 ) ) { $charge = $order->get_transaction_id(); $captured = $order->charge_captured(); - $refund_id = $order->get_meta( '_stripe_refund_id' ); + $refund_id = $order->get_refund_id(); $currency = $order->get_currency(); $raw_amount = $refund_object->amount; @@ -712,7 +712,7 @@ public function process_webhook_refund_updated( $notification ) { if ( 'stripe' === substr( (string) $order->get_payment_method(), 0, 6 ) ) { $charge = $order->get_transaction_id(); - $refund_id = $order->get_meta( '_stripe_refund_id' ); + $refund_id = $order->get_refund_id(); $currency = $order->get_currency(); $raw_amount = $refund_object->amount; @@ -944,7 +944,7 @@ public function process_payment_intent_success( $notification ) { return; } - if ( $this->lock_order_payment( $order, $intent ) ) { + if ( $order->lock_payment( $intent ) ) { return; } @@ -967,7 +967,7 @@ public function process_payment_intent_success( $notification ) { WC_Stripe_Logger::log( "Stripe PaymentIntent $intent->id succeeded for order $order_id" ); $process_webhook_async = apply_filters( 'wc_stripe_process_payment_intent_webhook_async', true, $order, $intent, $notification ); - $is_awaiting_action = $order->get_meta( '_stripe_upe_waiting_for_redirect' ) ?? false; + $is_awaiting_action = $order->upe_waiting_for_redirect(); // Process the webhook now if it's for a voucher or wallet payment , or if filtered to process immediately and order is not awaiting action. if ( $is_voucher_payment || $is_wallet_payment || ( ! $process_webhook_async && ! $is_awaiting_action ) ) { @@ -1021,7 +1021,7 @@ public function process_payment_intent_success( $notification ) { break; } - $this->unlock_order_payment( $order ); + $order->unlock_payment(); } public function process_setup_intent( $notification ) { @@ -1042,7 +1042,7 @@ public function process_setup_intent( $notification ) { return; } - if ( $this->lock_order_payment( $order, $intent ) ) { + if ( $order->lock_payment( $intent ) ) { return; } @@ -1072,7 +1072,7 @@ public function process_setup_intent( $notification ) { $this->send_failed_order_email( $order_id, $status_update ); } - $this->unlock_order_payment( $order ); + $order->unlock_payment(); } /** diff --git a/includes/compat/trait-wc-stripe-subscriptions.php b/includes/compat/trait-wc-stripe-subscriptions.php index 0af3b08bb1..9b354c04ec 100644 --- a/includes/compat/trait-wc-stripe-subscriptions.php +++ b/includes/compat/trait-wc-stripe-subscriptions.php @@ -445,7 +445,7 @@ public function process_subscription_payment( $amount, $renewal_order, $retry = $is_authentication_required = false; } else { - $this->lock_order_payment( $renewal_order ); + $renewal_order->lock_payment(); $response = $this->create_and_confirm_intent_for_off_session( $renewal_order, $prepared_source, $amount ); $is_authentication_required = $this->is_authentication_required_for_payment( $response ); } @@ -506,7 +506,7 @@ public function process_subscription_payment( $amount, $renewal_order, $retry = /* translators: error message */ $renewal_order->update_status( 'failed' ); - $this->unlock_order_payment( $renewal_order ); + $renewal_order->unlock_payment(); return; } @@ -559,7 +559,7 @@ public function process_subscription_payment( $amount, $renewal_order, $retry = do_action( 'wc_gateway_stripe_process_payment_error', $e, $renewal_order ); } - $this->unlock_order_payment( $renewal_order ); + $renewal_order->unlock_payment(); } /** diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index 1ce40f8ac1..d1231ffb50 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -815,7 +815,7 @@ private function process_payment_with_deferred_intent( int $order_id ) { } // Lock the order before we create and confirm the payment/setup intents to prevent Stripe sending the success webhook before this request is completed. - $this->lock_order_payment( $order ); + $order->lock_payment(); if ( $payment_needed ) { // Throw an exception if the minimum order amount isn't met. @@ -909,7 +909,7 @@ private function process_payment_with_deferred_intent( int $order_id ) { } } - $this->unlock_order_payment( $order ); + $order->unlock_payment(); return array_merge( [ diff --git a/tests/phpunit/test-wc-stripe-payment-gateway.php b/tests/phpunit/test-wc-stripe-payment-gateway.php index c8ca0bc70a..1e0ccc1f49 100644 --- a/tests/phpunit/test-wc-stripe-payment-gateway.php +++ b/tests/phpunit/test-wc-stripe-payment-gateway.php @@ -631,26 +631,26 @@ public function test_render_subscription_payment_method() { */ public function test_lock_order_payment() { $order_1 = WC_Helper_Order::create_order(); - $locked = $this->gateway->lock_order_payment( $order_1 ); + $locked = $order_1->lock_payment(); $this->assertFalse( $locked ); $current_lock = $order_1->get_meta( '_stripe_lock_payment' ); $this->assertEqualsWithDelta( (int) $current_lock, ( time() + 5 * MINUTE_IN_SECONDS ), 3 ); - $locked = $this->gateway->lock_order_payment( $order_1 ); + $locked = $order_1->lock_payment(); $this->assertTrue( $locked ); // lock with an intent ID. $order_2 = WC_Helper_Order::create_order(); $intent_id = 'pi_123intent'; - $locked = $this->gateway->lock_order_payment( $order_2, $intent_id ); + $locked = $order_2->lock_payment( $intent_id ); $current_lock = $order_2->get_meta( '_stripe_lock_payment' ); $this->assertFalse( $locked ); - $locked = $this->gateway->lock_order_payment( $order_2, $intent_id ); + $locked = $order_2->lock_payment( $intent_id ); $this->assertTrue( $locked ); - $locked = $this->gateway->lock_order_payment( $order_2 ); // test that you don't need to pass the intent ID to check lock. + $locked = $order_2->lock_payment(); // test that you don't need to pass the intent ID to check lock. $this->assertTrue( $locked ); // test expired locks. @@ -658,7 +658,7 @@ public function test_lock_order_payment() { $order_3->set_lock_payment( time() - 1 ); $order_3->save_meta_data(); - $locked = $this->gateway->lock_order_payment( $order_3, $intent_id ); + $locked = $order_3->lock_payment( $intent_id ); $current_lock = $order_3->get_lock_payment(); $this->assertFalse( $locked ); @@ -666,10 +666,10 @@ public function test_lock_order_payment() { // test two instances of the same order, one locked and one not. $order_4 = WC_Helper_Order::create_order(); - $dup_order = wc_get_order( $order_4->get_id() ); + $dup_order = WC_Stripe_Helper::get_order( $order_4->get_id() ); - $this->gateway->lock_order_payment( $order_4 ); - $dup_locked = $this->gateway->lock_order_payment( $dup_order ); + $order_4->lock_payment(); + $dup_locked = $dup_order->lock_payment(); $this->assertTrue( $dup_locked ); // Confirms lock from $order_4 prevents payment on $dup_order. } } From 778eb9f7c7ff394b21e7409005c1392d9dc2f895 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 26 Dec 2024 15:55:55 -0300 Subject: [PATCH 06/24] Currency, net and fee getters and setters --- .../abstract-wc-stripe-payment-gateway.php | 12 +- includes/class-wc-gateway-stripe.php | 12 +- includes/class-wc-stripe-helper.php | 150 +++++++++++------- includes/class-wc-stripe-order-handler.php | 4 +- includes/class-wc-stripe-order.php | 137 +++++++++++++++- includes/class-wc-stripe-webhook-handler.php | 2 +- .../compat/trait-wc-stripe-subscriptions.php | 6 +- 7 files changed, 244 insertions(+), 79 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index 515cf720de..ea4b8f0e5d 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -1032,7 +1032,7 @@ public function save_source_to_order( $order, $source ) { * * @since 4.0.0 * @version 4.0.6 - * @param object $order The order object + * @param WC_Stripe_Order $order The order object * @param int $balance_transaction_id */ public function update_fees( $order, $balance_transaction_id ) { @@ -1046,18 +1046,18 @@ public function update_fees( $order, $balance_transaction_id ) { $net_refund = ! empty( $balance_transaction->net ) ? WC_Stripe_Helper::format_balance_fee( $balance_transaction, 'net' ) : 0; // Current data fee & net. - $fee_current = WC_Stripe_Helper::get_stripe_fee( $order ); - $net_current = WC_Stripe_Helper::get_stripe_net( $order ); + $fee_current = $order->get_fee(); + $net_current = $order->get_net(); // Calculation. $fee = (float) $fee_current + (float) $fee_refund; $net = (float) $net_current + (float) $net_refund; - WC_Stripe_Helper::update_stripe_fee( $order, $fee ); - WC_Stripe_Helper::update_stripe_net( $order, $net ); + $order->set_fee( $fee ); + $order->set_net( $net ); $currency = ! empty( $balance_transaction->currency ) ? strtoupper( $balance_transaction->currency ) : null; - WC_Stripe_Helper::update_stripe_currency( $order, $currency ); + $order->set_stripe_currency( $currency ); if ( is_callable( [ $order, 'save' ] ) ) { $order->save(); diff --git a/includes/class-wc-gateway-stripe.php b/includes/class-wc-gateway-stripe.php index 79e516ff63..fb67c49a25 100644 --- a/includes/class-wc-gateway-stripe.php +++ b/includes/class-wc-gateway-stripe.php @@ -547,10 +547,10 @@ public function display_order_fee( $order_id ) { return; } - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); - $fee = WC_Stripe_Helper::get_stripe_fee( $order ); - $currency = WC_Stripe_Helper::get_stripe_currency( $order ); + $fee = $order->get_fee(); + $currency = $order->get_stripe_currency(); if ( ! $fee || ! $currency ) { return; @@ -584,10 +584,10 @@ public function display_order_payout( $order_id ) { return; } - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); - $net = WC_Stripe_Helper::get_stripe_net( $order ); - $currency = WC_Stripe_Helper::get_stripe_currency( $order ); + $net = $order->get_net(); + $currency = $order->get_stripe_currency(); if ( ! $net || ! $currency ) { return; diff --git a/includes/class-wc-stripe-helper.php b/includes/class-wc-stripe-helper.php index 06dcf00347..5f660315ff 100644 --- a/includes/class-wc-stripe-helper.php +++ b/includes/class-wc-stripe-helper.php @@ -9,12 +9,47 @@ * @since 4.0.0 */ class WC_Stripe_Helper { - const SETTINGS_OPTION = 'woocommerce_stripe_settings'; - const LEGACY_META_NAME_FEE = 'Stripe Fee'; - const LEGACY_META_NAME_NET = 'Net Revenue From Stripe'; - const META_NAME_FEE = '_stripe_fee'; - const META_NAME_NET = '_stripe_net'; - const META_NAME_STRIPE_CURRENCY = '_stripe_currency'; + const SETTINGS_OPTION = 'woocommerce_stripe_settings'; + + /** + * Legacy meta key for Stripe fee. + * + * @deprecated 9.1.0 + */ + const LEGACY_META_NAME_FEE = 'Stripe Fee'; + /** + * Legacy meta key for Stripe net. + * + * @deprecated 9.1.0 + */ + const LEGACY_META_NAME_NET = 'Net Revenue From Stripe'; + + /** + * Meta key for Stripe fee. + * + * @deprecated 9.1.0 + */ + const META_NAME_FEE = '_stripe_fee'; + + /** + * Meta key for Stripe net. + * + * @deprecated 9.1.0 + */ + const META_NAME_NET = '_stripe_net'; + + /** + * Meta key for Stripe currency. + * + * @deprecated 9.1.0 + */ + const META_NAME_STRIPE_CURRENCY = '_stripe_currency'; + + /** + * Meta key for payment awaiting action. + * + * @deprecated 9.1.0 + */ const PAYMENT_AWAITING_ACTION_META = '_stripe_payment_awaiting_action'; /** @@ -61,144 +96,141 @@ public static function delete_main_stripe_settings() { * Gets the Stripe currency for order. * * @since 4.1.0 - * @param object $order + * @param WC_Stripe_Order $order * @return string $currency + * + * @deprecated 9.1.0 */ public static function get_stripe_currency( $order = null ) { + _deprecated_function( __METHOD__, '9.1.0', 'WC_Stripe_Order::get_stripe_currency' ); if ( is_null( $order ) ) { return false; } - return $order->get_meta( self::META_NAME_STRIPE_CURRENCY, true ); + return $order->get_stripe_currency(); } /** * Updates the Stripe currency for order. * * @since 4.1.0 - * @param object $order + * @param WC_Stripe_Order $order * @param string $currency + * + * @deprecated 9.1.0 */ public static function update_stripe_currency( $order, $currency ) { if ( is_null( $order ) ) { return false; } - $order->update_meta_data( self::META_NAME_STRIPE_CURRENCY, $currency ); + $order->set_stripe_currency( $currency ); } /** * Gets the Stripe fee for order. With legacy check. * * @since 4.1.0 - * @param object $order + * @param WC_Stripe_Order $order * @return string $amount + * + * @deprecated 9.1.0 */ public static function get_stripe_fee( $order = null ) { + _deprecated_function( __METHOD__, '9.1.0', 'WC_Stripe_Order::get_fee' ); if ( is_null( $order ) ) { return false; } - $amount = $order->get_meta( self::META_NAME_FEE, true ); - - // If not found let's check for legacy name. - if ( empty( $amount ) ) { - $amount = $order->get_meta( self::LEGACY_META_NAME_FEE, true ); - - // If found update to new name. - if ( $amount ) { - self::update_stripe_fee( $order, $amount ); - } - } - - return $amount; + return $order->get_fee(); } /** * Updates the Stripe fee for order. * * @since 4.1.0 - * @param object $order + * @param WC_Stripe_Order $order * @param float $amount + * + * @deprecated 9.1.0 */ public static function update_stripe_fee( $order = null, $amount = 0.0 ) { + _deprecated_function( __METHOD__, '9.1.0', 'WC_Stripe_Order::set_fee' ); if ( is_null( $order ) ) { return false; } - $order->update_meta_data( self::META_NAME_FEE, $amount ); + $order->set_fee( $amount ); } /** * Deletes the Stripe fee for order. * * @since 4.1.0 - * @param object $order + * @param WC_Stripe_Order $order + * + * @deprecated 9.1.0 */ public static function delete_stripe_fee( $order = null ) { + _deprecated_function( __METHOD__, '9.1.0', 'WC_Stripe_Order::delete_fee' ); if ( is_null( $order ) ) { return false; } - $order->delete_meta_data( self::META_NAME_FEE ); - $order->delete_meta_data( self::LEGACY_META_NAME_FEE ); + $order->delete_fee(); } /** * Gets the Stripe net for order. With legacy check. * * @since 4.1.0 - * @param object $order + * @param WC_Stripe_Order $order * @return string $amount + * + * @deprecated 9.1.0 */ public static function get_stripe_net( $order = null ) { + _deprecated_function( __METHOD__, '9.1.0', 'WC_Stripe_Order::get_net' ); if ( is_null( $order ) ) { return false; } - $amount = $order->get_meta( self::META_NAME_NET, true ); - - // If not found let's check for legacy name. - if ( empty( $amount ) ) { - $amount = $order->get_meta( self::LEGACY_META_NAME_NET, true ); - - // If found update to new name. - if ( $amount ) { - self::update_stripe_net( $order, $amount ); - } - } - - return $amount; + return $order->get_net(); } /** * Updates the Stripe net for order. * * @since 4.1.0 - * @param object $order + * @param WC_Stripe_Order $order * @param float $amount + * + * @deprecated 9.1.0 */ public static function update_stripe_net( $order = null, $amount = 0.0 ) { + _deprecated_function( __METHOD__, '9.1.0', 'WC_Stripe_Order::set_net' ); if ( is_null( $order ) ) { return false; } - $order->update_meta_data( self::META_NAME_NET, $amount ); + $order->set_net( $amount ); } /** * Deletes the Stripe net for order. * * @since 4.1.0 - * @param object $order + * @param WC_Stripe_Order $order + * + * @deprecated 9.1.0 */ public static function delete_stripe_net( $order = null ) { + _deprecated_function( __METHOD__, '9.1.0', 'WC_Stripe_Order::delete_net' ); if ( is_null( $order ) ) { return false; } - $order->delete_meta_data( self::META_NAME_NET ); - $order->delete_meta_data( self::LEGACY_META_NAME_NET ); + $order->delete_net(); } /** @@ -1408,33 +1440,31 @@ public static function get_stripe_gateway_ids() { * * This meta is primarily used to prevent orders from being cancelled by WooCommerce's hold stock settings. * - * @param WC_Order $order The order to add the metadata to. + * @param WC_Stripe_Order $order The order to add the metadata to. * @param bool $save Whether to save the order after adding the metadata. * * @return void + * + * @deprecated 9.1.0 */ public static function set_payment_awaiting_action( $order, $save = true ) { - $order->update_meta_data( self::PAYMENT_AWAITING_ACTION_META, wc_bool_to_string( true ) ); - - if ( $save ) { - $order->save(); - } + wc_deprecated_function( __METHOD__, '9.1.0', 'WC_Stripe_Order::set_payment_awaiting_action' ); + $order->set_payment_awaiting_action( true ); } /** * Removes the metadata from the order that was used to indicate that the payment was awaiting action. * - * @param WC_Order $order The order to remove the metadata from. + * @param WC_Stripe_Order $order The order to remove the metadata from. * @param bool $save Whether to save the order after removing the metadata. * * @return void + * + * @deprecated 9.1.0 */ public static function remove_payment_awaiting_action( $order, $save = true ) { - $order->delete_meta_data( self::PAYMENT_AWAITING_ACTION_META ); - - if ( $save ) { - $order->save(); - } + wc_deprecated_function( __METHOD__, '9.1.0', 'WC_Stripe_Order::remove_payment_awaiting_action' ); + $order->remove_payment_awaiting_action(); } /** diff --git a/includes/class-wc-stripe-order-handler.php b/includes/class-wc-stripe-order-handler.php index 93ee8911af..a940cf3bc9 100644 --- a/includes/class-wc-stripe-order-handler.php +++ b/includes/class-wc-stripe-order-handler.php @@ -493,7 +493,7 @@ public function woocommerce_tracks_event_properties( $properties, $prefixed_even * @since 6.9.0 * * @param bool $cancel_order Whether to cancel the order. - * @param WC_Order $order The order object. + * @param WC_Stripe_Order $order The order object. * * @return bool */ @@ -508,7 +508,7 @@ public function prevent_cancelling_orders_awaiting_action( $cancel_order, $order } // If the order is awaiting action and was modified within the last day, don't cancel it. - if ( wc_string_to_bool( $order->get_meta( WC_Stripe_Helper::PAYMENT_AWAITING_ACTION_META, true ) ) && $order->get_date_modified( 'edit' )->getTimestamp() > strtotime( '-1 day' ) ) { + if ( $order->payment_awaiting_action() && $order->get_date_modified( 'edit' )->getTimestamp() > strtotime( '-1 day' ) ) { $cancel_order = false; } diff --git a/includes/class-wc-stripe-order.php b/includes/class-wc-stripe-order.php index 2d0ea71216..aaec932cfc 100644 --- a/includes/class-wc-stripe-order.php +++ b/includes/class-wc-stripe-order.php @@ -9,6 +9,141 @@ * Wrapper for the original WC_Order class to allow custom getters and setter with the extension's specific metadata. */ class WC_Stripe_Order extends WC_Order { + /** + * Gets the Stripe fee for order. With legacy check. + * + * @return string $amount + */ + public function get_fee() { + $amount = $this->get_meta( '_stripe_fee' ); + + // If not found let's check for legacy name. + if ( empty( $amount ) ) { + $amount = $this->get_meta( 'Stripe Fee' ); + + // If found update to new name. + if ( $amount ) { + $this->set_fee( $amount ); + } + } + + return $amount; + } + + /** + * Updates the Stripe fee for order. + * + * @param float $amount + */ + public function set_fee( $amount = 0.0 ) { + $this->update_meta_data( '_stripe_fee', $amount ); + } + + /** + * Deletes the Stripe fee for order. + */ + public function delete_fee() { + $this->delete_meta_data( '_stripe_fee' ); + $this->delete_meta_data( 'Stripe Fee' ); + } + + /** + * Gets the Stripe net for order. With legacy check. + * + * @return string $amount + */ + public function get_net() { + $amount = $this->get_meta( '_stripe_net', true ); + + // If not found let's check for legacy name. + if ( empty( $amount ) ) { + $amount = $this->get_meta( 'Net Revenue From Stripe', true ); + + // If found update to new name. + if ( $amount ) { + $this->set_net( $amount ); + } + } + + return $amount; + } + + /** + * Updates the Stripe net for order. + * + * @param float $amount + */ + public function set_net( $amount = 0.0 ) { + $this->update_meta_data( '_stripe_net', $amount ); + } + + /** + * Deletes the Stripe net for order. + */ + public function delete_net() { + $this->delete_meta_data( '_stripe_net' ); + $this->delete_meta_data( 'Net Revenue From Stripe' ); + } + + /** + * Gets the Stripe currency for order. + * + * @return string $currency + */ + public function get_stripe_currency() { + return $this->get_meta( '_stripe_currency' ); + } + + /** + * Updates the Stripe currency for order. + * + * @param string $currency + */ + public function set_stripe_currency( $currency ) { + $this->update_meta_data( '_stripe_currency', $currency ); + } + + /** + * Adds metadata to the order to indicate that the payment is awaiting action. + * + * This meta is primarily used to prevent orders from being cancelled by WooCommerce's hold stock settings. + * + * @param bool $save Whether to save the order after adding the metadata. + * + * @return void + */ + public function set_payment_awaiting_action( $value, $save = true ) { + $this->update_meta_data( '_stripe_payment_awaiting_action', wc_bool_to_string( $value ) ); + + if ( $save ) { + $this->save(); + } + } + + /** + * Gets the metadata that indicates that the payment is awaiting action. + * + * @return bool Whether the payment is awaiting action. + */ + public function payment_awaiting_action() { + return wc_string_to_bool( $this->get_meta( '_stripe_payment_awaiting_action' ) ); + } + + /** + * Removes the metadata from the order that was used to indicate that the payment was awaiting action. + * + * @param bool $save Whether to save the order after removing the metadata. + * + * @return void + */ + public function remove_payment_awaiting_action( $save = true ) { + $this->delete_meta_data( '_stripe_payment_awaiting_action' ); + + if ( $save ) { + $this->save(); + } + } + /** * Set the preferred card brand. * @@ -340,7 +475,7 @@ public function set_charge_captured( $value ) { * @return bool */ public function charge_captured() { - return $this->get_meta( '_stripe_charge_captured' ) === 'yes'; + return wc_string_to_bool( $this->get_meta( '_stripe_charge_captured' ) ); } /** diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index a0ebd30211..e819911213 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -342,7 +342,7 @@ public function process_webhook_dispute( $notification ) { return; } - $this->set_stripe_order_status_before_hold( $order, $order->get_status() ); + $order->set_status_before_hold( $order->get_status() ); $needs_response = in_array( $notification->data->object->status, [ 'needs_response', 'warning_needs_response' ], true ); if ( $needs_response ) { diff --git a/includes/compat/trait-wc-stripe-subscriptions.php b/includes/compat/trait-wc-stripe-subscriptions.php index 9b354c04ec..b5b01be260 100644 --- a/includes/compat/trait-wc-stripe-subscriptions.php +++ b/includes/compat/trait-wc-stripe-subscriptions.php @@ -624,11 +624,11 @@ public function delete_resubscribe_meta( $resubscribe_order ) { /** * Don't transfer Stripe fee/ID meta to renewal orders. * - * @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription + * @param WC_Stripe_Order $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription */ public function delete_renewal_meta( $renewal_order ) { - WC_Stripe_Helper::delete_stripe_fee( $renewal_order ); - WC_Stripe_Helper::delete_stripe_net( $renewal_order ); + $renewal_order->delete_fee(); + $renewal_order->delete_net(); // Delete payment intent ID. $renewal_order->delete_meta_data( '_stripe_intent_id' ); From 8ab7aa7ae9af8e9ff56b117982e98aafe3158254 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 26 Dec 2024 16:14:54 -0300 Subject: [PATCH 07/24] Min amount validator --- .../abstract-wc-stripe-payment-gateway.php | 12 ++++--- includes/class-wc-gateway-stripe.php | 2 +- includes/class-wc-stripe-helper.php | 20 +++-------- includes/class-wc-stripe-order-handler.php | 2 +- includes/class-wc-stripe-order.php | 36 +++++++++++++++++++ includes/class-wc-stripe-webhook-handler.php | 2 +- .../compat/trait-wc-stripe-pre-orders.php | 4 +-- .../class-wc-gateway-stripe-alipay.php | 4 +-- .../class-wc-gateway-stripe-bancontact.php | 2 +- .../class-wc-gateway-stripe-eps.php | 4 +-- .../class-wc-gateway-stripe-giropay.php | 2 +- .../class-wc-gateway-stripe-ideal.php | 4 +-- .../class-wc-gateway-stripe-multibanco.php | 4 +-- .../class-wc-gateway-stripe-p24.php | 4 +-- .../class-wc-gateway-stripe-sepa.php | 4 +-- .../class-wc-gateway-stripe-sofort.php | 4 +-- .../class-wc-stripe-upe-payment-gateway.php | 6 ++-- 17 files changed, 72 insertions(+), 44 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index ea4b8f0e5d..20be253296 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -361,15 +361,17 @@ public function payment_icons() { * Validates that the order meets the minimum order amount * set by Stripe. * + * @param WC_Stripe_Order $order + * + * @throws WC_Stripe_Exception If the order does not meet the minimum amount. + * * @since 4.0.0 + * @deprecated 9.1.0 * @version 4.0.0 - * @param object $order */ public function validate_minimum_order_amount( $order ) { - if ( $order->get_total() * 100 < WC_Stripe_Helper::get_minimum_amount() ) { - /* translators: 1) amount (including currency symbol) */ - throw new WC_Stripe_Exception( 'Did not meet minimum amount', sprintf( __( 'Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( WC_Stripe_Helper::get_minimum_amount() / 100 ) ) ); - } + _deprecated_function( __METHOD__, '9.1.0', 'WC_Stripe_Helper::validate_minimum_amount' ); + $order->validate_minimum_amount(); } /** diff --git a/includes/class-wc-gateway-stripe.php b/includes/class-wc-gateway-stripe.php index fb67c49a25..df63c85c89 100644 --- a/includes/class-wc-gateway-stripe.php +++ b/includes/class-wc-gateway-stripe.php @@ -433,7 +433,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = } // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + $order->validate_minimum_amount(); WC_Stripe_Logger::log( "Info: Begin processing payment for order $order_id for the amount of {$order->get_total()}" ); diff --git a/includes/class-wc-stripe-helper.php b/includes/class-wc-stripe-helper.php index 5f660315ff..e52009d38f 100644 --- a/includes/class-wc-stripe-helper.php +++ b/includes/class-wc-stripe-helper.php @@ -1294,23 +1294,13 @@ private static function should_load_scripts_for_prb_location( $location ) { * * @param $payment_intent_id * @param $order WC_Stripe_Order + * + * @deprecated 9.1.0 */ public static function add_payment_intent_to_order( $payment_intent_id, $order ) { - $old_intent_id = $order->get_intent_id(); - if ( $old_intent_id === $payment_intent_id ) { - return; - } + wc_deprecated_function( __METHOD__, '9.1.0', 'WC_Stripe_Order::add_payment_intent' ); - $order->add_order_note( - sprintf( - /* translators: $1%s payment intent ID */ - __( 'Stripe payment intent created (Payment Intent ID: %1$s)', 'woocommerce-gateway-stripe' ), - $payment_intent_id - ) - ); - - $order->set_intent_id( $payment_intent_id ); - $order->save(); + $order->add_payment_intent_to_order( $payment_intent_id ); } /** @@ -1638,7 +1628,7 @@ public static function get_klarna_preferred_locale( $store_locale, $billing_coun * Wrapper to return an order using the extension's custom WC_Stripe_Order class. * * @param $order_id int Order ID. - * @return bool|WC_Order + * @return bool|WC_Stripe_Order */ public static function get_order( $order_id ) { $order = wc_get_order( $order_id ); diff --git a/includes/class-wc-stripe-order-handler.php b/includes/class-wc-stripe-order-handler.php index a940cf3bc9..ce6d9f51ff 100644 --- a/includes/class-wc-stripe-order-handler.php +++ b/includes/class-wc-stripe-order-handler.php @@ -154,7 +154,7 @@ public function process_redirect_payment( $order_id, $retry = true, $previous_er $response = null; // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + $order->validate_minimum_amount(); WC_Stripe_Logger::log( "Info: (Redirect) Begin processing payment for order $order_id for the amount of {$order->get_total()}" ); diff --git a/includes/class-wc-stripe-order.php b/includes/class-wc-stripe-order.php index aaec932cfc..99083cd0c5 100644 --- a/includes/class-wc-stripe-order.php +++ b/includes/class-wc-stripe-order.php @@ -9,6 +9,42 @@ * Wrapper for the original WC_Order class to allow custom getters and setter with the extension's specific metadata. */ class WC_Stripe_Order extends WC_Order { + /** + * Validates that the order meets the minimum order amount + * set by Stripe. + * + * @throws WC_Stripe_Exception If the order does not meet the minimum amount. + */ + public function validate_minimum_amount() { + if ( $this->get_total() * 100 < WC_Stripe_Helper::get_minimum_amount() ) { + /* translators: 1) amount (including currency symbol) */ + throw new WC_Stripe_Exception( 'Did not meet minimum amount', sprintf( __( 'Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( WC_Stripe_Helper::get_minimum_amount() / 100 ) ) ); + } + } + + /** + * Adds payment intent id and order note to order if payment intent is not already saved + * + * @param $payment_intent_id string The payment intent id to add to the order. + */ + public function add_payment_intent_to_order( $payment_intent_id ) { + $old_intent_id = $this->get_intent_id(); + if ( $old_intent_id === $payment_intent_id ) { + return; + } + + $this->add_order_note( + sprintf( + /* translators: $1%s payment intent ID */ + __( 'Stripe payment intent created (Payment Intent ID: %1$s)', 'woocommerce-gateway-stripe' ), + $payment_intent_id + ) + ); + + $this->set_intent_id( $payment_intent_id ); + $this->save(); + } + /** * Gets the Stripe fee for order. With legacy check. * diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index e819911213..2c3ce95788 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -237,7 +237,7 @@ public function process_webhook_payment( $notification, $retry = true ) { $response = null; // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + $order->validate_minimum_amount(); WC_Stripe_Logger::log( "Info: (Webhook) Begin processing payment for order $order_id for the amount of {$order->get_total()}" ); diff --git a/includes/compat/trait-wc-stripe-pre-orders.php b/includes/compat/trait-wc-stripe-pre-orders.php index aaad727392..a270a64af8 100644 --- a/includes/compat/trait-wc-stripe-pre-orders.php +++ b/includes/compat/trait-wc-stripe-pre-orders.php @@ -197,10 +197,10 @@ public function mark_order_as_pre_ordered( $order ) { */ public function process_pre_order( $order_id ) { try { - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); // @phpstan-ignore-line (minimum amount is defined in the classes that use this trait) + $order->validate_minimum_amount(); // @phpstan-ignore-line (minimum amount is defined in the classes that use this trait) $prepared_source = $this->prepare_source( get_current_user_id(), true ); // @phpstan-ignore-line (prepare_source is defined in the classes that use this trait) diff --git a/includes/payment-methods/class-wc-gateway-stripe-alipay.php b/includes/payment-methods/class-wc-gateway-stripe-alipay.php index ca497b1997..783df3e531 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-alipay.php +++ b/includes/payment-methods/class-wc-gateway-stripe-alipay.php @@ -252,10 +252,10 @@ public function create_source( $order ) { */ public function process_payment( $order_id, $retry = true, $force_save_save = false ) { try { - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + $order->validate_minimum_amount(); // This comes from the create account checkbox in the checkout page. $create_account = ! empty( $_POST['createaccount'] ) ? true : false; diff --git a/includes/payment-methods/class-wc-gateway-stripe-bancontact.php b/includes/payment-methods/class-wc-gateway-stripe-bancontact.php index 206a13d5f2..4c7d186801 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-bancontact.php +++ b/includes/payment-methods/class-wc-gateway-stripe-bancontact.php @@ -243,7 +243,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = $order = WC_Stripe_Helper::get_order( $order_id ); // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + $order->validate_minimum_amount(); // This comes from the create account checkbox in the checkout page. $create_account = ! empty( $_POST['createaccount'] ) ? true : false; diff --git a/includes/payment-methods/class-wc-gateway-stripe-eps.php b/includes/payment-methods/class-wc-gateway-stripe-eps.php index a27976b2ef..91f9c22b3e 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-eps.php +++ b/includes/payment-methods/class-wc-gateway-stripe-eps.php @@ -239,10 +239,10 @@ public function create_source( $order ) { */ public function process_payment( $order_id, $retry = true, $force_save_source = false ) { try { - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + $order->validate_minimum_amount(); // This comes from the create account checkbox in the checkout page. $create_account = ! empty( $_POST['createaccount'] ) ? true : false; diff --git a/includes/payment-methods/class-wc-gateway-stripe-giropay.php b/includes/payment-methods/class-wc-gateway-stripe-giropay.php index 76923fa792..62d789573f 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-giropay.php +++ b/includes/payment-methods/class-wc-gateway-stripe-giropay.php @@ -238,7 +238,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = $order = WC_Stripe_Helper::get_order( $order_id ); // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + $order->validate_minimum_amount(); // This comes from the create account checkbox in the checkout page. $create_account = ! empty( $_POST['createaccount'] ) ? true : false; diff --git a/includes/payment-methods/class-wc-gateway-stripe-ideal.php b/includes/payment-methods/class-wc-gateway-stripe-ideal.php index 092d791c81..c4ade6f1bb 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-ideal.php +++ b/includes/payment-methods/class-wc-gateway-stripe-ideal.php @@ -239,10 +239,10 @@ public function create_source( $order ) { */ public function process_payment( $order_id, $retry = true, $force_save_source = false ) { try { - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + $order->validate_minimum_amount(); // This comes from the create account checkbox in the checkout page. $create_account = ! empty( $_POST['createaccount'] ) ? true : false; diff --git a/includes/payment-methods/class-wc-gateway-stripe-multibanco.php b/includes/payment-methods/class-wc-gateway-stripe-multibanco.php index 95955f5cbe..80114fd99f 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-multibanco.php +++ b/includes/payment-methods/class-wc-gateway-stripe-multibanco.php @@ -337,10 +337,10 @@ public function create_source( $order ) { */ public function process_payment( $order_id, $retry = true, $force_save_source = false ) { try { - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + $order->validate_minimum_amount(); // This comes from the create account checkbox in the checkout page. $create_account = ! empty( $_POST['createaccount'] ) ? true : false; diff --git a/includes/payment-methods/class-wc-gateway-stripe-p24.php b/includes/payment-methods/class-wc-gateway-stripe-p24.php index 0558c3072c..55493184b3 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-p24.php +++ b/includes/payment-methods/class-wc-gateway-stripe-p24.php @@ -236,10 +236,10 @@ public function create_source( $order ) { */ public function process_payment( $order_id, $retry = true, $force_save_source = false ) { try { - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + $order->validate_minimum_amount(); // This comes from the create account checkbox in the checkout page. $create_account = ! empty( $_POST['createaccount'] ) ? true : false; diff --git a/includes/payment-methods/class-wc-gateway-stripe-sepa.php b/includes/payment-methods/class-wc-gateway-stripe-sepa.php index 6f804a4e9a..924cfa2fe9 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-sepa.php +++ b/includes/payment-methods/class-wc-gateway-stripe-sepa.php @@ -280,7 +280,7 @@ public function payment_fields() { */ public function process_payment( $order_id, $retry = true, $force_save_source = false ) { try { - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); if ( $this->has_subscription( $order_id ) ) { $force_save_source = true; @@ -312,7 +312,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = if ( $order->get_total() > 0 ) { // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + $order->validate_minimum_amount(); WC_Stripe_Logger::log( "Info: Begin processing payment for order $order_id for the amount of {$order->get_total()}" ); diff --git a/includes/payment-methods/class-wc-gateway-stripe-sofort.php b/includes/payment-methods/class-wc-gateway-stripe-sofort.php index 05e5d4e21f..0f19da7b55 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-sofort.php +++ b/includes/payment-methods/class-wc-gateway-stripe-sofort.php @@ -244,10 +244,10 @@ public function create_source( $order ) { */ public function process_payment( $order_id, $retry = true, $force_save_source = false ) { try { - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + $order->validate_minimum_amount(); // This comes from the create account checkbox in the checkout page. $create_account = ! empty( $_POST['createaccount'] ) ? true : false; diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index d1231ffb50..2719ca5928 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -819,7 +819,7 @@ private function process_payment_with_deferred_intent( int $order_id ) { if ( $payment_needed ) { // Throw an exception if the minimum order amount isn't met. - $this->validate_minimum_order_amount( $order ); + $order->validate_minimum_amount(); // Create a payment intent, or update an existing one associated with the order. $payment_intent = $this->process_payment_intent_for_order( $order, $payment_information ); @@ -954,7 +954,7 @@ private function process_payment_with_deferred_intent( int $order_id ) { */ public function process_payment_with_saved_payment_method( $order_id, $can_retry = true ) { try { - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); if ( $this->maybe_process_pre_orders( $order_id ) ) { return $this->process_pre_order( $order_id ); @@ -977,7 +977,7 @@ public function process_payment_with_saved_payment_method( $order_id, $can_retry if ( $payment_needed ) { // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + $order->validate_minimum_amount(); $request_details = $this->generate_payment_request( $order, $prepared_payment_method ); $endpoint = false !== $intent ? "payment_intents/$intent->id" : 'payment_intents'; From 0038c6ab4cc0ae74ebdc098a8eb8c99596acf426 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 26 Dec 2024 16:26:04 -0300 Subject: [PATCH 08/24] Get owner details --- .../abstract-wc-stripe-payment-gateway.php | 74 +++++-------------- includes/class-wc-stripe-order.php | 72 ++++++++++++++++++ includes/class-wc-stripe-webhook-handler.php | 4 +- .../class-wc-gateway-stripe-alipay.php | 4 +- .../class-wc-gateway-stripe-bancontact.php | 4 +- .../class-wc-gateway-stripe-eps.php | 4 +- .../class-wc-gateway-stripe-giropay.php | 4 +- .../class-wc-gateway-stripe-ideal.php | 4 +- .../class-wc-gateway-stripe-multibanco.php | 4 +- .../class-wc-gateway-stripe-p24.php | 4 +- .../class-wc-gateway-stripe-sofort.php | 4 +- 11 files changed, 108 insertions(+), 74 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index 20be253296..bc6022bad1 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -659,39 +659,14 @@ public function send_failed_order_email( $order_id, $status_update = [] ) { * * @since 4.0.0 * @version 4.0.0 - * @param object $order + * @param WC_Stripe_Order $order * @return object $details + * + * @deprecated 9.1.0 */ public function get_owner_details( $order ) { - $billing_first_name = $order->get_billing_first_name(); - $billing_last_name = $order->get_billing_last_name(); - - $details = []; - - $name = $billing_first_name . ' ' . $billing_last_name; - $email = $order->get_billing_email(); - $phone = $order->get_billing_phone(); - - if ( ! empty( $phone ) ) { - $details['phone'] = $phone; - } - - if ( ! empty( $name ) ) { - $details['name'] = $name; - } - - if ( ! empty( $email ) ) { - $details['email'] = $email; - } - - $details['address']['line1'] = $order->get_billing_address_1(); - $details['address']['line2'] = $order->get_billing_address_2(); - $details['address']['state'] = $order->get_billing_state(); - $details['address']['city'] = $order->get_billing_city(); - $details['address']['postal_code'] = $order->get_billing_postcode(); - $details['address']['country'] = $order->get_billing_country(); - - return (object) apply_filters( 'wc_stripe_owner_details', $details, $order ); + _deprecated_function( __METHOD__, '9.1.0', 'WC_Stripe_Order::get_owner_details' ); + return $order->get_owner_details(); } /** @@ -1153,12 +1128,12 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) { } if ( ! $intent_cancelled && $captured ) { - $this->lock_order_refund( $order ); + $order->lock_refund(); $response = WC_Stripe_API::request( $request, 'refunds' ); } } catch ( WC_Stripe_Exception $e ) { WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() ); - $this->unlock_order_refund( $order ); + $order->unlock_refund(); return new WP_Error( 'stripe_error', @@ -1172,7 +1147,7 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) { if ( ! empty( $response->error ) ) { // @phpstan-ignore-line (return statement is added) WC_Stripe_Logger::log( 'Error: ' . $response->error->message ); - $this->unlock_order_refund( $order ); + $order->unlock_refund(); return new WP_Error( 'stripe_error', @@ -1214,7 +1189,7 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) { $refund_message = sprintf( __( 'Refunded %1$s - Refund ID: %2$s - Reason: %3$s', 'woocommerce-gateway-stripe' ), $formatted_amount, $response->id, $reason ); $order->add_order_note( $refund_message ); - $this->unlock_order_refund( $order ); + $order->unlock_refund(); WC_Stripe_Logger::log( 'Success: ' . html_entity_decode( wp_strip_all_tags( $refund_message ) ) ); @@ -1704,38 +1679,25 @@ public function unlock_order_payment( $order ) { * @since 9.1.0 * @param WC_Stripe_Order $order The order that is being refunded. * @return bool A flag that indicates whether the order is already locked. + * + * @deprecated 9.1.0 Use WC_Stripe_Order::lock_refund instead. */ public function lock_order_refund( $order ) { - $order->read_meta_data( true ); - - $existing_lock = $order->get_lock_refund(); - - if ( $existing_lock ) { - $expiration = (int) $existing_lock; - - // If the lock is still active, return true. - if ( time() <= $expiration ) { - return true; - } - } - - $new_lock = time() + 5 * MINUTE_IN_SECONDS; - - $order->set_lock_refund( $new_lock ); - $order->save_meta_data(); - - return false; + _deprecated_function( __FUNCTION__, '9.1.0', 'WC_Stripe_Order::lock_refund' ); + return $order->lock_refund(); } /** * Unlocks an order for processing refund. * * @since 9.1.0 - * @param WC_Order $order The order that is being unlocked. + * @param WC_Stripe_Order $order The order that is being unlocked. + * + * @deprecated 9.1.0 Use WC_Stripe_Order::unlock_refund instead. */ public function unlock_order_refund( $order ) { - $order->delete_meta_data( '_stripe_lock_refund' ); - $order->save_meta_data(); + _deprecated_function( __FUNCTION__, '9.1.0', 'WC_Stripe_Order::unlock_refund' ); + $order->unlock_refund(); } /** diff --git a/includes/class-wc-stripe-order.php b/includes/class-wc-stripe-order.php index 99083cd0c5..b7e0c2887d 100644 --- a/includes/class-wc-stripe-order.php +++ b/includes/class-wc-stripe-order.php @@ -9,6 +9,43 @@ * Wrapper for the original WC_Order class to allow custom getters and setter with the extension's specific metadata. */ class WC_Stripe_Order extends WC_Order { + /** + * Get owner details. + * + * @return object $details + */ + public function get_owner_details() { + $billing_first_name = $this->get_billing_first_name(); + $billing_last_name = $this->get_billing_last_name(); + + $details = []; + + $name = $billing_first_name . ' ' . $billing_last_name; + $email = $this->get_billing_email(); + $phone = $this->get_billing_phone(); + + if ( ! empty( $phone ) ) { + $details['phone'] = $phone; + } + + if ( ! empty( $name ) ) { + $details['name'] = $name; + } + + if ( ! empty( $email ) ) { + $details['email'] = $email; + } + + $details['address']['line1'] = $this->get_billing_address_1(); + $details['address']['line2'] = $this->get_billing_address_2(); + $details['address']['state'] = $this->get_billing_state(); + $details['address']['city'] = $this->get_billing_city(); + $details['address']['postal_code'] = $this->get_billing_postcode(); + $details['address']['country'] = $this->get_billing_country(); + + return (object) apply_filters( 'wc_stripe_owner_details', $details, $this ); + } + /** * Validates that the order meets the minimum order amount * set by Stripe. @@ -199,6 +236,41 @@ public function get_card_brand() { return $this->get_meta( '_stripe_card_brand' ); } + /** + * Locks an order for refund processing for 5 minutes. + * + * @return bool A flag that indicates whether the order is already locked. + */ + public function lock_refund() { + $this->read_meta_data( true ); + + $existing_lock = $this->get_lock_refund(); + + if ( $existing_lock ) { + $expiration = (int) $existing_lock; + + // If the lock is still active, return true. + if ( time() <= $expiration ) { + return true; + } + } + + $new_lock = time() + 5 * MINUTE_IN_SECONDS; + + $this->set_lock_refund( $new_lock ); + $this->save_meta_data(); + + return false; + } + + /** + * Unlocks an order for processing refund. + */ + public function unlock_refund() { + $this->delete_meta_data( '_stripe_lock_refund' ); + $this->save_meta_data(); + } + /** * Set the lock refund time. * diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index 2c3ce95788..8a7f2b0981 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -655,7 +655,7 @@ public function process_webhook_refund( $notification ) { return; } - if ( $this->lock_order_refund( $order ) ) { + if ( $order->lock_refund() ) { return; } @@ -686,7 +686,7 @@ public function process_webhook_refund( $notification ) { $this->update_fees( $order, $refund_object->balance_transaction ); } - $this->unlock_order_refund( $order ); + $order->unlock_refund(); /* translators: 1) amount (including currency symbol) 2) transaction id 3) refund message */ $order->add_order_note( sprintf( __( 'Refunded %1$s - Refund ID: %2$s - %3$s', 'woocommerce-gateway-stripe' ), $amount, $refund_object->id, $reason ) ); diff --git a/includes/payment-methods/class-wc-gateway-stripe-alipay.php b/includes/payment-methods/class-wc-gateway-stripe-alipay.php index 783df3e531..d74e285b72 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-alipay.php +++ b/includes/payment-methods/class-wc-gateway-stripe-alipay.php @@ -217,7 +217,7 @@ public function payment_fields() { * * @since 4.0.0 * @version 4.0.0 - * @param object $order + * @param WC_Stripe_Order $order * @return mixed */ public function create_source( $order ) { @@ -227,7 +227,7 @@ public function create_source( $order ) { $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); $post_data['currency'] = strtolower( $currency ); $post_data['type'] = WC_Stripe_Payment_Methods::ALIPAY; - $post_data['owner'] = $this->get_owner_details( $order ); + $post_data['owner'] = $order->get_owner_details(); $post_data['redirect'] = [ 'return_url' => $return_url ]; if ( ! empty( $this->statement_descriptor ) ) { diff --git a/includes/payment-methods/class-wc-gateway-stripe-bancontact.php b/includes/payment-methods/class-wc-gateway-stripe-bancontact.php index 4c7d186801..97185a5622 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-bancontact.php +++ b/includes/payment-methods/class-wc-gateway-stripe-bancontact.php @@ -204,7 +204,7 @@ public function payment_fields() { * * @since 4.0.0 * @version 4.0.0 - * @param object $order + * @param WC_Stripe_Order $order * @return mixed */ public function create_source( $order ) { @@ -214,7 +214,7 @@ public function create_source( $order ) { $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); $post_data['currency'] = strtolower( $currency ); $post_data['type'] = WC_Stripe_Payment_Methods::BANCONTACT; - $post_data['owner'] = $this->get_owner_details( $order ); + $post_data['owner'] = $order->get_owner_details(); $post_data['redirect'] = [ 'return_url' => $return_url ]; $post_data['bancontact'] = [ 'preferred_language' => $this->get_locale() ]; diff --git a/includes/payment-methods/class-wc-gateway-stripe-eps.php b/includes/payment-methods/class-wc-gateway-stripe-eps.php index 91f9c22b3e..fbdbcd18a4 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-eps.php +++ b/includes/payment-methods/class-wc-gateway-stripe-eps.php @@ -204,7 +204,7 @@ public function payment_fields() { * * @since 4.1.0 * @version 4.1.0 - * @param object $order + * @param WC_Stripe_Order $order * @return mixed */ public function create_source( $order ) { @@ -214,7 +214,7 @@ public function create_source( $order ) { $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); $post_data['currency'] = strtolower( $currency ); $post_data['type'] = WC_Stripe_Payment_Methods::EPS; - $post_data['owner'] = $this->get_owner_details( $order ); + $post_data['owner'] = $order->get_owner_details(); $post_data['redirect'] = [ 'return_url' => $return_url ]; if ( ! empty( $this->statement_descriptor ) ) { diff --git a/includes/payment-methods/class-wc-gateway-stripe-giropay.php b/includes/payment-methods/class-wc-gateway-stripe-giropay.php index 62d789573f..d888424d19 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-giropay.php +++ b/includes/payment-methods/class-wc-gateway-stripe-giropay.php @@ -200,7 +200,7 @@ public function payment_fields() { * * @since 4.0.0 * @version 4.0.0 - * @param object $order + * @param WC_Stripe_Order $order * @return mixed */ public function create_source( $order ) { @@ -210,7 +210,7 @@ public function create_source( $order ) { $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); $post_data['currency'] = strtolower( $currency ); $post_data['type'] = WC_Stripe_Payment_Methods::GIROPAY; - $post_data['owner'] = $this->get_owner_details( $order ); + $post_data['owner'] = $order->get_owner_details(); $post_data['redirect'] = [ 'return_url' => $return_url ]; if ( ! empty( $this->statement_descriptor ) ) { diff --git a/includes/payment-methods/class-wc-gateway-stripe-ideal.php b/includes/payment-methods/class-wc-gateway-stripe-ideal.php index c4ade6f1bb..a3de090c3b 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-ideal.php +++ b/includes/payment-methods/class-wc-gateway-stripe-ideal.php @@ -204,7 +204,7 @@ public function payment_fields() { * * @since 4.0.0 * @version 4.0.0 - * @param object $order + * @param WC_Stripe_Order $order * @return mixed */ public function create_source( $order ) { @@ -214,7 +214,7 @@ public function create_source( $order ) { $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); $post_data['currency'] = strtolower( $currency ); $post_data['type'] = WC_Stripe_Payment_Methods::IDEAL; - $post_data['owner'] = $this->get_owner_details( $order ); + $post_data['owner'] = $order->get_owner_details(); $post_data['redirect'] = [ 'return_url' => $return_url ]; if ( ! empty( $this->statement_descriptor ) ) { diff --git a/includes/payment-methods/class-wc-gateway-stripe-multibanco.php b/includes/payment-methods/class-wc-gateway-stripe-multibanco.php index 80114fd99f..ebc56ec07e 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-multibanco.php +++ b/includes/payment-methods/class-wc-gateway-stripe-multibanco.php @@ -302,7 +302,7 @@ public function save_instructions( $order, $source_object ) { * * @since 4.1.0 * @version 4.1.0 - * @param object $order + * @param WC_Stripe_Order $order * @return mixed */ public function create_source( $order ) { @@ -312,7 +312,7 @@ public function create_source( $order ) { $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); $post_data['currency'] = strtolower( $currency ); $post_data['type'] = WC_Stripe_Payment_Methods::MULTIBANCO; - $post_data['owner'] = $this->get_owner_details( $order ); + $post_data['owner'] = $order->get_owner_details(); $post_data['redirect'] = [ 'return_url' => $return_url ]; if ( ! empty( $this->statement_descriptor ) ) { diff --git a/includes/payment-methods/class-wc-gateway-stripe-p24.php b/includes/payment-methods/class-wc-gateway-stripe-p24.php index 55493184b3..571668f968 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-p24.php +++ b/includes/payment-methods/class-wc-gateway-stripe-p24.php @@ -205,7 +205,7 @@ public function payment_fields() { * * @since 4.0.0 * @version 4.0.0 - * @param object $order + * @param WC_Stripe_Order $order * @return mixed */ public function create_source( $order ) { @@ -215,7 +215,7 @@ public function create_source( $order ) { $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); $post_data['currency'] = strtolower( $currency ); $post_data['type'] = WC_Stripe_Payment_Methods::P24; - $post_data['owner'] = $this->get_owner_details( $order ); + $post_data['owner'] = $order->get_owner_details(); $post_data['redirect'] = [ 'return_url' => $return_url ]; WC_Stripe_Logger::log( 'Info: Begin creating P24 source' ); diff --git a/includes/payment-methods/class-wc-gateway-stripe-sofort.php b/includes/payment-methods/class-wc-gateway-stripe-sofort.php index 0f19da7b55..aa566c6466 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-sofort.php +++ b/includes/payment-methods/class-wc-gateway-stripe-sofort.php @@ -204,7 +204,7 @@ public function payment_fields() { * * @since 4.0.0 * @version 4.0.0 - * @param object $order + * @param WC_Stripe_Order $order * @return mixed */ public function create_source( $order ) { @@ -215,7 +215,7 @@ public function create_source( $order ) { $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); $post_data['currency'] = strtolower( $currency ); $post_data['type'] = WC_Stripe_Payment_Methods::SOFORT; - $post_data['owner'] = $this->get_owner_details( $order ); + $post_data['owner'] = $order->get_owner_details(); $post_data['redirect'] = [ 'return_url' => $return_url ]; $post_data['sofort'] = [ 'country' => $bank_country, From 9a9947ea08df1a5e651253d360cf7cc619fdff04 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 26 Dec 2024 16:35:55 -0300 Subject: [PATCH 09/24] Fix missing include --- woocommerce-gateway-stripe.php | 1 + 1 file changed, 1 insertion(+) diff --git a/woocommerce-gateway-stripe.php b/woocommerce-gateway-stripe.php index 4707c47820..1c7e9b9782 100644 --- a/woocommerce-gateway-stripe.php +++ b/woocommerce-gateway-stripe.php @@ -191,6 +191,7 @@ public function init() { require_once dirname( __FILE__ ) . '/includes/admin/class-wc-stripe-privacy.php'; } + require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-order.php'; require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-feature-flags.php'; require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-upe-compatibility.php'; require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-co-branded-cc-compatibility.php'; From 8807b059449ef3d1112c35632d828c7075c762c8 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 26 Dec 2024 16:39:47 -0300 Subject: [PATCH 10/24] Fix tests --- .../abstracts/abstract-wc-stripe-payment-gateway-voucher.php | 2 +- includes/abstracts/abstract-wc-stripe-payment-gateway.php | 2 +- includes/class-wc-stripe-intent-controller.php | 2 +- .../payment-methods/class-wc-stripe-upe-payment-gateway.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php b/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php index 1766887b7d..e31f127755 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php @@ -265,7 +265,7 @@ public function process_payment( $order_id, $retry = true, $force_save_save = fa $order->update_status( 'pending', __( 'Awaiting payment.', 'woocommerce-gateway-stripe' ) ); $order->save(); - WC_Stripe_Helper::add_payment_intent_to_order( $intent->id, $order ); + $order->add_payment_intent_to_order( $intent->id ); return [ 'result' => 'success', diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index bc6022bad1..71ddb751a6 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -1581,7 +1581,7 @@ public function save_intent_to_order( $order, $intent ) { } if ( 'payment_intent' === $intent->object ) { - WC_Stripe_Helper::add_payment_intent_to_order( $intent->id, $order ); + $order->add_payment_intent_to_order( $intent->id ); // Add the mandate id necessary for renewal payments with Indian cards if it's present. $charge = $this->get_latest_charge_from_intent( $intent ); diff --git a/includes/class-wc-stripe-intent-controller.php b/includes/class-wc-stripe-intent-controller.php index 21a044150d..f3f165eba4 100644 --- a/includes/class-wc-stripe-intent-controller.php +++ b/includes/class-wc-stripe-intent-controller.php @@ -475,7 +475,7 @@ public function update_payment_intent( $payment_intent_id = '', $order_id = null $order->update_status( 'pending', __( 'Awaiting payment.', 'woocommerce-gateway-stripe' ) ); $order->save(); - WC_Stripe_Helper::add_payment_intent_to_order( $payment_intent_id, $order ); + $order->add_payment_intent_to_order( $payment_intent_id ); } return [ diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index 2719ca5928..07b2ec4ab9 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -725,7 +725,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = null // $prepared_source parameter is not necessary for adding mandate information. ); - WC_Stripe_Helper::add_payment_intent_to_order( $payment_intent_id, $order ); + $order->add_payment_intent_to_order( $payment_intent_id ); $order->update_status( 'pending', __( 'Awaiting payment.', 'woocommerce-gateway-stripe' ) ); $order->set_upe_payment_type( $selected_upe_payment_type ); From 8d202d1842e2d4051f7b48e3f52b1964f28e5602 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 26 Dec 2024 16:43:26 -0300 Subject: [PATCH 11/24] Fix tests --- includes/class-wc-gateway-stripe.php | 2 +- includes/class-wc-stripe-intent-controller.php | 4 ++-- .../payment-methods/class-wc-stripe-upe-payment-gateway.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/class-wc-gateway-stripe.php b/includes/class-wc-gateway-stripe.php index df63c85c89..bc85c21498 100644 --- a/includes/class-wc-gateway-stripe.php +++ b/includes/class-wc-gateway-stripe.php @@ -909,7 +909,7 @@ public function verify_intent_after_checkout( $order ) { * This meta is to prevent stores with short hold stock settings from cancelling orders while waiting for payment to be finalised by Stripe or the customer (i.e. completing 3DS or payment redirects). * Now that payment is confirmed, we can remove this meta. */ - WC_Stripe_Helper::remove_payment_awaiting_action( $order ); + $order->remove_payment_awaiting_action(); } /** diff --git a/includes/class-wc-stripe-intent-controller.php b/includes/class-wc-stripe-intent-controller.php index f3f165eba4..62f0613868 100644 --- a/includes/class-wc-stripe-intent-controller.php +++ b/includes/class-wc-stripe-intent-controller.php @@ -571,7 +571,7 @@ public function update_order_status_ajax() { } $order_id = isset( $_POST['order_id'] ) ? absint( $_POST['order_id'] ) : false; - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); if ( ! $order ) { throw new WC_Stripe_Exception( 'order_not_found', __( "We're not able to process this payment. Please try again later.", 'woocommerce-gateway-stripe' ) ); } @@ -604,7 +604,7 @@ public function update_order_status_ajax() { /* translators: error message */ if ( $order ) { // Remove the awaiting confirmation order meta, don't save the order since it'll be saved in the next `update_status()` call. - WC_Stripe_Helper::remove_payment_awaiting_action( $order, false ); + $order->remove_payment_awaiting_action(); $order->update_status( 'failed' ); } diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index 07b2ec4ab9..875c63245e 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -1366,7 +1366,7 @@ public function process_order_for_confirmed_intent( $order, $intent_id, $save_pa * This meta is to prevent stores with short hold stock settings from cancelling orders while waiting for payment to be finalised by Stripe or the customer (i.e. completing 3DS or payment redirects). * Now that payment is confirmed, we can remove this meta. */ - WC_Stripe_Helper::remove_payment_awaiting_action( $order, false ); + $order->remove_payment_awaiting_action(); $order->save(); } From e33f54dd5f44e0ec3a63fe07722e13c651cac520 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 26 Dec 2024 19:11:43 -0300 Subject: [PATCH 12/24] Fix tests --- tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php | 2 +- tests/phpunit/test-wc-stripe-sub-initial.php | 2 +- tests/phpunit/test-wc-stripe-sub-renewal.php | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php b/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php index ff1d80b755..a7a0a8fc1c 100644 --- a/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php +++ b/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php @@ -2264,7 +2264,7 @@ function( $passed_order ) use ( $order ) { ); $response = $this->mock_gateway->process_payment( $order_id ); - $final_order = wc_get_order( $order_id ); + $final_order = WC_Stripe_Helper::get_order( $order_id ); $note = wc_get_order_notes( [ 'order_id' => $order_id, diff --git a/tests/phpunit/test-wc-stripe-sub-initial.php b/tests/phpunit/test-wc-stripe-sub-initial.php index 7582675f5d..f8b4b44402 100644 --- a/tests/phpunit/test-wc-stripe-sub-initial.php +++ b/tests/phpunit/test-wc-stripe-sub-initial.php @@ -194,7 +194,7 @@ public function test_initial_intent_parameters() { $this->assertEquals( $result['result'], 'success' ); $this->assertArrayHasKey( 'redirect', $result ); - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); $order_data = $order->get_intent_id(); $this->assertEquals( $order_data, 'pi_123abc' ); diff --git a/tests/phpunit/test-wc-stripe-sub-renewal.php b/tests/phpunit/test-wc-stripe-sub-renewal.php index 0014992edb..227e925aa2 100644 --- a/tests/phpunit/test-wc-stripe-sub-renewal.php +++ b/tests/phpunit/test-wc-stripe-sub-renewal.php @@ -224,7 +224,7 @@ public function test_renewal_successful() { // Assert that we saved the payment intent to the order. $order_id = $renewal_order->get_id(); - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); $order_data = $order->get_intent_id(); $this->assertEquals( $order_data, 'pi_123abc' ); @@ -340,7 +340,7 @@ public function test_renewal_authorization_required() { // Assert that we saved the payment intent to the order. $order_id = $renewal_order->get_id(); - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); $order_data = $order->get_intent_id(); $order_transaction_id = $order->get_transaction_id(); From ad86b14e48145a47c983dfb95ca6bf9326d1abd1 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 26 Dec 2024 19:20:13 -0300 Subject: [PATCH 13/24] Fix tests --- includes/class-wc-gateway-stripe.php | 2 +- includes/class-wc-stripe-helper.php | 2 +- includes/class-wc-stripe-order-handler.php | 2 +- includes/class-wc-stripe-order.php | 6 ++---- .../payment-methods/class-wc-stripe-upe-payment-gateway.php | 2 +- tests/phpunit/test-class-wc-stripe-order-handler.php | 2 +- tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php | 4 ++-- 7 files changed, 9 insertions(+), 11 deletions(-) diff --git a/includes/class-wc-gateway-stripe.php b/includes/class-wc-gateway-stripe.php index bc85c21498..68296a0c82 100644 --- a/includes/class-wc-gateway-stripe.php +++ b/includes/class-wc-gateway-stripe.php @@ -476,7 +476,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = $order->unlock_payment(); // If the order requires some action from the customer, add meta to the order to prevent it from being cancelled by WooCommerce's hold stock settings. - WC_Stripe_Helper::set_payment_awaiting_action( $order ); + $order->set_payment_awaiting_action(); if ( is_wc_endpoint_url( 'order-pay' ) ) { $redirect_url = add_query_arg( 'wc-stripe-confirmation', 1, $order->get_checkout_payment_url( false ) ); diff --git a/includes/class-wc-stripe-helper.php b/includes/class-wc-stripe-helper.php index e52009d38f..d4ec24bf4a 100644 --- a/includes/class-wc-stripe-helper.php +++ b/includes/class-wc-stripe-helper.php @@ -1439,7 +1439,7 @@ public static function get_stripe_gateway_ids() { */ public static function set_payment_awaiting_action( $order, $save = true ) { wc_deprecated_function( __METHOD__, '9.1.0', 'WC_Stripe_Order::set_payment_awaiting_action' ); - $order->set_payment_awaiting_action( true ); + $order->set_payment_awaiting_action( $save ); } /** diff --git a/includes/class-wc-stripe-order-handler.php b/includes/class-wc-stripe-order-handler.php index ce6d9f51ff..ba277f6e01 100644 --- a/includes/class-wc-stripe-order-handler.php +++ b/includes/class-wc-stripe-order-handler.php @@ -324,7 +324,7 @@ private function maybe_process_legacy_redirect() { */ public function capture_payment( $order_id ) { $result = new stdClass(); - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); if ( WC_Stripe_Helper::payment_method_allows_manual_capture( $order->get_payment_method() ) ) { $charge = $order->get_transaction_id(); diff --git a/includes/class-wc-stripe-order.php b/includes/class-wc-stripe-order.php index b7e0c2887d..df878fdc7a 100644 --- a/includes/class-wc-stripe-order.php +++ b/includes/class-wc-stripe-order.php @@ -181,12 +181,10 @@ public function set_stripe_currency( $currency ) { * * This meta is primarily used to prevent orders from being cancelled by WooCommerce's hold stock settings. * - * @param bool $save Whether to save the order after adding the metadata. - * * @return void */ - public function set_payment_awaiting_action( $value, $save = true ) { - $this->update_meta_data( '_stripe_payment_awaiting_action', wc_bool_to_string( $value ) ); + public function set_payment_awaiting_action( $save = true ) { + $this->update_meta_data( '_stripe_payment_awaiting_action', wc_bool_to_string( true ) ); if ( $save ) { $this->save(); diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index 875c63245e..ab75627de1 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -884,7 +884,7 @@ private function process_payment_with_deferred_intent( int $order_id ) { } // If the order requires some action from the customer, add meta to the order to prevent it from being cancelled by WooCommerce's hold stock settings. - WC_Stripe_Helper::set_payment_awaiting_action( $order, false ); + $order->set_payment_awaiting_action( false ); // Prevent processing the payment intent webhooks while also processing the redirect payment (also prevents duplicate Stripe meta stored on the order). $order->set_upe_waiting_for_redirect( true ); diff --git a/tests/phpunit/test-class-wc-stripe-order-handler.php b/tests/phpunit/test-class-wc-stripe-order-handler.php index a156251afe..68bcfd593d 100644 --- a/tests/phpunit/test-class-wc-stripe-order-handler.php +++ b/tests/phpunit/test-class-wc-stripe-order-handler.php @@ -19,7 +19,7 @@ public function set_up() { public function test_prevent_cancelling_orders_awaiting_action() { $order = WC_Helper_Order::create_order(); - WC_Stripe_Helper::set_payment_awaiting_action( $order ); + $order->set_payment_awaiting_action(); // Read in a fresh order object with meta like `date_modified` set. $order = wc_get_order( $order->get_id() ); diff --git a/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php b/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php index a7a0a8fc1c..8e9e9242c1 100644 --- a/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php +++ b/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php @@ -1185,7 +1185,7 @@ public function test_process_response_updates_order_by_charge_status() { $charge_mock['captured'] = true; $charge_mock['id'] = 'ch_mock_2'; $this->mock_gateway->process_response( $this->array_to_object( $charge_mock ), wc_get_order( $order_id ) ); - $test_order = wc_get_order( $order_id ); + $test_order = WC_Stripe_Helper::get_order( $order_id ); $this->assertTrue( $test_order->charge_captured() ); $this->assertEquals( 'processing', $test_order->get_status() ); @@ -1194,7 +1194,7 @@ public function test_process_response_updates_order_by_charge_status() { $charge_mock['status'] = 'pending'; $charge_mock['id'] = 'ch_mock_3'; $this->mock_gateway->process_response( $this->array_to_object( $charge_mock ), wc_get_order( $order_id ) ); - $test_order = wc_get_order( $order_id ); + $test_order = WC_Stripe_Helper::get_order( $order_id ); $this->assertTrue( $test_order->charge_captured() ); $this->assertEquals( $charge_mock['id'], $test_order->get_transaction_id() ); From c856d7ee45ee6bbaefe398aa770a1b7894e83972 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 26 Dec 2024 19:23:58 -0300 Subject: [PATCH 14/24] Fix tests --- .../phpunit/test-class-wc-stripe-upe-payment-gateway.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php b/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php index 8e9e9242c1..9bee5c1267 100644 --- a/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php +++ b/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php @@ -1174,7 +1174,7 @@ public function test_process_response_updates_order_by_charge_status() { // Test no charge captured. $charge_mock['captured'] = false; $charge_mock['id'] = 'ch_mock_1'; - $this->mock_gateway->process_response( $this->array_to_object( $charge_mock ), wc_get_order( $order_id ) ); + $this->mock_gateway->process_response( $this->array_to_object( $charge_mock ), WC_Stripe_Helper::get_order( $order_id ) ); $test_order = WC_Stripe_Helper::get_order( $order_id ); $this->assertFalse( $test_order->charge_captured() ); @@ -1184,7 +1184,7 @@ public function test_process_response_updates_order_by_charge_status() { // Test charge succeeds. $charge_mock['captured'] = true; $charge_mock['id'] = 'ch_mock_2'; - $this->mock_gateway->process_response( $this->array_to_object( $charge_mock ), wc_get_order( $order_id ) ); + $this->mock_gateway->process_response( $this->array_to_object( $charge_mock ), WC_Stripe_Helper::get_order( $order_id ) ); $test_order = WC_Stripe_Helper::get_order( $order_id ); $this->assertTrue( $test_order->charge_captured() ); @@ -1193,7 +1193,7 @@ public function test_process_response_updates_order_by_charge_status() { // Test charge pending. $charge_mock['status'] = 'pending'; $charge_mock['id'] = 'ch_mock_3'; - $this->mock_gateway->process_response( $this->array_to_object( $charge_mock ), wc_get_order( $order_id ) ); + $this->mock_gateway->process_response( $this->array_to_object( $charge_mock ), WC_Stripe_Helper::get_order( $order_id ) ); $test_order = WC_Stripe_Helper::get_order( $order_id ); $this->assertTrue( $test_order->charge_captured() ); @@ -1205,7 +1205,7 @@ public function test_process_response_updates_order_by_charge_status() { $charge_mock['id'] = 'ch_mock_4'; $exception = null; try { - $this->mock_gateway->process_response( $this->array_to_object( $charge_mock ), wc_get_order( $order_id ) ); + $this->mock_gateway->process_response( $this->array_to_object( $charge_mock ), WC_Stripe_Helper::get_order( $order_id ) ); } catch ( WC_Stripe_Exception $e ) { // Test that exception is thrown. $exception = $e; From 45f0177768408737e2ec0538980ae8df5bd25f0e Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 26 Dec 2024 19:27:01 -0300 Subject: [PATCH 15/24] Fix tests --- ...st-class-wc-stripe-upe-payment-gateway.php | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php b/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php index 9bee5c1267..8f3c53c97e 100644 --- a/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php +++ b/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php @@ -330,7 +330,7 @@ public function test_process_payment_returns_valid_response() { $this->mock_gateway->expects( $this->any() ) ->method( 'get_stripe_customer_from_order' ) - ->with( wc_get_order( $order_id ) ) + ->with( WC_Stripe_Helper::get_order( $order_id ) ) ->will( $this->returnValue( $this->mock_stripe_customer ) ); @@ -632,7 +632,7 @@ public function test_process_payment_deferred_intent_handles_exception() { $this->assertEquals( 'failure', $response['result'] ); - $processed_order = wc_get_order( $order_id ); + $processed_order = WC_Stripe_Helper::get_order( $order_id ); $this->assertEquals( 'failed', $processed_order->get_status() ); } @@ -678,7 +678,7 @@ public function test_process_payment_deferred_intent_bails_with_empty_payment_ty $this->assertEquals( 'failure', $response['result'] ); - $processed_order = wc_get_order( $order_id ); + $processed_order = WC_Stripe_Helper::get_order( $order_id ); $this->assertEquals( 'failed', $processed_order->get_status() ); } @@ -724,7 +724,7 @@ public function test_process_payment_deferred_intent_bails_with_invalid_payment_ $this->assertEquals( 'failure', $response['result'] ); - $processed_order = wc_get_order( $order_id ); + $processed_order = WC_Stripe_Helper::get_order( $order_id ); $this->assertEquals( 'failed', $processed_order->get_status() ); } @@ -862,7 +862,7 @@ public function test_process_redirect_payment_only_runs_once() { // attempt to reprocess the order and confirm status is unchanged $this->mock_gateway->process_upe_redirect_payment( $order_id, $payment_intent_id, false ); - $final_order = wc_get_order( $order_id ); + $final_order = WC_Stripe_Helper::get_order( $order_id ); $this->assertEquals( 'failed', $final_order->get_status() ); } @@ -893,7 +893,7 @@ public function test_checkout_without_payment_uses_setup_intents() { $this->mock_gateway->expects( $this->any() ) ->method( 'get_stripe_customer_from_order' ) - ->with( wc_get_order( $order_id ) ) + ->with( WC_Stripe_Helper::get_order( $order_id ) ) ->will( $this->returnValue( $this->mock_stripe_customer ) ); @@ -908,7 +908,7 @@ public function test_checkout_without_payment_uses_setup_intents() { $this->mock_gateway->process_upe_redirect_payment( $order_id, $setup_intent_id, true ); - $final_order = wc_get_order( $order_id ); + $final_order = WC_Stripe_Helper::get_order( $order_id ); $this->assertEquals( 'processing', $final_order->get_status() ); $this->assertEquals( $customer_id, $final_order->get_customer_id() ); @@ -944,7 +944,7 @@ public function test_checkout_saves_payment_method_to_order() { $this->mock_gateway->expects( $this->any() ) ->method( 'get_stripe_customer_from_order' ) - ->with( wc_get_order( $order_id ) ) + ->with( WC_Stripe_Helper::get_order( $order_id ) ) ->will( $this->returnValue( $this->mock_stripe_customer ) ); @@ -970,7 +970,7 @@ public function test_checkout_saves_payment_method_to_order() { $this->mock_gateway->process_upe_redirect_payment( $order_id, $payment_intent_id, true ); - $final_order = wc_get_order( $order_id ); + $final_order = WC_Stripe_Helper::get_order( $order_id ); $this->assertEquals( 'processing', $final_order->get_status() ); $this->assertEquals( $payment_intent_id, $final_order->get_intent_id() ); @@ -1009,7 +1009,7 @@ public function test_checkout_saves_sepa_generated_payment_method_to_order() { $this->mock_gateway->expects( $this->any() ) ->method( 'get_stripe_customer_from_order' ) - ->with( wc_get_order( $order_id ) ) + ->with( WC_Stripe_Helper::get_order( $order_id ) ) ->will( $this->returnValue( $this->mock_stripe_customer ) ); @@ -1038,7 +1038,7 @@ public function test_checkout_saves_sepa_generated_payment_method_to_order() { $this->mock_gateway->process_upe_redirect_payment( $order_id, $payment_intent_id, true ); - $final_order = wc_get_order( $order_id ); + $final_order = WC_Stripe_Helper::get_order( $order_id ); $this->assertEquals( 'processing', $final_order->get_status() ); $this->assertEquals( $payment_intent_id, $final_order->get_intent_id() ); @@ -1084,7 +1084,7 @@ public function test_setup_intent_checkout_saves_sepa_generated_payment_method_t $this->mock_gateway->expects( $this->any() ) ->method( 'get_stripe_customer_from_order' ) - ->with( wc_get_order( $order_id ) ) + ->with( WC_Stripe_Helper::get_order( $order_id ) ) ->will( $this->returnValue( $this->mock_stripe_customer ) ); @@ -1296,7 +1296,7 @@ function( $passed_order ) use ( $order ) { ->willReturn( $this->array_to_object( $charge ) ); $response = $this->mock_gateway->process_payment( $order_id ); - $final_order = wc_get_order( $order_id ); + $final_order = WC_Stripe_Helper::get_order( $order_id ); $note = wc_get_order_notes( [ 'order_id' => $order_id, @@ -1384,7 +1384,7 @@ function( $passed_order ) use ( $order ) { ->willReturn( $this->array_to_object( $charge ) ); $response = $this->mock_gateway->process_payment( $order_id ); - $final_order = wc_get_order( $order_id ); + $final_order = WC_Stripe_Helper::get_order( $order_id ); $client_secret = $payment_intent_mock->client_secret; $this->assertEquals( 'success', $response['result'] ); @@ -1447,7 +1447,7 @@ function( $passed_order ) use ( $order ) { ); $response = $this->mock_gateway->process_payment( $order_id ); - $final_order = wc_get_order( $order_id ); + $final_order = WC_Stripe_Helper::get_order( $order_id ); $this->assertEquals( 'failure', $response['result'] ); $this->assertEquals( 'failed', $final_order->get_status() ); @@ -1542,7 +1542,7 @@ function( $passed_order ) use ( $order ) { ->willReturn( $this->array_to_object( $charge ) ); $response = $this->mock_gateway->process_payment( $order_id ); - $final_order = wc_get_order( $order_id ); + $final_order = WC_Stripe_Helper::get_order( $order_id ); $note = wc_get_order_notes( [ 'order_id' => $order_id, @@ -1689,7 +1689,7 @@ public function test_if_order_has_subscription_payment_method_will_be_saved() { $this->mock_gateway->expects( $this->any() ) ->method( 'get_stripe_customer_from_order' ) - ->with( wc_get_order( $order_id ) ) + ->with( WC_Stripe_Helper::get_order( $order_id ) ) ->will( $this->returnValue( $this->mock_stripe_customer ) ); @@ -1699,7 +1699,7 @@ public function test_if_order_has_subscription_payment_method_will_be_saved() { ->with( "payment_intents/$payment_intent_id", $expected_request, - wc_get_order( $order_id ) + WC_Stripe_Helper::get_order( $order_id ) ) ->will( $this->returnValue( [] ) @@ -1772,7 +1772,7 @@ public function test_subscription_renewal_is_successful() { $order->set_lock_payment( ( time() + MINUTE_IN_SECONDS ) ); // To assist with comparing expected order objects, set an existing lock. $order->save(); - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); $payment_method_mock = self::MOCK_CARD_PAYMENT_METHOD_TEMPLATE; $payment_method_mock['id'] = $payment_method_id; @@ -1826,7 +1826,7 @@ public function test_subscription_renewal_is_successful() { $this->mock_gateway->process_subscription_payment( $amount, $order, false, false ); - $final_order = wc_get_order( $order_id ); + $final_order = WC_Stripe_Helper::get_order( $order_id ); $note = wc_get_order_notes( [ 'order_id' => $order_id, @@ -1866,7 +1866,7 @@ public function test_subscription_renewal_checks_payment_method_authorization() $order->set_lock_payment( ( time() + MINUTE_IN_SECONDS ) ); // To assist with comparing expected order objects, set an existing lock. $order->save(); - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); $payment_method_mock = self::MOCK_CARD_PAYMENT_METHOD_TEMPLATE; $payment_method_mock['id'] = $payment_method_id; @@ -1928,7 +1928,7 @@ public function test_subscription_renewal_checks_payment_method_authorization() $this->mock_gateway->process_subscription_payment( $amount, $order, false, false ); - $final_order = wc_get_order( $order_id ); + $final_order = WC_Stripe_Helper::get_order( $order_id ); $note = wc_get_order_notes( [ 'order_id' => $order_id, @@ -1999,14 +1999,14 @@ public function test_pre_order_payment_is_successful() { ); $this->mock_gateway->expects( $this->any() ) ->method( 'get_stripe_customer_from_order' ) - ->with( wc_get_order( $order_id ) ) + ->with( WC_Stripe_Helper::get_order( $order_id ) ) ->will( $this->returnValue( $this->mock_stripe_customer ) ); $this->mock_gateway->expects( $this->any() ) ->method( 'has_pre_order_charged_upon_release' ) - ->with( wc_get_order( $order_id ) ) + ->with( WC_Stripe_Helper::get_order( $order_id ) ) ->will( $this->returnValue( true ) ); $this->mock_gateway->expects( $this->once() ) @@ -2060,7 +2060,7 @@ public function test_pre_order_without_payment_uses_setup_intents() { $this->mock_gateway->expects( $this->any() ) ->method( 'get_stripe_customer_from_order' ) - ->with( wc_get_order( $order_id ) ) + ->with( WC_Stripe_Helper::get_order( $order_id ) ) ->will( $this->returnValue( $this->mock_stripe_customer ) ); From 4bd86a68fb03cc1b52a8e2e438f3f9ed7b58fdf5 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 26 Dec 2024 19:29:42 -0300 Subject: [PATCH 16/24] Fix tests --- tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php b/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php index 8f3c53c97e..a3ca3c2de9 100644 --- a/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php +++ b/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php @@ -339,7 +339,7 @@ public function test_process_payment_returns_valid_response() { ->with( "payment_intents/$payment_intent_id", $expected_request, - wc_get_order( $order_id ) + WC_Stripe_Helper::get_order( $order_id ) ) ->will( $this->returnValue( [] ) @@ -911,7 +911,7 @@ public function test_checkout_without_payment_uses_setup_intents() { $final_order = WC_Stripe_Helper::get_order( $order_id ); $this->assertEquals( 'processing', $final_order->get_status() ); - $this->assertEquals( $customer_id, $final_order->get_customer_id() ); + $this->assertEquals( $customer_id, $final_order->get_stripe_customer_id() ); $this->assertEquals( $payment_method_id, $final_order->get_source_id() ); $this->assertEquals( 'Credit / Debit Card', $final_order->get_payment_method_title() ); } @@ -2029,7 +2029,7 @@ public function test_pre_order_payment_is_successful() { $this->assertEquals( 'Credit / Debit Card', $final_order->get_payment_method_title() ); $this->assertEquals( $payment_method_id, $final_order->get_source_id() ); - $this->assertEquals( $customer_id, $final_order->get_customer_id() ); + $this->assertEquals( $customer_id, $final_order->get_stripe_customer_id() ); $this->assertEquals( $payment_intent_id, $final_order->get_intent_id() ); $this->assertTrue( $final_order->upe_redirect_processed() ); } From 6b1f0e850d71c3149af571e2ad6561fb8eb99679 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 26 Dec 2024 20:03:44 -0300 Subject: [PATCH 17/24] Replacing get_order calls --- .../abstract-wc-stripe-payment-gateway-voucher.php | 4 ++-- .../abstracts/abstract-wc-stripe-payment-gateway.php | 8 ++++---- .../admin/class-wc-rest-stripe-orders-controller.php | 2 +- includes/admin/class-wc-stripe-privacy.php | 2 +- includes/class-wc-gateway-stripe.php | 10 +++++----- includes/class-wc-stripe-intent-controller.php | 8 ++++---- includes/class-wc-stripe-order-handler.php | 6 +++++- includes/class-wc-stripe-webhook-handler.php | 2 +- includes/compat/trait-wc-stripe-subscriptions.php | 8 ++++---- .../payment-methods/class-wc-gateway-stripe-alipay.php | 2 +- .../class-wc-gateway-stripe-bancontact.php | 2 +- .../payment-methods/class-wc-gateway-stripe-eps.php | 2 +- .../class-wc-gateway-stripe-giropay.php | 2 +- .../payment-methods/class-wc-gateway-stripe-ideal.php | 2 +- .../class-wc-gateway-stripe-multibanco.php | 4 ++-- .../payment-methods/class-wc-gateway-stripe-p24.php | 2 +- .../payment-methods/class-wc-gateway-stripe-sepa.php | 2 +- .../payment-methods/class-wc-gateway-stripe-sofort.php | 2 +- .../class-wc-stripe-express-checkout-ajax-handler.php | 2 +- .../class-wc-stripe-express-checkout-element.php | 4 ++-- .../class-wc-stripe-payment-request.php | 4 ++-- .../class-wc-stripe-upe-payment-gateway.php | 8 ++++---- .../class-wc-stripe-upe-payment-method-link.php | 2 +- .../class-wc-stripe-upe-payment-method-multibanco.php | 2 +- .../class-wc-stripe-upe-payment-method.php | 2 +- tests/phpunit/test-class-wc-stripe-order-handler.php | 2 +- 26 files changed, 50 insertions(+), 46 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php b/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php index e31f127755..7894025d9c 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php @@ -224,7 +224,7 @@ public function javascript_params() { if ( $this->is_valid_pay_for_order_endpoint() ) { $order_id = absint( get_query_var( 'order-pay' ) ); - $stripe_params['stripe_order_key'] = ! empty( $order_id ) ? wc_get_order( $order_id )->get_order_key() : null; + $stripe_params['stripe_order_key'] = ! empty( $order_id ) ? WC_Stripe_Helper::get_order( $order_id )->get_order_key() : null; } return $stripe_params; @@ -372,7 +372,7 @@ public function update_payment_intent_ajax() { throw new \Exception( __( 'Order Id not found, send an order id', 'woocommerce-gateway-stripe' ) ); } - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); $order_key = isset( $_POST['stripe_order_key'] ) ? wc_clean( wp_unslash( $_POST['stripe_order_key'] ) ) : null; if ( $order->get_order_key() !== $order_key ) { diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index 71ddb751a6..ab9b857b6c 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -1855,7 +1855,7 @@ public function is_valid_pay_for_order_endpoint(): bool { } $order_id = absint( get_query_var( 'order-pay' ) ); - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); // If the order is not found or the param `key` is not set or the order key does not match the order key in the URL param, return false. if ( ! $order || ! isset( $_GET['key'] ) || wc_clean( wp_unslash( $_GET['key'] ) ) !== $order->get_order_key() ) { @@ -1894,7 +1894,7 @@ public function is_valid_order_received_endpoint(): bool { return false; } - $order = wc_get_order( $order_id_from_order_key ); + $order = WC_Stripe_Helper::get_order( $order_id_from_order_key ); // If the order doesn't need payment, return false. if ( ! $order->needs_payment() ) { @@ -2053,7 +2053,7 @@ public function javascript_params() { // If we're on the pay page we need to pass stripe.js the address of the order. if ( $this->is_valid_pay_for_order_endpoint() || $this->is_changing_payment_method_for_subscription() ) { $order_id = absint( get_query_var( 'order-pay' ) ); - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); if ( is_a( $order, 'WC_Order' ) ) { $stripe_params['billing_first_name'] = $order->get_billing_first_name(); @@ -2185,7 +2185,7 @@ protected function get_risk_outcome( $event_data ) { * @param WC_Order|int $order Order object or id. */ public function update_saved_payment_method( $payment_method_id, $order ) { - $order = ! is_a( $order, 'WC_Order' ) ? wc_get_order( $order ) : $order; + $order = ! is_a( $order, 'WC_Order' ) ? WC_Stripe_Helper::get_order( $order ) : $order; if ( ! $order || ! $this->is_type_payment_method( $payment_method_id ) ) { return; diff --git a/includes/admin/class-wc-rest-stripe-orders-controller.php b/includes/admin/class-wc-rest-stripe-orders-controller.php index 4b31b85c6c..4593c7129d 100644 --- a/includes/admin/class-wc-rest-stripe-orders-controller.php +++ b/includes/admin/class-wc-rest-stripe-orders-controller.php @@ -118,7 +118,7 @@ public function capture_terminal_payment( $request ) { try { $intent_id = $request['payment_intent_id']; $order_id = $request['order_id']; - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); // Check that order exists before capturing payment. if ( ! $order ) { diff --git a/includes/admin/class-wc-stripe-privacy.php b/includes/admin/class-wc-stripe-privacy.php index 570137f876..01316093c3 100644 --- a/includes/admin/class-wc-stripe-privacy.php +++ b/includes/admin/class-wc-stripe-privacy.php @@ -305,7 +305,7 @@ public function order_data_eraser( $email_address, $page ) { $messages = []; foreach ( (array) $orders as $order ) { - $order = wc_get_order( $order->get_id() ); + $order = WC_Stripe_Helper::get_order( $order->get_id() ); list( $removed, $retained, $msgs ) = $this->maybe_handle_order( $order ); $items_removed |= $removed; diff --git a/includes/class-wc-gateway-stripe.php b/includes/class-wc-gateway-stripe.php index 68296a0c82..a89a67a74f 100644 --- a/includes/class-wc-gateway-stripe.php +++ b/includes/class-wc-gateway-stripe.php @@ -220,7 +220,7 @@ public function payment_fields() { // If paying for order, we need to get email from the order not the user account. if ( parent::is_valid_pay_for_order_endpoint() ) { - $order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) ); + $order = WC_Stripe_Helper::get_order( wc_clean( $wp->query_vars['order-pay'] ) ); $user_email = $order->get_billing_email(); } else { if ( $user->ID ) { @@ -690,7 +690,7 @@ public function change_no_available_methods_message() { */ public function prepare_intent_for_order_pay_page( $order = null ) { if ( ! isset( $order ) || empty( $order ) ) { - $order = wc_get_order( absint( get_query_var( 'order-pay' ) ) ); + $order = WC_Stripe_Helper::get_order( absint( get_query_var( 'order-pay' ) ) ); } $intent = $this->get_intent_from_order( $order ); @@ -735,7 +735,7 @@ public function prepare_intent_for_order_pay_page( $order = null ) { */ public function render_payment_intent_inputs( $order = null ) { if ( ! isset( $order ) || empty( $order ) ) { - $order = wc_get_order( absint( get_query_var( 'order-pay' ) ) ); + $order = WC_Stripe_Helper::get_order( absint( get_query_var( 'order-pay' ) ) ); } if ( ! isset( $this->order_pay_intent ) ) { $this->prepare_intent_for_order_pay_page( $order ); @@ -781,7 +781,7 @@ public function check_intent_status_on_order_page( $order_id ) { return; } - $order = wc_get_order( absint( $order_id ) ); + $order = WC_Stripe_Helper::get_order( absint( $order_id ) ); if ( ! $order ) { return; @@ -871,7 +871,7 @@ public function verify_intent_after_checkout( $order ) { // A webhook might have modified or locked the order while the intent was retreived. This ensures we are reading the right status. clean_post_cache( $order->get_id() ); - $order = wc_get_order( $order->get_id() ); + $order = WC_Stripe_Helper::get_order( $order->get_id() ); if ( ! $order->has_status( apply_filters( diff --git a/includes/class-wc-stripe-intent-controller.php b/includes/class-wc-stripe-intent-controller.php index 62f0613868..4be0c8b8f7 100644 --- a/includes/class-wc-stripe-intent-controller.php +++ b/includes/class-wc-stripe-intent-controller.php @@ -88,7 +88,7 @@ protected function get_order_from_request() { } // Retrieve the order. - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); if ( ! $order ) { throw new WC_Stripe_Exception( 'missing-order', __( 'Missing order ID for payment confirmation', 'woocommerce-gateway-stripe' ) ); @@ -310,7 +310,7 @@ public function create_payment_intent_ajax() { $order_id = isset( $_POST['stripe_order_id'] ) ? absint( $_POST['stripe_order_id'] ) : null; if ( $order_id ) { - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); if ( ! $order || ! $order->needs_payment() ) { throw new Exception( __( 'Unable to process your request. Please reload the page and try again.', 'woocommerce-gateway-stripe' ) ); } @@ -339,7 +339,7 @@ public function create_payment_intent_ajax() { */ public function create_payment_intent( $order_id = null ) { $amount = WC()->cart->get_total( false ); - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); if ( is_a( $order, 'WC_Order' ) ) { $amount = $order->get_total(); } @@ -635,7 +635,7 @@ public function update_failed_order_ajax() { $order_id = isset( $_POST['order_id'] ) ? absint( $_POST['order_id'] ) : null; $intent_id = isset( $_POST['intent_id'] ) ? wc_clean( wp_unslash( $_POST['intent_id'] ) ) : ''; - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); $order_from_payment = WC_Stripe_Helper::get_order_by_intent_id( $intent_id ); if ( ! $order_from_payment || $order_from_payment->get_id() !== $order_id ) { diff --git a/includes/class-wc-stripe-order-handler.php b/includes/class-wc-stripe-order-handler.php index ba277f6e01..c496cc47af 100644 --- a/includes/class-wc-stripe-order-handler.php +++ b/includes/class-wc-stripe-order-handler.php @@ -331,7 +331,7 @@ public function capture_payment( $order_id ) { $captured = $order->charge_captured(); $is_stripe_captured = false; - if ( $charge && 'no' === $captured ) { + if ( $charge && ! $captured ) { $order_total = $order->get_total(); if ( 0 < $order->get_total_refunded() ) { @@ -502,6 +502,10 @@ public function prevent_cancelling_orders_awaiting_action( $cancel_order, $order return $cancel_order; } + if ( ! $order instanceof WC_Stripe_Order ) { + $order = new WC_Stripe_Order( $order ); + } + // Bail if payment method is not stripe or `stripe_{apm_method}` or doesn't have an intent yet. if ( substr( (string) $order->get_payment_method(), 0, 6 ) !== 'stripe' || ! $this->get_intent_from_order( $order ) ) { return $cancel_order; diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index 8a7f2b0981..f09ebec6ea 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -1107,7 +1107,7 @@ public function process_deferred_webhook( $webhook_type, $additional_data ) { switch ( $webhook_type ) { case 'payment_intent.succeeded': case 'payment_intent.amount_capturable_updated': - $order = isset( $additional_data['order_id'] ) ? wc_get_order( $additional_data['order_id'] ) : null; + $order = isset( $additional_data['order_id'] ) ? WC_Stripe_Helper::get_order( $additional_data['order_id'] ) : null; $intent_id = $additional_data['intent_id'] ?? ''; if ( empty( $order ) ) { diff --git a/includes/compat/trait-wc-stripe-subscriptions.php b/includes/compat/trait-wc-stripe-subscriptions.php index b5b01be260..2b5920dfab 100644 --- a/includes/compat/trait-wc-stripe-subscriptions.php +++ b/includes/compat/trait-wc-stripe-subscriptions.php @@ -227,7 +227,7 @@ public function maybe_change_subscription_payment_method( $order_id ) { */ public function process_change_subscription_payment_method( $order_id ) { try { - $subscription = wc_get_order( $order_id ); + $subscription = WC_Stripe_Helper::get_order( $order_id ); $prepared_source = $this->prepare_source( get_current_user_id(), true ); $this->maybe_disallow_prepaid_card( $prepared_source->source_object ); @@ -792,7 +792,7 @@ private function get_mandate_for_subscription( $order, $payment_method ) { $renewal_order_ids = $order->get_related_orders( 'ids' ); foreach ( $renewal_order_ids as $renewal_order_id ) { - $renewal_order = wc_get_order( $renewal_order_id ); + $renewal_order = WC_Stripe_Helper::get_order( $renewal_order_id ); if ( ! $renewal_order instanceof WC_Order ) { continue; } @@ -936,7 +936,7 @@ public function maybe_render_subscription_payment_method( $payment_method_to_dis // If we couldn't find a Stripe customer linked to the account, fallback to the order meta data. if ( ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) && false !== $subscription->get_parent() ) { - $parent_order = wc_get_order( $subscription->get_parent_id() ); + $parent_order = WC_Stripe_Helper::get_order( $subscription->get_parent_id() ); $stripe_customer_id = $parent_order->get_meta( '_stripe_customer_id', true ); $stripe_source_id = $parent_order->get_source_id(); @@ -1147,7 +1147,7 @@ public function update_subscription_payment_method_from_order( $order, $payment_ * @return boolean true if the subscription can be edited, false otherwise. */ public function disable_subscription_edit_for_india( $editable, $order ) { - $parent_order = wc_get_order( $order->get_parent_id() ); + $parent_order = WC_Stripe_Helper::get_order( $order->get_parent_id() ); if ( $this->is_subscriptions_enabled() && $this->is_subscription( $order ) && $parent_order diff --git a/includes/payment-methods/class-wc-gateway-stripe-alipay.php b/includes/payment-methods/class-wc-gateway-stripe-alipay.php index d74e285b72..224f077d6b 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-alipay.php +++ b/includes/payment-methods/class-wc-gateway-stripe-alipay.php @@ -189,7 +189,7 @@ public function payment_fields() { // If paying from order, we need to get total from order not cart. if ( parent::is_valid_pay_for_order_endpoint() ) { - $order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) ); + $order = WC_Stripe_Helper::get_order( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } diff --git a/includes/payment-methods/class-wc-gateway-stripe-bancontact.php b/includes/payment-methods/class-wc-gateway-stripe-bancontact.php index 97185a5622..ab422e9dc9 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-bancontact.php +++ b/includes/payment-methods/class-wc-gateway-stripe-bancontact.php @@ -176,7 +176,7 @@ public function payment_fields() { // If paying from order, we need to get total from order not cart. if ( parent::is_valid_pay_for_order_endpoint() ) { - $order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) ); + $order = WC_Stripe_Helper::get_order( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } diff --git a/includes/payment-methods/class-wc-gateway-stripe-eps.php b/includes/payment-methods/class-wc-gateway-stripe-eps.php index fbdbcd18a4..dd4a8fb1cb 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-eps.php +++ b/includes/payment-methods/class-wc-gateway-stripe-eps.php @@ -176,7 +176,7 @@ public function payment_fields() { // If paying from order, we need to get total from order not cart. if ( parent::is_valid_pay_for_order_endpoint() ) { - $order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) ); + $order = WC_Stripe_Helper::get_order( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } diff --git a/includes/payment-methods/class-wc-gateway-stripe-giropay.php b/includes/payment-methods/class-wc-gateway-stripe-giropay.php index d888424d19..333365361a 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-giropay.php +++ b/includes/payment-methods/class-wc-gateway-stripe-giropay.php @@ -172,7 +172,7 @@ public function payment_fields() { // If paying from order, we need to get total from order not cart. if ( parent::is_valid_pay_for_order_endpoint() ) { - $order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) ); + $order = WC_Stripe_Helper::get_order( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } diff --git a/includes/payment-methods/class-wc-gateway-stripe-ideal.php b/includes/payment-methods/class-wc-gateway-stripe-ideal.php index a3de090c3b..911c2dceb6 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-ideal.php +++ b/includes/payment-methods/class-wc-gateway-stripe-ideal.php @@ -176,7 +176,7 @@ public function payment_fields() { // If paying from order, we need to get total from order not cart. if ( parent::is_valid_pay_for_order_endpoint() ) { - $order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) ); + $order = WC_Stripe_Helper::get_order( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } diff --git a/includes/payment-methods/class-wc-gateway-stripe-multibanco.php b/includes/payment-methods/class-wc-gateway-stripe-multibanco.php index ebc56ec07e..4b9e80b9a6 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-multibanco.php +++ b/includes/payment-methods/class-wc-gateway-stripe-multibanco.php @@ -180,7 +180,7 @@ public function payment_fields() { // If paying from order, we need to get total from order not cart. if ( parent::is_valid_pay_for_order_endpoint() ) { - $order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) ); + $order = WC_Stripe_Helper::get_order( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } @@ -242,7 +242,7 @@ public function email_instructions( $order, $sent_to_admin, $plain_text = false */ public function get_instructions( $order, $plain_text = false ) { if ( true === is_int( $order ) ) { - $order = wc_get_order( $order ); + $order = WC_Stripe_Helper::get_order( $order ); } $data = $order->get_multibanco_data(); diff --git a/includes/payment-methods/class-wc-gateway-stripe-p24.php b/includes/payment-methods/class-wc-gateway-stripe-p24.php index 571668f968..f7eb3e4560 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-p24.php +++ b/includes/payment-methods/class-wc-gateway-stripe-p24.php @@ -177,7 +177,7 @@ public function payment_fields() { // If paying from order, we need to get total from order not cart. if ( parent::is_valid_pay_for_order_endpoint() ) { - $order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) ); + $order = WC_Stripe_Helper::get_order( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } diff --git a/includes/payment-methods/class-wc-gateway-stripe-sepa.php b/includes/payment-methods/class-wc-gateway-stripe-sepa.php index 924cfa2fe9..3cd500ae16 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-sepa.php +++ b/includes/payment-methods/class-wc-gateway-stripe-sepa.php @@ -230,7 +230,7 @@ public function payment_fields() { // If paying from order, we need to get total from order not cart. if ( parent::is_valid_pay_for_order_endpoint() ) { - $order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) ); + $order = WC_Stripe_Helper::get_order( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } diff --git a/includes/payment-methods/class-wc-gateway-stripe-sofort.php b/includes/payment-methods/class-wc-gateway-stripe-sofort.php index aa566c6466..7cf68a2964 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-sofort.php +++ b/includes/payment-methods/class-wc-gateway-stripe-sofort.php @@ -176,7 +176,7 @@ public function payment_fields() { // If paying from order, we need to get total from order not cart. if ( parent::is_valid_pay_for_order_endpoint() ) { - $order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) ); + $order = WC_Stripe_Helper::get_order( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } diff --git a/includes/payment-methods/class-wc-stripe-express-checkout-ajax-handler.php b/includes/payment-methods/class-wc-stripe-express-checkout-ajax-handler.php index 1a6c4e0f17..1f6c94e33c 100644 --- a/includes/payment-methods/class-wc-stripe-express-checkout-ajax-handler.php +++ b/includes/payment-methods/class-wc-stripe-express-checkout-ajax-handler.php @@ -372,7 +372,7 @@ public function ajax_pay_for_order() { wc_set_time_limit( 0 ); // Load the order. - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); if ( ! is_a( $order, WC_Order::class ) ) { throw new Exception( __( 'Invalid order!', 'woocommerce-gateway-stripe' ) ); diff --git a/includes/payment-methods/class-wc-stripe-express-checkout-element.php b/includes/payment-methods/class-wc-stripe-express-checkout-element.php index 35ef041662..0482d91f0b 100644 --- a/includes/payment-methods/class-wc-stripe-express-checkout-element.php +++ b/includes/payment-methods/class-wc-stripe-express-checkout-element.php @@ -359,7 +359,7 @@ public function add_order_meta( $order_id, $posted_data ) { return; } - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); $express_checkout_type = wc_clean( wp_unslash( $_POST['express_checkout_type'] ) ); @@ -390,7 +390,7 @@ public function filter_gateway_title( $title, $id ) { // If $theorder is empty (i.e. non-HPOS), fallback to using the global post object. if ( empty( $theorder ) && ! empty( $GLOBALS['post']->ID ) ) { - $theorder = wc_get_order( $GLOBALS['post']->ID ); + $theorder = WC_Stripe_Helper::get_order( $GLOBALS['post']->ID ); } if ( ! is_object( $theorder ) ) { diff --git a/includes/payment-methods/class-wc-stripe-payment-request.php b/includes/payment-methods/class-wc-stripe-payment-request.php index ca1372d627..06ac8e52b3 100644 --- a/includes/payment-methods/class-wc-stripe-payment-request.php +++ b/includes/payment-methods/class-wc-stripe-payment-request.php @@ -473,7 +473,7 @@ public function filter_gateway_title( $title, $id ) { // If $theorder is empty (i.e. non-HPOS), fallback to using the global post object. if ( empty( $theorder ) && ! empty( $GLOBALS['post']->ID ) ) { - $theorder = wc_get_order( $GLOBALS['post']->ID ); + $theorder = WC_Stripe_Helper::get_order( $GLOBALS['post']->ID ); } if ( ! is_object( $theorder ) ) { @@ -564,7 +564,7 @@ public function add_order_meta( $order_id, $posted_data ) { return; } - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); $payment_request_type = wc_clean( wp_unslash( $_POST['payment_request_type'] ) ); diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index ab75627de1..64b5ddb363 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -421,7 +421,7 @@ public function javascript_params() { if ( parent::is_valid_pay_for_order_endpoint() || $is_change_payment_method ) { $order_id = absint( get_query_var( 'order-pay' ) ); - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); // Make billing country available for subscriptions as well, so country-restricted payment methods can be shown. if ( is_a( $order, 'WC_Order' ) ) { @@ -1219,7 +1219,7 @@ private function is_order_associated_to_payment_intent( int $order_id, string $i * @return bool */ private function is_order_associated_to_setup_intent( int $order_id, string $intent_id ): bool { - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); if ( ! $order ) { return false; } @@ -1482,7 +1482,7 @@ public function is_payment_needed( $order_id = null ) { // Free trial subscriptions without a sign up fee, or any other type // of order with a `0` amount should fall into the logic below. $amount = is_null( WC()->cart ) ? 0 : WC()->cart->get_total( false ); - $order = isset( $order_id ) ? wc_get_order( $order_id ) : null; + $order = isset( $order_id ) ? WC_Stripe_Helper::get_order( $order_id ) : null; if ( is_a( $order, 'WC_Order' ) ) { $amount = $order->get_total(); } @@ -2403,7 +2403,7 @@ private function get_return_url_for_redirect( $order, $save_payment_method ) { */ private function get_existing_compatible_payment_intent( $order, $payment_method_types ) { // Reload the order to make sure we have the latest data. - $order = wc_get_order( $order->get_id() ); + $order = WC_Stripe_Helper::get_order( $order->get_id() ); $intent = $this->get_intent_from_order( $order ); if ( ! $intent ) { return null; diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-method-link.php b/includes/payment-methods/class-wc-stripe-upe-payment-method-link.php index 3ee1093cb8..54f0bb3f41 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-method-link.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-method-link.php @@ -120,7 +120,7 @@ public function filter_gateway_title( $title, $id ) { // If $theorder is empty (i.e. non-HPOS), fallback to using the global post object. if ( empty( $theorder ) && ! empty( $GLOBALS['post']->ID ) ) { - $theorder = wc_get_order( $GLOBALS['post']->ID ); + $theorder = WC_Stripe_Helper::get_order( $GLOBALS['post']->ID ); } if ( ! is_object( $theorder ) ) { diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-method-multibanco.php b/includes/payment-methods/class-wc-stripe-upe-payment-method-multibanco.php index 76afa70e57..9fdf13bd84 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-method-multibanco.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-method-multibanco.php @@ -41,7 +41,7 @@ public function __construct() { * @param int $order_id */ public function thankyou_page( $order_id ) { - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Helper::get_order( $order_id ); if ( ! $order ) { return; } diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-method.php b/includes/payment-methods/class-wc-stripe-upe-payment-method.php index ef86532f85..0bbf927d40 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-method.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-method.php @@ -599,7 +599,7 @@ public function get_limits_per_currency(): array { */ public function get_current_order_amount() { if ( is_wc_endpoint_url( 'order-pay' ) && isset( $_GET['key'] ) ) { - $order = wc_get_order( absint( get_query_var( 'order-pay' ) ) ); + $order = WC_Stripe_Helper::get_order( absint( get_query_var( 'order-pay' ) ) ); return $order->get_total( '' ); } elseif ( WC()->cart ) { return WC()->cart->get_total( '' ); diff --git a/tests/phpunit/test-class-wc-stripe-order-handler.php b/tests/phpunit/test-class-wc-stripe-order-handler.php index 68bcfd593d..229649318e 100644 --- a/tests/phpunit/test-class-wc-stripe-order-handler.php +++ b/tests/phpunit/test-class-wc-stripe-order-handler.php @@ -22,7 +22,7 @@ public function test_prevent_cancelling_orders_awaiting_action() { $order->set_payment_awaiting_action(); // Read in a fresh order object with meta like `date_modified` set. - $order = wc_get_order( $order->get_id() ); + $order = WC_Stripe_Helper::get_order( $order->get_id() ); // Test when false is passed that the order is not cancelled. $this->assertFalse( $this->order_handler->prevent_cancelling_orders_awaiting_action( false, $order ) ); From c9f0860483a6abde2766a53cda7cb4898e715565 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Fri, 27 Dec 2024 09:52:42 -0300 Subject: [PATCH 18/24] Fix tests --- .../admin/test-class-wc-rest-stripe-orders-controller.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/phpunit/admin/test-class-wc-rest-stripe-orders-controller.php b/tests/phpunit/admin/test-class-wc-rest-stripe-orders-controller.php index a1ea086029..1b4c578fc1 100644 --- a/tests/phpunit/admin/test-class-wc-rest-stripe-orders-controller.php +++ b/tests/phpunit/admin/test-class-wc-rest-stripe-orders-controller.php @@ -140,6 +140,8 @@ public function test_capture_payment_success() { $request->set_param( 'payment_intent_id', 'pi_12345' ); $response = rest_do_request( $request ); + $order = WC_Stripe_Helper::get_order( $order->get_id() ); + $this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 'succeeded', $response->get_data()['status'] ); $this->assertEquals( 'ch_12345', $response->get_data()['id'] ); From 5f4e34f16adb74891a952fccd192d418a1795ffb Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Fri, 27 Dec 2024 10:21:29 -0300 Subject: [PATCH 19/24] Moving order methods to the new order class --- ...ract-wc-stripe-payment-gateway-voucher.php | 6 +- .../abstract-wc-stripe-payment-gateway.php | 10 +-- ...class-wc-rest-stripe-orders-controller.php | 4 +- includes/admin/class-wc-stripe-privacy.php | 2 +- includes/class-wc-gateway-stripe.php | 16 ++-- includes/class-wc-stripe-helper.php | 70 +++------------- .../class-wc-stripe-intent-controller.php | 12 +-- includes/class-wc-stripe-order-handler.php | 6 +- includes/class-wc-stripe-order.php | 50 ++++++++++++ includes/class-wc-stripe-webhook-handler.php | 4 +- .../compat/trait-wc-stripe-pre-orders.php | 2 +- .../compat/trait-wc-stripe-subscriptions.php | 8 +- .../class-wc-gateway-stripe-alipay.php | 4 +- .../class-wc-gateway-stripe-bancontact.php | 4 +- .../class-wc-gateway-stripe-eps.php | 4 +- .../class-wc-gateway-stripe-giropay.php | 4 +- .../class-wc-gateway-stripe-ideal.php | 4 +- .../class-wc-gateway-stripe-multibanco.php | 6 +- .../class-wc-gateway-stripe-p24.php | 4 +- .../class-wc-gateway-stripe-sepa.php | 4 +- .../class-wc-gateway-stripe-sofort.php | 4 +- ...c-stripe-express-checkout-ajax-handler.php | 2 +- ...ass-wc-stripe-express-checkout-element.php | 4 +- .../class-wc-stripe-payment-request.php | 4 +- .../class-wc-stripe-upe-payment-gateway.php | 16 ++-- ...lass-wc-stripe-upe-payment-method-link.php | 2 +- ...c-stripe-upe-payment-method-multibanco.php | 2 +- .../class-wc-stripe-upe-payment-method.php | 2 +- ...class-wc-rest-stripe-orders-controller.php | 2 +- .../phpunit/helpers/class-wc-helper-order.php | 2 +- .../test-class-wc-stripe-order-handler.php | 2 +- ...st-class-wc-stripe-upe-payment-gateway.php | 80 +++++++++---------- .../test-wc-stripe-payment-gateway.php | 2 +- tests/phpunit/test-wc-stripe-sub-initial.php | 2 +- tests/phpunit/test-wc-stripe-sub-renewal.php | 4 +- 35 files changed, 177 insertions(+), 177 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php b/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php index 7894025d9c..750913b6e5 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php @@ -224,7 +224,7 @@ public function javascript_params() { if ( $this->is_valid_pay_for_order_endpoint() ) { $order_id = absint( get_query_var( 'order-pay' ) ); - $stripe_params['stripe_order_key'] = ! empty( $order_id ) ? WC_Stripe_Helper::get_order( $order_id )->get_order_key() : null; + $stripe_params['stripe_order_key'] = ! empty( $order_id ) ? WC_Stripe_Order::get_by_id( $order_id )->get_order_key() : null; } return $stripe_params; @@ -253,7 +253,7 @@ public function init_form_fields() { */ public function process_payment( $order_id, $retry = true, $force_save_save = false ) { try { - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); if ( ! in_array( $order->get_billing_country(), $this->supported_countries ) ) { throw new \Exception( __( 'This payment method is not available in the selected country', 'woocommerce-gateway-stripe' ) ); @@ -372,7 +372,7 @@ public function update_payment_intent_ajax() { throw new \Exception( __( 'Order Id not found, send an order id', 'woocommerce-gateway-stripe' ) ); } - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); $order_key = isset( $_POST['stripe_order_key'] ) ? wc_clean( wp_unslash( $_POST['stripe_order_key'] ) ) : null; if ( $order->get_order_key() !== $order_key ) { diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index ab9b857b6c..870174e3f4 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -1057,7 +1057,7 @@ public function update_fees( $order, $balance_transaction_id ) { * @throws Exception Throws exception when charge wasn't captured. */ public function process_refund( $order_id, $amount = null, $reason = '' ) { - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); if ( ! $order ) { return false; @@ -1855,7 +1855,7 @@ public function is_valid_pay_for_order_endpoint(): bool { } $order_id = absint( get_query_var( 'order-pay' ) ); - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); // If the order is not found or the param `key` is not set or the order key does not match the order key in the URL param, return false. if ( ! $order || ! isset( $_GET['key'] ) || wc_clean( wp_unslash( $_GET['key'] ) ) !== $order->get_order_key() ) { @@ -1894,7 +1894,7 @@ public function is_valid_order_received_endpoint(): bool { return false; } - $order = WC_Stripe_Helper::get_order( $order_id_from_order_key ); + $order = WC_Stripe_Order::get_by_id( $order_id_from_order_key ); // If the order doesn't need payment, return false. if ( ! $order->needs_payment() ) { @@ -2053,7 +2053,7 @@ public function javascript_params() { // If we're on the pay page we need to pass stripe.js the address of the order. if ( $this->is_valid_pay_for_order_endpoint() || $this->is_changing_payment_method_for_subscription() ) { $order_id = absint( get_query_var( 'order-pay' ) ); - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); if ( is_a( $order, 'WC_Order' ) ) { $stripe_params['billing_first_name'] = $order->get_billing_first_name(); @@ -2185,7 +2185,7 @@ protected function get_risk_outcome( $event_data ) { * @param WC_Order|int $order Order object or id. */ public function update_saved_payment_method( $payment_method_id, $order ) { - $order = ! is_a( $order, 'WC_Order' ) ? WC_Stripe_Helper::get_order( $order ) : $order; + $order = ! is_a( $order, 'WC_Order' ) ? WC_Stripe_Order::get_by_id( $order ) : $order; if ( ! $order || ! $this->is_type_payment_method( $payment_method_id ) ) { return; diff --git a/includes/admin/class-wc-rest-stripe-orders-controller.php b/includes/admin/class-wc-rest-stripe-orders-controller.php index 4593c7129d..7426da5a30 100644 --- a/includes/admin/class-wc-rest-stripe-orders-controller.php +++ b/includes/admin/class-wc-rest-stripe-orders-controller.php @@ -72,7 +72,7 @@ public function create_customer( $request ) { $order_id = $request['order_id']; // Ensure order exists. - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); if ( false === $order || ! ( $order instanceof WC_Order ) ) { return new WP_Error( 'wc_stripe', __( 'Order not found', 'woocommerce-gateway-stripe' ), [ 'status' => 404 ] ); } @@ -118,7 +118,7 @@ public function capture_terminal_payment( $request ) { try { $intent_id = $request['payment_intent_id']; $order_id = $request['order_id']; - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); // Check that order exists before capturing payment. if ( ! $order ) { diff --git a/includes/admin/class-wc-stripe-privacy.php b/includes/admin/class-wc-stripe-privacy.php index 01316093c3..ff909f1116 100644 --- a/includes/admin/class-wc-stripe-privacy.php +++ b/includes/admin/class-wc-stripe-privacy.php @@ -305,7 +305,7 @@ public function order_data_eraser( $email_address, $page ) { $messages = []; foreach ( (array) $orders as $order ) { - $order = WC_Stripe_Helper::get_order( $order->get_id() ); + $order = WC_Stripe_Order::get_by_id( $order->get_id() ); list( $removed, $retained, $msgs ) = $this->maybe_handle_order( $order ); $items_removed |= $removed; diff --git a/includes/class-wc-gateway-stripe.php b/includes/class-wc-gateway-stripe.php index a89a67a74f..b6cf3a2de7 100644 --- a/includes/class-wc-gateway-stripe.php +++ b/includes/class-wc-gateway-stripe.php @@ -220,7 +220,7 @@ public function payment_fields() { // If paying for order, we need to get email from the order not the user account. if ( parent::is_valid_pay_for_order_endpoint() ) { - $order = WC_Stripe_Helper::get_order( wc_clean( $wp->query_vars['order-pay'] ) ); + $order = WC_Stripe_Order::get_by_id( wc_clean( $wp->query_vars['order-pay'] ) ); $user_email = $order->get_billing_email(); } else { if ( $user->ID ) { @@ -387,7 +387,7 @@ public function complete_free_order( $order, $prepared_source, $force_save_sourc */ public function process_payment( $order_id, $retry = true, $force_save_source = false, $previous_error = false, $use_order_source = false ) { try { - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); if ( $this->has_subscription( $order_id ) ) { $force_save_source = true; @@ -547,7 +547,7 @@ public function display_order_fee( $order_id ) { return; } - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); $fee = $order->get_fee(); $currency = $order->get_stripe_currency(); @@ -584,7 +584,7 @@ public function display_order_payout( $order_id ) { return; } - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); $net = $order->get_net(); $currency = $order->get_stripe_currency(); @@ -690,7 +690,7 @@ public function change_no_available_methods_message() { */ public function prepare_intent_for_order_pay_page( $order = null ) { if ( ! isset( $order ) || empty( $order ) ) { - $order = WC_Stripe_Helper::get_order( absint( get_query_var( 'order-pay' ) ) ); + $order = WC_Stripe_Order::get_by_id( absint( get_query_var( 'order-pay' ) ) ); } $intent = $this->get_intent_from_order( $order ); @@ -735,7 +735,7 @@ public function prepare_intent_for_order_pay_page( $order = null ) { */ public function render_payment_intent_inputs( $order = null ) { if ( ! isset( $order ) || empty( $order ) ) { - $order = WC_Stripe_Helper::get_order( absint( get_query_var( 'order-pay' ) ) ); + $order = WC_Stripe_Order::get_by_id( absint( get_query_var( 'order-pay' ) ) ); } if ( ! isset( $this->order_pay_intent ) ) { $this->prepare_intent_for_order_pay_page( $order ); @@ -781,7 +781,7 @@ public function check_intent_status_on_order_page( $order_id ) { return; } - $order = WC_Stripe_Helper::get_order( absint( $order_id ) ); + $order = WC_Stripe_Order::get_by_id( absint( $order_id ) ); if ( ! $order ) { return; @@ -871,7 +871,7 @@ public function verify_intent_after_checkout( $order ) { // A webhook might have modified or locked the order while the intent was retreived. This ensures we are reading the right status. clean_post_cache( $order->get_id() ); - $order = WC_Stripe_Helper::get_order( $order->get_id() ); + $order = WC_Stripe_Order::get_by_id( $order->get_id() ); if ( ! $order->has_status( apply_filters( diff --git a/includes/class-wc-stripe-helper.php b/includes/class-wc-stripe-helper.php index d4ec24bf4a..03aadcfe0c 100644 --- a/includes/class-wc-stripe-helper.php +++ b/includes/class-wc-stripe-helper.php @@ -900,7 +900,7 @@ public static function get_order_by_source_id( $source_id ) { global $wpdb; if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { - $orders = self::get_orders( + $orders = WC_Stripe_Order::query( [ 'limit' => 1, 'meta_query' => [ @@ -917,7 +917,7 @@ public static function get_order_by_source_id( $source_id ) { } if ( ! empty( $order_id ) ) { - return self::get_order( $order_id ); + return self::get_by_id( $order_id ); } return false; @@ -938,7 +938,7 @@ public static function get_order_by_charge_id( $charge_id ) { } if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { - $orders = self::get_orders( + $orders = WC_Stripe_Order::query( [ 'transaction_id' => $charge_id, 'limit' => 1, @@ -950,7 +950,7 @@ public static function get_order_by_charge_id( $charge_id ) { } if ( ! empty( $order_id ) ) { - return self::get_order( $order_id ); + return self::get_by_id( $order_id ); } return false; @@ -966,7 +966,7 @@ public static function get_order_by_refund_id( $refund_id ) { global $wpdb; if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { - $orders = self::get_orders( + $orders = WC_Stripe_Order::query( [ 'limit' => 1, 'meta_query' => [ @@ -983,7 +983,7 @@ public static function get_order_by_refund_id( $refund_id ) { } if ( ! empty( $order_id ) ) { - return self::get_order( $order_id ); + return self::get_by_id( $order_id ); } return false; @@ -1000,7 +1000,7 @@ public static function get_order_by_intent_id( $intent_id ) { global $wpdb; if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { - $orders = self::get_orders( + $orders = WC_Stripe_Order::query( [ 'limit' => 1, 'meta_query' => [ @@ -1017,7 +1017,7 @@ public static function get_order_by_intent_id( $intent_id ) { } if ( ! empty( $order_id ) ) { - $order = self::get_order( $order_id ); + $order = self::get_by_id( $order_id ); } if ( ! empty( $order ) && $order->get_status() !== 'trash' ) { @@ -1038,7 +1038,7 @@ public static function get_order_by_setup_intent_id( $intent_id ) { global $wpdb; if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { - $orders = self::get_orders( + $orders = WC_Stripe_Order::query( [ 'limit' => 1, 'meta_query' => [ @@ -1055,7 +1055,7 @@ public static function get_order_by_setup_intent_id( $intent_id ) { } if ( ! empty( $order_id ) ) { - return self::get_order( $order_id ); + return self::get_by_id( $order_id ); } return false; @@ -1623,54 +1623,4 @@ public static function get_klarna_preferred_locale( $store_locale, $billing_coun return $target_locale; } - - /** - * Wrapper to return an order using the extension's custom WC_Stripe_Order class. - * - * @param $order_id int Order ID. - * @return bool|WC_Stripe_Order - */ - public static function get_order( $order_id ) { - $order = wc_get_order( $order_id ); - if ( ! $order ) { - return false; - } - - return new WC_Stripe_Order( $order ); - } - - /** - * Wrapper to get orders using the extension's custom WC_Stripe_Order class. - * - * @param $args array Arguments to pass to wc_get_orders. - * @return array|WC_Stripe_Order[] - */ - public static function get_orders( $args ) { - $orders = wc_get_orders( $args ); - if ( empty( $orders ) ) { - return []; - } - - return array_map( - function ( $order ) { - return new WC_Stripe_Order( $order ); - }, - $orders - ); - } - - /** - * Wrapper to create an order using the extension's custom WC_Stripe_Order class. - * - * @param $order_data array Order data. - * @return bool|WC_Stripe_Order - */ - public static function create_order( $order_data ) { - $order = wc_create_order( $order_data ); - if ( ! $order ) { - return false; - } - - return new WC_Stripe_Order( $order ); - } } diff --git a/includes/class-wc-stripe-intent-controller.php b/includes/class-wc-stripe-intent-controller.php index 4be0c8b8f7..12bbbc9dcb 100644 --- a/includes/class-wc-stripe-intent-controller.php +++ b/includes/class-wc-stripe-intent-controller.php @@ -88,7 +88,7 @@ protected function get_order_from_request() { } // Retrieve the order. - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); if ( ! $order ) { throw new WC_Stripe_Exception( 'missing-order', __( 'Missing order ID for payment confirmation', 'woocommerce-gateway-stripe' ) ); @@ -310,7 +310,7 @@ public function create_payment_intent_ajax() { $order_id = isset( $_POST['stripe_order_id'] ) ? absint( $_POST['stripe_order_id'] ) : null; if ( $order_id ) { - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); if ( ! $order || ! $order->needs_payment() ) { throw new Exception( __( 'Unable to process your request. Please reload the page and try again.', 'woocommerce-gateway-stripe' ) ); } @@ -339,7 +339,7 @@ public function create_payment_intent_ajax() { */ public function create_payment_intent( $order_id = null ) { $amount = WC()->cart->get_total( false ); - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); if ( is_a( $order, 'WC_Order' ) ) { $amount = $order->get_total(); } @@ -418,7 +418,7 @@ public function update_payment_intent_ajax() { * @return array|null An array with result of the update, or nothing */ public function update_payment_intent( $payment_intent_id = '', $order_id = null, $save_payment_method = false, $selected_upe_payment_type = '' ) { - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); if ( ! is_a( $order, 'WC_Order' ) ) { return; @@ -571,7 +571,7 @@ public function update_order_status_ajax() { } $order_id = isset( $_POST['order_id'] ) ? absint( $_POST['order_id'] ) : false; - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); if ( ! $order ) { throw new WC_Stripe_Exception( 'order_not_found', __( "We're not able to process this payment. Please try again later.", 'woocommerce-gateway-stripe' ) ); } @@ -635,7 +635,7 @@ public function update_failed_order_ajax() { $order_id = isset( $_POST['order_id'] ) ? absint( $_POST['order_id'] ) : null; $intent_id = isset( $_POST['intent_id'] ) ? wc_clean( wp_unslash( $_POST['intent_id'] ) ) : ''; - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); $order_from_payment = WC_Stripe_Helper::get_order_by_intent_id( $intent_id ); if ( ! $order_from_payment || $order_from_payment->get_id() !== $order_id ) { diff --git a/includes/class-wc-stripe-order-handler.php b/includes/class-wc-stripe-order-handler.php index c496cc47af..af79fb4ca3 100644 --- a/includes/class-wc-stripe-order-handler.php +++ b/includes/class-wc-stripe-order-handler.php @@ -140,7 +140,7 @@ public function process_redirect_payment( $order_id, $retry = true, $previous_er return; } - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); if ( ! is_object( $order ) ) { return; @@ -324,7 +324,7 @@ private function maybe_process_legacy_redirect() { */ public function capture_payment( $order_id ) { $result = new stdClass(); - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); if ( WC_Stripe_Helper::payment_method_allows_manual_capture( $order->get_payment_method() ) ) { $charge = $order->get_transaction_id(); @@ -432,7 +432,7 @@ public function capture_payment( $order_id ) { * @param int $order_id */ public function cancel_payment( $order_id ) { - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); if ( WC_Stripe_Helper::payment_method_allows_manual_capture( $order->get_payment_method() ) ) { if ( ! $order->charge_captured() ) { diff --git a/includes/class-wc-stripe-order.php b/includes/class-wc-stripe-order.php index df878fdc7a..619757b0d7 100644 --- a/includes/class-wc-stripe-order.php +++ b/includes/class-wc-stripe-order.php @@ -9,6 +9,56 @@ * Wrapper for the original WC_Order class to allow custom getters and setter with the extension's specific metadata. */ class WC_Stripe_Order extends WC_Order { + /** + * Wrapper to return an order using the extension's custom WC_Stripe_Order class. + * + * @param $order_id int Order ID. + * @return bool|WC_Stripe_Order + */ + public static function get_by_id( $order_id ) { + $order = wc_get_order( $order_id ); + if ( ! $order ) { + return false; + } + + return new WC_Stripe_Order( $order ); + } + + /** + * Wrapper to get orders using the extension's custom WC_Stripe_Order class. + * + * @param $args array Arguments to pass to wc_get_orders. + * @return array|WC_Stripe_Order[] + */ + public static function query( $args ) { + $orders = wc_get_orders( $args ); + if ( empty( $orders ) ) { + return []; + } + + return array_map( + function ( $order ) { + return new WC_Stripe_Order( $order ); + }, + $orders + ); + } + + /** + * Wrapper to create an order using the extension's custom WC_Stripe_Order class. + * + * @param $order_data array Order data. + * @return bool|WC_Stripe_Order + */ + public static function create( $order_data ) { + $order = wc_create_order( $order_data ); + if ( ! $order ) { + return false; + } + + return new WC_Stripe_Order( $order ); + } + /** * Get owner details. * diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index f09ebec6ea..0216913008 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -1107,7 +1107,7 @@ public function process_deferred_webhook( $webhook_type, $additional_data ) { switch ( $webhook_type ) { case 'payment_intent.succeeded': case 'payment_intent.amount_capturable_updated': - $order = isset( $additional_data['order_id'] ) ? WC_Stripe_Helper::get_order( $additional_data['order_id'] ) : null; + $order = isset( $additional_data['order_id'] ) ? WC_Stripe_Order::get_by_id( $additional_data['order_id'] ) : null; $intent_id = $additional_data['intent_id'] ?? ''; if ( empty( $order ) ) { @@ -1249,7 +1249,7 @@ private function get_order_from_intent( $intent ) { $data = explode( ':', $signature ); // Verify we received the order ID and signature (hash). - $order = isset( $data[0], $data[1] ) ? WC_Stripe_Helper::get_order( absint( $data[0] ) ) : false; + $order = isset( $data[0], $data[1] ) ? WC_Stripe_Order::get_by_id( absint( $data[0] ) ) : false; if ( $order ) { $intent_id = WC_Stripe_Helper::get_intent_id_from_order( $order ); diff --git a/includes/compat/trait-wc-stripe-pre-orders.php b/includes/compat/trait-wc-stripe-pre-orders.php index a270a64af8..1f9e00276b 100644 --- a/includes/compat/trait-wc-stripe-pre-orders.php +++ b/includes/compat/trait-wc-stripe-pre-orders.php @@ -197,7 +197,7 @@ public function mark_order_as_pre_ordered( $order ) { */ public function process_pre_order( $order_id ) { try { - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); // This will throw exception if not valid. $order->validate_minimum_amount(); // @phpstan-ignore-line (minimum amount is defined in the classes that use this trait) diff --git a/includes/compat/trait-wc-stripe-subscriptions.php b/includes/compat/trait-wc-stripe-subscriptions.php index 2b5920dfab..1949f83cbf 100644 --- a/includes/compat/trait-wc-stripe-subscriptions.php +++ b/includes/compat/trait-wc-stripe-subscriptions.php @@ -227,7 +227,7 @@ public function maybe_change_subscription_payment_method( $order_id ) { */ public function process_change_subscription_payment_method( $order_id ) { try { - $subscription = WC_Stripe_Helper::get_order( $order_id ); + $subscription = WC_Stripe_Order::get_by_id( $order_id ); $prepared_source = $this->prepare_source( get_current_user_id(), true ); $this->maybe_disallow_prepaid_card( $prepared_source->source_object ); @@ -792,7 +792,7 @@ private function get_mandate_for_subscription( $order, $payment_method ) { $renewal_order_ids = $order->get_related_orders( 'ids' ); foreach ( $renewal_order_ids as $renewal_order_id ) { - $renewal_order = WC_Stripe_Helper::get_order( $renewal_order_id ); + $renewal_order = WC_Stripe_Order::get_by_id( $renewal_order_id ); if ( ! $renewal_order instanceof WC_Order ) { continue; } @@ -936,7 +936,7 @@ public function maybe_render_subscription_payment_method( $payment_method_to_dis // If we couldn't find a Stripe customer linked to the account, fallback to the order meta data. if ( ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) && false !== $subscription->get_parent() ) { - $parent_order = WC_Stripe_Helper::get_order( $subscription->get_parent_id() ); + $parent_order = WC_Stripe_Order::get_by_id( $subscription->get_parent_id() ); $stripe_customer_id = $parent_order->get_meta( '_stripe_customer_id', true ); $stripe_source_id = $parent_order->get_source_id(); @@ -1147,7 +1147,7 @@ public function update_subscription_payment_method_from_order( $order, $payment_ * @return boolean true if the subscription can be edited, false otherwise. */ public function disable_subscription_edit_for_india( $editable, $order ) { - $parent_order = WC_Stripe_Helper::get_order( $order->get_parent_id() ); + $parent_order = WC_Stripe_Order::get_by_id( $order->get_parent_id() ); if ( $this->is_subscriptions_enabled() && $this->is_subscription( $order ) && $parent_order diff --git a/includes/payment-methods/class-wc-gateway-stripe-alipay.php b/includes/payment-methods/class-wc-gateway-stripe-alipay.php index 224f077d6b..98049327ac 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-alipay.php +++ b/includes/payment-methods/class-wc-gateway-stripe-alipay.php @@ -189,7 +189,7 @@ public function payment_fields() { // If paying from order, we need to get total from order not cart. if ( parent::is_valid_pay_for_order_endpoint() ) { - $order = WC_Stripe_Helper::get_order( wc_clean( $wp->query_vars['order-pay'] ) ); + $order = WC_Stripe_Order::get_by_id( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } @@ -252,7 +252,7 @@ public function create_source( $order ) { */ public function process_payment( $order_id, $retry = true, $force_save_save = false ) { try { - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); // This will throw exception if not valid. $order->validate_minimum_amount(); diff --git a/includes/payment-methods/class-wc-gateway-stripe-bancontact.php b/includes/payment-methods/class-wc-gateway-stripe-bancontact.php index ab422e9dc9..68fb4fdb60 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-bancontact.php +++ b/includes/payment-methods/class-wc-gateway-stripe-bancontact.php @@ -176,7 +176,7 @@ public function payment_fields() { // If paying from order, we need to get total from order not cart. if ( parent::is_valid_pay_for_order_endpoint() ) { - $order = WC_Stripe_Helper::get_order( wc_clean( $wp->query_vars['order-pay'] ) ); + $order = WC_Stripe_Order::get_by_id( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } @@ -240,7 +240,7 @@ public function create_source( $order ) { */ public function process_payment( $order_id, $retry = true, $force_save_source = false ) { try { - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); // This will throw exception if not valid. $order->validate_minimum_amount(); diff --git a/includes/payment-methods/class-wc-gateway-stripe-eps.php b/includes/payment-methods/class-wc-gateway-stripe-eps.php index dd4a8fb1cb..b1d03c4198 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-eps.php +++ b/includes/payment-methods/class-wc-gateway-stripe-eps.php @@ -176,7 +176,7 @@ public function payment_fields() { // If paying from order, we need to get total from order not cart. if ( parent::is_valid_pay_for_order_endpoint() ) { - $order = WC_Stripe_Helper::get_order( wc_clean( $wp->query_vars['order-pay'] ) ); + $order = WC_Stripe_Order::get_by_id( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } @@ -239,7 +239,7 @@ public function create_source( $order ) { */ public function process_payment( $order_id, $retry = true, $force_save_source = false ) { try { - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); // This will throw exception if not valid. $order->validate_minimum_amount(); diff --git a/includes/payment-methods/class-wc-gateway-stripe-giropay.php b/includes/payment-methods/class-wc-gateway-stripe-giropay.php index 333365361a..551f1a12b7 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-giropay.php +++ b/includes/payment-methods/class-wc-gateway-stripe-giropay.php @@ -172,7 +172,7 @@ public function payment_fields() { // If paying from order, we need to get total from order not cart. if ( parent::is_valid_pay_for_order_endpoint() ) { - $order = WC_Stripe_Helper::get_order( wc_clean( $wp->query_vars['order-pay'] ) ); + $order = WC_Stripe_Order::get_by_id( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } @@ -235,7 +235,7 @@ public function create_source( $order ) { */ public function process_payment( $order_id, $retry = true, $force_save_source = false ) { try { - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); // This will throw exception if not valid. $order->validate_minimum_amount(); diff --git a/includes/payment-methods/class-wc-gateway-stripe-ideal.php b/includes/payment-methods/class-wc-gateway-stripe-ideal.php index 911c2dceb6..bfd28a9ffe 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-ideal.php +++ b/includes/payment-methods/class-wc-gateway-stripe-ideal.php @@ -176,7 +176,7 @@ public function payment_fields() { // If paying from order, we need to get total from order not cart. if ( parent::is_valid_pay_for_order_endpoint() ) { - $order = WC_Stripe_Helper::get_order( wc_clean( $wp->query_vars['order-pay'] ) ); + $order = WC_Stripe_Order::get_by_id( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } @@ -239,7 +239,7 @@ public function create_source( $order ) { */ public function process_payment( $order_id, $retry = true, $force_save_source = false ) { try { - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); // This will throw exception if not valid. $order->validate_minimum_amount(); diff --git a/includes/payment-methods/class-wc-gateway-stripe-multibanco.php b/includes/payment-methods/class-wc-gateway-stripe-multibanco.php index 4b9e80b9a6..fd68b923c9 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-multibanco.php +++ b/includes/payment-methods/class-wc-gateway-stripe-multibanco.php @@ -180,7 +180,7 @@ public function payment_fields() { // If paying from order, we need to get total from order not cart. if ( parent::is_valid_pay_for_order_endpoint() ) { - $order = WC_Stripe_Helper::get_order( wc_clean( $wp->query_vars['order-pay'] ) ); + $order = WC_Stripe_Order::get_by_id( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } @@ -242,7 +242,7 @@ public function email_instructions( $order, $sent_to_admin, $plain_text = false */ public function get_instructions( $order, $plain_text = false ) { if ( true === is_int( $order ) ) { - $order = WC_Stripe_Helper::get_order( $order ); + $order = WC_Stripe_Order::get_by_id( $order ); } $data = $order->get_multibanco_data(); @@ -337,7 +337,7 @@ public function create_source( $order ) { */ public function process_payment( $order_id, $retry = true, $force_save_source = false ) { try { - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); // This will throw exception if not valid. $order->validate_minimum_amount(); diff --git a/includes/payment-methods/class-wc-gateway-stripe-p24.php b/includes/payment-methods/class-wc-gateway-stripe-p24.php index f7eb3e4560..1524177b81 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-p24.php +++ b/includes/payment-methods/class-wc-gateway-stripe-p24.php @@ -177,7 +177,7 @@ public function payment_fields() { // If paying from order, we need to get total from order not cart. if ( parent::is_valid_pay_for_order_endpoint() ) { - $order = WC_Stripe_Helper::get_order( wc_clean( $wp->query_vars['order-pay'] ) ); + $order = WC_Stripe_Order::get_by_id( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } @@ -236,7 +236,7 @@ public function create_source( $order ) { */ public function process_payment( $order_id, $retry = true, $force_save_source = false ) { try { - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); // This will throw exception if not valid. $order->validate_minimum_amount(); diff --git a/includes/payment-methods/class-wc-gateway-stripe-sepa.php b/includes/payment-methods/class-wc-gateway-stripe-sepa.php index 3cd500ae16..4581ecf585 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-sepa.php +++ b/includes/payment-methods/class-wc-gateway-stripe-sepa.php @@ -230,7 +230,7 @@ public function payment_fields() { // If paying from order, we need to get total from order not cart. if ( parent::is_valid_pay_for_order_endpoint() ) { - $order = WC_Stripe_Helper::get_order( wc_clean( $wp->query_vars['order-pay'] ) ); + $order = WC_Stripe_Order::get_by_id( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } @@ -280,7 +280,7 @@ public function payment_fields() { */ public function process_payment( $order_id, $retry = true, $force_save_source = false ) { try { - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); if ( $this->has_subscription( $order_id ) ) { $force_save_source = true; diff --git a/includes/payment-methods/class-wc-gateway-stripe-sofort.php b/includes/payment-methods/class-wc-gateway-stripe-sofort.php index 7cf68a2964..df61c13d21 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-sofort.php +++ b/includes/payment-methods/class-wc-gateway-stripe-sofort.php @@ -176,7 +176,7 @@ public function payment_fields() { // If paying from order, we need to get total from order not cart. if ( parent::is_valid_pay_for_order_endpoint() ) { - $order = WC_Stripe_Helper::get_order( wc_clean( $wp->query_vars['order-pay'] ) ); + $order = WC_Stripe_Order::get_by_id( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } @@ -244,7 +244,7 @@ public function create_source( $order ) { */ public function process_payment( $order_id, $retry = true, $force_save_source = false ) { try { - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); // This will throw exception if not valid. $order->validate_minimum_amount(); diff --git a/includes/payment-methods/class-wc-stripe-express-checkout-ajax-handler.php b/includes/payment-methods/class-wc-stripe-express-checkout-ajax-handler.php index 1f6c94e33c..31336accc0 100644 --- a/includes/payment-methods/class-wc-stripe-express-checkout-ajax-handler.php +++ b/includes/payment-methods/class-wc-stripe-express-checkout-ajax-handler.php @@ -372,7 +372,7 @@ public function ajax_pay_for_order() { wc_set_time_limit( 0 ); // Load the order. - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); if ( ! is_a( $order, WC_Order::class ) ) { throw new Exception( __( 'Invalid order!', 'woocommerce-gateway-stripe' ) ); diff --git a/includes/payment-methods/class-wc-stripe-express-checkout-element.php b/includes/payment-methods/class-wc-stripe-express-checkout-element.php index 0482d91f0b..23072d90ac 100644 --- a/includes/payment-methods/class-wc-stripe-express-checkout-element.php +++ b/includes/payment-methods/class-wc-stripe-express-checkout-element.php @@ -359,7 +359,7 @@ public function add_order_meta( $order_id, $posted_data ) { return; } - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); $express_checkout_type = wc_clean( wp_unslash( $_POST['express_checkout_type'] ) ); @@ -390,7 +390,7 @@ public function filter_gateway_title( $title, $id ) { // If $theorder is empty (i.e. non-HPOS), fallback to using the global post object. if ( empty( $theorder ) && ! empty( $GLOBALS['post']->ID ) ) { - $theorder = WC_Stripe_Helper::get_order( $GLOBALS['post']->ID ); + $theorder = WC_Stripe_Order::get_by_id( $GLOBALS['post']->ID ); } if ( ! is_object( $theorder ) ) { diff --git a/includes/payment-methods/class-wc-stripe-payment-request.php b/includes/payment-methods/class-wc-stripe-payment-request.php index 06ac8e52b3..9dc6c776c7 100644 --- a/includes/payment-methods/class-wc-stripe-payment-request.php +++ b/includes/payment-methods/class-wc-stripe-payment-request.php @@ -473,7 +473,7 @@ public function filter_gateway_title( $title, $id ) { // If $theorder is empty (i.e. non-HPOS), fallback to using the global post object. if ( empty( $theorder ) && ! empty( $GLOBALS['post']->ID ) ) { - $theorder = WC_Stripe_Helper::get_order( $GLOBALS['post']->ID ); + $theorder = WC_Stripe_Order::get_by_id( $GLOBALS['post']->ID ); } if ( ! is_object( $theorder ) ) { @@ -564,7 +564,7 @@ public function add_order_meta( $order_id, $posted_data ) { return; } - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); $payment_request_type = wc_clean( wp_unslash( $_POST['payment_request_type'] ) ); diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index 64b5ddb363..a1e3b7767b 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -421,7 +421,7 @@ public function javascript_params() { if ( parent::is_valid_pay_for_order_endpoint() || $is_change_payment_method ) { $order_id = absint( get_query_var( 'order-pay' ) ); - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); // Make billing country available for subscriptions as well, so country-restricted payment methods can be shown. if ( is_a( $order, 'WC_Order' ) ) { @@ -651,7 +651,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = } $payment_intent_id = isset( $_POST['wc_payment_intent_id'] ) ? wc_clean( wp_unslash( $_POST['wc_payment_intent_id'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); $payment_needed = $this->is_payment_needed( $order_id ); $save_payment_method = $this->has_subscription( $order_id ) || ! empty( $_POST[ 'wc-' . self::ID . '-new-payment-method' ] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing $selected_upe_payment_type = ! empty( $_POST['wc_stripe_selected_upe_payment_type'] ) ? wc_clean( wp_unslash( $_POST['wc_stripe_selected_upe_payment_type'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing @@ -779,7 +779,7 @@ private function process_payment_with_deferred_intent( int $order_id ) { return $this->process_change_subscription_payment_with_deferred_intent( $order_id ); } - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); try { $payment_information = $this->prepare_payment_information_from_request( $order ); @@ -954,7 +954,7 @@ private function process_payment_with_deferred_intent( int $order_id ) { */ public function process_payment_with_saved_payment_method( $order_id, $can_retry = true ) { try { - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); if ( $this->maybe_process_pre_orders( $order_id ) ) { return $this->process_pre_order( $order_id ); @@ -1219,7 +1219,7 @@ private function is_order_associated_to_payment_intent( int $order_id, string $i * @return bool */ private function is_order_associated_to_setup_intent( int $order_id, string $intent_id ): bool { - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); if ( ! $order ) { return false; } @@ -1251,7 +1251,7 @@ private function is_order_associated_to_setup_intent( int $order_id, string $int * @version 5.5.0 */ public function process_upe_redirect_payment( $order_id, $intent_id, $save_payment_method ) { - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); if ( ! is_object( $order ) ) { return; @@ -1482,7 +1482,7 @@ public function is_payment_needed( $order_id = null ) { // Free trial subscriptions without a sign up fee, or any other type // of order with a `0` amount should fall into the logic below. $amount = is_null( WC()->cart ) ? 0 : WC()->cart->get_total( false ); - $order = isset( $order_id ) ? WC_Stripe_Helper::get_order( $order_id ) : null; + $order = isset( $order_id ) ? WC_Stripe_Order::get_by_id( $order_id ) : null; if ( is_a( $order, 'WC_Order' ) ) { $amount = $order->get_total(); } @@ -2403,7 +2403,7 @@ private function get_return_url_for_redirect( $order, $save_payment_method ) { */ private function get_existing_compatible_payment_intent( $order, $payment_method_types ) { // Reload the order to make sure we have the latest data. - $order = WC_Stripe_Helper::get_order( $order->get_id() ); + $order = WC_Stripe_Order::get_by_id( $order->get_id() ); $intent = $this->get_intent_from_order( $order ); if ( ! $intent ) { return null; diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-method-link.php b/includes/payment-methods/class-wc-stripe-upe-payment-method-link.php index 54f0bb3f41..efcbb20b28 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-method-link.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-method-link.php @@ -120,7 +120,7 @@ public function filter_gateway_title( $title, $id ) { // If $theorder is empty (i.e. non-HPOS), fallback to using the global post object. if ( empty( $theorder ) && ! empty( $GLOBALS['post']->ID ) ) { - $theorder = WC_Stripe_Helper::get_order( $GLOBALS['post']->ID ); + $theorder = WC_Stripe_Order::get_by_id( $GLOBALS['post']->ID ); } if ( ! is_object( $theorder ) ) { diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-method-multibanco.php b/includes/payment-methods/class-wc-stripe-upe-payment-method-multibanco.php index 9fdf13bd84..804c6392ae 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-method-multibanco.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-method-multibanco.php @@ -41,7 +41,7 @@ public function __construct() { * @param int $order_id */ public function thankyou_page( $order_id ) { - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); if ( ! $order ) { return; } diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-method.php b/includes/payment-methods/class-wc-stripe-upe-payment-method.php index 0bbf927d40..2f4476be49 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-method.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-method.php @@ -599,7 +599,7 @@ public function get_limits_per_currency(): array { */ public function get_current_order_amount() { if ( is_wc_endpoint_url( 'order-pay' ) && isset( $_GET['key'] ) ) { - $order = WC_Stripe_Helper::get_order( absint( get_query_var( 'order-pay' ) ) ); + $order = WC_Stripe_Order::get_by_id( absint( get_query_var( 'order-pay' ) ) ); return $order->get_total( '' ); } elseif ( WC()->cart ) { return WC()->cart->get_total( '' ); diff --git a/tests/phpunit/admin/test-class-wc-rest-stripe-orders-controller.php b/tests/phpunit/admin/test-class-wc-rest-stripe-orders-controller.php index 1b4c578fc1..562b1e80f6 100644 --- a/tests/phpunit/admin/test-class-wc-rest-stripe-orders-controller.php +++ b/tests/phpunit/admin/test-class-wc-rest-stripe-orders-controller.php @@ -140,7 +140,7 @@ public function test_capture_payment_success() { $request->set_param( 'payment_intent_id', 'pi_12345' ); $response = rest_do_request( $request ); - $order = WC_Stripe_Helper::get_order( $order->get_id() ); + $order = WC_Stripe_Order::get_by_id( $order->get_id() ); $this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 'succeeded', $response->get_data()['status'] ); diff --git a/tests/phpunit/helpers/class-wc-helper-order.php b/tests/phpunit/helpers/class-wc-helper-order.php index edab7a6bc2..6271819201 100644 --- a/tests/phpunit/helpers/class-wc-helper-order.php +++ b/tests/phpunit/helpers/class-wc-helper-order.php @@ -60,7 +60,7 @@ public static function create_order( $customer_id = 1, $product = null, $order_p ]; $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; // Required, else wc_create_order throws an exception. - $order = WC_Stripe_Helper::create_order( $order_data ); + $order = WC_Stripe_Order::create( $order_data ); // Add order products. $item = new WC_Order_Item_Product(); diff --git a/tests/phpunit/test-class-wc-stripe-order-handler.php b/tests/phpunit/test-class-wc-stripe-order-handler.php index 229649318e..6981dd5e1b 100644 --- a/tests/phpunit/test-class-wc-stripe-order-handler.php +++ b/tests/phpunit/test-class-wc-stripe-order-handler.php @@ -22,7 +22,7 @@ public function test_prevent_cancelling_orders_awaiting_action() { $order->set_payment_awaiting_action(); // Read in a fresh order object with meta like `date_modified` set. - $order = WC_Stripe_Helper::get_order( $order->get_id() ); + $order = WC_Stripe_Order::get_by_id( $order->get_id() ); // Test when false is passed that the order is not cancelled. $this->assertFalse( $this->order_handler->prevent_cancelling_orders_awaiting_action( false, $order ) ); diff --git a/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php b/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php index a3ca3c2de9..34d787be7a 100644 --- a/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php +++ b/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php @@ -330,7 +330,7 @@ public function test_process_payment_returns_valid_response() { $this->mock_gateway->expects( $this->any() ) ->method( 'get_stripe_customer_from_order' ) - ->with( WC_Stripe_Helper::get_order( $order_id ) ) + ->with( WC_Stripe_Order::get_by_id( $order_id ) ) ->will( $this->returnValue( $this->mock_stripe_customer ) ); @@ -339,7 +339,7 @@ public function test_process_payment_returns_valid_response() { ->with( "payment_intents/$payment_intent_id", $expected_request, - WC_Stripe_Helper::get_order( $order_id ) + WC_Stripe_Order::get_by_id( $order_id ) ) ->will( $this->returnValue( [] ) @@ -632,7 +632,7 @@ public function test_process_payment_deferred_intent_handles_exception() { $this->assertEquals( 'failure', $response['result'] ); - $processed_order = WC_Stripe_Helper::get_order( $order_id ); + $processed_order = WC_Stripe_Order::get_by_id( $order_id ); $this->assertEquals( 'failed', $processed_order->get_status() ); } @@ -678,7 +678,7 @@ public function test_process_payment_deferred_intent_bails_with_empty_payment_ty $this->assertEquals( 'failure', $response['result'] ); - $processed_order = WC_Stripe_Helper::get_order( $order_id ); + $processed_order = WC_Stripe_Order::get_by_id( $order_id ); $this->assertEquals( 'failed', $processed_order->get_status() ); } @@ -724,7 +724,7 @@ public function test_process_payment_deferred_intent_bails_with_invalid_payment_ $this->assertEquals( 'failure', $response['result'] ); - $processed_order = WC_Stripe_Helper::get_order( $order_id ); + $processed_order = WC_Stripe_Order::get_by_id( $order_id ); $this->assertEquals( 'failed', $processed_order->get_status() ); } @@ -776,7 +776,7 @@ public function test_process_redirect_payment_returns_valid_response() { $this->mock_gateway->process_upe_redirect_payment( $order_id, $payment_intent_id, false ); - $final_order = WC_Stripe_Helper::get_order( $order_id ); + $final_order = WC_Stripe_Order::get_by_id( $order_id ); $note = wc_get_order_notes( [ 'order_id' => $order_id, @@ -839,7 +839,7 @@ public function test_process_redirect_payment_only_runs_once() { $this->mock_gateway->process_upe_redirect_payment( $order_id, $payment_intent_id, false ); - $success_order = WC_Stripe_Helper::get_order( $order_id ); + $success_order = WC_Stripe_Order::get_by_id( $order_id ); $note = wc_get_order_notes( [ @@ -862,7 +862,7 @@ public function test_process_redirect_payment_only_runs_once() { // attempt to reprocess the order and confirm status is unchanged $this->mock_gateway->process_upe_redirect_payment( $order_id, $payment_intent_id, false ); - $final_order = WC_Stripe_Helper::get_order( $order_id ); + $final_order = WC_Stripe_Order::get_by_id( $order_id ); $this->assertEquals( 'failed', $final_order->get_status() ); } @@ -893,7 +893,7 @@ public function test_checkout_without_payment_uses_setup_intents() { $this->mock_gateway->expects( $this->any() ) ->method( 'get_stripe_customer_from_order' ) - ->with( WC_Stripe_Helper::get_order( $order_id ) ) + ->with( WC_Stripe_Order::get_by_id( $order_id ) ) ->will( $this->returnValue( $this->mock_stripe_customer ) ); @@ -908,7 +908,7 @@ public function test_checkout_without_payment_uses_setup_intents() { $this->mock_gateway->process_upe_redirect_payment( $order_id, $setup_intent_id, true ); - $final_order = WC_Stripe_Helper::get_order( $order_id ); + $final_order = WC_Stripe_Order::get_by_id( $order_id ); $this->assertEquals( 'processing', $final_order->get_status() ); $this->assertEquals( $customer_id, $final_order->get_stripe_customer_id() ); @@ -944,7 +944,7 @@ public function test_checkout_saves_payment_method_to_order() { $this->mock_gateway->expects( $this->any() ) ->method( 'get_stripe_customer_from_order' ) - ->with( WC_Stripe_Helper::get_order( $order_id ) ) + ->with( WC_Stripe_Order::get_by_id( $order_id ) ) ->will( $this->returnValue( $this->mock_stripe_customer ) ); @@ -970,7 +970,7 @@ public function test_checkout_saves_payment_method_to_order() { $this->mock_gateway->process_upe_redirect_payment( $order_id, $payment_intent_id, true ); - $final_order = WC_Stripe_Helper::get_order( $order_id ); + $final_order = WC_Stripe_Order::get_by_id( $order_id ); $this->assertEquals( 'processing', $final_order->get_status() ); $this->assertEquals( $payment_intent_id, $final_order->get_intent_id() ); @@ -1009,7 +1009,7 @@ public function test_checkout_saves_sepa_generated_payment_method_to_order() { $this->mock_gateway->expects( $this->any() ) ->method( 'get_stripe_customer_from_order' ) - ->with( WC_Stripe_Helper::get_order( $order_id ) ) + ->with( WC_Stripe_Order::get_by_id( $order_id ) ) ->will( $this->returnValue( $this->mock_stripe_customer ) ); @@ -1038,7 +1038,7 @@ public function test_checkout_saves_sepa_generated_payment_method_to_order() { $this->mock_gateway->process_upe_redirect_payment( $order_id, $payment_intent_id, true ); - $final_order = WC_Stripe_Helper::get_order( $order_id ); + $final_order = WC_Stripe_Order::get_by_id( $order_id ); $this->assertEquals( 'processing', $final_order->get_status() ); $this->assertEquals( $payment_intent_id, $final_order->get_intent_id() ); @@ -1084,7 +1084,7 @@ public function test_setup_intent_checkout_saves_sepa_generated_payment_method_t $this->mock_gateway->expects( $this->any() ) ->method( 'get_stripe_customer_from_order' ) - ->with( WC_Stripe_Helper::get_order( $order_id ) ) + ->with( WC_Stripe_Order::get_by_id( $order_id ) ) ->will( $this->returnValue( $this->mock_stripe_customer ) ); @@ -1097,7 +1097,7 @@ public function test_setup_intent_checkout_saves_sepa_generated_payment_method_t $this->mock_gateway->process_upe_redirect_payment( $order_id, $setup_intent_id, true ); - $final_order = WC_Stripe_Helper::get_order( $order_id ); + $final_order = WC_Stripe_Order::get_by_id( $order_id ); $this->assertEquals( 'processing', $final_order->get_status() ); $this->assertEquals( $customer_id, $final_order->get_stripe_customer_id() ); @@ -1174,8 +1174,8 @@ public function test_process_response_updates_order_by_charge_status() { // Test no charge captured. $charge_mock['captured'] = false; $charge_mock['id'] = 'ch_mock_1'; - $this->mock_gateway->process_response( $this->array_to_object( $charge_mock ), WC_Stripe_Helper::get_order( $order_id ) ); - $test_order = WC_Stripe_Helper::get_order( $order_id ); + $this->mock_gateway->process_response( $this->array_to_object( $charge_mock ), WC_Stripe_Order::get_by_id( $order_id ) ); + $test_order = WC_Stripe_Order::get_by_id( $order_id ); $this->assertFalse( $test_order->charge_captured() ); $this->assertEquals( $charge_mock['id'], $test_order->get_transaction_id() ); @@ -1184,8 +1184,8 @@ public function test_process_response_updates_order_by_charge_status() { // Test charge succeeds. $charge_mock['captured'] = true; $charge_mock['id'] = 'ch_mock_2'; - $this->mock_gateway->process_response( $this->array_to_object( $charge_mock ), WC_Stripe_Helper::get_order( $order_id ) ); - $test_order = WC_Stripe_Helper::get_order( $order_id ); + $this->mock_gateway->process_response( $this->array_to_object( $charge_mock ), WC_Stripe_Order::get_by_id( $order_id ) ); + $test_order = WC_Stripe_Order::get_by_id( $order_id ); $this->assertTrue( $test_order->charge_captured() ); $this->assertEquals( 'processing', $test_order->get_status() ); @@ -1193,8 +1193,8 @@ public function test_process_response_updates_order_by_charge_status() { // Test charge pending. $charge_mock['status'] = 'pending'; $charge_mock['id'] = 'ch_mock_3'; - $this->mock_gateway->process_response( $this->array_to_object( $charge_mock ), WC_Stripe_Helper::get_order( $order_id ) ); - $test_order = WC_Stripe_Helper::get_order( $order_id ); + $this->mock_gateway->process_response( $this->array_to_object( $charge_mock ), WC_Stripe_Order::get_by_id( $order_id ) ); + $test_order = WC_Stripe_Order::get_by_id( $order_id ); $this->assertTrue( $test_order->charge_captured() ); $this->assertEquals( $charge_mock['id'], $test_order->get_transaction_id() ); @@ -1205,7 +1205,7 @@ public function test_process_response_updates_order_by_charge_status() { $charge_mock['id'] = 'ch_mock_4'; $exception = null; try { - $this->mock_gateway->process_response( $this->array_to_object( $charge_mock ), WC_Stripe_Helper::get_order( $order_id ) ); + $this->mock_gateway->process_response( $this->array_to_object( $charge_mock ), WC_Stripe_Order::get_by_id( $order_id ) ); } catch ( WC_Stripe_Exception $e ) { // Test that exception is thrown. $exception = $e; @@ -1296,7 +1296,7 @@ function( $passed_order ) use ( $order ) { ->willReturn( $this->array_to_object( $charge ) ); $response = $this->mock_gateway->process_payment( $order_id ); - $final_order = WC_Stripe_Helper::get_order( $order_id ); + $final_order = WC_Stripe_Order::get_by_id( $order_id ); $note = wc_get_order_notes( [ 'order_id' => $order_id, @@ -1384,7 +1384,7 @@ function( $passed_order ) use ( $order ) { ->willReturn( $this->array_to_object( $charge ) ); $response = $this->mock_gateway->process_payment( $order_id ); - $final_order = WC_Stripe_Helper::get_order( $order_id ); + $final_order = WC_Stripe_Order::get_by_id( $order_id ); $client_secret = $payment_intent_mock->client_secret; $this->assertEquals( 'success', $response['result'] ); @@ -1447,7 +1447,7 @@ function( $passed_order ) use ( $order ) { ); $response = $this->mock_gateway->process_payment( $order_id ); - $final_order = WC_Stripe_Helper::get_order( $order_id ); + $final_order = WC_Stripe_Order::get_by_id( $order_id ); $this->assertEquals( 'failure', $response['result'] ); $this->assertEquals( 'failed', $final_order->get_status() ); @@ -1542,7 +1542,7 @@ function( $passed_order ) use ( $order ) { ->willReturn( $this->array_to_object( $charge ) ); $response = $this->mock_gateway->process_payment( $order_id ); - $final_order = WC_Stripe_Helper::get_order( $order_id ); + $final_order = WC_Stripe_Order::get_by_id( $order_id ); $note = wc_get_order_notes( [ 'order_id' => $order_id, @@ -1637,7 +1637,7 @@ function( $passed_order ) use ( $order ) { ); $response = $this->mock_gateway->process_payment( $order_id ); - $final_order = WC_Stripe_Helper::get_order( $order_id ); + $final_order = WC_Stripe_Order::get_by_id( $order_id ); $this->assertEquals( 'failure', $response['result'] ); $this->assertEquals( 'failed', $final_order->get_status() ); @@ -1689,7 +1689,7 @@ public function test_if_order_has_subscription_payment_method_will_be_saved() { $this->mock_gateway->expects( $this->any() ) ->method( 'get_stripe_customer_from_order' ) - ->with( WC_Stripe_Helper::get_order( $order_id ) ) + ->with( WC_Stripe_Order::get_by_id( $order_id ) ) ->will( $this->returnValue( $this->mock_stripe_customer ) ); @@ -1699,7 +1699,7 @@ public function test_if_order_has_subscription_payment_method_will_be_saved() { ->with( "payment_intents/$payment_intent_id", $expected_request, - WC_Stripe_Helper::get_order( $order_id ) + WC_Stripe_Order::get_by_id( $order_id ) ) ->will( $this->returnValue( [] ) @@ -1772,7 +1772,7 @@ public function test_subscription_renewal_is_successful() { $order->set_lock_payment( ( time() + MINUTE_IN_SECONDS ) ); // To assist with comparing expected order objects, set an existing lock. $order->save(); - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); $payment_method_mock = self::MOCK_CARD_PAYMENT_METHOD_TEMPLATE; $payment_method_mock['id'] = $payment_method_id; @@ -1826,7 +1826,7 @@ public function test_subscription_renewal_is_successful() { $this->mock_gateway->process_subscription_payment( $amount, $order, false, false ); - $final_order = WC_Stripe_Helper::get_order( $order_id ); + $final_order = WC_Stripe_Order::get_by_id( $order_id ); $note = wc_get_order_notes( [ 'order_id' => $order_id, @@ -1866,7 +1866,7 @@ public function test_subscription_renewal_checks_payment_method_authorization() $order->set_lock_payment( ( time() + MINUTE_IN_SECONDS ) ); // To assist with comparing expected order objects, set an existing lock. $order->save(); - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); $payment_method_mock = self::MOCK_CARD_PAYMENT_METHOD_TEMPLATE; $payment_method_mock['id'] = $payment_method_id; @@ -1928,7 +1928,7 @@ public function test_subscription_renewal_checks_payment_method_authorization() $this->mock_gateway->process_subscription_payment( $amount, $order, false, false ); - $final_order = WC_Stripe_Helper::get_order( $order_id ); + $final_order = WC_Stripe_Order::get_by_id( $order_id ); $note = wc_get_order_notes( [ 'order_id' => $order_id, @@ -1999,14 +1999,14 @@ public function test_pre_order_payment_is_successful() { ); $this->mock_gateway->expects( $this->any() ) ->method( 'get_stripe_customer_from_order' ) - ->with( WC_Stripe_Helper::get_order( $order_id ) ) + ->with( WC_Stripe_Order::get_by_id( $order_id ) ) ->will( $this->returnValue( $this->mock_stripe_customer ) ); $this->mock_gateway->expects( $this->any() ) ->method( 'has_pre_order_charged_upon_release' ) - ->with( WC_Stripe_Helper::get_order( $order_id ) ) + ->with( WC_Stripe_Order::get_by_id( $order_id ) ) ->will( $this->returnValue( true ) ); $this->mock_gateway->expects( $this->once() ) @@ -2025,7 +2025,7 @@ public function test_pre_order_payment_is_successful() { $this->mock_gateway->process_upe_redirect_payment( $order_id, $payment_intent_id, false ); - $final_order = WC_Stripe_Helper::get_order( $order_id ); + $final_order = WC_Stripe_Order::get_by_id( $order_id ); $this->assertEquals( 'Credit / Debit Card', $final_order->get_payment_method_title() ); $this->assertEquals( $payment_method_id, $final_order->get_source_id() ); @@ -2060,7 +2060,7 @@ public function test_pre_order_without_payment_uses_setup_intents() { $this->mock_gateway->expects( $this->any() ) ->method( 'get_stripe_customer_from_order' ) - ->with( WC_Stripe_Helper::get_order( $order_id ) ) + ->with( WC_Stripe_Order::get_by_id( $order_id ) ) ->will( $this->returnValue( $this->mock_stripe_customer ) ); @@ -2092,7 +2092,7 @@ public function test_pre_order_without_payment_uses_setup_intents() { $this->mock_gateway->process_upe_redirect_payment( $order_id, $setup_intent_id, true ); - $final_order = WC_Stripe_Helper::get_order( $order_id ); + $final_order = WC_Stripe_Order::get_by_id( $order_id ); $this->assertEquals( $payment_method_id, $final_order->get_source_id() ); $this->assertEquals( $customer_id, $final_order->get_stripe_customer_id() ); @@ -2264,7 +2264,7 @@ function( $passed_order ) use ( $order ) { ); $response = $this->mock_gateway->process_payment( $order_id ); - $final_order = WC_Stripe_Helper::get_order( $order_id ); + $final_order = WC_Stripe_Order::get_by_id( $order_id ); $note = wc_get_order_notes( [ 'order_id' => $order_id, diff --git a/tests/phpunit/test-wc-stripe-payment-gateway.php b/tests/phpunit/test-wc-stripe-payment-gateway.php index 1e0ccc1f49..b878c0823b 100644 --- a/tests/phpunit/test-wc-stripe-payment-gateway.php +++ b/tests/phpunit/test-wc-stripe-payment-gateway.php @@ -666,7 +666,7 @@ public function test_lock_order_payment() { // test two instances of the same order, one locked and one not. $order_4 = WC_Helper_Order::create_order(); - $dup_order = WC_Stripe_Helper::get_order( $order_4->get_id() ); + $dup_order = WC_Stripe_Order::get_by_id( $order_4->get_id() ); $order_4->lock_payment(); $dup_locked = $dup_order->lock_payment(); diff --git a/tests/phpunit/test-wc-stripe-sub-initial.php b/tests/phpunit/test-wc-stripe-sub-initial.php index f8b4b44402..0f64974842 100644 --- a/tests/phpunit/test-wc-stripe-sub-initial.php +++ b/tests/phpunit/test-wc-stripe-sub-initial.php @@ -194,7 +194,7 @@ public function test_initial_intent_parameters() { $this->assertEquals( $result['result'], 'success' ); $this->assertArrayHasKey( 'redirect', $result ); - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); $order_data = $order->get_intent_id(); $this->assertEquals( $order_data, 'pi_123abc' ); diff --git a/tests/phpunit/test-wc-stripe-sub-renewal.php b/tests/phpunit/test-wc-stripe-sub-renewal.php index 227e925aa2..0ea792964f 100644 --- a/tests/phpunit/test-wc-stripe-sub-renewal.php +++ b/tests/phpunit/test-wc-stripe-sub-renewal.php @@ -224,7 +224,7 @@ public function test_renewal_successful() { // Assert that we saved the payment intent to the order. $order_id = $renewal_order->get_id(); - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); $order_data = $order->get_intent_id(); $this->assertEquals( $order_data, 'pi_123abc' ); @@ -340,7 +340,7 @@ public function test_renewal_authorization_required() { // Assert that we saved the payment intent to the order. $order_id = $renewal_order->get_id(); - $order = WC_Stripe_Helper::get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); $order_data = $order->get_intent_id(); $order_transaction_id = $order->get_transaction_id(); From a8add020eb9484ec3dbbcd9a249363fcb0819dd7 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Fri, 27 Dec 2024 10:44:03 -0300 Subject: [PATCH 20/24] Moving meta query methods --- .../abstract-wc-stripe-payment-gateway.php | 2 +- includes/class-wc-stripe-helper.php | 143 ++------------ .../class-wc-stripe-intent-controller.php | 4 +- includes/class-wc-stripe-order.php | 179 +++++++++++++++++- includes/class-wc-stripe-webhook-handler.php | 34 ++-- .../class-wc-stripe-upe-payment-gateway.php | 2 +- tests/phpunit/test-wc-stripe-helper.php | 2 +- 7 files changed, 214 insertions(+), 152 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index 870174e3f4..b8966280dd 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -528,7 +528,7 @@ public function generate_payment_request( $order, $prepared_payment_method ) { public function process_response( $response, $order ) { WC_Stripe_Logger::log( 'Processing response: ' . print_r( $response, true ) ); - $potential_order = WC_Stripe_Helper::get_order_by_charge_id( $response->id ); + $potential_order = WC_Stripe_Order::get_by_charge_id( $response->id ); if ( $potential_order && $potential_order->get_id() !== $order->get_id() ) { WC_Stripe_Logger::log( 'Aborting, transaction already consumed by another order.' ); $localized_message = __( 'Payment processing failed. Please retry.', 'woocommerce-gateway-stripe' ); diff --git a/includes/class-wc-stripe-helper.php b/includes/class-wc-stripe-helper.php index 03aadcfe0c..9e6920502c 100644 --- a/includes/class-wc-stripe-helper.php +++ b/includes/class-wc-stripe-helper.php @@ -895,32 +895,12 @@ public static function get_webhook_url() { * @since 4.0.0 * @version 4.0.0 * @param string $source_id + * + * @deprecated 9.1.0 */ public static function get_order_by_source_id( $source_id ) { - global $wpdb; - - if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { - $orders = WC_Stripe_Order::query( - [ - 'limit' => 1, - 'meta_query' => [ - [ - 'key' => '_stripe_source_id', - 'value' => $source_id, - ], - ], - ] - ); - $order_id = current( $orders ) ? current( $orders )->get_id() : false; - } else { - $order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $source_id, '_stripe_source_id' ) ); - } - - if ( ! empty( $order_id ) ) { - return self::get_by_id( $order_id ); - } - - return false; + _deprecated_function( __METHOD__, '9.1.0', 'WC_Stripe_Order::get_by_source_id' ); + return WC_Stripe_Order::get_by_source_id( $source_id ); } /** @@ -929,31 +909,12 @@ public static function get_order_by_source_id( $source_id ) { * @since 4.0.0 * @since 4.1.16 Return false if charge_id is empty. * @param string $charge_id + * + * @deprecated 9.1.0 */ public static function get_order_by_charge_id( $charge_id ) { - global $wpdb; - - if ( empty( $charge_id ) ) { - return false; - } - - if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { - $orders = WC_Stripe_Order::query( - [ - 'transaction_id' => $charge_id, - 'limit' => 1, - ] - ); - $order_id = current( $orders ) ? current( $orders )->get_id() : false; - } else { - $order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $charge_id, '_transaction_id' ) ); - } - - if ( ! empty( $order_id ) ) { - return self::get_by_id( $order_id ); - } - - return false; + _deprecated_function( __METHOD__, '9.1.0', 'WC_Stripe_Order::get_by_charge_id' ); + return WC_Stripe_Order::get_by_charge_id( $charge_id ); } /** @@ -961,32 +922,12 @@ public static function get_order_by_charge_id( $charge_id ) { * * @since 7.5.0 * @param string $refund_id + * + * @deprecated 9.1.0 */ public static function get_order_by_refund_id( $refund_id ) { - global $wpdb; - - if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { - $orders = WC_Stripe_Order::query( - [ - 'limit' => 1, - 'meta_query' => [ - [ - 'key' => '_stripe_refund_id', - 'value' => $refund_id, - ], - ], - ] - ); - $order_id = current( $orders ) ? current( $orders )->get_id() : false; - } else { - $order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $refund_id, '_stripe_refund_id' ) ); - } - - if ( ! empty( $order_id ) ) { - return self::get_by_id( $order_id ); - } - - return false; + _deprecated_function( __METHOD__, '9.1.0', 'WC_Stripe_Order::get_by_refund_id' ); + return WC_Stripe_Order::get_by_refund_id( $refund_id ); } /** @@ -995,36 +936,12 @@ public static function get_order_by_refund_id( $refund_id ) { * @since 4.2 * @param string $intent_id The ID of the intent. * @return WC_Order|bool Either an order or false when not found. + * + * @deprecated 9.1.0 */ public static function get_order_by_intent_id( $intent_id ) { - global $wpdb; - - if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { - $orders = WC_Stripe_Order::query( - [ - 'limit' => 1, - 'meta_query' => [ - [ - 'key' => '_stripe_intent_id', - 'value' => $intent_id, - ], - ], - ] - ); - $order_id = current( $orders ) ? current( $orders )->get_id() : false; - } else { - $order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $intent_id, '_stripe_intent_id' ) ); - } - - if ( ! empty( $order_id ) ) { - $order = self::get_by_id( $order_id ); - } - - if ( ! empty( $order ) && $order->get_status() !== 'trash' ) { - return $order; - } - - return false; + _deprecated_function( __METHOD__, '9.1.0', 'WC_Stripe_Order::get_by_intent_id' ); + return WC_Stripe_Order::get_by_intent_id( $intent_id ); } /** @@ -1033,32 +950,12 @@ public static function get_order_by_intent_id( $intent_id ) { * @since 4.3 * @param string $intent_id The ID of the intent. * @return WC_Order|bool Either an order or false when not found. + * + * @deprecated 9.1.0 */ public static function get_order_by_setup_intent_id( $intent_id ) { - global $wpdb; - - if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { - $orders = WC_Stripe_Order::query( - [ - 'limit' => 1, - 'meta_query' => [ - [ - 'key' => '_stripe_setup_intent', - 'value' => $intent_id, - ], - ], - ] - ); - $order_id = current( $orders ) ? current( $orders )->get_id() : false; - } else { - $order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $intent_id, '_stripe_setup_intent' ) ); - } - - if ( ! empty( $order_id ) ) { - return self::get_by_id( $order_id ); - } - - return false; + _deprecated_function( __METHOD__, '9.1.0', 'WC_Stripe_Order::get_by_setup_intent_id' ); + return WC_Stripe_Order::get_by_setup_intent_id( $intent_id ); } /** diff --git a/includes/class-wc-stripe-intent-controller.php b/includes/class-wc-stripe-intent-controller.php index 12bbbc9dcb..fab978afe7 100644 --- a/includes/class-wc-stripe-intent-controller.php +++ b/includes/class-wc-stripe-intent-controller.php @@ -386,7 +386,7 @@ public function update_payment_intent_ajax() { $save_payment_method = isset( $_POST['save_payment_method'] ) ? 'yes' === wc_clean( wp_unslash( $_POST['save_payment_method'] ) ) : false; $selected_upe_payment_type = ! empty( $_POST['selected_upe_payment_type'] ) ? wc_clean( wp_unslash( $_POST['selected_upe_payment_type'] ) ) : ''; - $order_from_payment = WC_Stripe_Helper::get_order_by_intent_id( $payment_intent_id ); + $order_from_payment = WC_Stripe_Order::get_by_intent_id( $payment_intent_id ); if ( ! $order_from_payment || $order_from_payment->get_id() !== $order_id ) { throw new Exception( __( 'Unable to verify your request. Please reload the page and try again.', 'woocommerce-gateway-stripe' ) ); } @@ -637,7 +637,7 @@ public function update_failed_order_ajax() { $intent_id = isset( $_POST['intent_id'] ) ? wc_clean( wp_unslash( $_POST['intent_id'] ) ) : ''; $order = WC_Stripe_Order::get_by_id( $order_id ); - $order_from_payment = WC_Stripe_Helper::get_order_by_intent_id( $intent_id ); + $order_from_payment = WC_Stripe_Order::get_by_intent_id( $intent_id ); if ( ! $order_from_payment || $order_from_payment->get_id() !== $order_id ) { wp_send_json_error( __( 'Unable to verify your request. Please reload the page and try again.', 'woocommerce-gateway-stripe' ) ); } diff --git a/includes/class-wc-stripe-order.php b/includes/class-wc-stripe-order.php index 619757b0d7..1c259cefd1 100644 --- a/includes/class-wc-stripe-order.php +++ b/includes/class-wc-stripe-order.php @@ -9,6 +9,21 @@ * Wrapper for the original WC_Order class to allow custom getters and setter with the extension's specific metadata. */ class WC_Stripe_Order extends WC_Order { + /** + * Wrapper to create an order using the extension's custom WC_Stripe_Order class. + * + * @param $order_data array Order data. + * @return bool|WC_Stripe_Order + */ + public static function create( $order_data ) { + $order = wc_create_order( $order_data ); + if ( ! $order ) { + return false; + } + + return new WC_Stripe_Order( $order ); + } + /** * Wrapper to return an order using the extension's custom WC_Stripe_Order class. * @@ -45,18 +60,168 @@ function ( $order ) { } /** - * Wrapper to create an order using the extension's custom WC_Stripe_Order class. + * Gets the order by Stripe source ID. * - * @param $order_data array Order data. - * @return bool|WC_Stripe_Order + * @param string $source_id */ - public static function create( $order_data ) { - $order = wc_create_order( $order_data ); - if ( ! $order ) { + public static function get_by_source_id( $source_id ) { + global $wpdb; + + if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { + $orders = self::query( + [ + 'limit' => 1, + 'meta_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query + [ + 'key' => '_stripe_source_id', + 'value' => $source_id, + ], + ], + ] + ); + $order_id = current( $orders ) ? current( $orders )->get_id() : false; + } else { + $order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $source_id, '_stripe_source_id' ) ); + } + + if ( ! empty( $order_id ) ) { + return self::get_by_id( $order_id ); + } + + return false; + } + + /** + * Gets the order by Stripe charge ID. + * + * @param string $charge_id + */ + public static function get_by_charge_id( $charge_id ) { + global $wpdb; + + if ( empty( $charge_id ) ) { return false; } - return new WC_Stripe_Order( $order ); + if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { + $orders = self::query( + [ + 'transaction_id' => $charge_id, + 'limit' => 1, + ] + ); + $order_id = current( $orders ) ? current( $orders )->get_id() : false; + } else { + $order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $charge_id, '_transaction_id' ) ); + } + + if ( ! empty( $order_id ) ) { + return self::get_by_id( $order_id ); + } + + return false; + } + + /** + * Gets the order by Stripe refund ID. + * + * @param string $refund_id + */ + public static function get_by_refund_id( $refund_id ) { + global $wpdb; + + if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { + $orders = self::query( + [ + 'limit' => 1, + 'meta_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query + [ + 'key' => '_stripe_refund_id', + 'value' => $refund_id, + ], + ], + ] + ); + $order_id = current( $orders ) ? current( $orders )->get_id() : false; + } else { + $order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $refund_id, '_stripe_refund_id' ) ); + } + + if ( ! empty( $order_id ) ) { + return self::get_by_id( $order_id ); + } + + return false; + } + + /** + * Gets the order by Stripe PaymentIntent ID. + * + * @param string $intent_id The ID of the intent. + * @return WC_Order|bool Either an order or false when not found. + */ + public static function get_by_intent_id( $intent_id ) { + global $wpdb; + + if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { + $orders = self::query( + [ + 'limit' => 1, + 'meta_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query + [ + 'key' => '_stripe_intent_id', + 'value' => $intent_id, + ], + ], + ] + ); + $order_id = current( $orders ) ? current( $orders )->get_id() : false; + } else { + $order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $intent_id, '_stripe_intent_id' ) ); + } + + if ( ! empty( $order_id ) ) { + $order = self::get_by_id( $order_id ); + } + + if ( ! empty( $order ) && $order->get_status() !== 'trash' ) { + return $order; + } + + return false; + } + + /** + * Gets the order by Stripe SetupIntent ID. + * + * @param string $intent_id The ID of the intent. + * @return WC_Order|bool Either an order or false when not found. + */ + public static function get_by_setup_intent_id( $intent_id ) { + global $wpdb; + + if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { + $orders = self::query( + [ + 'limit' => 1, + 'meta_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query + [ + 'key' => '_stripe_setup_intent', + 'value' => $intent_id, + ], + ], + ] + ); + $order_id = current( $orders ) ? current( $orders )->get_id() : false; + } else { + $order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $intent_id, '_stripe_setup_intent' ) ); + } + + if ( ! empty( $order_id ) ) { + return self::get_by_id( $order_id ); + } + + return false; } /** diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index 0216913008..bcf9864512 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -209,7 +209,7 @@ public function process_webhook_payment( $notification, $retry = true ) { return; } - $order = WC_Stripe_Helper::get_order_by_source_id( $notification->data->object->id ); + $order = WC_Stripe_Order::get_by_id( $notification->data->object->id ); if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via source ID: ' . $notification->data->object->id ); @@ -335,7 +335,7 @@ public function process_webhook_payment( $notification, $retry = true ) { * @param object $notification */ public function process_webhook_dispute( $notification ) { - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->charge ); + $order = WC_Stripe_Order::get_by_charge_id( $notification->data->object->charge ); if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->charge ); @@ -376,7 +376,7 @@ public function process_webhook_dispute( $notification ) { * @param object $notification */ public function process_webhook_dispute_closed( $notification ) { - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->charge ); + $order = WC_Stripe_Order::get_by_charge_id( $notification->data->object->charge ); $status = $notification->data->object->status; if ( ! $order ) { @@ -428,7 +428,7 @@ public function process_webhook_dispute_closed( $notification ) { * @param object $notification */ public function process_webhook_capture( $notification ) { - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id ); + $order = WC_Stripe_Order::get_by_charge_id( $notification->data->object->id ); if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id ); @@ -485,7 +485,7 @@ public function process_webhook_charge_succeeded( $notification ) { return; } - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id ); + $order = WC_Stripe_Order::get_by_charge_id( $notification->data->object->id ); if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id ); @@ -543,7 +543,7 @@ public function process_webhook_charge_succeeded( $notification ) { * @param object $notification */ public function process_webhook_charge_failed( $notification ) { - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id ); + $order = WC_Stripe_Order::get_by_charge_id( $notification->data->object->id ); if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id ); @@ -578,11 +578,11 @@ public function process_webhook_charge_failed( $notification ) { * @param object $notification */ public function process_webhook_source_canceled( $notification ) { - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id ); + $order = WC_Stripe_Order::get_by_charge_id( $notification->data->object->id ); // If can't find order by charge ID, try source ID. if ( ! $order ) { - $order = WC_Stripe_Helper::get_order_by_source_id( $notification->data->object->id ); + $order = WC_Stripe_Order::get_by_id( $notification->data->object->id ); if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via charge/source ID: ' . $notification->data->object->id ); @@ -615,11 +615,11 @@ public function process_webhook_source_canceled( $notification ) { */ public function process_webhook_refund( $notification ) { $refund_object = $this->get_refund_object( $notification ); - $order = WC_Stripe_Helper::get_order_by_refund_id( $refund_object->id ); + $order = WC_Stripe_Order::get_by_refund_id( $refund_object->id ); if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via refund ID: ' . $refund_object->id ); - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id ); + $order = WC_Stripe_Order::get_by_charge_id( $notification->data->object->id ); } if ( ! $order ) { @@ -701,7 +701,7 @@ public function process_webhook_refund( $notification ) { */ public function process_webhook_refund_updated( $notification ) { $refund_object = $notification->data->object; - $order = WC_Stripe_Helper::get_order_by_charge_id( $refund_object->charge ); + $order = WC_Stripe_Order::get_by_charge_id( $refund_object->charge ); if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order to update refund via charge ID: ' . $refund_object->charge ); @@ -770,14 +770,14 @@ public function process_webhook_refund_updated( $notification ) { */ public function process_review_opened( $notification ) { if ( isset( $notification->data->object->payment_intent ) ) { - $order = WC_Stripe_Helper::get_order_by_intent_id( $notification->data->object->payment_intent ); + $order = WC_Stripe_Order::get_by_intent_id( $notification->data->object->payment_intent ); if ( ! $order ) { WC_Stripe_Logger::log( '[Review Opened] Could not find order via intent ID: ' . $notification->data->object->payment_intent ); return; } } else { - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->charge ); + $order = WC_Stripe_Order::get_by_charge_id( $notification->data->object->charge ); if ( ! $order ) { WC_Stripe_Logger::log( '[Review Opened] Could not find order via charge ID: ' . $notification->data->object->charge ); @@ -811,14 +811,14 @@ public function process_review_opened( $notification ) { */ public function process_review_closed( $notification ) { if ( isset( $notification->data->object->payment_intent ) ) { - $order = WC_Stripe_Helper::get_order_by_intent_id( $notification->data->object->payment_intent ); + $order = WC_Stripe_Order::get_by_intent_id( $notification->data->object->payment_intent ); if ( ! $order ) { WC_Stripe_Logger::log( '[Review Closed] Could not find order via intent ID: ' . $notification->data->object->payment_intent ); return; } } else { - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->charge ); + $order = WC_Stripe_Order::get_by_charge_id( $notification->data->object->charge ); if ( ! $order ) { WC_Stripe_Logger::log( '[Review Closed] Could not find order via charge ID: ' . $notification->data->object->charge ); @@ -1026,7 +1026,7 @@ public function process_payment_intent_success( $notification ) { public function process_setup_intent( $notification ) { $intent = $notification->data->object; - $order = WC_Stripe_Helper::get_order_by_setup_intent_id( $intent->id ); + $order = WC_Stripe_Order::get_by_setup_intent_id( $intent->id ); if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via setup intent ID: ' . $intent->id ); @@ -1270,7 +1270,7 @@ private function get_order_from_intent( $intent ) { } // Fall back to finding the order via the intent ID. - return WC_Stripe_Helper::get_order_by_intent_id( $intent->id ); + return WC_Stripe_Order::get_by_intent_id( $intent->id ); } } diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index a1e3b7767b..631fea7751 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -1207,7 +1207,7 @@ public function maybe_process_upe_redirect() { * @return bool */ private function is_order_associated_to_payment_intent( int $order_id, string $intent_id ): bool { - $order_from_payment_intent = WC_Stripe_Helper::get_order_by_intent_id( $intent_id ); + $order_from_payment_intent = WC_Stripe_Order::get_by_intent_id( $intent_id ); return $order_from_payment_intent && $order_from_payment_intent->get_id() === $order_id; } diff --git a/tests/phpunit/test-wc-stripe-helper.php b/tests/phpunit/test-wc-stripe-helper.php index 0a80142e7d..72791ada20 100644 --- a/tests/phpunit/test-wc-stripe-helper.php +++ b/tests/phpunit/test-wc-stripe-helper.php @@ -213,7 +213,7 @@ public function test_get_order_by_intent_id( $status, $success ) { $intent_id = 'pi_mock'; update_post_meta( $order_id, '_stripe_intent_id', $intent_id ); - $order = WC_Stripe_Helper::get_order_by_intent_id( $intent_id ); + $order = WC_Stripe_Order::get_by_intent_id( $intent_id ); if ( $success ) { $this->assertInstanceOf( WC_Order::class, $order ); } else { From 92e8275a88bf9f37d7fd97600706497a5a590649 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Fri, 27 Dec 2024 10:52:58 -0300 Subject: [PATCH 21/24] Abstracting meta query methods --- includes/class-wc-stripe-order.php | 166 ++++++++--------------------- 1 file changed, 43 insertions(+), 123 deletions(-) diff --git a/includes/class-wc-stripe-order.php b/includes/class-wc-stripe-order.php index 1c259cefd1..70e9ae3b0b 100644 --- a/includes/class-wc-stripe-order.php +++ b/includes/class-wc-stripe-order.php @@ -65,30 +65,7 @@ function ( $order ) { * @param string $source_id */ public static function get_by_source_id( $source_id ) { - global $wpdb; - - if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { - $orders = self::query( - [ - 'limit' => 1, - 'meta_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query - [ - 'key' => '_stripe_source_id', - 'value' => $source_id, - ], - ], - ] - ); - $order_id = current( $orders ) ? current( $orders )->get_id() : false; - } else { - $order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $source_id, '_stripe_source_id' ) ); - } - - if ( ! empty( $order_id ) ) { - return self::get_by_id( $order_id ); - } - - return false; + return self::get_by_meta( '_stripe_source_id', $source_id ); } /** @@ -97,29 +74,7 @@ public static function get_by_source_id( $source_id ) { * @param string $charge_id */ public static function get_by_charge_id( $charge_id ) { - global $wpdb; - - if ( empty( $charge_id ) ) { - return false; - } - - if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { - $orders = self::query( - [ - 'transaction_id' => $charge_id, - 'limit' => 1, - ] - ); - $order_id = current( $orders ) ? current( $orders )->get_id() : false; - } else { - $order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $charge_id, '_transaction_id' ) ); - } - - if ( ! empty( $order_id ) ) { - return self::get_by_id( $order_id ); - } - - return false; + return self::get_by_meta( '_transaction_id', $charge_id ); } /** @@ -128,30 +83,7 @@ public static function get_by_charge_id( $charge_id ) { * @param string $refund_id */ public static function get_by_refund_id( $refund_id ) { - global $wpdb; - - if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { - $orders = self::query( - [ - 'limit' => 1, - 'meta_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query - [ - 'key' => '_stripe_refund_id', - 'value' => $refund_id, - ], - ], - ] - ); - $order_id = current( $orders ) ? current( $orders )->get_id() : false; - } else { - $order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $refund_id, '_stripe_refund_id' ) ); - } - - if ( ! empty( $order_id ) ) { - return self::get_by_id( $order_id ); - } - - return false; + return self::get_by_meta( '_stripe_refund_id', $refund_id ); } /** @@ -161,34 +93,7 @@ public static function get_by_refund_id( $refund_id ) { * @return WC_Order|bool Either an order or false when not found. */ public static function get_by_intent_id( $intent_id ) { - global $wpdb; - - if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { - $orders = self::query( - [ - 'limit' => 1, - 'meta_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query - [ - 'key' => '_stripe_intent_id', - 'value' => $intent_id, - ], - ], - ] - ); - $order_id = current( $orders ) ? current( $orders )->get_id() : false; - } else { - $order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $intent_id, '_stripe_intent_id' ) ); - } - - if ( ! empty( $order_id ) ) { - $order = self::get_by_id( $order_id ); - } - - if ( ! empty( $order ) && $order->get_status() !== 'trash' ) { - return $order; - } - - return false; + return self::get_by_meta( '_stripe_intent_id', $intent_id ); } /** @@ -198,30 +103,7 @@ public static function get_by_intent_id( $intent_id ) { * @return WC_Order|bool Either an order or false when not found. */ public static function get_by_setup_intent_id( $intent_id ) { - global $wpdb; - - if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { - $orders = self::query( - [ - 'limit' => 1, - 'meta_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query - [ - 'key' => '_stripe_setup_intent', - 'value' => $intent_id, - ], - ], - ] - ); - $order_id = current( $orders ) ? current( $orders )->get_id() : false; - } else { - $order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $intent_id, '_stripe_setup_intent' ) ); - } - - if ( ! empty( $order_id ) ) { - return self::get_by_id( $order_id ); - } - - return false; + return self::get_by_meta( '_stripe_setup_intent', $intent_id ); } /** @@ -817,4 +699,42 @@ public function set_status_final( $value ) { public function status_final() { return (bool) $this->get_meta( '_stripe_status_final' ); } + + /** + * Queries for an order by a specific meta key and value. + * + * @param $meta_key string The meta key to search for. + * @param $meta_value string The meta value to search for. + * @return bool|WC_Stripe_Order + */ + private static function get_by_meta( $meta_key, $meta_value ) { + global $wpdb; + + if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { + $orders = self::query( + [ + 'limit' => 1, + 'meta_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query + [ + 'key' => $meta_key, + 'value' => $meta_value, + ], + ], + ] + ); + $order_id = current( $orders ) ? current( $orders )->get_id() : false; + } else { + $order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $meta_value, $meta_key ) ); + } + + if ( ! empty( $order_id ) ) { + $order = self::get_by_id( $order_id ); + } + + if ( ! empty( $order ) && $order->get_status() !== 'trash' ) { + return $order; + } + + return false; + } } From 1dc95df6d048882b482e40171c6af42b7ce32796 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 6 Feb 2025 17:40:43 -0300 Subject: [PATCH 22/24] Renaming Stripe Order methods for consistency --- includes/class-wc-stripe-order-handler.php | 2 +- includes/class-wc-stripe-order.php | 8 ++++---- includes/class-wc-stripe-webhook-handler.php | 16 ++++++++-------- .../class-wc-stripe-upe-payment-gateway.php | 2 +- .../test-class-wc-stripe-upe-payment-gateway.php | 8 ++++---- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/includes/class-wc-stripe-order-handler.php b/includes/class-wc-stripe-order-handler.php index 7d3d6a446b..ac0c4477c3 100644 --- a/includes/class-wc-stripe-order-handler.php +++ b/includes/class-wc-stripe-order-handler.php @@ -469,7 +469,7 @@ public function prevent_cancelling_orders_awaiting_action( $cancel_order, $order } // If the order is awaiting action and was modified within the last day, don't cancel it. - if ( $order->payment_awaiting_action() && $order->get_date_modified( 'edit' )->getTimestamp() > strtotime( '-1 day' ) ) { + if ( $order->is_payment_awaiting_action() && $order->get_date_modified( 'edit' )->getTimestamp() > strtotime( '-1 day' ) ) { $cancel_order = false; } diff --git a/includes/class-wc-stripe-order.php b/includes/class-wc-stripe-order.php index 70e9ae3b0b..5fb6b58657 100644 --- a/includes/class-wc-stripe-order.php +++ b/includes/class-wc-stripe-order.php @@ -293,7 +293,7 @@ public function set_payment_awaiting_action( $save = true ) { * * @return bool Whether the payment is awaiting action. */ - public function payment_awaiting_action() { + public function is_payment_awaiting_action() { return wc_string_to_bool( $this->get_meta( '_stripe_payment_awaiting_action' ) ); } @@ -419,7 +419,7 @@ public function set_upe_redirect_processed( $value ) { * * @return bool The value of the flag. */ - public function upe_redirect_processed() { + public function is_upe_redirect_processed() { return (bool) $this->get_meta( '_stripe_upe_redirect_processed' ); } @@ -469,7 +469,7 @@ public function set_upe_waiting_for_redirect( $value ) { * * @return bool */ - public function upe_waiting_for_redirect() { + public function is_upe_waiting_for_redirect() { return (bool) $this->get_meta( '_stripe_upe_waiting_for_redirect' ); } @@ -696,7 +696,7 @@ public function set_status_final( $value ) { * * @return bool */ - public function status_final() { + public function is_status_final() { return (bool) $this->get_meta( '_stripe_status_final' ); } diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index b9bfd92fdb..e90b8faf2c 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -393,7 +393,7 @@ public function process_webhook_dispute( $notification ) { $message = __( 'A dispute was created for this order.', 'woocommerce-gateway-stripe' ); } - if ( ! $order->has_status( 'cancelled' ) && ! $order->status_final() ) { + if ( ! $order->has_status( 'cancelled' ) && ! $order->is_status_final() ) { $order->update_status( 'on-hold', $message ); } else { $order->add_order_note( $message ); @@ -597,7 +597,7 @@ public function process_webhook_charge_failed( $notification ) { } else { $message = __( 'This payment failed to clear.', 'woocommerce-gateway-stripe' ); } - if ( ! $order->status_final() ) { + if ( ! $order->is_status_final() ) { $order->update_status( 'failed', $message ); } else { $order->add_order_note( $message ); @@ -634,7 +634,7 @@ public function process_webhook_source_canceled( $notification ) { } $message = __( 'This payment was cancelled.', 'woocommerce-gateway-stripe' ); - if ( ! $order->has_status( 'cancelled' ) && ! $order->status_final() ) { + if ( ! $order->has_status( 'cancelled' ) && ! $order->is_status_final() ) { $order->update_status( 'cancelled', $message ); } else { $order->add_order_note( $message ); @@ -832,7 +832,7 @@ public function process_review_opened( $notification ) { esc_html( $notification->data->object->reason ) ); - if ( apply_filters( 'wc_stripe_webhook_review_change_order_status', true, $order, $notification ) && ! $order->status_final() ) { + if ( apply_filters( 'wc_stripe_webhook_review_change_order_status', true, $order, $notification ) && ! $order->is_status_final() ) { $order->update_status( 'on-hold', $message ); } else { $order->add_order_note( $message ); @@ -868,7 +868,7 @@ public function process_review_closed( $notification ) { // Only change the status if the charge was captured, status is not final, the order is on-hold and the review was approved. if ( $order->charge_captured() && - ! $order->status_final() && + ! $order->is_status_final() && $order->has_status( 'on-hold' ) && ( ! empty( $notification->data->object->closed_reason ) && 'approved' === $notification->data->object->closed_reason ) && apply_filters( 'wc_stripe_webhook_review_change_order_status', true, $order, $notification ) @@ -1004,7 +1004,7 @@ public function process_payment_intent( $notification ) { WC_Stripe_Logger::log( "Stripe PaymentIntent $intent->id succeeded for order $order_id" ); $process_webhook_async = apply_filters( 'wc_stripe_process_payment_intent_webhook_async', true, $order, $intent, $notification ); - $is_awaiting_action = $order->upe_waiting_for_redirect(); + $is_awaiting_action = $order->is_upe_waiting_for_redirect(); // Process the webhook now if it's for a voucher or wallet payment , or if filtered to process immediately and order is not awaiting action. if ( $is_voucher_payment || $is_wallet_payment || ( ! $process_webhook_async && ! $is_awaiting_action ) ) { @@ -1044,7 +1044,7 @@ public function process_payment_intent( $notification ) { $message = sprintf( __( 'Stripe SCA authentication failed. Reason: %s', 'woocommerce-gateway-stripe' ), $error_message ); $status_update = []; - if ( ! $order->status_final() ) { + if ( ! $order->is_status_final() ) { $status_update['from'] = $order->get_status(); $status_update['to'] = 'failed'; $order->update_status( 'failed', $message ); @@ -1098,7 +1098,7 @@ public function process_setup_intent( $notification ) { $message = sprintf( __( 'Stripe SCA authentication failed. Reason: %s', 'woocommerce-gateway-stripe' ), $error_message ); $status_update = []; - if ( ! $order->status_final() ) { + if ( ! $order->is_status_final() ) { $status_update['from'] = $order->get_status(); $status_update['to'] = 'failed'; $order->update_status( 'failed', $message ); diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index 787dcf0e0b..1f85c41584 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -1373,7 +1373,7 @@ public function process_upe_redirect_payment( $order_id, $intent_id, $save_payme return; } - if ( $order->upe_redirect_processed() ) { + if ( $order->is_upe_redirect_processed() ) { return; } diff --git a/tests/phpunit/payment-methods/test-class-wc-stripe-upe-payment-gateway.php b/tests/phpunit/payment-methods/test-class-wc-stripe-upe-payment-gateway.php index 684a161192..0340b8a145 100644 --- a/tests/phpunit/payment-methods/test-class-wc-stripe-upe-payment-gateway.php +++ b/tests/phpunit/payment-methods/test-class-wc-stripe-upe-payment-gateway.php @@ -888,7 +888,7 @@ public function test_process_redirect_payment_returns_valid_response() { $this->assertEquals( 'processing', $final_order->get_status() ); $this->assertEquals( 'Credit / Debit Card', $final_order->get_payment_method_title() ); $this->assertEquals( $payment_intent_id, $final_order->get_intent_id() ); - $this->assertTrue( $final_order->upe_redirect_processed() ); + $this->assertTrue( $final_order->is_upe_redirect_processed() ); $this->assertMatchesRegularExpression( '/Charge ID: ch_mock/', $note->content ); } @@ -953,7 +953,7 @@ public function test_process_redirect_payment_only_runs_once() { $this->assertEquals( 'processing', $success_order->get_status() ); $this->assertEquals( 'Credit / Debit Card', $success_order->get_payment_method_title() ); $this->assertEquals( $payment_intent_id, $success_order->get_intent_id() ); - $this->assertTrue( $success_order->upe_redirect_processed() ); + $this->assertTrue( $success_order->is_upe_redirect_processed() ); $this->assertMatchesRegularExpression( '/Charge ID: ch_mock/', $note->content ); // simulate an order getting marked as failed as if from a webhook @@ -2132,7 +2132,7 @@ public function test_pre_order_payment_is_successful() { $this->assertEquals( $payment_method_id, $final_order->get_source_id() ); $this->assertEquals( $customer_id, $final_order->get_stripe_customer_id() ); $this->assertEquals( $payment_intent_id, $final_order->get_intent_id() ); - $this->assertTrue( $final_order->upe_redirect_processed() ); + $this->assertTrue( $final_order->is_upe_redirect_processed() ); } /** @@ -2197,7 +2197,7 @@ public function test_pre_order_without_payment_uses_setup_intents() { $this->assertEquals( $payment_method_id, $final_order->get_source_id() ); $this->assertEquals( $customer_id, $final_order->get_stripe_customer_id() ); - $this->assertTrue( $final_order->upe_redirect_processed() ); + $this->assertTrue( $final_order->is_upe_redirect_processed() ); } /** From c7fc075e5d5b1155eb9dfab0d5b47b119671f547 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 6 Feb 2025 18:08:33 -0300 Subject: [PATCH 23/24] Adding fallback to avoid fatal error --- .../abstracts/abstract-wc-stripe-payment-gateway.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index 46edaf0141..ac09a02294 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -1608,19 +1608,16 @@ public function save_intent_to_order( $order, $intent ) { * Retrieves the payment intent, associated with an order. * * @since 4.2 - * @param WC_Stripe_Order $order The order to retrieve an intent for. - * @return obect|bool Either the intent object or `false`. + * @param WC_Order $order The order to retrieve an intent for. + * @return object|bool Either the intent object or `false`. */ public function get_intent_from_order( $order ) { - $intent_id = $order->get_intent_id(); - + $intent_id = $order instanceof WC_Stripe_Order ? $order->get_intent_id() : $order->get_meta( '_stripe_intent_id' ); if ( $intent_id ) { return $this->get_intent( 'payment_intents', $intent_id ); } - // The order doesn't have a payment intent, but it may have a setup intent. - $intent_id = $order->get_setup_intent(); - + $intent_id = $order instanceof WC_Stripe_Order ? $order->get_setup_intent() : $order->get_meta( '_stripe_setup_intent' ); if ( $intent_id ) { return $this->get_intent( 'setup_intents', $intent_id ); } From 63fbd5e3746efdd6a33857f6d0c10e24168b667a Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 6 Feb 2025 19:09:41 -0300 Subject: [PATCH 24/24] Renaming additional methods + unit tests --- .../abstract-wc-stripe-payment-gateway.php | 2 +- includes/class-wc-stripe-order-handler.php | 4 +- includes/class-wc-stripe-order.php | 2 +- includes/class-wc-stripe-webhook-handler.php | 6 +- ...st-class-wc-stripe-upe-payment-gateway.php | 6 +- tests/phpunit/test-wc-stripe-order.php | 279 ++++++++++++++++++ 6 files changed, 289 insertions(+), 10 deletions(-) create mode 100644 tests/phpunit/test-wc-stripe-order.php diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index ac09a02294..26d54b4182 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -1067,7 +1067,7 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) { $request = []; $order_currency = $order->get_currency(); - $captured = $order->charge_captured(); + $captured = $order->is_charge_captured(); $charge_id = $order->get_transaction_id(); if ( ! $charge_id ) { diff --git a/includes/class-wc-stripe-order-handler.php b/includes/class-wc-stripe-order-handler.php index ac0c4477c3..7ed9668fbd 100644 --- a/includes/class-wc-stripe-order-handler.php +++ b/includes/class-wc-stripe-order-handler.php @@ -285,7 +285,7 @@ public function capture_payment( $order_id ) { if ( WC_Stripe_Helper::payment_method_allows_manual_capture( $order->get_payment_method() ) ) { $charge = $order->get_transaction_id(); - $captured = $order->charge_captured(); + $captured = $order->is_charge_captured(); $is_stripe_captured = false; if ( $charge && ! $captured ) { @@ -392,7 +392,7 @@ public function cancel_payment( $order_id ) { $order = WC_Stripe_Order::get_by_id( $order_id ); if ( WC_Stripe_Helper::payment_method_allows_manual_capture( $order->get_payment_method() ) ) { - if ( ! $order->charge_captured() ) { + if ( ! $order->is_charge_captured() ) { // To cancel a pre-auth, we need to refund the charge. $this->process_refund( $order_id ); } diff --git a/includes/class-wc-stripe-order.php b/includes/class-wc-stripe-order.php index 5fb6b58657..0f754f90f8 100644 --- a/includes/class-wc-stripe-order.php +++ b/includes/class-wc-stripe-order.php @@ -677,7 +677,7 @@ public function set_charge_captured( $value ) { * * @return bool */ - public function charge_captured() { + public function is_charge_captured() { return wc_string_to_bool( $this->get_meta( '_stripe_charge_captured' ) ); } diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index e90b8faf2c..21ffc6d5bf 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -474,7 +474,7 @@ public function process_webhook_capture( $notification ) { if ( WC_Stripe_Helper::payment_method_allows_manual_capture( $order->get_payment_method() ) ) { $charge = $order->get_transaction_id(); - $captured = $order->charge_captured(); + $captured = $order->is_charge_captured(); if ( $charge && ! $captured ) { $order->set_charge_captured( 'yes' ); @@ -668,7 +668,7 @@ public function process_webhook_refund( $notification ) { if ( 'stripe' === substr( (string) $order->get_payment_method(), 0, 6 ) ) { $charge = $order->get_transaction_id(); - $captured = $order->charge_captured(); + $captured = $order->is_charge_captured(); $refund_id = $order->get_refund_id(); $currency = $order->get_currency(); $raw_amount = $refund_object->amount; @@ -867,7 +867,7 @@ public function process_review_closed( $notification ) { $message = sprintf( __( 'The opened review for this order is now closed. Reason: (%s)', 'woocommerce-gateway-stripe' ), $notification->data->object->reason ); // Only change the status if the charge was captured, status is not final, the order is on-hold and the review was approved. - if ( $order->charge_captured() && + if ( $order->is_charge_captured() && ! $order->is_status_final() && $order->has_status( 'on-hold' ) && ( ! empty( $notification->data->object->closed_reason ) && 'approved' === $notification->data->object->closed_reason ) && diff --git a/tests/phpunit/payment-methods/test-class-wc-stripe-upe-payment-gateway.php b/tests/phpunit/payment-methods/test-class-wc-stripe-upe-payment-gateway.php index 0340b8a145..4e6e6e121a 100644 --- a/tests/phpunit/payment-methods/test-class-wc-stripe-upe-payment-gateway.php +++ b/tests/phpunit/payment-methods/test-class-wc-stripe-upe-payment-gateway.php @@ -1278,7 +1278,7 @@ public function test_process_response_updates_order_by_charge_status() { $this->mock_gateway->process_response( $this->array_to_object( $charge_mock ), WC_Stripe_Order::get_by_id( $order_id ) ); $test_order = WC_Stripe_Order::get_by_id( $order_id ); - $this->assertFalse( $test_order->charge_captured() ); + $this->assertFalse( $test_order->is_charge_captured() ); $this->assertEquals( $charge_mock['id'], $test_order->get_transaction_id() ); $this->assertEquals( 'on-hold', $test_order->get_status() ); @@ -1288,7 +1288,7 @@ public function test_process_response_updates_order_by_charge_status() { $this->mock_gateway->process_response( $this->array_to_object( $charge_mock ), WC_Stripe_Order::get_by_id( $order_id ) ); $test_order = WC_Stripe_Order::get_by_id( $order_id ); - $this->assertTrue( $test_order->charge_captured() ); + $this->assertTrue( $test_order->is_charge_captured() ); $this->assertEquals( 'processing', $test_order->get_status() ); // Test charge pending. @@ -1297,7 +1297,7 @@ public function test_process_response_updates_order_by_charge_status() { $this->mock_gateway->process_response( $this->array_to_object( $charge_mock ), WC_Stripe_Order::get_by_id( $order_id ) ); $test_order = WC_Stripe_Order::get_by_id( $order_id ); - $this->assertTrue( $test_order->charge_captured() ); + $this->assertTrue( $test_order->is_charge_captured() ); $this->assertEquals( $charge_mock['id'], $test_order->get_transaction_id() ); $this->assertEquals( 'on-hold', $test_order->get_status() ); diff --git a/tests/phpunit/test-wc-stripe-order.php b/tests/phpunit/test-wc-stripe-order.php new file mode 100644 index 0000000000..ecfbfb9c78 --- /dev/null +++ b/tests/phpunit/test-wc-stripe-order.php @@ -0,0 +1,279 @@ + 'pending', + 'customer_id' => 123, + 'customer_note' => '', + 'total' => '', + ] + ); + $this->assertInstanceOf( WC_Stripe_Order::class, $order ); + } + + /** + * Tests for `get_by_id`, `query`, `get_by_source_id`, `get_by_charge_id`, `get_by_refund_id`, + * `get_by_intent_id`, and `get_by_setup_intent_id`. + * + * @return void + */ + public function test_retrieve() { + // setup + $source_id = 'src_123'; + $charge_id = 'ch_123'; + $refund_id = 're_123'; + $intent_id = 'pi_123'; + $setup_intent_id = 'seti_123'; + + $order = WC_Stripe_Order::create( + [ + 'status' => 'pending', + 'customer_id' => 123, + 'customer_note' => '', + 'total' => '', + ] + ); + + $order->set_source_id( $source_id ); + $order->set_transaction_id( $charge_id ); + $order->set_refund_id( $refund_id ); + $order->set_intent_id( $intent_id ); + $order->set_setup_intent( $setup_intent_id ); + $order->save_meta_data(); + $order->save(); + + // get_by_id + $order_id = $order->get_id(); + $this->assertEquals( $order, WC_Stripe_Order::get_by_id( $order_id ) ); + + // query + $orders = WC_Stripe_Order::query( [ 'status' => 'pending' ] ); + $this->assertEquals( $order, $orders[0] ); + + // get_by_source_id + $this->assertEquals( $order, WC_Stripe_Order::get_by_source_id( $source_id ) ); + + // get_by_charge_id + $this->assertEquals( $order, WC_Stripe_Order::get_by_charge_id( $charge_id ) ); + + // get_by_refund_id + $this->assertEquals( $order, WC_Stripe_Order::get_by_refund_id( $refund_id ) ); + + // get_by_intent_id + $this->assertEquals( $order, WC_Stripe_Order::get_by_intent_id( $intent_id ) ); + + // get_by_setup_intent_id + $this->assertEquals( $order, WC_Stripe_Order::get_by_setup_intent_id( $setup_intent_id ) ); + } + + /** + * Tests for getters and setters. + * + * @return void + */ + public function test_properties() { + $order = WC_Stripe_Order::create( + [ + 'status' => 'pending', + 'customer_id' => 123, + 'customer_note' => '', + 'total' => '', + ] + ); + + $order->set_source_id( 'src_123' ); + $order->set_transaction_id( 'ch_123' ); + $order->set_refund_id( 're_123' ); + $order->set_intent_id( 'pi_123' ); + $order->set_setup_intent( 'seti_123' ); + $order->set_stripe_currency( 'usd' ); + $order->set_card_brand( 'visa' ); + $order->set_status_before_hold( 'pending' ); + $order->set_mandate_id( 'mandate_123' ); + $order->set_upe_payment_type( 'card' ); + $order->set_stripe_customer_id( 'cus_123' ); + $order->set_multibanco_data( + [ + 'entity' => '123', + 'reference' => '123', + 'amount' => 100, + ] + ); + $order->set_upe_redirect_processed( true ); + $order->set_upe_waiting_for_redirect( true ); + $order->set_charge_captured( true ); + $order->set_status_final( true ); + + $order->set_fee( 100 ); + $order->set_net( 100 ); + + $order->save_meta_data(); + $order->save(); + + $this->assertEquals( 'src_123', $order->get_source_id() ); + $this->assertEquals( 'ch_123', $order->get_transaction_id() ); + $this->assertEquals( 're_123', $order->get_refund_id() ); + $this->assertEquals( 'pi_123', $order->get_intent_id() ); + $this->assertEquals( 'seti_123', $order->get_setup_intent() ); + $this->assertEquals( 'usd', $order->get_stripe_currency() ); + $this->assertEquals( 'visa', $order->get_card_brand() ); + $this->assertEquals( 'pending', $order->get_status_before_hold() ); + $this->assertEquals( 'mandate_123', $order->get_mandate_id() ); + $this->assertEquals( 'card', $order->get_upe_payment_type() ); + $this->assertEquals( 'cus_123', $order->get_stripe_customer_id() ); + $this->assertEquals( + [ + 'entity' => '123', + 'reference' => '123', + 'amount' => 100, + ], + $order->get_multibanco_data() + ); + $this->assertTrue( $order->is_upe_redirect_processed() ); + $this->assertTrue( $order->is_upe_waiting_for_redirect() ); + $this->assertTrue( $order->is_charge_captured() ); + $this->assertTrue( $order->is_status_final() ); + + // Tests for `get_payment_awaiting_action`, `set_payment_awaiting_action`, and `remove_payment_awaiting_action`. + $order->set_payment_awaiting_action( true ); + $this->assertTrue( $order->is_payment_awaiting_action() ); + + $order->remove_payment_awaiting_action( true ); + $this->assertFalse( $order->is_payment_awaiting_action() ); + + $this->assertEquals( 100, $order->get_fee() ); + $this->assertEquals( 100, $order->get_net() ); + + $order->delete_fee(); + $order->delete_net(); + $order->save_meta_data(); + + $this->assertEmpty( $order->get_fee() ); + $this->assertEmpty( $order->get_net() ); + } + + /** + * Tests for `lock_refund`, `get_lock_refund`, `unlock_refund`, `lock_payment`, `get_lock_payment`, and `unlock_payment`. + * + * @return void + */ + public function test_lockers() { + // setup + $order = WC_Stripe_Order::create( + [ + 'status' => 'pending', + 'customer_id' => 123, + 'customer_note' => '', + 'total' => '', + ] + ); + + // refund + $order->lock_refund(); + $this->assertTrue( $order->get_lock_refund() > 0 ); + $order->unlock_refund(); + $this->assertEmpty( $order->get_lock_refund() ); + + // payment + $order->lock_payment(); + $this->assertTrue( $order->get_lock_payment() > 0 ); + $order->unlock_payment(); + $this->assertEmpty( $order->get_lock_payment() ); + } + + /** + * Tests for `add_payment_intent_to_order`. + * + * @return void + */ + public function test_add_payment_intent_to_order() { + // setup + $order = WC_Stripe_Order::create( + [ + 'status' => 'pending', + 'customer_id' => 123, + 'customer_note' => '', + 'total' => '', + ] + ); + $order_id = $order->get_id(); + + // add_payment_intent_to_order + $intent_id = 'pi_123'; + $order->add_payment_intent_to_order( $intent_id ); + $this->assertEquals( $intent_id, $order->get_intent_id() ); + + $note = wc_get_order_notes( + [ + 'order_id' => $order_id, + 'limit' => 1, + ] + )[0]; + $this->assertStringContainsString( 'Stripe payment intent created (Payment Intent ID: pi_123)', $note->content ); + } + + /** + * Test for `validate_minimum_amount`. + * + * @return void + */ + public function test_validate_minimum_amount() { + $order = WC_Stripe_Order::create( + [ + 'status' => 'pending', + 'customer_id' => 123, + 'customer_note' => '', + 'total' => '', + ] + ); + $order->set_total( 0.01 ); + $order->save(); + + $this->expectException( WC_Stripe_Exception::class ); + $this->expectExceptionMessage( 'Did not meet minimum amount' ); + + $order->validate_minimum_amount(); + } + + /** + * Tests for `get_owner_details`. + * + * @return void + */ + public function test_get_owner_details() { + $order = WC_Stripe_Order::create( + [ + 'status' => 'pending', + 'customer_id' => 123, + 'customer_note' => '', + 'total' => '', + ] + ); + $order->set_billing_phone( '+1 123 1234' ); + $order->set_billing_first_name( 'John' ); + $order->set_billing_last_name( 'Doe' ); + $order->set_billing_email( 'test@example.com' ); + $order->save_meta_data(); + + $owner_details = $order->get_owner_details(); + + $this->assertEquals( '+1 123 1234', $owner_details->phone ); + $this->assertEquals( 'John Doe', $owner_details->name ); + $this->assertEquals( 'test@example.com', $owner_details->email ); + } +}