Skip to content

Implementing the order wrapper class in the new checkout #4111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*** Changelog ***

= 9.5.0 - xxxx-xx-xx =
* Dev - Implements the new Stripe order class into the new checkout experience files.
* Dev - Splits the code coverage GitHub Actions Workflow into two separate actions.
* Dev - Implements the new Stripe order class into the root extension files.
* Fix - Fixes the listing of payment methods in the Stripe settings page when the Smart Checkout is enabled.
Expand Down
68 changes: 35 additions & 33 deletions includes/payment-methods/class-wc-stripe-upe-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,9 @@ public function set_cookie_on_current_request( $cookie ) {
* @return array|bool
*/
public function can_refund_order( $order ) {
$upe_payment_type = $order->get_meta( '_stripe_upe_payment_type' );
$order = WC_Stripe_Order::to_instance( $order );

$upe_payment_type = $order->get_upe_payment_type();

if ( ! $upe_payment_type ) {
return true;
Expand Down Expand Up @@ -543,7 +545,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 );

$stripe_params['orderId'] = $order_id;

Expand Down Expand Up @@ -924,15 +926,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( OrderStatus::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();

Expand Down Expand Up @@ -993,7 +995,7 @@ private function process_payment_with_payment_method( 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 );
Expand Down Expand Up @@ -1047,11 +1049,11 @@ private function process_payment_with_payment_method( 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 );
Expand Down Expand Up @@ -1116,10 +1118,10 @@ private function process_payment_with_payment_method( 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();

$redirect = $this->get_redirect_url( $this->get_return_url( $order ), $payment_intent, $payment_information, $order, $payment_needed );
Expand All @@ -1142,7 +1144,7 @@ private function process_payment_with_payment_method( int $order_id ) {
$redirect = $this->get_return_url( $order );
}

$this->unlock_order_payment( $order );
$order->unlock_payment();

return array_merge(
[
Expand All @@ -1164,7 +1166,7 @@ private function process_payment_with_payment_method( int $order_id ) {
* @return array An array with the result of the payment processing, and a redirect URL on success.
*/
private function process_payment_with_confirmation_token( int $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 );
Expand Down Expand Up @@ -1263,7 +1265,7 @@ private function handle_process_payment_error( WC_Stripe_Exception $e, $order )
*/
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 );
Expand All @@ -1286,7 +1288,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';
Expand Down Expand Up @@ -1526,7 +1528,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;
}
Expand Down Expand Up @@ -1569,7 +1571,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->is_upe_redirect_processed() ) {
return;
}

Expand Down Expand Up @@ -1600,7 +1602,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.
*/
Expand Down Expand Up @@ -1670,7 +1672,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
Expand All @@ -1682,7 +1684,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();
}
Expand All @@ -1708,15 +1710,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();
Expand Down Expand Up @@ -1798,7 +1800,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();
}
Expand Down Expand Up @@ -2540,15 +2542,15 @@ private function get_payment_method_options( $selected_payment_type, $order, $pa
/**
* 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' ) ) {
Expand Down Expand Up @@ -2704,12 +2706,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();
}

Expand All @@ -2718,22 +2720,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();
}
/**
Expand Down Expand Up @@ -2898,7 +2900,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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,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( OrderStatus::ON_HOLD, $allowed_statuses, true ) ) {
if ( WC_Stripe_Payment_Methods::BOLETO === $order->get_upe_payment_type() && ! in_array( OrderStatus::ON_HOLD, $allowed_statuses, true ) ) {
$allowed_statuses[] = OrderStatus::ON_HOLD;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,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 ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,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;
}
Expand All @@ -69,11 +69,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;
}
Expand Down Expand Up @@ -114,7 +114,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 ) {
Expand All @@ -128,19 +128,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( OrderStatus::ON_HOLD, $allowed_statuses, true ) ) {
if ( WC_Stripe_Payment_Methods::MULTIBANCO === $order->get_upe_payment_type() && ! in_array( OrderStatus::ON_HOLD, $allowed_statuses, true ) ) {
$allowed_statuses[] = OrderStatus::ON_HOLD;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,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( OrderStatus::ON_HOLD, $allowed_statuses, true ) ) {
if ( WC_Stripe_Payment_Methods::OXXO === $order->get_upe_payment_type() && ! in_array( OrderStatus::ON_HOLD, $allowed_statuses, true ) ) {
$allowed_statuses[] = OrderStatus::ON_HOLD;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public function is_enabled_at_checkout( $order_id = null, $account_domestic_curr
$currencies = $this->get_supported_currencies();
if ( ! empty( $currencies ) ) {
if ( is_wc_endpoint_url( 'order-pay' ) && isset( $_GET['key'] ) ) {
$order = wc_get_order( $order_id ? $order_id : absint( get_query_var( 'order-pay' ) ) );
$order = WC_Stripe_Order::get_by_id( $order_id ? $order_id : absint( get_query_var( 'order-pay' ) ) );
$order_currency = $order->get_currency();
if ( ! in_array( $order_currency, $currencies, true ) ) {
return false;
Expand Down Expand Up @@ -681,7 +681,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( '' );
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
== Changelog ==

= 9.5.0 - xxxx-xx-xx =
* Dev - Implements the new Stripe order class into the new checkout experience files.
* Dev - Splits the code coverage GitHub Actions Workflow into two separate actions.
* Dev - Implements the new Stripe order class into the root extension files.
* Fix - Fixes the listing of payment methods in the Stripe settings page when the Smart Checkout is enabled.
Expand Down
Loading