From 2108b35a21ed4ea0d26ef57b82159b59065dce95 Mon Sep 17 00:00:00 2001 From: dinamiko Date: Tue, 4 Jan 2022 12:11:19 +0100 Subject: [PATCH] Do not check saved payments via webhooks for now, see #405 --- .../src/Gateway/ProcessPaymentTrait.php | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/modules/ppcp-wc-gateway/src/Gateway/ProcessPaymentTrait.php b/modules/ppcp-wc-gateway/src/Gateway/ProcessPaymentTrait.php index 944594f36..7e974664c 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/ProcessPaymentTrait.php +++ b/modules/ppcp-wc-gateway/src/Gateway/ProcessPaymentTrait.php @@ -174,6 +174,97 @@ 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() ) + ) + ); + + $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(); $this->session_handler->destroy_session_data();