mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
call partners endpoint when rendering the dcc settings to check whether the pp account is able to process dcc.
In order to do so, it was also necessary to store the merchant id during onboarding. includes a bugfix where the cache invalidation did not work because the cache object was constructed with the wrong prefix key.
This commit is contained in:
parent
22c7e3975f
commit
c09e314baa
14 changed files with 638 additions and 29 deletions
|
@ -20,6 +20,7 @@ use WooCommerce\PayPalCommerce\WcGateway\Checkout\DisableGateways;
|
|||
use WooCommerce\PayPalCommerce\WcGateway\Endpoint\ReturnUrlEndpoint;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||
use Woocommerce\PayPalCommerce\WcGateway\Helper\DccProductStatus;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Notice\ConnectAdminNotice;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
|
||||
|
@ -92,12 +93,14 @@ return array(
|
|||
$fields = $container->get( 'wcgateway.settings.fields' );
|
||||
$dcc_applies = $container->get( 'api.helpers.dccapplies' );
|
||||
$messages_apply = $container->get( 'button.helper.messages-apply' );
|
||||
$dcc_product_status = $container->get( 'wcgateway.helper.dcc-product-status' );
|
||||
return new SettingsRenderer(
|
||||
$settings,
|
||||
$state,
|
||||
$fields,
|
||||
$dcc_applies,
|
||||
$messages_apply
|
||||
$messages_apply,
|
||||
$dcc_product_status
|
||||
);
|
||||
},
|
||||
'wcgateway.settings.listener' => static function ( $container ): SettingsListener {
|
||||
|
@ -105,7 +108,7 @@ return array(
|
|||
$fields = $container->get( 'wcgateway.settings.fields' );
|
||||
$webhook_registrar = $container->get( 'webhook.registrar' );
|
||||
$state = $container->get( 'onboarding.state' );
|
||||
$cache = new Cache( 'ppcp-token' );
|
||||
$cache = new Cache( 'ppcp-paypal-bearer' );
|
||||
return new SettingsListener( $settings, $fields, $webhook_registrar, $cache, $state );
|
||||
},
|
||||
'wcgateway.order-processor' => static function ( $container ): OrderProcessor {
|
||||
|
@ -242,6 +245,20 @@ return array(
|
|||
'requirements' => array(),
|
||||
'gateway' => 'paypal',
|
||||
),
|
||||
'merchant_id' => array(
|
||||
'title' => __( 'Merchant Id', 'paypal-payments-for-woocommerce' ),
|
||||
'type' => 'ppcp-text-input',
|
||||
'desc_tip' => true,
|
||||
'description' => __( 'The merchant id of your account ', 'paypal-payments-for-woocommerce' ),
|
||||
'default' => false,
|
||||
'screens' => array(
|
||||
State::STATE_START,
|
||||
State::STATE_PROGRESSIVE,
|
||||
State::STATE_ONBOARDED,
|
||||
),
|
||||
'requirements' => array(),
|
||||
'gateway' => 'paypal',
|
||||
),
|
||||
'client_id' => array(
|
||||
'title' => __( 'Client Id', 'paypal-payments-for-woocommerce' ),
|
||||
'type' => 'ppcp-text-input',
|
||||
|
@ -1589,4 +1606,10 @@ return array(
|
|||
$prefix
|
||||
);
|
||||
},
|
||||
'wcgateway.helper.dcc-product-status' => static function ( $container ) : DccProductStatus {
|
||||
|
||||
$settings = $container->get( 'wcgateway.settings' );
|
||||
$partner_endpoint = $container->get( 'api.endpoint.partners' );
|
||||
return new DccProductStatus( $settings, $partner_endpoint );
|
||||
},
|
||||
);
|
||||
|
|
102
modules/ppcp-wc-gateway/src/Helper/class-dccproductstatus.php
Normal file
102
modules/ppcp-wc-gateway/src/Helper/class-dccproductstatus.php
Normal file
|
@ -0,0 +1,102 @@
|
|||
<?php
|
||||
/**
|
||||
* Manage the Seller status.
|
||||
*
|
||||
* @package Woocommerce\PayPalCommerce\WcGateway\Helper
|
||||
*/
|
||||
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace Woocommerce\PayPalCommerce\WcGateway\Helper;
|
||||
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnersEndpoint;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatusProduct;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
|
||||
/**
|
||||
* Class DccProductStatus
|
||||
*/
|
||||
class DccProductStatus {
|
||||
|
||||
/**
|
||||
* Caches the status for the current load.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
private $current_status_cache;
|
||||
/**
|
||||
* The settings.
|
||||
*
|
||||
* @var Settings
|
||||
*/
|
||||
private $settings;
|
||||
|
||||
/**
|
||||
* The partners endpoint.
|
||||
*
|
||||
* @var PartnersEndpoint
|
||||
*/
|
||||
private $partners_endpoint;
|
||||
|
||||
/**
|
||||
* DccProductStatus constructor.
|
||||
*
|
||||
* @param Settings $settings The Settings.
|
||||
* @param PartnersEndpoint $partners_endpoint The Partner Endpoint.
|
||||
*/
|
||||
public function __construct(
|
||||
Settings $settings,
|
||||
PartnersEndpoint $partners_endpoint
|
||||
) {
|
||||
$this->settings = $settings;
|
||||
$this->partners_endpoint = $partners_endpoint;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the active/subscribed products support DCC.
|
||||
*
|
||||
* @return bool
|
||||
* @throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException Should a setting not be found.
|
||||
*/
|
||||
public function dcc_is_active() : bool {
|
||||
if ( is_bool( $this->current_status_cache ) ) {
|
||||
return $this->current_status_cache;
|
||||
}
|
||||
if ( $this->settings->has( 'products_dcc_enabled' ) && $this->settings->get( 'products_dcc_enabled' ) ) {
|
||||
$this->current_status_cache = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
$seller_status = $this->partners_endpoint->seller_status();
|
||||
} catch ( RuntimeException $error ) {
|
||||
$this->current_status_cache = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ( $seller_status->products() as $product ) {
|
||||
if ( ! in_array(
|
||||
$product->vetting_status(),
|
||||
array(
|
||||
SellerStatusProduct::VETTING_STATUS_APPROVED,
|
||||
SellerStatusProduct::VETTING_STATUS_SUBSCRIBED,
|
||||
),
|
||||
true
|
||||
)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( in_array( 'CUSTOM_CARD_PROCESSING', $product->capabilities(), true ) ) {
|
||||
$this->settings->set( 'products_dcc_enabled', true );
|
||||
$this->settings->persist();
|
||||
$this->current_status_cache = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
$this->current_status_cache = false;
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -83,6 +83,29 @@ class SettingsListener {
|
|||
$this->state = $state;
|
||||
}
|
||||
|
||||
/**
|
||||
* Listens if the merchant ID should be updated.
|
||||
*/
|
||||
public function listen_for_merchant_id() {
|
||||
|
||||
if ( ! $this->is_valid_site_request() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* No nonce provided.
|
||||
* phpcs:disable WordPress.Security.NonceVerification.Missing
|
||||
* phpcs:disable WordPress.Security.NonceVerification.Recommended
|
||||
*/
|
||||
if ( isset( $_GET['merchantIdInPayPal'] ) ) {
|
||||
$this->settings->set( 'merchant_id', sanitize_text_field( wp_unslash( $_GET['merchantIdInPayPal'] ) ) );
|
||||
$this->settings->persist();
|
||||
}
|
||||
// phpcs:enable WordPress.Security.NonceVerification.Missing
|
||||
// phpcs:enable WordPress.Security.NonceVerification.Recommended
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Listens to the request.
|
||||
*
|
||||
|
@ -246,18 +269,7 @@ class SettingsListener {
|
|||
*/
|
||||
private function is_valid_update_request(): bool {
|
||||
|
||||
if (
|
||||
! isset( $_REQUEST['section'] )
|
||||
|| ! in_array(
|
||||
sanitize_text_field( wp_unslash( $_REQUEST['section'] ) ),
|
||||
array( 'ppcp-gateway', 'ppcp-credit-card-gateway' ),
|
||||
true
|
||||
)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
if ( ! $this->is_valid_site_request() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -272,4 +284,37 @@ class SettingsListener {
|
|||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether we are on the settings page and are allowed to be here.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function is_valid_site_request() : bool {
|
||||
|
||||
/**
|
||||
* No nonce needed at this point.
|
||||
*
|
||||
* phpcs:disable WordPress.Security.NonceVerification.Missing
|
||||
* phpcs:disable WordPress.Security.NonceVerification.Recommended
|
||||
*/
|
||||
if (
|
||||
! isset( $_REQUEST['section'] )
|
||||
|| ! in_array(
|
||||
sanitize_text_field( wp_unslash( $_REQUEST['section'] ) ),
|
||||
array( 'ppcp-gateway', 'ppcp-credit-card-gateway' ),
|
||||
true
|
||||
)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// phpcs:enable WordPress.Security.NonceVerification.Missing
|
||||
// phpcs:enable WordPress.Security.NonceVerification.Recommended
|
||||
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ use WooCommerce\PayPalCommerce\Button\Helper\MessagesApply;
|
|||
use WooCommerce\PayPalCommerce\Onboarding\State;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Woocommerce\PayPalCommerce\WcGateway\Helper\DccProductStatus;
|
||||
|
||||
/**
|
||||
* Class SettingsRenderer
|
||||
|
@ -55,28 +56,38 @@ class SettingsRenderer {
|
|||
*/
|
||||
private $messages_apply;
|
||||
|
||||
/**
|
||||
* The DCC Product Status.
|
||||
*
|
||||
* @var DccProductStatus
|
||||
*/
|
||||
private $dcc_product_status;
|
||||
|
||||
/**
|
||||
* SettingsRenderer constructor.
|
||||
*
|
||||
* @param ContainerInterface $settings The Settings.
|
||||
* @param State $state The current state.
|
||||
* @param array $fields The setting fields.
|
||||
* @param DccApplies $dcc_applies Whether DCC gateway can be shown.
|
||||
* @param State $state The current state.
|
||||
* @param array $fields The setting fields.
|
||||
* @param DccApplies $dcc_applies Whether DCC gateway can be shown.
|
||||
* @param MessagesApply $messages_apply Whether messages can be shown.
|
||||
* @param DccProductStatus $dcc_product_status The product status.
|
||||
*/
|
||||
public function __construct(
|
||||
ContainerInterface $settings,
|
||||
State $state,
|
||||
array $fields,
|
||||
DccApplies $dcc_applies,
|
||||
MessagesApply $messages_apply
|
||||
MessagesApply $messages_apply,
|
||||
DccProductStatus $dcc_product_status
|
||||
) {
|
||||
|
||||
$this->settings = $settings;
|
||||
$this->state = $state;
|
||||
$this->fields = $fields;
|
||||
$this->dcc_applies = $dcc_applies;
|
||||
$this->messages_apply = $messages_apply;
|
||||
$this->settings = $settings;
|
||||
$this->state = $state;
|
||||
$this->fields = $fields;
|
||||
$this->dcc_applies = $dcc_applies;
|
||||
$this->messages_apply = $messages_apply;
|
||||
$this->dcc_product_status = $dcc_product_status;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -235,6 +246,12 @@ class SettingsRenderer {
|
|||
) {
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
in_array( 'dcc', $config['requirements'], true )
|
||||
&& ! $this->dcc_product_status->dcc_is_active()
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
in_array( 'messages', $config['requirements'], true )
|
||||
&& ! $this->messages_apply->for_country()
|
||||
|
@ -281,9 +298,10 @@ class SettingsRenderer {
|
|||
if ( $this->dcc_applies->for_country_currency() ) {
|
||||
if ( State::STATE_ONBOARDED > $this->state->current_state() ) {
|
||||
$this->render_dcc_onboarding_info();
|
||||
}
|
||||
if ( State::STATE_ONBOARDED === $this->state->current_state() ) {
|
||||
} elseif ( State::STATE_ONBOARDED === $this->state->current_state() && $this->dcc_product_status->dcc_is_active() ) {
|
||||
$this->render_3d_secure_info();
|
||||
} elseif ( ! $this->dcc_product_status->dcc_is_active() ) {
|
||||
$this->render_dcc_not_active_yet();
|
||||
}
|
||||
} else {
|
||||
$this->render_dcc_does_not_apply_info();
|
||||
|
@ -310,6 +328,27 @@ class SettingsRenderer {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the information that the PayPal account can not yet process DCC.
|
||||
*/
|
||||
private function render_dcc_not_active_yet() {
|
||||
?>
|
||||
<tr>
|
||||
<th><?php esc_html_e( 'Onboarding', 'paypal-payments-for-woocommerce' ); ?></th>
|
||||
<td class="notice notice-error">
|
||||
<p>
|
||||
<?php
|
||||
esc_html_e(
|
||||
'Credit Card processing for your account has not yet been activated by PayPal. If your account is new, this can take some days. Otherwise, please get in contact with PayPal.',
|
||||
'paypal-payments-for-woocommerce'
|
||||
);
|
||||
?>
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the 3d secure info text.
|
||||
*/
|
||||
|
|
|
@ -24,6 +24,7 @@ use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
|||
use WooCommerce\PayPalCommerce\WcGateway\Notice\ConnectAdminNotice;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\SectionsRenderer;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\SettingsListener;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\SettingsRenderer;
|
||||
use Interop\Container\ServiceProviderInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
@ -193,9 +194,27 @@ class WcGatewayModule implements ModuleInterface {
|
|||
'woocommerce_settings_save_checkout',
|
||||
static function () use ( $container ) {
|
||||
$listener = $container->get( 'wcgateway.settings.listener' );
|
||||
|
||||
/**
|
||||
* The settings listener.
|
||||
*
|
||||
* @var SettingsListener $listener
|
||||
*/
|
||||
$listener->listen();
|
||||
}
|
||||
);
|
||||
add_action(
|
||||
'admin_init',
|
||||
static function () use ( $container ) {
|
||||
$listener = $container->get( 'wcgateway.settings.listener' );
|
||||
/**
|
||||
* The settings listener.
|
||||
*
|
||||
* @var SettingsListener $listener
|
||||
*/
|
||||
$listener->listen_for_merchant_id();
|
||||
}
|
||||
);
|
||||
|
||||
add_filter(
|
||||
'woocommerce_form_field',
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue