diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php b/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php index 741436c22..4c4bc4671 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php @@ -12,11 +12,11 @@ use Exception; use Psr\Log\LoggerInterface; use WC_Order; +use WC_Payment_Tokens; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus; use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken; use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException; -use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\Onboarding\Environment; use WooCommerce\PayPalCommerce\Onboarding\State; use WooCommerce\PayPalCommerce\Session\SessionHandler; @@ -522,10 +522,21 @@ function ( PaymentToken $token ): bool { // phpcs:ignore WordPress.Security.NonceVerification.Missing $saved_paypal_payment = wc_clean( wp_unslash( $_POST['saved_paypal_payment'] ?? '' ) ); if ( $saved_paypal_payment ) { - $wc_order->update_meta_data( 'payment_token_id', $saved_paypal_payment ); - $wc_order->save(); + $payment_token = WC_Payment_Tokens::get( $saved_paypal_payment ); + if ( $payment_token ) { + $wc_order->add_payment_token( $payment_token ); + $wc_order->save(); - return $this->handle_payment_success( $wc_order ); + return $this->handle_payment_success( $wc_order ); + } + + wc_add_notice( __( 'Could not change payment.', 'woocommerce-paypal-payments' ), 'error' ); + + return array( + 'result' => 'failure', + 'redirect' => wc_get_checkout_url(), + 'errorMessage' => __( 'Could not change payment.', 'woocommerce-paypal-payments' ), + ); } } diff --git a/modules/ppcp-wc-subscriptions/src/RenewalHandler.php b/modules/ppcp-wc-subscriptions/src/RenewalHandler.php index adee662f2..650329714 100644 --- a/modules/ppcp-wc-subscriptions/src/RenewalHandler.php +++ b/modules/ppcp-wc-subscriptions/src/RenewalHandler.php @@ -427,9 +427,11 @@ private function token_id( WC_Order $wc_order, WC_Customer $customer ): string { $tokens = $wc_order->get_payment_tokens(); if ( $tokens ) { - $token = WC_Payment_Tokens::get( $tokens[0] ); - if ( $token ) { - $token_id = $token->get_token(); + foreach ( $tokens as $token_id ) { + $token = WC_Payment_Tokens::get( $token_id ); + if ( $token && $token->get_gateway_id() === $wc_order->get_payment_method() ) { + return $token->get_token(); + } } } diff --git a/modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php b/modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php index 393a7ef8f..d15906d46 100644 --- a/modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php +++ b/modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php @@ -251,30 +251,21 @@ protected function display_saved_paypal_payments( && PayPalGateway::ID === $id && $subscription_helper->is_subscription_change_payment() ) { - $tokens = $payment_token_repository->all_for_user_id( get_current_user_id() ); - if ( ! $tokens || ! $payment_token_repository->tokens_contains_paypal( $tokens ) ) { - return esc_html__( - 'No PayPal payments saved, in order to use a saved payment you first need to create it through a purchase.', - 'woocommerce-paypal-payments' - ); - } - + $tokens = WC_Payment_Tokens::get_customer_tokens( get_current_user_id(), PayPalGateway::ID ); $output = sprintf( '

'; + $output .= '

'; - return $output; + return $output; } return $description;