mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 12:25:15 +08:00
👔 Wire up JS onboarding-flags with PHP back-end
This commit is contained in:
parent
57c2287739
commit
84dd8a3dec
2 changed files with 39 additions and 10 deletions
|
@ -82,6 +82,16 @@ class LoginLinkRestEndpoint extends RestEndpoint {
|
|||
return array_map( 'sanitize_text_field', $products );
|
||||
},
|
||||
),
|
||||
'options' => array(
|
||||
'requires' => false,
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'bool',
|
||||
),
|
||||
'sanitize_callback' => function ( $flags ) {
|
||||
return array_map( array( $this, 'to_boolean' ), $flags );
|
||||
},
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
@ -97,9 +107,10 @@ class LoginLinkRestEndpoint extends RestEndpoint {
|
|||
public function get_login_url( WP_REST_Request $request ) : WP_REST_Response {
|
||||
$use_sandbox = $request->get_param( 'useSandbox' );
|
||||
$products = $request->get_param( 'products' );
|
||||
$flags = (array) $request->get_param( 'options' );
|
||||
|
||||
try {
|
||||
$url = $this->url_generator->generate( $products, $use_sandbox );
|
||||
$url = $this->url_generator->generate( $products, $flags, $use_sandbox );
|
||||
|
||||
return $this->return_success( $url );
|
||||
} catch ( \Exception $e ) {
|
||||
|
|
|
@ -82,12 +82,13 @@ class ConnectionUrlGenerator {
|
|||
*
|
||||
* @param array $products An array of product identifiers to include in the sign-up process.
|
||||
* These determine the PayPal onboarding experience.
|
||||
* @param array $flags Onboarding choices that will customize the ISU payload.
|
||||
* @param bool $use_sandbox Whether to generate a sandbox URL.
|
||||
*
|
||||
* @return string The generated PayPal onboarding URL.
|
||||
*/
|
||||
public function generate( array $products = array(), bool $use_sandbox = false ) : string {
|
||||
$cache_key = $this->cache_key( $products, $use_sandbox );
|
||||
public function generate( array $products = array(), array $flags = array(), bool $use_sandbox = false ) : string {
|
||||
$cache_key = $this->cache_key( $products, $flags, $use_sandbox );
|
||||
$user_id = get_current_user_id();
|
||||
$onboarding_url = $this->url_manager->get( $cache_key, $user_id );
|
||||
$cached_url = $this->try_get_from_cache( $onboarding_url, $cache_key );
|
||||
|
@ -100,7 +101,7 @@ class ConnectionUrlGenerator {
|
|||
|
||||
$this->logger->info( 'Generating onboarding URL for: ' . $cache_key );
|
||||
|
||||
$url = $this->generate_new_url( $use_sandbox, $products, $onboarding_url, $cache_key );
|
||||
$url = $this->generate_new_url( $use_sandbox, $products, $flags, $onboarding_url, $cache_key );
|
||||
|
||||
if ( $url ) {
|
||||
$this->persist_url( $onboarding_url, $url );
|
||||
|
@ -112,18 +113,28 @@ class ConnectionUrlGenerator {
|
|||
/**
|
||||
* Generates a cache key from the environment and sorted product array.
|
||||
*
|
||||
* Q: Why do we cache the connection URL?
|
||||
* A: The URL is generated by a partner-referrals API, i.e. it requires a
|
||||
* remote request; caching the response avoids unnecessary API calls.
|
||||
*
|
||||
* @param array $products Product identifiers that are part of the cache key.
|
||||
* @param array $flags Onboarding flags.
|
||||
* @param bool $for_sandbox Whether the cache contains a sandbox URL.
|
||||
*
|
||||
* @return string The cache key, defining the product list and environment.
|
||||
*/
|
||||
protected function cache_key( array $products, bool $for_sandbox ) : string {
|
||||
protected function cache_key( array $products, array $flags, bool $for_sandbox ) : string {
|
||||
$environment = $for_sandbox ? 'sandbox' : 'production';
|
||||
|
||||
// Sort products alphabetically, to improve cache implementation.
|
||||
sort( $products );
|
||||
|
||||
return $environment . '-' . implode( '-', $products );
|
||||
// Extract the names of active flags.
|
||||
$active_flags = array_keys( array_filter( $flags ) );
|
||||
|
||||
return strtolower(
|
||||
$environment . '-' . implode( '-', $products ) . '-' . implode( '-', $active_flags )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -162,12 +173,13 @@ class ConnectionUrlGenerator {
|
|||
*
|
||||
* @param bool $for_sandbox Whether to generate a sandbox URL.
|
||||
* @param array $products The products array.
|
||||
* @param array $flags Onboarding flags.
|
||||
* @param OnboardingUrl $onboarding_url The OnboardingUrl object.
|
||||
* @param string $cache_key The cache key.
|
||||
*
|
||||
* @return string The generated URL or an empty string on failure.
|
||||
*/
|
||||
protected function generate_new_url( bool $for_sandbox, array $products, OnboardingUrl $onboarding_url, string $cache_key ) : string {
|
||||
protected function generate_new_url( bool $for_sandbox, array $products, array $flags, OnboardingUrl $onboarding_url, string $cache_key ) : string {
|
||||
$query_args = array( 'displayMode' => 'minibrowser' );
|
||||
$onboarding_url->init();
|
||||
|
||||
|
@ -179,7 +191,7 @@ class ConnectionUrlGenerator {
|
|||
return '';
|
||||
}
|
||||
|
||||
$data = $this->prepare_referral_data( $products, $onboarding_token );
|
||||
$data = $this->prepare_referral_data( $products, $flags, $onboarding_token );
|
||||
|
||||
try {
|
||||
$referral = $this->partner_referrals->get_value( $for_sandbox );
|
||||
|
@ -197,12 +209,18 @@ class ConnectionUrlGenerator {
|
|||
* Prepares the referral data.
|
||||
*
|
||||
* @param array $products The products array.
|
||||
* @param array $flags Onboarding flags.
|
||||
* @param string $onboarding_token The onboarding token.
|
||||
*
|
||||
* @return array The prepared referral data.
|
||||
*/
|
||||
protected function prepare_referral_data( array $products, string $onboarding_token ) : array {
|
||||
return $this->referrals_data->data( $products, $onboarding_token );
|
||||
protected function prepare_referral_data( array $products, array $flags, string $onboarding_token ) : array {
|
||||
return $this->referrals_data->data(
|
||||
$products,
|
||||
$onboarding_token,
|
||||
(bool) ( $flags['useSubscriptions'] ?? false ),
|
||||
(bool) ( $flags['useCardPayments'] ?? false )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue