Skip to content

Commit 0c6e65e

Browse files
mordethvladolaru
andauthored
Add disable test drive account REST API endpoint (#10719)
Co-authored-by: Vlad Olaru <vlad.olaru@automattic.com>
1 parent 35ca0c7 commit 0c6e65e

File tree

4 files changed

+131
-22
lines changed

4 files changed

+131
-22
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: add
3+
4+
Add dedicated onboarding REST API endpoint for disabling test drive account, when possible.

includes/admin/class-wc-rest-payments-onboarding-controller.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,28 @@ public function register_routes() {
184184
],
185185
]
186186
);
187+
188+
register_rest_route(
189+
$this->namespace,
190+
'/' . $this->rest_base . '/test_drive_account/disable',
191+
[
192+
'methods' => WP_REST_Server::CREATABLE,
193+
'callback' => [ $this, 'disable_test_drive_account' ],
194+
'permission_callback' => [ $this, 'check_permission' ],
195+
'args' => [
196+
'source' => [
197+
'required' => false,
198+
'description' => 'The very first entry point the merchant entered our onboarding flow.',
199+
'type' => 'string',
200+
],
201+
'from' => [
202+
'required' => false,
203+
'description' => 'The previous step in the onboarding flow leading the merchant to arrive at the current step.',
204+
'type' => 'string',
205+
],
206+
],
207+
]
208+
);
187209
}
188210

189211
/**
@@ -327,4 +349,26 @@ public function init_test_drive_account( WP_REST_Request $request ) {
327349
]
328350
);
329351
}
352+
353+
/**
354+
* Disable Test Drive account API.
355+
*
356+
* @param WP_REST_Request $request Request object.
357+
*
358+
* @return WP_REST_Response|WP_Error
359+
*/
360+
public function disable_test_drive_account( WP_REST_Request $request ) {
361+
$context = [
362+
'from' => $request->get_param( 'from' ) ?? '',
363+
'source' => $request->get_param( 'source' ) ?? '',
364+
];
365+
366+
try {
367+
$result = $this->onboarding_service->disable_test_drive_account( $context );
368+
} catch ( Exception $e ) {
369+
return new WP_Error( self::RESULT_BAD_REQUEST, $e->getMessage(), [ 'status' => 400 ] );
370+
}
371+
372+
return rest_ensure_response( [ 'success' => $result ] );
373+
}
330374
}

includes/class-wc-payments-account.php

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2656,6 +2656,29 @@ function (): array {
26562656
);
26572657
}
26582658

2659+
/**
2660+
* Temporarily store the test drive account settings.
2661+
*
2662+
* If the current account is a test-drive account,
2663+
* we need to collect the test drive settings before we delete the test-drive account,
2664+
* and apply those settings to the live account.
2665+
*
2666+
* @return void
2667+
*/
2668+
public function save_test_drive_settings(): void {
2669+
$account = $this->get_cached_account_data();
2670+
2671+
if ( ! empty( $account['is_test_drive'] ) && true === $account['is_test_drive'] ) {
2672+
$test_drive_account_data = $this->get_test_drive_settings_for_live_account();
2673+
2674+
// Store the test drive settings for the live account in a transient,
2675+
// We don't pass the data around, as the merchant might cancel and start
2676+
// the onboarding from scratch. In this case, we won't have the test drive
2677+
// account anymore to collect the settings.
2678+
set_transient( self::ONBOARDING_TEST_DRIVE_SETTINGS_FOR_LIVE_ACCOUNT, $test_drive_account_data, HOUR_IN_SECONDS );
2679+
}
2680+
}
2681+
26592682
/**
26602683
* Send a Tracks event.
26612684
*
@@ -2749,25 +2772,4 @@ private function get_test_drive_settings_for_live_account(): array {
27492772

27502773
return [ 'capabilities' => $capabilities ];
27512774
}
2752-
2753-
/**
2754-
* If we're in test mode and dealing with a test-drive account,
2755-
* we need to collect the test drive settings before we delete the test-drive account,
2756-
* and apply those settings to the live account.
2757-
*
2758-
* @return void
2759-
*/
2760-
private function save_test_drive_settings(): void {
2761-
$account = $this->get_cached_account_data();
2762-
2763-
if ( ! empty( $account['is_test_drive'] ) && true === $account['is_test_drive'] ) {
2764-
$test_drive_account_data = $this->get_test_drive_settings_for_live_account();
2765-
2766-
// Store the test drive settings for the live account in a transient,
2767-
// We don't passing the data around, as the merchant might cancel and start
2768-
// the onboarding from scratch. In this case, we won't have the test drive
2769-
// account anymore to collect the settings.
2770-
set_transient( self::ONBOARDING_TEST_DRIVE_SETTINGS_FOR_LIVE_ACCOUNT, $test_drive_account_data, HOUR_IN_SECONDS );
2771-
}
2772-
}
27732775
}

includes/class-wc-payments-onboarding-service.php

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ class WC_Payments_Onboarding_Service {
7272
const FROM_STRIPE_EMBEDDED = 'STRIPE_EMBEDDED';
7373
const FROM_REFERRAL = 'REFERRAL';
7474

75-
const TRACKS_EVENT_ONBOARDING_RESET = 'wcpay_onboarding_flow_reset';
75+
const TRACKS_EVENT_ONBOARDING_RESET = 'wcpay_onboarding_flow_reset';
76+
const TRACKS_EVENT_TEST_DRIVE_ACCOUNT_DISABLE = 'wcpay_onboarding_test_account_disable';
7677

7778
/**
7879
* Client for making requests to the WooCommerce Payments API
@@ -740,6 +741,64 @@ public function reset_onboarding( array $context ): bool {
740741
return true;
741742
}
742743

744+
/**
745+
* Disable the Test Drive account.
746+
*
747+
* This means:
748+
* - preserve the currently connected Stripe test drive account settings.
749+
* - delete the currently connected Stripe test drive account.
750+
* - cleanup the gateway state for a fresh onboarding flow.
751+
*
752+
* @param array $context Context for the disable test drive account request.
753+
* - 'from' (string) The source of the request.
754+
* - 'source' (string) The source of the onboarding flow.
755+
*
756+
* @return bool Whether the test drive account was disabled successfully.
757+
*
758+
* @throws API_Exception When the platform API request fails or is not successful.
759+
*/
760+
public function disable_test_drive_account( array $context ): bool {
761+
if ( ! $this->payments_api_client->is_server_connected() ) {
762+
return false;
763+
}
764+
765+
// If the test mode onboarding is not enabled, we don't need to do anything.
766+
if ( ! self::is_test_mode_enabled() ) {
767+
return false;
768+
}
769+
770+
// If the test mode onboarding is enabled:
771+
// - Delete the current account;
772+
// - Cleanup the gateway state for a fresh onboarding flow.
773+
try {
774+
// If we're in test mode and dealing with a test-drive account,
775+
// we need to collect the test drive settings before we delete the test-drive account,
776+
// and apply those settings to the live account.
777+
WC_Payments::get_account_service()->save_test_drive_settings();
778+
779+
// Delete the currently connected Stripe account.
780+
$this->payments_api_client->delete_account( true );
781+
} catch ( API_Exception $e ) {
782+
throw new API_Exception( __( 'Failed to disable test drive account.', 'woocommerce-payments' ), 'wcpay-onboarding-account-error', 400 );
783+
}
784+
785+
$this->cleanup_on_account_reset();
786+
787+
// Track disabling test drive account.
788+
$event_properties = [
789+
'mode' => self::is_test_mode_enabled() ? 'test' : 'live',
790+
'from' => ! empty( $context['from'] ) ? sanitize_text_field( $context['from'] ) : '',
791+
'source' => ! empty( $context['source'] ) ? sanitize_text_field( $context['source'] ) : '',
792+
];
793+
794+
$this->tracks_event(
795+
self::TRACKS_EVENT_TEST_DRIVE_ACCOUNT_DISABLE,
796+
$event_properties
797+
);
798+
799+
return true;
800+
}
801+
743802
/**
744803
* Sets things up for a fresh onboarding flow.
745804
*

0 commit comments

Comments
 (0)