From a7290f9ce073caec67e25ab8544120680eb7d020 Mon Sep 17 00:00:00 2001 From: dinamiko Date: Mon, 28 Feb 2022 17:31:15 +0100 Subject: [PATCH] Schedule checking for saved payment (WIP) --- .../src/SubscriptionModule.php | 12 +++ .../src/Gateway/ProcessPaymentTrait.php | 95 ++----------------- 2 files changed, 19 insertions(+), 88 deletions(-) diff --git a/modules/ppcp-subscription/src/SubscriptionModule.php b/modules/ppcp-subscription/src/SubscriptionModule.php index 3be24af3d..770d4b967 100644 --- a/modules/ppcp-subscription/src/SubscriptionModule.php +++ b/modules/ppcp-subscription/src/SubscriptionModule.php @@ -94,6 +94,18 @@ class SubscriptionModule implements ModuleInterface { 20, 2 ); + + add_action('woocommerce_paypal_payments_check_saved_payment', function ($order_id, $customer_id) use ($c) { + $payment_token_repository = $c->get( 'vaulting.repository.payment-token' ); + $settings = $c->get( 'wcgateway.settings' ); + + $tokens = $payment_token_repository->all_for_user_id( $customer_id ); + if($tokens) { + if ( $settings->has( 'intent' ) && strtoupper( (string) $settings->get( 'intent' ) ) === 'CAPTURE' ) { + //$this->authorized_payments_processor->capture_authorized_payment( $wc_order ); + } + } + }, 10, 2); } /** diff --git a/modules/ppcp-wc-gateway/src/Gateway/ProcessPaymentTrait.php b/modules/ppcp-wc-gateway/src/Gateway/ProcessPaymentTrait.php index 96bd2252b..39ddbd029 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/ProcessPaymentTrait.php +++ b/modules/ppcp-wc-gateway/src/Gateway/ProcessPaymentTrait.php @@ -173,96 +173,15 @@ trait ProcessPaymentTrait { try { if ( $this->order_processor->process( $wc_order ) ) { - if ( $this->subscription_helper->has_subscription( $order_id ) ) { - $this->logger->info( "Trying to save payment for subscription parent order #{$order_id}." ); - - $tokens = $this->payment_token_repository->all_for_user_id( $wc_order->get_customer_id() ); - if ( $tokens ) { - $this->logger->info( "Payment for subscription parent order #{$order_id} was saved correctly." ); - - if ( $this->config->has( 'intent' ) && strtoupper( (string) $this->config->get( 'intent' ) ) === 'CAPTURE' ) { - $this->authorized_payments_processor->capture_authorized_payment( $wc_order ); - } - - $this->session_handler->destroy_session_data(); - - return array( - 'result' => 'success', - 'redirect' => $this->get_return_url( $wc_order ), - ); - } - - $this->logger->error( "Payment for subscription parent order #{$order_id} was not saved." ); - - $paypal_order_id = $wc_order->get_meta( PayPalGateway::ORDER_ID_META_KEY ); - if ( ! $paypal_order_id ) { - throw new RuntimeException( 'PayPal order ID not found in meta.' ); - } - $order = $this->order_endpoint->order( $paypal_order_id ); - - $purchase_units = $order->purchase_units(); - if ( ! $purchase_units ) { - throw new RuntimeException( 'No purchase units.' ); - } - - $payments = $purchase_units[0]->payments(); - if ( ! $payments ) { - throw new RuntimeException( 'No payments.' ); - } - - $this->logger->debug( - sprintf( - 'Trying to void order %1$s, payments: %2$s.', - $order->id(), - wp_json_encode( $payments->to_array() ) - ) + as_schedule_single_action( + time() + (1 * MINUTE_IN_SECONDS), + 'woocommerce_paypal_payments_check_saved_payment', + array( + 'order_id' => $order_id, + 'customer_id' => $wc_order->get_customer_id(), + ) ); - - $voidable_authorizations = array_filter( - $payments->authorizations(), - function ( Authorization $authorization ): bool { - return $authorization->is_voidable(); - } - ); - if ( ! $voidable_authorizations ) { - throw new RuntimeException( 'No voidable authorizations.' ); - } - - foreach ( $voidable_authorizations as $authorization ) { - $this->payments_endpoint->void( $authorization ); - } - - $this->logger->debug( - sprintf( - 'Order %1$s voided successfully.', - $order->id() - ) - ); - - $error_message = __( 'Could not process order because it was not possible to save the payment.', 'woocommerce-paypal-payments' ); - $wc_order->update_status( 'failed', $error_message ); - - $subscriptions = wcs_get_subscriptions_for_order( $order_id ); - foreach ( $subscriptions as $key => $subscription ) { - if ( $subscription->get_parent_id() === $order_id ) { - try { - $subscription->update_status( 'cancelled' ); - break; - } catch ( Exception $exception ) { - $this->logger->error( "Could not update cancelled status on subscription #{$subscription->get_id()} " . $exception->getMessage() ); - } - } - } - - // Adds retry counter meta to avoid duplicate invoice id error on consequent tries. - $wc_order->update_meta_data( 'ppcp-retry', (int) $wc_order->get_meta( 'ppcp-retry' ) + 1 ); - $wc_order->save_meta_data(); - - $this->session_handler->destroy_session_data(); - wc_add_notice( $error_message, 'error' ); - - return $failure_data; } WC()->cart->empty_cart();