From b04f535a54274b1036b294c9940bcc33d4329ec1 Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Thu, 22 Aug 2024 10:27:35 +0800 Subject: [PATCH] [Beta tester] Add fake WooPayments completion tool (#50699) * Add woocommerce beta tester fake wcpay tool * Add changelog --- plugins/woocommerce-beta-tester/api/api.php | 1 + .../api/tools/class-fake-wcpayments.php | 62 ++++++++++++++++ .../api/tools/fake-woo-payments-gateway.php | 73 +++++++++++++++++++ .../changelog/add-beta-tester-tool-fake-wcpay | 4 + .../src/tools/commands/fake-woo-payments.js | 31 ++++++++ .../src/tools/commands/index.js | 9 +++ .../src/tools/data/actions.js | 25 +++++++ .../src/tools/data/reducer.js | 1 + .../src/tools/data/resolvers.js | 15 ++++ .../src/tools/data/selectors.js | 4 + 10 files changed, 225 insertions(+) create mode 100644 plugins/woocommerce-beta-tester/api/tools/class-fake-wcpayments.php create mode 100644 plugins/woocommerce-beta-tester/api/tools/fake-woo-payments-gateway.php create mode 100644 plugins/woocommerce-beta-tester/changelog/add-beta-tester-tool-fake-wcpay create mode 100644 plugins/woocommerce-beta-tester/src/tools/commands/fake-woo-payments.js diff --git a/plugins/woocommerce-beta-tester/api/api.php b/plugins/woocommerce-beta-tester/api/api.php index 16c1ef5e7cc..65b1d151aa8 100644 --- a/plugins/woocommerce-beta-tester/api/api.php +++ b/plugins/woocommerce-beta-tester/api/api.php @@ -55,6 +55,7 @@ require 'tools/trigger-update-callbacks.php'; require 'tools/reset-cys.php'; require 'tools/set-block-template-logging-threshold.php'; require 'tools/set-coming-soon-mode.php'; +require 'tools/fake-woo-payments-gateway.php'; require 'tracks/class-tracks-debug-log.php'; require 'features/features.php'; require 'rest-api-filters/class-wca-test-helper-rest-api-filters.php'; diff --git a/plugins/woocommerce-beta-tester/api/tools/class-fake-wcpayments.php b/plugins/woocommerce-beta-tester/api/tools/class-fake-wcpayments.php new file mode 100644 index 00000000000..01fa658c3a4 --- /dev/null +++ b/plugins/woocommerce-beta-tester/api/tools/class-fake-wcpayments.php @@ -0,0 +1,62 @@ +id = 'woocommerce_payments'; + $this->has_fields = true; + $this->method_title = 'WooPayments'; + $this->method_description = $this->get_method_description(); + + $this->description = ''; + $this->supports = array( + 'products', + 'refunds', + ); + } + + /** + * Returns true if the gateway needs additional configuration, false if it's ready to use. + * + * @see WC_Payment_Gateway::needs_setup + * @return bool + */ + public function needs_setup() { + return false; + } + + /** + * Check if the payment gateway is connected. This method is also used by + * external plugins to check if a connection has been established. + */ + public function is_connected() { + return true; + } + + /** + * Get the connection URL. + * Called directly by WooCommerce Core. + * + * @return string Connection URL. + */ + public function get_connection_url() { + return ''; + } + + /** + * Checks if the gateway is enabled, and also if it's configured enough to accept payments from customers. + * + * Use parent method value alongside other business rules to make the decision. + * + * @return bool Whether the gateway is enabled and ready to accept payments. + */ + public function is_available() { + return true; + } +} diff --git a/plugins/woocommerce-beta-tester/api/tools/fake-woo-payments-gateway.php b/plugins/woocommerce-beta-tester/api/tools/fake-woo-payments-gateway.php new file mode 100644 index 00000000000..c1f5d9f00e7 --- /dev/null +++ b/plugins/woocommerce-beta-tester/api/tools/fake-woo-payments-gateway.php @@ -0,0 +1,73 @@ + 'GET', + ) +); + +register_woocommerce_admin_test_helper_rest_route( + '/tools/fake-wcpay-completion/v1', + 'tools_set_fake_wcpay_completion', + array( + 'methods' => 'POST', + 'args' => array( + 'enabled' => array( + 'type' => 'enum', + 'enum' => array( 'yes', 'no' ), + 'required' => true, + 'description' => 'Whether to enable or disable fake WooPayments completion', + ), + ), + ) +); + +/** + * Get the current status of fake WooPayments completion. + */ +function tools_get_fake_wcpay_completion_status() { + return new WP_REST_Response( array( 'enabled' => get_option( 'wc_beta_tester_fake_wcpay_completion', 'no' ) ), 200 ); +} + +/** + * A tool to enable/disable fake WooPayments completion. + * + * @param WP_REST_Request $request Request object. + */ +function tools_set_fake_wcpay_completion( $request ) { + $enabled = $request->get_param( 'enabled' ); + update_option( 'wc_beta_tester_fake_wcpay_completion', $enabled ); + + return new WP_REST_Response( array( 'enabled' => $enabled ), 200 ); +} + + +if ( + 'yes' === get_option( 'wc_beta_tester_fake_wcpay_completion', 'no' ) && + class_exists( 'WC_Payment_Gateway_WCPay' ) +) { + add_filter( 'woocommerce_payment_gateways', 'tools_fake_wcpay' ); + add_filter( 'woocommerce_available_payment_gateways', 'tools_fake_wcpay' ); + + require_once dirname( __FILE__ ) . '/class-fake-wcpayments.php'; + + /** + * Fake WooPayments completion. + * + * @param array $gateways List of available payment gateways. + */ + function tools_fake_wcpay( $gateways ) { + $gateways['woocommerce_payments'] = new Fake_WCPayments(); + return $gateways; + } +} diff --git a/plugins/woocommerce-beta-tester/changelog/add-beta-tester-tool-fake-wcpay b/plugins/woocommerce-beta-tester/changelog/add-beta-tester-tool-fake-wcpay new file mode 100644 index 00000000000..35549787397 --- /dev/null +++ b/plugins/woocommerce-beta-tester/changelog/add-beta-tester-tool-fake-wcpay @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Add fake WooPayments completion tool diff --git a/plugins/woocommerce-beta-tester/src/tools/commands/fake-woo-payments.js b/plugins/woocommerce-beta-tester/src/tools/commands/fake-woo-payments.js new file mode 100644 index 00000000000..ef41cf76fb6 --- /dev/null +++ b/plugins/woocommerce-beta-tester/src/tools/commands/fake-woo-payments.js @@ -0,0 +1,31 @@ +/** + * External dependencies + */ +import { useSelect } from '@wordpress/data'; + +/** + * Internal dependencies + */ +import { STORE_KEY } from '../data/constants'; + +export const FAKE_WOO_PAYMENTS_ACTION_NAME = 'fakeWooPayments'; + +export const FakeWooPayments = () => { + const isEnabled = useSelect( ( select ) => + select( STORE_KEY ).getIsFakeWooPaymentsEnabled() + ); + const getDescription = () => { + switch ( isEnabled ) { + case 'yes': + return 'Enabled 🟢'; + case 'no': + return 'Disabled 🔴'; + case 'error': + return 'Error 🙁'; + default: + return 'Loading ...'; + } + }; + + return
{ getDescription() }
; +}; diff --git a/plugins/woocommerce-beta-tester/src/tools/commands/index.js b/plugins/woocommerce-beta-tester/src/tools/commands/index.js index 6283f0e35a8..66ba187eefa 100644 --- a/plugins/woocommerce-beta-tester/src/tools/commands/index.js +++ b/plugins/woocommerce-beta-tester/src/tools/commands/index.js @@ -15,6 +15,10 @@ import { SetComingSoonMode, UPDATE_COMING_SOON_MODE_ACTION_NAME, } from './set-coming-soon-mode'; +import { + FakeWooPayments, + FAKE_WOO_PAYMENTS_ACTION_NAME, +} from './fake-woo-payments'; import { UPDATE_WCCOM_REQUEST_ERRORS_MODE, @@ -97,4 +101,9 @@ export default [ description: , action: UPDATE_WCCOM_REQUEST_ERRORS_MODE, }, + { + command: 'Toggle Fake WooPayments Completion Status', + description: , + action: FAKE_WOO_PAYMENTS_ACTION_NAME, + }, ]; diff --git a/plugins/woocommerce-beta-tester/src/tools/data/actions.js b/plugins/woocommerce-beta-tester/src/tools/data/actions.js index e4455482694..e2cc1e67cb4 100644 --- a/plugins/woocommerce-beta-tester/src/tools/data/actions.js +++ b/plugins/woocommerce-beta-tester/src/tools/data/actions.js @@ -282,3 +282,28 @@ export function* updateWccomRequestErrorsMode( params ) { } ); } ); } + +export function* fakeWooPayments( params ) { + yield runCommand( 'Toggle Fake WooPayments Completion', function* () { + const newStatus = params.enabled === 'yes' ? 'no' : 'yes'; + + yield apiFetch( { + path: API_NAMESPACE + '/tools/fake-wcpay-completion/v1', + method: 'POST', + data: { + enabled: newStatus, + }, + } ); + + yield updateCommandParams( 'fakeWooPayments', { + enabled: newStatus, + } ); + + yield updateMessage( + 'Toggle Fake WooPayments Completion', + `Fake WooPayments completion ${ + newStatus === 'yes' ? 'disabled' : 'enabled' + }` + ); + } ); +} diff --git a/plugins/woocommerce-beta-tester/src/tools/data/reducer.js b/plugins/woocommerce-beta-tester/src/tools/data/reducer.js index 2545784a6db..481561117d8 100644 --- a/plugins/woocommerce-beta-tester/src/tools/data/reducer.js +++ b/plugins/woocommerce-beta-tester/src/tools/data/reducer.js @@ -14,6 +14,7 @@ const DEFAULT_STATE = { updateBlockTemplateLoggingThreshold: {}, runSelectedUpdateCallbacks: {}, updateWccomRequestErrorsMode: {}, + fakeWooPayments: {}, }, status: '', dbUpdateVersions: [], diff --git a/plugins/woocommerce-beta-tester/src/tools/data/resolvers.js b/plugins/woocommerce-beta-tester/src/tools/data/resolvers.js index 81065ce3170..0e839b27760 100644 --- a/plugins/woocommerce-beta-tester/src/tools/data/resolvers.js +++ b/plugins/woocommerce-beta-tester/src/tools/data/resolvers.js @@ -18,6 +18,7 @@ import { UPDATE_BLOCK_TEMPLATE_LOGGING_THRESHOLD_ACTION_NAME } from '../commands import { UPDATE_COMING_SOON_MODE_ACTION_NAME } from '../commands/set-coming-soon-mode'; import { TRIGGER_UPDATE_CALLBACKS_ACTION_NAME } from '../commands/trigger-update-callbacks'; import { UPDATE_WCCOM_REQUEST_ERRORS_MODE } from '../commands/set-wccom-request-errors'; +import { FAKE_WOO_PAYMENTS_ACTION_NAME } from '../commands/fake-woo-payments'; export function* getCronJobs() { const path = `${ API_NAMESPACE }/tools/get-cron-list/v1`; @@ -135,3 +136,17 @@ export function* getWccomRequestErrorsMode() { throw new Error( error ); } } + +export function* getIsFakeWooPaymentsEnabled() { + try { + const response = yield apiFetch( { + path: API_NAMESPACE + '/tools/fake-wcpay-completion/v1', + method: 'GET', + } ); + yield updateCommandParams( FAKE_WOO_PAYMENTS_ACTION_NAME, { + enabled: response.enabled || 'no', + } ); + } catch ( error ) { + throw new Error( error ); + } +} diff --git a/plugins/woocommerce-beta-tester/src/tools/data/selectors.js b/plugins/woocommerce-beta-tester/src/tools/data/selectors.js index b53025ff4b1..bca03b9bbf9 100644 --- a/plugins/woocommerce-beta-tester/src/tools/data/selectors.js +++ b/plugins/woocommerce-beta-tester/src/tools/data/selectors.js @@ -41,3 +41,7 @@ export function getComingSoonMode( state ) { export function getWccomRequestErrorsMode( state ) { return state.params.updateWccomRequestErrorsMode.mode; } + +export function getIsFakeWooPaymentsEnabled( state ) { + return state.params.fakeWooPayments.enabled; +}