From 3327a23378b58e8639424bab849b697a98cfa511 Mon Sep 17 00:00:00 2001 From: Joshua T Flowers Date: Tue, 22 Jun 2021 19:50:53 -0400 Subject: [PATCH] Add notice and enable gateway on successful connection (https://github.com/woocommerce/woocommerce-admin/pull/7209) * Add notice and enabled gateway on successfull connection * Record event on payment gateway connection * Redirect to payments task after successful connection --- .../PaymentGatewaysController.php | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/plugins/woocommerce-admin/src/Features/PaymentGatewaySuggestions/PaymentGatewaysController.php b/plugins/woocommerce-admin/src/Features/PaymentGatewaySuggestions/PaymentGatewaysController.php index 42fde6e4ec8..5a1128d6ba9 100644 --- a/plugins/woocommerce-admin/src/Features/PaymentGatewaySuggestions/PaymentGatewaysController.php +++ b/plugins/woocommerce-admin/src/Features/PaymentGatewaySuggestions/PaymentGatewaysController.php @@ -5,6 +5,8 @@ namespace Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions; +use Automattic\WooCommerce\Admin\Features\TransientNotices; + defined( 'ABSPATH' ) || exit; /** @@ -18,6 +20,7 @@ class PaymentGatewaysController { public static function init() { add_filter( 'woocommerce_rest_prepare_payment_gateway', array( __CLASS__, 'extend_response' ), 10, 3 ); add_filter( 'admin_init', array( __CLASS__, 'possibly_do_connection_return_action' ) ); + add_action( 'woocommerce_admin_payment_gateway_connection_return', array( __CLASS__, 'handle_successfull_connection' ) ); } /** @@ -97,4 +100,48 @@ class PaymentGatewaysController { do_action( 'woocommerce_admin_payment_gateway_connection_return', $gateway_id ); } + + /** + * Handle a successful gateway connection. + * + * @param string $gateway_id Gateway ID. + */ + public static function handle_successfull_connection( $gateway_id ) { + // phpcs:disable WordPress.Security.NonceVerification + if ( ! isset( $_GET['success'] ) || 1 !== intval( $_GET['success'] ) ) { + return; + } + // phpcs:enable WordPress.Security.NonceVerification + + $payment_gateways = WC()->payment_gateways()->payment_gateways(); + $payment_gateway = isset( $payment_gateways[ $gateway_id ] ) ? $payment_gateways[ $gateway_id ] : null; + + if ( ! $payment_gateway ) { + return; + } + + $payment_gateway->update_option( 'enabled', 'yes' ); + + TransientNotices::add( + array( + 'user_id' => get_current_user_id(), + 'id' => 'payment-gateway-connection-return-' . str_replace( ',', '-', $gateway_id ), + 'status' => 'success', + 'content' => sprintf( + /* translators: the title of the payment gateway */ + __( '%s connected successfully', 'woocommerce-admin' ), + $payment_gateway->method_title + ), + ) + ); + + wc_admin_record_tracks_event( + 'tasklist_payment_connect_method', + array( + 'payment_method' => $gateway_id, + ) + ); + + wp_safe_redirect( wc_admin_url( '&task=payments' ) ); + } }