Add capture auth ignore to subs fail handler

This commit is contained in:
dinamiko 2022-09-26 12:48:20 +02:00
parent 6a5cc5d129
commit 5df633037a
2 changed files with 134 additions and 122 deletions

View file

@ -16,6 +16,7 @@ use WC_Order;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Repository\OrderRepository;
use WooCommerce\PayPalCommerce\Subscription\FreeTrialHandlerTrait;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CardButtonGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
@ -117,64 +118,74 @@ class PaymentTokenChecker {
return;
}
$tokens = $this->payment_token_repository->all_for_user_id( $customer_id );
$subscription_behavior_when_vault_fails_setting_name = 'subscription_behavior_when_vault_fails';
$subscription_behavior_when_vault_fails = $this->settings->get( $subscription_behavior_when_vault_fails_setting_name );
if ( $tokens || $subscription_behavior_when_vault_fails === 'capture_auth' ) {
try {
if ( $this->is_free_trial_order( $wc_order ) ) {
if ( in_array( $wc_order->get_payment_method(), array( CreditCardGateway::ID, CardButtonGateway::ID ), true )
|| ( PayPalGateway::ID === $wc_order->get_payment_method() && 'card' === $wc_order->get_meta( PayPalGateway::ORDER_PAYMENT_SOURCE_META_KEY ) )
) {
$order = $this->order_repository->for_wc_order( $wc_order );
$this->authorized_payments_processor->void_authorizations( $order );
$wc_order->payment_complete();
}
return;
}
if ( ! $tokens ) {
update_post_meta( $wc_order->get_id(), self::VAULTING_FAILED_META_KEY, $subscription_behavior_when_vault_fails );
}
$this->capture_authorized_payment( $wc_order );
if ( ! $tokens && $subscription_behavior_when_vault_fails === 'capture_auth' ) {
$subscriptions = function_exists( 'wcs_get_subscriptions_for_order' ) ? wcs_get_subscriptions_for_order( $wc_order ) : array();
foreach ( $subscriptions as $subscription ) {
try {
$subscription->set_requires_manual_renewal( true );
$subscription->save();
$message = __( 'Subscription set to Manual Renewal because payment method was not saved at PayPal.', 'woocommerce-paypal-payments' );
$wc_order->add_order_note( $message );
} catch ( Exception $exception ) {
$this->logger->error( "Could not update payment method on subscription #{$subscription->get_id()} " . $exception->getMessage() );
}
}
}
} catch ( Exception $exception ) {
$this->logger->error( $exception->getMessage() );
if ( $this->is_free_trial_order( $wc_order ) ) {
if ( in_array( $wc_order->get_payment_method(), array( CreditCardGateway::ID, CardButtonGateway::ID ), true )
|| ( PayPalGateway::ID === $wc_order->get_payment_method() && 'card' === $wc_order->get_meta( PayPalGateway::ORDER_PAYMENT_SOURCE_META_KEY ) )
) {
$order = $this->order_repository->for_wc_order( $wc_order );
$this->authorized_payments_processor->void_authorizations( $order );
$wc_order->payment_complete();
}
return;
}
$this->logger->error( "Payment for subscription parent order #{$order_id} was not saved on PayPal." );
$tokens = $this->payment_token_repository->all_for_user_id( $customer_id );
if ( $tokens ) {
try {
$this->capture_authorized_payment( $wc_order );
} catch ( Exception $exception ) {
$this->logger->warning( $exception->getMessage() );
}
try {
$order = $this->order_repository->for_wc_order( $wc_order );
update_post_meta( $wc_order->get_id(), self::VAULTING_FAILED_META_KEY, $subscription_behavior_when_vault_fails );
$this->authorized_payments_processor->void_authorizations( $order );
} catch ( RuntimeException $exception ) {
$this->logger->warning( $exception->getMessage() );
return;
}
$this->update_failed_status( $wc_order );
try {
$subscription_behavior_when_fails = $this->settings->get( 'subscription_behavior_when_vault_fails' );
} catch ( NotFoundException $exception ) {
return;
}
switch ( $subscription_behavior_when_fails ) {
case 'void_auth':
$order = $this->order_repository->for_wc_order( $wc_order );
$this->authorized_payments_processor->void_authorizations( $order );
$this->logger->error( "Payment for subscription parent order #{$order_id} was not saved on PayPal." );
$this->update_failed_status( $wc_order );
break;
case 'capture_auth':
try {
$this->capture_authorized_payment( $wc_order );
} catch ( Exception $exception ) {
$this->logger->warning( $exception->getMessage() );
return;
}
$subscriptions = function_exists( 'wcs_get_subscriptions_for_order' ) ? wcs_get_subscriptions_for_order( $wc_order ) : array();
foreach ( $subscriptions as $subscription ) {
try {
$subscription->set_requires_manual_renewal( true );
$subscription->save();
$message = __( 'Subscription set to Manual Renewal because payment method was not saved at PayPal.', 'woocommerce-paypal-payments' );
$wc_order->add_order_note( $message );
} catch ( Exception $exception ) {
$this->logger->error( "Could not update payment method on subscription #{$subscription->get_id()} " . $exception->getMessage() );
}
}
break;
case 'capture_auth_ignore':
try {
$this->capture_authorized_payment( $wc_order );
} catch ( Exception $exception ) {
$this->logger->warning( $exception->getMessage() );
return;
}
break;
}
}
/**