Refactor onboarding link update endpoint refactor.

This commit is contained in:
Pedro Silva 2023-09-26 13:58:33 +01:00
parent 26a2b80b1b
commit 43136ecf3c
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
10 changed files with 168 additions and 85 deletions

View file

@ -10,6 +10,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Onboarding\Assets;
use WooCommerce\PayPalCommerce\Onboarding\Endpoint\LoginSellerEndpoint;
use WooCommerce\PayPalCommerce\Onboarding\Endpoint\UpdateSignupLinksEndpoint;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
@ -155,8 +156,8 @@ class OnboardingAssets {
'error_messages' => array(
'no_credentials' => __( 'API credentials must be entered to save the settings.', 'woocommerce-paypal-payments' ),
),
'pui_endpoint' => \WC_AJAX::get_endpoint( 'ppc-pui' ),
'pui_nonce' => wp_create_nonce( 'ppc-pui' ),
'pui_endpoint' => \WC_AJAX::get_endpoint( UpdateSignupLinksEndpoint::ENDPOINT ),
'pui_nonce' => wp_create_nonce( UpdateSignupLinksEndpoint::ENDPOINT ),
);
}

View file

@ -14,14 +14,17 @@ use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\Button\Endpoint\EndpointInterface;
use WooCommerce\PayPalCommerce\Button\Endpoint\RequestData;
use WooCommerce\PayPalCommerce\Onboarding\Helper\OnboardingUrl;
use WooCommerce\PayPalCommerce\Onboarding\Render\OnboardingRenderer;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
/**
* Class PayUponInvoiceEndpoint
* Class UpdateSignupLinksEndpoint
*/
class PayUponInvoiceEndpoint implements EndpointInterface {
class UpdateSignupLinksEndpoint implements EndpointInterface {
const ENDPOINT = 'ppc-update-signup-links';
/**
* The settings.
@ -66,7 +69,7 @@ class PayUponInvoiceEndpoint implements EndpointInterface {
protected $logger;
/**
* PayUponInvoiceEndpoint constructor.
* UpdateSignupLinksEndpoint constructor.
*
* @param Settings $settings The settings.
* @param RequestData $request_data The request data.
@ -97,7 +100,7 @@ class PayUponInvoiceEndpoint implements EndpointInterface {
* @return string
*/
public static function nonce(): string {
return 'ppc-pui';
return self::ENDPOINT;
}
/**
@ -116,13 +119,23 @@ class PayUponInvoiceEndpoint implements EndpointInterface {
try {
$data = $this->request_data->read_request( $this->nonce() );
$this->settings->set( 'ppcp-onboarding-pui', $data['checked'] );
foreach ( $data['settings'] ?? array() as $field => $value ) {
$option = apply_filters( 'ppcp_partner_referrals_option', array(
'field' => $field,
'value' => $value,
'valid' => false,
) );
if ( $option['valid'] ) {
$this->settings->set( $field, $value );
}
}
$this->settings->persist();
foreach ( $this->signup_link_ids as $key ) {
if ( $this->signup_link_cache->has( $key ) ) {
$this->signup_link_cache->delete( $key );
}
( new OnboardingUrl( $this->signup_link_cache, $key, get_current_user_id() ) )->delete();
}
foreach ( $this->signup_link_ids as $key ) {

View file

@ -9,6 +9,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Onboarding;
use WooCommerce\PayPalCommerce\Onboarding\Endpoint\UpdateSignupLinksEndpoint;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
use WooCommerce\PayPalCommerce\Onboarding\Assets\OnboardingAssets;
@ -96,7 +97,7 @@ class OnboardingModule implements ModuleInterface {
);
add_action(
'wc_ajax_ppc-pui',
'wc_ajax_' . UpdateSignupLinksEndpoint::ENDPOINT,
static function () use ( $c ) {
$endpoint = $c->get( 'onboarding.endpoint.pui' );
$endpoint->handle_request();

View file

@ -95,7 +95,7 @@ class OnboardingOptionsRenderer {
$checked = '';
}
return '<li><label><input type="checkbox" id="ppcp-onboarding-pui" ' . $checked . '> ' .
return '<li><label><input type="checkbox" id="ppcp-onboarding-pui" ' . $checked . ' data-onboarding-option="ppcp-onboarding-pui"> ' .
__( 'Onboard with Pay upon Invoice', 'woocommerce-paypal-payments' ) . '
</label></li>';
}