From e86667de028d3cb86cd1918cec5bb50917646e20 Mon Sep 17 00:00:00 2001 From: Joshua T Flowers Date: Thu, 3 Jun 2021 10:15:41 -0400 Subject: [PATCH] Add payment gateway return URL and action (https://github.com/woocommerce/woocommerce-admin/pull/7095) * Add return URL argument to connection URL method * Remove 'oauth' from connection URL method * Add changelog entry --- .../components/PaymentConnect.js | 6 ++-- .../docs/features/remote-payments.md | 2 +- plugins/woocommerce-admin/readme.txt | 1 + .../PaymentGatewaysController.php | 28 +++++++++++++++++-- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/plugins/woocommerce-admin/client/task-list/tasks/payments/RemotePayments/components/PaymentConnect.js b/plugins/woocommerce-admin/client/task-list/tasks/payments/RemotePayments/components/PaymentConnect.js index 65bc3390164..d57d5c8ab81 100644 --- a/plugins/woocommerce-admin/client/task-list/tasks/payments/RemotePayments/components/PaymentConnect.js +++ b/plugins/woocommerce-admin/client/task-list/tasks/payments/RemotePayments/components/PaymentConnect.js @@ -21,7 +21,7 @@ export const PaymentConnect = ( { } ) => { const { id, - oauth_connection_url: oAuthConnectionUrl, + connection_url: connectionUrl, setup_help_text: setupHelpText, required_settings_keys: settingKeys, settings, @@ -126,11 +126,11 @@ export const PaymentConnect = ( { ); } - if ( oAuthConnectionUrl ) { + if ( connectionUrl ) { return ( <> { helpText } - diff --git a/plugins/woocommerce-admin/docs/features/remote-payments.md b/plugins/woocommerce-admin/docs/features/remote-payments.md index 56c43c98ea2..0abb312df3f 100644 --- a/plugins/woocommerce-admin/docs/features/remote-payments.md +++ b/plugins/woocommerce-admin/docs/features/remote-payments.md @@ -48,7 +48,7 @@ Name | Return | Default | Description --- | --- | --- | --- `needs_setup()` | boolean | `false` | Used to determine if the gateway still requires setup in order to be used. `get_required_settings_keys()` | array | `[]` | An array of keys for fields required to properly configure the gateway. The keys must match those of already registered form fields in the payment gateway. -`get_oauth_connection_url()` | string | `null` | The oAuth connection URL to be used to quickly connect a payment gateway provider. If provided, this will be used in place of required setting fields. +`get_connection_url()` | string | `null` | The connection URL to be used to quickly connect a payment gateway provider. If provided, this will be used in place of required setting fields. `get_post_install_script_handles()` | array | `[]` | An array of script handles previously registered with `wp_register_script` to enqueue after the payment gateway has been installed. This is primarily used to `SlotFill` the payment connection step, but can allow any script to be added to assist in payment gateway setup. `get_setup_help_text()` | string | `null` | Help text to be shown above the connection step's submit button. diff --git a/plugins/woocommerce-admin/readme.txt b/plugins/woocommerce-admin/readme.txt index 8952670e0c0..2d401c75175 100644 --- a/plugins/woocommerce-admin/readme.txt +++ b/plugins/woocommerce-admin/readme.txt @@ -98,6 +98,7 @@ Release and roadmap notes are available on the [WooCommerce Developers Blog](htt - Add: Add COD method to default payment gateway recommendations #7057 - Add: Add BACS as default fallback payment gateway #7073 - Add: A/B test of progressive checklist features. #7089 +- Add: Add payment gateway return URL and action #7095 - Dev: Update package-lock to fix versioning of local packages. #6843 - Dev: Use rule processing for remote payment methods #6830 - Dev: Update E2E jest config, so it correctly creates screenshots on failure. #6858 diff --git a/plugins/woocommerce-admin/src/Features/RemotePaymentMethods/PaymentGatewaysController.php b/plugins/woocommerce-admin/src/Features/RemotePaymentMethods/PaymentGatewaysController.php index bce1474fa86..57d2e9fa69f 100644 --- a/plugins/woocommerce-admin/src/Features/RemotePaymentMethods/PaymentGatewaysController.php +++ b/plugins/woocommerce-admin/src/Features/RemotePaymentMethods/PaymentGatewaysController.php @@ -17,6 +17,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' ) ); } /** @@ -34,8 +35,9 @@ class PaymentGatewaysController { $data['post_install_scripts'] = self::get_post_install_scripts( $gateway ); $data['settings_url'] = admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=' . strtolower( $gateway->id ) ); - if ( method_exists( $gateway, 'get_oauth_connection_url' ) ) { - $data['oauth_connection_url'] = $gateway->get_oauth_connection_url(); + if ( method_exists( $gateway, 'get_connection_url' ) ) { + $return_url = wc_admin_url( '&task=payments&connection-return=' . strtolower( $gateway->id ) ); + $data['connection_url'] = $gateway->get_connection_url( $return_url ); } if ( method_exists( $gateway, 'get_setup_help_text' ) ) { @@ -73,4 +75,26 @@ class PaymentGatewaysController { return $scripts; } + + /** + * Call an action after a gating has been successfully returned. + */ + public static function possibly_do_connection_return_action() { + // phpcs:disable WordPress.Security.NonceVerification + if ( + ! isset( $_GET['page'] ) || + 'wc-admin' !== $_GET['page'] || + ! isset( $_GET['task'] ) || + 'payments' !== $_GET['task'] || + ! isset( $_GET['connection-return'] ) + ) { + return; + } + + $gateway_id = sanitize_text_field( wp_unslash( $_GET['connection-return'] ) ); + + // phpcs:enable WordPress.Security.NonceVerification + + do_action( 'woocommerce_admin_payment_gateway_connection_return', $gateway_id ); + } }