diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php b/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php index 8cf964bfa..750913b6e 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_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_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' ) ); @@ -261,11 +261,11 @@ 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(); - WC_Stripe_Helper::add_payment_intent_to_order( $intent->id, $order ); + $order->add_payment_intent_to_order( $intent->id ); return [ 'result' => 'success', @@ -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_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 ) { @@ -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 d018b98cf..b8966280d 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(); } /** @@ -392,8 +394,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() ); } @@ -520,12 +521,14 @@ 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 ) { 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' ); @@ -536,14 +539,14 @@ 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 ); } 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 ) ) { @@ -581,7 +584,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 ); @@ -656,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(); } /** @@ -951,14 +929,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 +983,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' ] ) ) { @@ -1031,7 +1009,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 ) { @@ -1045,18 +1023,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(); @@ -1079,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_get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); if ( ! $order ) { return false; @@ -1088,7 +1066,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 +1078,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,13 +1127,13 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) { } } - if ( ! $intent_cancelled && 'yes' === $captured ) { - $this->lock_order_refund( $order ); + if ( ! $intent_cancelled && $captured ) { + $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', @@ -1169,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', @@ -1187,7 +1165,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' ); @@ -1201,7 +1179,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 ); @@ -1211,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 ) ) ); @@ -1593,7 +1571,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 ) { @@ -1603,16 +1581,16 @@ 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 ); 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 ); + $order->set_setup_intent( $intent->id ); } if ( is_callable( [ $order, 'save' ] ) ) { @@ -1624,18 +1602,18 @@ 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 ); } // 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 ); @@ -1673,83 +1651,53 @@ 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. + * @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->update_meta_data( '_stripe_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(); } /** * 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. + * + * @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_meta( '_stripe_lock_refund', true ); - - 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->update_meta_data( '_stripe_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(); } /** @@ -1767,7 +1715,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. */ @@ -1792,7 +1740,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; @@ -1907,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_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() ) { @@ -1946,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_Order::get_by_id( $order_id_from_order_key ); // If the order doesn't need payment, return false. if ( ! $order->needs_payment() ) { @@ -2105,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_Order::get_by_id( $order_id ); if ( is_a( $order, 'WC_Order' ) ) { $stripe_params['billing_first_name'] = $order->get_billing_first_name(); @@ -2211,45 +2159,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. * @@ -2276,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_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 8e16a7b5a..7426da5a3 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_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 ] ); } @@ -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 ] ); @@ -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_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 de01186b9..ff909f111 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(), ], ], ]; @@ -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_Order::get_by_id( $order->get_id() ); list( $removed, $retained, $msgs ) = $this->maybe_handle_order( $order ); $items_removed |= $removed; @@ -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_refund_id = $order->get_meta( '_stripe_refund_id', true ); - $stripe_customer_id = $order->get_meta( '_stripe_customer_id', true ); + $stripe_source_id = $order->get_source_id(); + $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() ) ) { /* translators: %d Order ID */ diff --git a/includes/class-wc-gateway-stripe.php b/includes/class-wc-gateway-stripe.php index 4b856783d..b6cf3a2de 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_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_get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); if ( $this->has_subscription( $order_id ) ) { $force_save_source = true; @@ -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()}" ); @@ -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,10 +473,10 @@ 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 ); + $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 ) ); @@ -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 [ @@ -547,10 +547,10 @@ public function display_order_fee( $order_id ) { return; } - $order = wc_get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $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_Order::get_by_id( $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; @@ -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_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_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_get_order( absint( $order_id ) ); + $order = WC_Stripe_Order::get_by_id( absint( $order_id ) ); if ( ! $order ) { 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(); @@ -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_Order::get_by_id( $order->get_id() ); if ( ! $order->has_status( apply_filters( @@ -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,13 +903,13 @@ 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). * 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-helper.php b/includes/class-wc-stripe-helper.php index 6c6c72cdb..9e6920502 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(); } /** @@ -617,7 +649,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', [] ); @@ -736,7 +768,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'. @@ -863,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_get_orders( - [ - '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 wc_get_order( $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 ); } /** @@ -897,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_get_orders( - [ - '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 wc_get_order( $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 ); } /** @@ -929,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_get_orders( - [ - '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 wc_get_order( $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 ); } /** @@ -963,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_get_orders( - [ - '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 = wc_get_order( $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 ); } /** @@ -1001,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_get_orders( - [ - '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 wc_get_order( $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 ); } /** @@ -1261,26 +1190,14 @@ 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 + * + * @deprecated 9.1.0 */ public static function add_payment_intent_to_order( $payment_intent_id, $order ) { + wc_deprecated_function( __METHOD__, '9.1.0', 'WC_Stripe_Order::add_payment_intent' ); - $old_intent_id = $order->get_meta( '_stripe_intent_id' ); - - if ( $old_intent_id === $payment_intent_id ) { - return; - } - - $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->update_meta_data( '_stripe_intent_id', $payment_intent_id ); - $order->save(); + $order->add_payment_intent_to_order( $payment_intent_id ); } /** @@ -1374,15 +1291,14 @@ 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' ); + $intent_id = $order->get_setup_intent(); } return $intent_id ?? false; @@ -1411,33 +1327,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( $save ); } /** * 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(); } /** @@ -1495,14 +1409,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 ); } /** diff --git a/includes/class-wc-stripe-intent-controller.php b/includes/class-wc-stripe-intent-controller.php index c6a976879..fab978afe 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_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_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_get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $order_id ); if ( is_a( $order, 'WC_Order' ) ) { $amount = $order->get_total(); } @@ -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' ) ); } @@ -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_Order::get_by_id( $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(); @@ -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 [ @@ -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_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' ) ); } @@ -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' ); } @@ -635,9 +635,9 @@ 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_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' ) ); } @@ -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-handler.php b/includes/class-wc-stripe-order-handler.php index 5f2cc6bbd..af79fb4ca 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_Order::get_by_id( $order_id ); if ( ! is_object( $order ) ) { return; @@ -154,12 +154,12 @@ 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()}" ); // 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(); } /** @@ -324,14 +324,14 @@ private function maybe_process_legacy_redirect() { */ public function capture_payment( $order_id ) { $result = new stdClass(); - $order = wc_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(); - $captured = $order->get_meta( '_stripe_charge_captured', true ); + $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() ) { @@ -401,7 +401,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 ); @@ -432,12 +432,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_Order::get_by_id( $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 ); } @@ -495,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 */ @@ -504,13 +502,17 @@ 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; } // 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 new file mode 100644 index 000000000..70e9ae3b0 --- /dev/null +++ b/includes/class-wc-stripe-order.php @@ -0,0 +1,740 @@ +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. + * + * @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. + * + * @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. + * + * @return void + */ + 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(); + } + } + + /** + * 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. + * + * @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' ); + } + + /** + * 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. + * + * @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. + * + * @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' ); + } + + /** + * 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. + * + * @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. + * + * @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. + * + * @param $value string The value to set. + * @return void + */ + public function set_charge_captured( $value ) { + $this->update_meta_data( '_stripe_charge_captured', $value ); + } + /** + * Whether the charge has been captured. + * + * @return bool + */ + public function charge_captured() { + return wc_string_to_bool( $this->get_meta( '_stripe_charge_captured' ) ); + } + + /** + * 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' ); + } + + /** + * 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; + } +} diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index 572903a36..bcf986451 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 ); @@ -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; } @@ -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()}" ); @@ -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(); } /** @@ -335,14 +335,14 @@ 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 ); 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 ) { @@ -356,7 +356,7 @@ public function process_webhook_dispute( $notification ) { $message = __( 'A dispute was created for this order.', '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( 'on-hold', $message ); } else { $order->add_order_note( $message ); @@ -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 ) { @@ -396,10 +396,10 @@ 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 ); + $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 ) { @@ -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 ); @@ -437,10 +437,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 ); @@ -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 ); @@ -560,7 +560,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 ); @@ -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 ); @@ -597,7 +597,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 ); @@ -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 ) { @@ -631,8 +631,8 @@ 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' ); - $refund_id = $order->get_meta( '_stripe_refund_id' ); + $captured = $order->charge_captured(); + $refund_id = $order->get_refund_id(); $currency = $order->get_currency(); $raw_amount = $refund_object->amount; @@ -643,7 +643,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() ) { @@ -655,7 +655,7 @@ public function process_webhook_refund( $notification ) { return; } - if ( $this->lock_order_refund( $order ) ) { + if ( $order->lock_refund() ) { return; } @@ -680,13 +680,13 @@ 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 ); } - $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 ) ); @@ -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 ); @@ -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; @@ -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 ); @@ -795,7 +795,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 ); @@ -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 ); @@ -830,14 +830,14 @@ 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 ( 'yes' === $order->get_meta( '_stripe_charge_captured' ) && - ! $order->get_meta( '_stripe_status_final', false ) && + if ( $order->charge_captured() && + ! $order->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 ) ) { // 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 ); } @@ -944,12 +944,12 @@ public function process_payment_intent_success( $notification ) { return; } - if ( $this->lock_order_payment( $order, $intent ) ) { + if ( $order->lock_payment( $intent ) ) { return; } $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 ); @@ -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 ) ) { @@ -1007,7 +1007,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 ); @@ -1021,12 +1021,12 @@ public function process_payment_intent_success( $notification ) { break; } - $this->unlock_order_payment( $order ); + $order->unlock_payment(); } 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 ); @@ -1042,7 +1042,7 @@ public function process_setup_intent( $notification ) { return; } - if ( $this->lock_order_payment( $order, $intent ) ) { + if ( $order->lock_payment( $intent ) ) { return; } @@ -1061,7 +1061,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 ); @@ -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(); } /** @@ -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_Order::get_by_id( $additional_data['order_id'] ) : null; $intent_id = $additional_data['intent_id'] ?? ''; if ( empty( $order ) ) { @@ -1240,7 +1240,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. @@ -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_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 ); @@ -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/compat/trait-wc-stripe-pre-orders.php b/includes/compat/trait-wc-stripe-pre-orders.php index aaad72739..1f9e00276 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_Order::get_by_id( $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/compat/trait-wc-stripe-subscriptions.php b/includes/compat/trait-wc-stripe-subscriptions.php index 69e0f89f1..1949f83cb 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_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 ); @@ -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(); } /** @@ -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' ); @@ -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(); } @@ -792,13 +792,13 @@ 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_Order::get_by_id( $renewal_order_id ); if ( ! $renewal_order instanceof WC_Order ) { continue; } $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. @@ -936,16 +936,16 @@ 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_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_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(); } } @@ -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_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 f7a9bce21..98049327a 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_Order::get_by_id( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } @@ -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 ) ) { @@ -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_Order::get_by_id( $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; @@ -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 ea35f7527..68fb4fdb6 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_Order::get_by_id( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } @@ -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() ]; @@ -240,10 +240,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_Order::get_by_id( $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; @@ -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 b14b9c378..b270f7d31 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 b11a5cded..b1d03c419 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_Order::get_by_id( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } @@ -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 ) ) { @@ -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_Order::get_by_id( $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; @@ -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 2e0dd24c8..551f1a12b 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_Order::get_by_id( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } @@ -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 ) ) { @@ -235,10 +235,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_Order::get_by_id( $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; @@ -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 791e9527b..bfd28a9ff 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_Order::get_by_id( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } @@ -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 ) ) { @@ -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_Order::get_by_id( $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; @@ -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 14a499167..fd68b923c 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_Order::get_by_id( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } @@ -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 ); + $order = WC_Stripe_Order::get_by_id( $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 ); } /** @@ -304,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 ) { @@ -314,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 ) ) { @@ -339,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_Order::get_by_id( $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; @@ -361,7 +359,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 74f4b2edb..2a2ed792c 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 c63736147..1524177b8 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_Order::get_by_id( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } @@ -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' ); @@ -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_Order::get_by_id( $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; @@ -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-sepa.php b/includes/payment-methods/class-wc-gateway-stripe-sepa.php index 6f804a4e9..4581ecf58 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_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_get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $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 ee721cc7b..df61c13d2 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_Order::get_by_id( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } @@ -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, @@ -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_Order::get_by_id( $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; @@ -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-express-checkout-ajax-handler.php b/includes/payment-methods/class-wc-stripe-express-checkout-ajax-handler.php index 1a6c4e0f1..31336accc 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_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 35ef04166..23072d90a 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_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_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 ca1372d62..9dc6c776c 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_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_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 6b7f19f8e..631fea775 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -250,12 +250,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; @@ -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_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_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 @@ -725,15 +725,15 @@ 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->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 // 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_Order::get_by_id( $order_id ); try { $payment_information = $this->prepare_payment_information_from_request( $order ); @@ -815,11 +815,11 @@ 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. - $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 ); @@ -884,10 +884,10 @@ 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->update_meta_data( '_stripe_upe_waiting_for_redirect', true ); + $order->set_upe_waiting_for_redirect( true ); $order->save(); } else { $redirect = $return_url; @@ -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( [ @@ -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_Order::get_by_id( $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'; @@ -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; } @@ -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_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_get_order( $order_id ); + $order = WC_Stripe_Order::get_by_id( $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 @@ -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(); } @@ -1392,15 +1392,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(); @@ -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_Order::get_by_id( $order_id ) : null; if ( is_a( $order, 'WC_Order' ) ) { $amount = $order->get_total(); } @@ -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' ) ) { @@ -2222,12 +2222,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(); } @@ -2236,22 +2236,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(); } /** @@ -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_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-boleto.php b/includes/payment-methods/class-wc-stripe-upe-payment-method-boleto.php index 60bcf5d4a..d3ec64816 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-link.php b/includes/payment-methods/class-wc-stripe-upe-payment-method-link.php index 3ee1093cb..efcbb20b2 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_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 1aade178b..804c6392a 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_Order::get_by_id( $order_id ); if ( ! $order ) { return; } @@ -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,19 +125,19 @@ 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 ); } /** * 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 48681bdd2..71cff0a72 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/includes/payment-methods/class-wc-stripe-upe-payment-method.php b/includes/payment-methods/class-wc-stripe-upe-payment-method.php index d10a795cc..2fe61d8c2 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-method.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-method.php @@ -608,7 +608,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_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 96226a0c8..562b1e80f 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,9 +73,9 @@ 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_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 ) { @@ -140,10 +140,12 @@ public function test_capture_payment_success() { $request->set_param( 'payment_intent_id', 'pi_12345' ); $response = rest_do_request( $request ); + $order = WC_Stripe_Order::get_by_id( $order->get_id() ); + $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 ffd967439..627181920 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 = [] ) { @@ -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_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 a156251af..6981dd5e1 100644 --- a/tests/phpunit/test-class-wc-stripe-order-handler.php +++ b/tests/phpunit/test-class-wc-stripe-order-handler.php @@ -19,10 +19,10 @@ 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() ); + $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 679af7137..34d787be7 100644 --- a/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php +++ b/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php @@ -311,9 +311,9 @@ 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->update_meta_data( '_stripe_upe_payment_type', '' ); - $order->update_meta_data( '_stripe_upe_waiting_for_redirect', true ); + $order->set_intent_id( $payment_intent_id ); + $order->set_upe_payment_type( '' ); + $order->set_upe_waiting_for_redirect( true ); $order->save(); list( $amount, $description, $metadata ) = $this->get_order_details( $order ); @@ -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_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_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_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_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_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_get_order( $order_id ); + $final_order = WC_Stripe_Order::get_by_id( $order_id ); $note = wc_get_order_notes( [ 'order_id' => $order_id, @@ -786,8 +786,8 @@ 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->assertTrue( (bool) $final_order->get_meta( '_stripe_upe_redirect_processed', true ) ); + $this->assertEquals( $payment_intent_id, $final_order->get_intent_id() ); + $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_Order::get_by_id( $order_id ); $note = wc_get_order_notes( [ @@ -851,8 +851,8 @@ 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->assertTrue( (bool) $success_order->get_meta( '_stripe_upe_redirect_processed', true ) ); + $this->assertEquals( $payment_intent_id, $success_order->get_intent_id() ); + $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 @@ -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_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_get_order( $order_id ) ) + ->with( WC_Stripe_Order::get_by_id( $order_id ) ) ->will( $this->returnValue( $this->mock_stripe_customer ) ); @@ -908,11 +908,11 @@ 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_Order::get_by_id( $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_stripe_customer_id() ); + $this->assertEquals( $payment_method_id, $final_order->get_source_id() ); $this->assertEquals( 'Credit / Debit Card', $final_order->get_payment_method_title() ); } @@ -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_Order::get_by_id( $order_id ) ) ->will( $this->returnValue( $this->mock_stripe_customer ) ); @@ -970,12 +970,12 @@ 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_Order::get_by_id( $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() ); } /** @@ -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_Order::get_by_id( $order_id ) ) ->will( $this->returnValue( $this->mock_stripe_customer ) ); @@ -1038,12 +1038,12 @@ 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_Order::get_by_id( $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() ); } /** @@ -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_Order::get_by_id( $order_id ) ) ->will( $this->returnValue( $this->mock_stripe_customer ) ); @@ -1097,11 +1097,11 @@ 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_Order::get_by_id( $order_id ); $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( $customer_id, $final_order->get_stripe_customer_id() ); + $this->assertEquals( $generated_payment_method_id, $final_order->get_source_id() ); } /** @@ -1174,29 +1174,29 @@ 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 ) ); - $test_order = wc_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->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() ); // 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 ) ); - $test_order = wc_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->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. $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 ); + $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->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() ); @@ -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_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_get_order( $order_id ); + $final_order = WC_Stripe_Order::get_by_id( $order_id ); $note = wc_get_order_notes( [ 'order_id' => $order_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 ); } @@ -1384,14 +1384,14 @@ 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_Order::get_by_id( $order_id ); $client_secret = $payment_intent_mock->client_secret; $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'] ); } @@ -1447,12 +1447,12 @@ function( $passed_order ) use ( $order ) { ); $response = $this->mock_gateway->process_payment( $order_id ); - $final_order = wc_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() ); - $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() ); } /** @@ -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_Order::get_by_id( $order_id ); $note = wc_get_order_notes( [ 'order_id' => $order_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 ); } @@ -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_Order::get_by_id( $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() ); } /** @@ -1658,9 +1658,9 @@ 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->update_meta_data( '_stripe_upe_waiting_for_redirect', true ); + $order->set_intent_id( $payment_intent_id ); + $order->set_upe_payment_type( '' ); + $order->set_upe_waiting_for_redirect( true ); $order->save(); list( $amount, $description, $metadata ) = $this->get_order_details( $order ); @@ -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_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_get_order( $order_id ) + WC_Stripe_Order::get_by_id( $order_id ) ) ->will( $this->returnValue( [] ) @@ -1769,10 +1769,10 @@ 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 ); + $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_get_order( $order_id ); + $final_order = WC_Stripe_Order::get_by_id( $order_id ); $note = wc_get_order_notes( [ 'order_id' => $order_id, @@ -1863,10 +1863,10 @@ 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 ); + $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_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_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_get_order( $order_id ) ) + ->with( WC_Stripe_Order::get_by_id( $order_id ) ) ->will( $this->returnValue( true ) ); $this->mock_gateway->expects( $this->once() ) @@ -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_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_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->assertTrue( (bool) $final_order->get_meta( '_stripe_upe_redirect_processed', true ) ); + $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() ); } /** @@ -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_Order::get_by_id( $order_id ) ) ->will( $this->returnValue( $this->mock_stripe_customer ) ); @@ -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_Order::get_by_id( $order_id ); - $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->assertTrue( (bool) $final_order->get_meta( '_stripe_upe_redirect_processed', true ) ); + $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() ); } /** @@ -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_Order::get_by_id( $order_id ); $note = wc_get_order_notes( [ 'order_id' => $order_id, @@ -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-helper.php b/tests/phpunit/test-wc-stripe-helper.php index 0a80142e7..72791ada2 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 { diff --git a/tests/phpunit/test-wc-stripe-payment-gateway.php b/tests/phpunit/test-wc-stripe-payment-gateway.php index 86f4be565..b878c0823 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. @@ -631,45 +631,45 @@ 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. $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' ); + $locked = $order_3->lock_payment( $intent_id ); + $current_lock = $order_3->get_lock_payment(); $this->assertFalse( $locked ); $this->assertEqualsWithDelta( (int) $current_lock, ( time() + 5 * MINUTE_IN_SECONDS ), 3 ); // 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_Order::get_by_id( $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. } } diff --git a/tests/phpunit/test-wc-stripe-sub-initial.php b/tests/phpunit/test-wc-stripe-sub-initial.php index 13fc395d3..0f6497484 100644 --- a/tests/phpunit/test-wc-stripe-sub-initial.php +++ b/tests/phpunit/test-wc-stripe-sub-initial.php @@ -194,8 +194,8 @@ public function test_initial_intent_parameters() { $this->assertEquals( $result['result'], 'success' ); $this->assertArrayHasKey( 'redirect', $result ); - $order = wc_get_order( $order_id ); - $order_data = $order->get_meta( '_stripe_intent_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 7e408868d..0ea792964 100644 --- a/tests/phpunit/test-wc-stripe-sub-renewal.php +++ b/tests/phpunit/test-wc-stripe-sub-renewal.php @@ -224,8 +224,8 @@ 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 = WC_Stripe_Order::get_by_id( $order_id ); + $order_data = $order->get_intent_id(); $this->assertEquals( $order_data, 'pi_123abc' ); @@ -340,8 +340,8 @@ 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 = WC_Stripe_Order::get_by_id( $order_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. diff --git a/tests/phpunit/test-wc-stripe-webhook-handler.php b/tests/phpunit/test-wc-stripe-webhook-handler.php index f83de370b..6b8c26942 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(); @@ -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(); diff --git a/woocommerce-gateway-stripe.php b/woocommerce-gateway-stripe.php index 4707c4782..1c7e9b978 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';