diff --git a/modules/ppcp-admin-notices/src/AdminNotices.php b/modules/ppcp-admin-notices/src/AdminNotices.php index 73fe24b71..edb8d0817 100644 --- a/modules/ppcp-admin-notices/src/AdminNotices.php +++ b/modules/ppcp-admin-notices/src/AdminNotices.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\AdminNotices; +use WooCommerce\PayPalCommerce\AdminNotices\Entity\Message; +use WooCommerce\PayPalCommerce\AdminNotices\Repository\Repository; use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider; use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface; use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface; @@ -40,6 +42,22 @@ class AdminNotices implements ModuleInterface { $renderer->render(); } ); + + add_action( + Repository::NOTICES_FILTER, + function ( $notices ) use ( $c ) { + $admin_notices = $c->get( 'admin-notices.repository' ); + assert( $admin_notices instanceof Repository ); + + $persisted_notices = $admin_notices->get_persisted_and_clear(); + + if ( $persisted_notices ) { + $notices = array_merge( $notices, $persisted_notices ); + } + + return $notices; + } + ); } /** diff --git a/modules/ppcp-admin-notices/src/Entity/Message.php b/modules/ppcp-admin-notices/src/Entity/Message.php index 0e8bc959e..97624d872 100644 --- a/modules/ppcp-admin-notices/src/Entity/Message.php +++ b/modules/ppcp-admin-notices/src/Entity/Message.php @@ -92,4 +92,18 @@ class Message { public function wrapper(): string { return $this->wrapper; } + + /** + * Returns the object as array. + * + * @return array + */ + public function to_array(): array { + return array( + 'type' => $this->type, + 'message' => $this->message, + 'dismissable' => $this->dismissable, + 'wrapper' => $this->wrapper, + ); + } } diff --git a/modules/ppcp-admin-notices/src/Repository/Repository.php b/modules/ppcp-admin-notices/src/Repository/Repository.php index e13a5dd6c..9573a81a4 100644 --- a/modules/ppcp-admin-notices/src/Repository/Repository.php +++ b/modules/ppcp-admin-notices/src/Repository/Repository.php @@ -16,7 +16,8 @@ use WooCommerce\PayPalCommerce\AdminNotices\Entity\Message; */ class Repository implements RepositoryInterface { - const NOTICES_FILTER = 'ppcp.admin-notices.current-notices'; + const NOTICES_FILTER = 'ppcp.admin-notices.current-notices'; + const PERSISTED_NOTICES_OPTION = 'woocommerce_ppcp-admin-notices'; /** * Returns the current messages. @@ -37,4 +38,40 @@ class Repository implements RepositoryInterface { } ); } + + /** + * Adds a message to persist between page reloads. + * + * @param Message $message The message. + * @return void + */ + public function persist( Message $message ): void { + $persisted_notices = get_option( self::PERSISTED_NOTICES_OPTION ) ?: array(); + + $persisted_notices[] = $message->to_array(); + + update_option( self::PERSISTED_NOTICES_OPTION, $persisted_notices ); + } + + /** + * Adds a message to persist between page reloads. + * + * @return array|Message[] + */ + public function get_persisted_and_clear(): array { + $notices = array(); + + $persisted_data = get_option( self::PERSISTED_NOTICES_OPTION ) ?: array(); + foreach ( $persisted_data as $notice_data ) { + $notices[] = new Message( + (string) ( $notice_data['message'] ?? '' ), + (string) ( $notice_data['type'] ?? '' ), + (bool) ( $notice_data['dismissable'] ?? true ), + (string) ( $notice_data['wrapper'] ?? '' ) + ); + } + + update_option( self::PERSISTED_NOTICES_OPTION, array(), true ); + return $notices; + } } diff --git a/modules/ppcp-wc-gateway/src/WCGatewayModule.php b/modules/ppcp-wc-gateway/src/WCGatewayModule.php index cdf638131..fd4395223 100644 --- a/modules/ppcp-wc-gateway/src/WCGatewayModule.php +++ b/modules/ppcp-wc-gateway/src/WCGatewayModule.php @@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\WcGateway; use Psr\Log\LoggerInterface; use Throwable; +use WooCommerce\PayPalCommerce\AdminNotices\Entity\Message; use WooCommerce\PayPalCommerce\ApiClient\Entity\Authorization; use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache; @@ -646,6 +647,8 @@ class WCGatewayModule implements ModuleInterface { add_action( 'woocommerce_order_action_ppcp_reauthorize_order', static function ( WC_Order $wc_order ) use ( $container ) { + $admin_notices = $container->get( 'admin-notices.repository' ); + assert( $admin_notices instanceof Repository ); /** * The authorized payments processor. @@ -653,7 +656,17 @@ class WCGatewayModule implements ModuleInterface { * @var AuthorizedPaymentsProcessor $authorized_payments_processor */ $authorized_payments_processor = $container->get( 'wcgateway.processor.authorized-payments' ); - $authorized_payments_processor->reauthorize_payment( $wc_order ); + + if ( $authorized_payments_processor->reauthorize_payment( $wc_order ) !== AuthorizedPaymentsProcessor::SUCCESSFUL ) { + $message = sprintf( + '%1$s %2$s', + esc_html__( 'Reauthorization with PayPal failed: ', 'woocommerce-paypal-payments' ), + $authorized_payments_processor->reauthorization_failure_reason() ?: '' + ); + $admin_notices->persist( new Message( $message, 'error' ) ); + } else { + $admin_notices->persist( new Message( 'Payment reauthorized.', 'info' ) ); + } } ); }