woocommerce-paypal-payments/modules/ppcp-status-report/src/StatusReportModule.php

198 lines
6.6 KiB
PHP
Raw Normal View History

<?php
/**
* The status report module.
*
* @package WooCommerce\PayPalCommerce\StatusReport
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\StatusReport;
use Dhii\Container\ServiceProvider;
use Dhii\Modular\Module\ModuleInterface;
use Interop\Container\ServiceProviderInterface;
use Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\PayPalBearer;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Helper\CurrencySupport;
use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies;
use WooCommerce\PayPalCommerce\Button\Helper\MessagesApply;
2021-11-22 18:21:01 +02:00
use WooCommerce\PayPalCommerce\Compat\PPEC\PPECHelper;
use WooCommerce\PayPalCommerce\Onboarding\State;
2021-11-24 11:52:14 +02:00
use WooCommerce\PayPalCommerce\Webhooks\WebhookInfoStorage;
/**
* Class StatusReportModule
*/
class StatusReportModule implements ModuleInterface {
/**
* {@inheritDoc}
*/
public function setup(): ServiceProviderInterface {
return new ServiceProvider(
require __DIR__ . '/../services.php',
require __DIR__ . '/../extensions.php'
);
}
/**
* {@inheritDoc}
*
2021-08-30 08:10:43 +02:00
* @param ContainerInterface $c A services container instance.
*/
2021-08-30 08:10:43 +02:00
public function run( ContainerInterface $c ): void {
add_action(
'woocommerce_system_status_report',
2021-08-30 08:10:43 +02:00
function () use ( $c ) {
2021-11-19 17:25:47 +02:00
$settings = $c->get( 'wcgateway.settings' );
assert( $settings instanceof ContainerInterface );
/* @var State $state The state. */
2021-08-30 08:10:43 +02:00
$state = $c->get( 'onboarding.state' );
/* @var Bearer $bearer The bearer. */
2021-08-30 08:10:43 +02:00
$bearer = $c->get( 'api.bearer' );
$currency_support = $c->get( 'api.helpers.currency-support' );
assert( $currency_support instanceof CurrencySupport );
/* @var DccApplies $dcc_applies The ddc applies. */
2021-08-30 08:10:43 +02:00
$dcc_applies = $c->get( 'api.helpers.dccapplies' );
/* @var MessagesApply $messages_apply The messages apply. */
2021-08-30 08:10:43 +02:00
$messages_apply = $c->get( 'button.helper.messages-apply' );
2021-11-24 11:52:14 +02:00
$last_webhook_storage = $c->get( 'webhook.last-webhook-storage' );
assert( $last_webhook_storage instanceof WebhookInfoStorage );
/* @var Renderer $renderer The renderer. */
2021-08-30 08:10:43 +02:00
$renderer = $c->get( 'status-report.renderer' );
2021-11-22 18:21:01 +02:00
$had_ppec_plugin = PPECHelper::is_plugin_configured();
$items = array(
array(
2021-07-28 12:22:30 +02:00
'label' => esc_html__( 'Onboarded', 'woocommerce-paypal-payments' ),
'description' => esc_html__( 'Whether PayPal account is correctly configured or not.', 'woocommerce-paypal-payments' ),
2021-11-19 17:25:08 +02:00
'value' => $this->bool_to_text(
$this->onboarded( $bearer, $state )
),
),
array(
2021-07-28 12:22:30 +02:00
'label' => esc_html__( 'Shop country code', 'woocommerce-paypal-payments' ),
'description' => esc_html__( 'Country / State value on Settings / General / Store Address.', 'woocommerce-paypal-payments' ),
'value' => wc_get_base_location()['country'],
),
array(
'label' => esc_html__( 'WooCommerce currency supported', 'woocommerce-paypal-payments' ),
'description' => esc_html__( 'Whether PayPal supports the default store currency or not.', 'woocommerce-paypal-payments' ),
'value' => $this->bool_to_text(
$currency_support->supports_wc_currency()
),
),
array(
2021-07-28 12:22:30 +02:00
'label' => esc_html__( 'PayPal card processing available in country', 'woocommerce-paypal-payments' ),
'description' => esc_html__( 'Whether PayPal card processing is available in country or not.', 'woocommerce-paypal-payments' ),
2021-11-19 17:25:08 +02:00
'value' => $this->bool_to_text(
$dcc_applies->for_country_currency()
),
),
array(
2021-07-28 12:22:30 +02:00
'label' => esc_html__( 'Pay Later messaging available in country', 'woocommerce-paypal-payments' ),
'description' => esc_html__( 'Whether Pay Later is available in country or not.', 'woocommerce-paypal-payments' ),
2021-11-19 17:25:08 +02:00
'value' => $this->bool_to_text(
$messages_apply->for_country()
),
),
2021-11-24 11:52:14 +02:00
array(
'label' => esc_html__( 'Webhook status', 'woocommerce-paypal-payments' ),
'description' => esc_html__( 'Whether we received webhooks successfully.', 'woocommerce-paypal-payments' ),
'value' => $last_webhook_storage->is_empty() ? 'Unknown' : 'OK',
),
array(
2021-07-28 12:22:30 +02:00
'label' => esc_html__( 'Vault enabled', 'woocommerce-paypal-payments' ),
2021-07-28 12:26:25 +02:00
'description' => esc_html__( 'Whether vaulting is enabled on PayPal account or not.', 'woocommerce-paypal-payments' ),
2021-11-19 17:25:08 +02:00
'value' => $this->bool_to_text(
$this->vault_enabled( $bearer )
),
),
2021-11-19 17:25:47 +02:00
array(
'label' => esc_html__( 'Logging enabled', 'woocommerce-paypal-payments' ),
'description' => esc_html__( 'Whether logging of plugin events and errors is enabled.', 'woocommerce-paypal-payments' ),
'value' => $this->bool_to_text(
$settings->has( 'logging_enabled' ) && $settings->get( 'logging_enabled' )
),
),
2021-11-22 18:21:01 +02:00
array(
'label' => esc_html__( 'Used PayPal Checkout plugin', 'woocommerce-paypal-payments' ),
'description' => esc_html__( 'Whether the PayPal Checkout Gateway plugin was configured previously or not', 'woocommerce-paypal-payments' ),
'value' => $this->bool_to_text(
$had_ppec_plugin
),
),
);
2021-07-22 15:39:57 +02:00
echo wp_kses_post(
$renderer->render(
esc_html__( 'WooCommerce PayPal Payments', 'woocommerce-paypal-payments' ),
$items
)
);
}
);
}
/**
* {@inheritDoc}
*/
2021-07-22 15:39:57 +02:00
public function getKey() { }
/**
* It returns the current onboarding status.
2021-07-22 15:39:57 +02:00
*
* @param Bearer $bearer The bearer.
2021-07-27 14:29:05 +02:00
* @param State $state The state.
2021-11-19 17:25:08 +02:00
* @return bool
2021-07-22 15:39:57 +02:00
*/
2021-11-19 17:25:08 +02:00
private function onboarded( Bearer $bearer, State $state ): bool {
try {
$token = $bearer->bearer();
} catch ( RuntimeException $exception ) {
2021-11-19 17:25:08 +02:00
return false;
}
2021-07-22 15:39:57 +02:00
$current_state = $state->current_state();
2021-11-19 17:25:08 +02:00
return $token->is_valid() && $current_state === $state::STATE_ONBOARDED;
2021-07-22 15:39:57 +02:00
}
/**
* It returns whether vaulting is enabled or not.
*
* @param Bearer $bearer The bearer.
2021-11-19 17:25:08 +02:00
* @return bool
2021-07-22 15:39:57 +02:00
*/
2021-11-19 17:25:08 +02:00
private function vault_enabled( Bearer $bearer ): bool {
2021-07-22 15:39:57 +02:00
try {
$token = $bearer->bearer();
2021-11-19 17:25:08 +02:00
return $token->vaulting_available();
2021-07-22 15:39:57 +02:00
} catch ( RuntimeException $exception ) {
2021-11-19 17:25:08 +02:00
return false;
2021-07-22 15:39:57 +02:00
}
}
2021-11-19 17:25:08 +02:00
/**
* Converts the bool value to "Yes" or "No".
*
* @param bool $value The value.
* @return string
*/
private function bool_to_text( bool $value ): string {
return $value
? esc_html__( 'Yes', 'woocommerce-paypal-payments' )
: esc_html__( 'No', 'woocommerce-paypal-payments' );
}
}