Merge pull request #3549 from woocommerce/PCP-5026-subscriptions-migrated-from-ppec-to-ppc-lose-gateway-id-during-renewal

Subscriptions migrated from PPEC to PPC fails on renewal
This commit is contained in:
Niklas Gutberlet 2025-08-04 19:37:07 +02:00 committed by GitHub
commit e52866bae0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -76,7 +76,7 @@ class SubscriptionsHandler {
* @return array
*/
public function add_mock_ppec_gateway( $gateways ) {
if ( ! isset( $gateways[ PPECHelper::PPEC_GATEWAY_ID ] ) && $this->should_mock_ppec_gateway() ) {
if ( ! isset( $gateways[ PPECHelper::PPEC_GATEWAY_ID ] ) ) {
$gateways[ PPECHelper::PPEC_GATEWAY_ID ] = $this->mock_gateway;
}
@ -125,87 +125,25 @@ class SubscriptionsHandler {
public function use_billing_agreement_as_token( $token, $customer, $order ) {
if ( PPECHelper::PPEC_GATEWAY_ID === $order->get_payment_method() && wcs_order_contains_renewal( $order ) ) {
$billing_agreement_id = $order->get_meta( '_ppec_billing_agreement_id', true );
if ( $billing_agreement_id ) {
$token = new PaymentToken( $billing_agreement_id, new stdClass(), 'BILLING_AGREEMENT' );
return new PaymentToken( $billing_agreement_id, new stdClass(), 'BILLING_AGREEMENT' );
}
$subscriptions = wcs_get_subscriptions_for_renewal_order( $order );
if ( ! empty( $subscriptions ) ) {
$subscription = reset( $subscriptions ); // Get first subscription.
$parent_order = $subscription->get_parent();
if ( $parent_order ) {
$billing_agreement_id = $parent_order->get_meta( '_ppec_billing_agreement_id', true );
if ( $billing_agreement_id ) {
return new PaymentToken( $billing_agreement_id, new stdClass(), 'BILLING_AGREEMENT' );
}
}
}
}
return $token;
}
/**
* Checks whether the mock PPEC gateway should be used or not.
*
* @return bool
*/
private function should_mock_ppec_gateway() {
// Are we processing a renewal?
if ( doing_action( 'woocommerce_scheduled_subscription_payment' ) ) {
return true;
}
// My Account > Subscriptions.
if ( is_wc_endpoint_url( 'subscriptions' ) ) {
return true;
}
// phpcs:disable WordPress.Security.NonceVerification
// Checks that require Subscriptions.
if ( class_exists( \WC_Subscriptions::class ) ) {
// My Account > Subscriptions > (Subscription).
if ( wcs_is_view_subscription_page() ) {
$subscription = wcs_get_subscription( absint( get_query_var( 'view-subscription' ) ) );
return ( $subscription && PPECHelper::PPEC_GATEWAY_ID === $subscription->get_payment_method() );
}
// Changing payment method?
if ( is_wc_endpoint_url( 'order-pay' ) && isset( $_GET['change_payment_method'] ) ) {
$subscription = wcs_get_subscription( absint( get_query_var( 'order-pay' ) ) );
return ( $subscription && PPECHelper::PPEC_GATEWAY_ID === $subscription->get_payment_method() );
}
// Early renew (via modal).
if ( isset( $_GET['process_early_renewal'], $_GET['subscription_id'] ) ) {
$subscription = wcs_get_subscription( absint( $_GET['subscription_id'] ) );
return ( $subscription && PPECHelper::PPEC_GATEWAY_ID === $subscription->get_payment_method() );
}
}
// Admin-only from here onwards.
if ( ! is_admin() ) {
return false;
}
// Are we saving metadata for a subscription?
if ( doing_action( 'woocommerce_process_shop_order_meta' ) ) {
return true;
}
// Are we editing an order or subscription tied to PPEC?
$order_id = wc_clean( wp_unslash( $_GET['id'] ?? $_GET['post'] ?? $_POST['post_ID'] ?? '' ) );
if ( $order_id ) {
$order = wc_get_order( $order_id );
return ( $order && PPECHelper::PPEC_GATEWAY_ID === $order->get_payment_method() );
}
// Are we on the WC > Subscriptions screen?
/**
* Class exist in WooCommerce.
*
* @psalm-suppress UndefinedClass
*/
$post_type_or_page = class_exists( OrderUtil::class ) && OrderUtil::custom_orders_table_usage_is_enabled()
? wc_clean( wp_unslash( $_GET['page'] ?? '' ) )
: wc_clean( wp_unslash( $_GET['post_type'] ?? $_POST['post_type'] ?? '' ) );
if ( $post_type_or_page === 'shop_subscription' || $post_type_or_page === 'wc-orders--shop_subscription' ) {
return true;
}
return false;
}
}