Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Payment Methods preselected by NOX after onboarding accounts #10018

Merged
merged 13 commits into from
Dec 20, 2024
Merged
4 changes: 4 additions & 0 deletions changelog/add-9973-enable-pms-from-capabilities
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: dev

Enable Payment Methods preselected by NOX after onboarding accounts
30 changes: 30 additions & 0 deletions includes/class-wc-payments-account.php
Original file line number Diff line number Diff line change
Expand Up @@ -2049,6 +2049,21 @@ private function init_stripe_onboarding( string $setup_mode, string $wcpay_conne
$gateway->update_option( 'enabled', 'yes' );
$gateway->update_option( 'test_mode', empty( $onboarding_data['is_live'] ) ? 'yes' : 'no' );

/**
* ==================
* Enforces the update of payment methods to 'enabled' based on the capabilities
* provided during the NOX onboarding process.
*
* @see WC_Payments_Onboarding_Service::update_enabled_payment_methods_ids
* ==================
*/
$capabilities = $this->onboarding_service->get_capabilities_from_request();
vladolaru marked this conversation as resolved.
Show resolved Hide resolved

// Activate enabled Payment Methods IDs.
if ( ! empty( $capabilities ) ) {
$this->onboarding_service->update_enabled_payment_methods_ids( $gateway, $capabilities );
}

// Store a state after completing KYC for tracks. This is stored temporarily in option because
// user might not have agreed to TOS yet.
update_option( '_wcpay_onboarding_stripe_connected', [ 'is_existing_stripe_account' => true ] );
Expand Down Expand Up @@ -2161,6 +2176,20 @@ private function finalize_connection( string $state, string $mode, array $additi
$gateway->update_option( 'enabled', 'yes' );
$gateway->update_option( 'test_mode', 'live' !== $mode ? 'yes' : 'no' );

/**
* ==================
* Enforces the update of payment methods to 'enabled' based on the capabilities
* provided during the NOX onboarding process.
*
* @see WC_Payments_Onboarding_Service::update_enabled_payment_methods_ids
* ==================
*/
$capabilities = $this->onboarding_service->get_capabilities_from_request();
vladolaru marked this conversation as resolved.
Show resolved Hide resolved
// Activate enabled Payment Methods IDs.
if ( ! empty( $capabilities ) ) {
$this->onboarding_service->update_enabled_payment_methods_ids( $gateway, $capabilities );
}

// Store a state after completing KYC for tracks. This is stored temporarily in option because
// user might not have agreed to TOS yet.
update_option( '_wcpay_onboarding_stripe_connected', [ 'is_existing_stripe_account' => false ] );
Expand Down Expand Up @@ -2570,6 +2599,7 @@ function (): array {
);
}


/**
* Send a Tracks event.
*
Expand Down
92 changes: 92 additions & 0 deletions includes/class-wc-payments-onboarding-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,22 @@ public function create_embedded_kyc_session( array $self_assessment_data, bool $
);
$actioned_notes = self::get_actioned_notes();

/**
* ==================
* Enforces the update of payment methods to 'enabled' based on the capabilities
* provided during the NOX onboarding process.
*
* @see self::update_enabled_payment_methods_ids
* ==================
*/
$capabilities = $this->get_capabilities_from_request();
vladolaru marked this conversation as resolved.
Show resolved Hide resolved
$gateway = WC_Payments::get_gateway();

// Activate enabled Payment Methods IDs.
if ( ! empty( $capabilities ) ) {
$this->update_enabled_payment_methods_ids( $gateway, $capabilities );
}

try {
$account_session = $this->payments_api_client->initialize_onboarding_embedded_kyc(
'live' === $setup_mode,
Expand Down Expand Up @@ -1024,4 +1040,80 @@ public function maybe_add_test_drive_settings_to_new_account_request( array $arg

return $args;
}

/**
* Update payment methods to 'enabled' based on the capabilities
* provided during the NOX onboarding process. Merchants can preselect their preferred
* payment methods as part of this flow.
*
* The capabilities are provided in the following format:
*
* [
* 'card' => true,
* 'affirm' => true,
* ...
* ]
*
* @param WC_Payment_Gateway_WCPay $gateway Payment gateway instance.
* @param array $capabilities Provided capabilities.
*/
public function update_enabled_payment_methods_ids( $gateway, $capabilities = [] ): void {
$enabled_gateways = $gateway->get_upe_enabled_payment_method_ids();

$enabled_payment_methods = array_unique(
array_merge(
$enabled_gateways,
$this->exclude_placeholder_payment_methods( $capabilities )
)
);

// Update the gateway option.
$gateway->update_option( 'upe_enabled_payment_method_ids', $enabled_payment_methods );

/**
* Keeps the list of enabled payment method IDs synchronized between the default
* `woocommerce_woocommerce_payments_settings` and duplicates in individual gateway settings.
*/
foreach ( $enabled_payment_methods as $payment_method_id ) {
vladolaru marked this conversation as resolved.
Show resolved Hide resolved
$payment_gateway = WC_Payments::get_payment_gateway_by_id( $payment_method_id );
if ( $payment_gateway ) {
$payment_gateway->enable();
$payment_gateway->update_option( 'upe_enabled_payment_method_ids', $enabled_payment_methods );
}
}

// If WooPay is enabled, update the gateway option.
if ( ! empty( $capabilities['woopay'] ) ) {
$gateway->update_is_woopay_enabled( true );
}

// If Apple Pay and Google Pay are disabled update the gateway option,
// otherwise they are enabled by default.
if ( empty( $capabilities['apple_google'] ) ) {
$gateway->update_option( 'payment_request', 'no' );
}
}

/**
* Excludes placeholder payment methods and removes duplicates.
*
* WooPay and Apple Pay & Google Pay are considered placeholder payment methods and are excluded.
*
* @param array $payment_methods Array of payment methods to process.
*
* @return array Filtered array of unique payment methods.
*/
private function exclude_placeholder_payment_methods( array $payment_methods ): array {
// Placeholder payment methods.
$excluded_methods = [ 'woopay', 'apple_google' ];

return array_filter(
array_unique(
array_keys( array_filter( $payment_methods ) )
),
function ( $payment_method ) use ( $excluded_methods ) {
return ! in_array( $payment_method, $excluded_methods, true );
}
);
}
}
Loading