Skip to content
New issue

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

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

Already on GitHub? # to your account

Implementing the order wrapper class in compatibility classes #4109

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions includes/compat/trait-wc-stripe-pre-orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
50 changes: 29 additions & 21 deletions includes/compat/trait-wc-stripe-subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,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 );
Expand Down Expand Up @@ -358,6 +358,7 @@ public function process_change_subscription_payment_with_deferred_intent( $subsc
* @param $renewal_order WC_Order A WC_Order object created to record the renewal payment.
*/
public function scheduled_subscription_payment( $amount_to_charge, $renewal_order ) {
$renewal_order = WC_Stripe_Order::to_instance( $renewal_order );
$this->process_subscription_payment( $amount_to_charge, $renewal_order, true, false );
}

Expand All @@ -369,10 +370,10 @@ public function scheduled_subscription_payment( $amount_to_charge, $renewal_orde
* @since 4.1.0 Add fourth parameter to log previous errors.
* @since 5.6.0 Process renewal payments for SEPA and UPE.
*
* @param float $amount
* @param mixed $renewal_order
* @param bool $retry Should we retry the process?
* @param object $previous_error
* @param float $amount
* @param WC_Stripe_Order|WC_Order $renewal_order
* @param bool $retry Should we retry the process?
* @param object $previous_error
*/
public function process_subscription_payment( $amount, $renewal_order, $retry = true, $previous_error = false ) {
try {
Expand Down Expand Up @@ -453,7 +454,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 );
}
Expand Down Expand Up @@ -514,7 +515,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;
}
Expand Down Expand Up @@ -567,7 +568,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();
}

/**
Expand Down Expand Up @@ -632,11 +633,13 @@ 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|WC_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 = WC_Stripe_Order::get_by_id( $renewal_order );

$renewal_order->delete_fee();
$renewal_order->delete_net();

// Delete payment intent ID.
$renewal_order->delete_meta_data( '_stripe_intent_id' );
Expand All @@ -648,13 +651,18 @@ public function delete_renewal_meta( $renewal_order ) {
* Update the customer_id for a subscription after using Stripe to complete a payment to make up for
* an automatic renewal payment which previously failed.
*
* @param WC_Subscription $subscription The subscription for which the failing payment method relates.
* @param WC_Order $renewal_order The order which recorded the successful payment (to make up for the failed automatic payment).
* @param WC_Subscription $subscription The subscription for which the failing payment method relates.
* @param WC_Order|WC_Stripe_Order $renewal_order The order which recorded the successful payment (to make up for the failed automatic payment).
* @return void
*/
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 ) );
$renewal_order = WC_Stripe_Order::to_instance( $renewal_order );

$customer_id = $renewal_order->get_customer_id();
$source_id = $renewal_order->get_source_id();

$subscription->update_meta_data( '_stripe_customer_id', $customer_id );
$subscription->update_meta_data( '_stripe_source_id', $source_id );
$subscription->save();
}

Expand Down Expand Up @@ -800,13 +808,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.
Expand Down Expand Up @@ -945,16 +953,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();
}
}
Expand Down Expand Up @@ -1168,7 +1176,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 ( WC_Stripe_Subscriptions_Helper::is_subscriptions_enabled()
&& $this->is_subscription( $order )
&& $parent_order
Expand Down
Loading