From 84dd8a3dec074235627aebb43145a01097fa85bd Mon Sep 17 00:00:00 2001
From: Philipp Stracker
Date: Wed, 5 Mar 2025 16:57:51 +0100
Subject: [PATCH] =?UTF-8?q?=F0=9F=91=94=20Wire=20up=20JS=20onboarding-flag?=
=?UTF-8?q?s=20with=20PHP=20back-end?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../src/Endpoint/LoginLinkRestEndpoint.php | 13 ++++++-
.../src/Service/ConnectionUrlGenerator.php | 36 ++++++++++++++-----
2 files changed, 39 insertions(+), 10 deletions(-)
diff --git a/modules/ppcp-settings/src/Endpoint/LoginLinkRestEndpoint.php b/modules/ppcp-settings/src/Endpoint/LoginLinkRestEndpoint.php
index b9972349f..3d6c45843 100644
--- a/modules/ppcp-settings/src/Endpoint/LoginLinkRestEndpoint.php
+++ b/modules/ppcp-settings/src/Endpoint/LoginLinkRestEndpoint.php
@@ -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 ) {
diff --git a/modules/ppcp-settings/src/Service/ConnectionUrlGenerator.php b/modules/ppcp-settings/src/Service/ConnectionUrlGenerator.php
index 1d9084912..c3204ccf1 100644
--- a/modules/ppcp-settings/src/Service/ConnectionUrlGenerator.php
+++ b/modules/ppcp-settings/src/Service/ConnectionUrlGenerator.php
@@ -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 )
+ );
}
/**