Skip to content

Commit

Permalink
Merge branch 'develop' into fix/unhandled-promises
Browse files Browse the repository at this point in the history
  • Loading branch information
timur27 authored Dec 13, 2024
2 parents 2c3797e + 42bf30b commit 0d29fe4
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
4 changes: 4 additions & 0 deletions changelog/add-6924-migrate-test-drive-capabilities
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: add

Migrate active capabilities from test-drive account when switching to live account.
47 changes: 44 additions & 3 deletions includes/class-wc-payments-account.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class WC_Payments_Account implements MultiCurrencyAccountInterface {
const ONBOARDING_STARTED_TRANSIENT = 'wcpay_on_boarding_started';
const ONBOARDING_STATE_TRANSIENT = 'wcpay_stripe_onboarding_state';
const WOOPAY_ENABLED_BY_DEFAULT_TRANSIENT = 'woopay_enabled_by_default';
const ONBOARDING_TEST_DRIVE_SETTINGS_FOR_LIVE_ACCOUNT = 'test_drive_account_settings_for_live_account';
const EMBEDDED_KYC_IN_PROGRESS_OPTION = 'wcpay_onboarding_embedded_kyc_in_progress';
const ERROR_MESSAGE_TRANSIENT = 'wcpay_error_message';
const INSTANT_DEPOSITS_REMINDER_ACTION = 'wcpay_instant_deposit_reminder';
Expand Down Expand Up @@ -1317,6 +1318,7 @@ public function maybe_handle_onboarding() {
}

$this->cleanup_on_account_reset();
delete_transient( self::ONBOARDING_TEST_DRIVE_SETTINGS_FOR_LIVE_ACCOUNT );

// When we reset the account and want to go back to the settings page - redirect immediately!
if ( $redirect_to_settings_page ) {
Expand All @@ -1342,6 +1344,10 @@ public function maybe_handle_onboarding() {
// in the "everything OK" scenario).
if ( WC_Payments_Onboarding_Service::is_test_mode_enabled() ) {
try {
// If we're in test mode and dealing with a test-drive account,
// we need to collect the test drive settings before we delete the test-drive account,
// and apply those settings to the live account.
$this->save_test_drive_settings();
// Delete the currently connected Stripe account.
$this->payments_api_client->delete_account( true );
} catch ( API_Exception $e ) {
Expand Down Expand Up @@ -1426,7 +1432,6 @@ public function maybe_handle_onboarding() {
if ( ! $collect_payout_requirements
&& $this->has_working_jetpack_connection()
&& $this->is_stripe_account_valid() ) {

$params = [
'source' => $onboarding_source,
// Carry over some parameters as they may be used by our frontend logic.
Expand Down Expand Up @@ -2149,13 +2154,11 @@ private function finalize_connection( string $state, string $mode, array $additi
// If we get this parameter, but we have a valid state, it means the merchant left KYC early and didn't finish it.
// While we do have an account, it is not yet valid. We need to redirect them back to the connect page.
$params['wcpay-connection-error'] = '1';

$this->redirect_service->redirect_to_connect_page( '', WC_Payments_Onboarding_Service::FROM_STRIPE, $params );
return;
}

$params['wcpay-connection-success'] = '1';

$this->redirect_service->redirect_to_overview_page( WC_Payments_Onboarding_Service::FROM_STRIPE, $params );
}

Expand Down Expand Up @@ -2582,4 +2585,42 @@ public function get_lifetime_total_payment_volume(): int {
$account = $this->get_cached_account_data();
return (int) ! empty( $account ) && isset( $account['lifetime_total_payment_volume'] ) ? $account['lifetime_total_payment_volume'] : 0;
}

/**
* Extract the test drive settings from the account data that we want to store for the live account.
* ATM we only store the enabled payment methods.
*
* @return array The test drive settings for the live account.
*/
private function get_test_drive_settings_for_live_account(): array {
$gateway = WC_Payments::get_gateway();

$capabilities = [];
foreach ( $gateway->get_upe_enabled_payment_method_ids() as $payment_method_id ) {
$capabilities[ $payment_method_id . '_payments' ] = [ 'requested' => 'true' ];
}

return [ 'capabilities' => $capabilities ];
}

/**
* If we're in test mode and dealing with a test-drive account,
* we need to collect the test drive settings before we delete the test-drive account,
* and apply those settings to the live account.
*
* @return void
*/
private function save_test_drive_settings(): void {
$account = $this->get_cached_account_data();

if ( ! empty( $account['is_test_drive'] ) && true === $account['is_test_drive'] ) {
$test_drive_account_data = $this->get_test_drive_settings_for_live_account();

// Store the test drive settings for the live account in a transient,
// We don't passing the data around, as the merchant might cancel and start
// the onboarding from scratch. In this case, we won't have the test drive
// account anymore to collect the settings.
set_transient( self::ONBOARDING_TEST_DRIVE_SETTINGS_FOR_LIVE_ACCOUNT, $test_drive_account_data, HOUR_IN_SECONDS );
}
}
}
24 changes: 24 additions & 0 deletions includes/class-wc-payments-onboarding-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public function __construct( WC_Payments_API_Client $payments_api_client, Databa
*/
public function init_hooks() {
add_filter( 'admin_body_class', [ $this, 'add_admin_body_classes' ] );
add_filter( 'wc_payments_get_onboarding_data_args', [ $this, 'maybe_add_test_drive_settings_to_new_account_request' ] );
}

/**
Expand Down Expand Up @@ -902,4 +903,27 @@ public static function get_source( ?string $referer = null, ?array $get_params =
// Default to an unknown source.
return self::SOURCE_UNKNOWN;
}

/**
* If settings are collected from the test-drive account,
* include them in the existing arguments when creating the new account.
*
* @param array $args The request args to create new account.
*
* @return array The request args, possible updated with the test drive account settings, used to create new account.
*/
public function maybe_add_test_drive_settings_to_new_account_request( array $args ): array {
if (
get_transient( WC_Payments_Account::ONBOARDING_TEST_DRIVE_SETTINGS_FOR_LIVE_ACCOUNT ) &&
is_array( get_transient( WC_Payments_Account::ONBOARDING_TEST_DRIVE_SETTINGS_FOR_LIVE_ACCOUNT ) )
) {
$args['account_data'] = array_merge(
$args['account_data'],
get_transient( WC_Payments_Account::ONBOARDING_TEST_DRIVE_SETTINGS_FOR_LIVE_ACCOUNT )
);
delete_transient( WC_Payments_Account::ONBOARDING_TEST_DRIVE_SETTINGS_FOR_LIVE_ACCOUNT );
}

return $args;
}
}

0 comments on commit 0d29fe4

Please sign in to comment.