add reset possibility

This commit is contained in:
David Remer 2020-06-30 08:35:28 +03:00
parent a1148df786
commit 71501e85d2
8 changed files with 168 additions and 10 deletions

View file

@ -9,6 +9,7 @@ use Inpsyde\PayPalCommerce\Onboarding\State;
use Inpsyde\PayPalCommerce\WcGateway\Admin\OrderTablePaymentStatusColumn;
use Inpsyde\PayPalCommerce\WcGateway\Admin\PaymentStatusOrderDetail;
use Inpsyde\PayPalCommerce\WcGateway\Checkout\DisableGateways;
use Inpsyde\PayPalCommerce\WcGateway\Gateway\ResetGateway;
use Inpsyde\PayPalCommerce\WcGateway\Gateway\WcGateway;
use Inpsyde\PayPalCommerce\WcGateway\Gateway\WcGatewayBase;
use Inpsyde\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
@ -25,18 +26,26 @@ return [
'wcgateway.gateway.base' => static function (ContainerInterface $container): WcGatewayBase {
return new WcGatewayBase();
},
'wcgateway.gateway.reset' => static function (ContainerInterface $container): ResetGateway {
return new ResetGateway(
$container->get('wcgateway.settings')
);
},
'wcgateway.gateway' => static function (ContainerInterface $container): WcGateway {
$orderProcessor = $container->get('wcgateway.order-processor');
$settingsFields = $container->get('wcgateway.settings.fields');
$authorizedPayments = $container->get('wcgateway.processor.authorized-payments');
$notice = $container->get('wcgateway.notice.authorize-order-action');
$onboardingRender = $container->get('onboarding.render');
$reset = $container->get('wcgateway.gateway.reset');
return new WcGateway(
$settingsFields,
$orderProcessor,
$authorizedPayments,
$notice,
$onboardingRender
$onboardingRender,
$reset
);
},
'wcgateway.disabler' => static function (ContainerInterface $container): DisableGateways {

View file

@ -0,0 +1,92 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Gateway;
use Inpsyde\PayPalCommerce\AdminNotices\Entity\Message;
use Inpsyde\PayPalCommerce\AdminNotices\Repository\Repository;
use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
class ResetGateway
{
private const NONCE = 'ppcp-reset';
private $settings;
public function __construct(Settings $settings)
{
$this->settings = $settings;
}
public function listen() : bool {
if (isset($_GET['ppcp-reset'])) {
return $this->reset();
}
if (isset($_GET['ppcp-resetted'])) {
return $this->resetted();
}
return false;
}
private function resetted() : bool {
add_filter(
Repository::NOTICES_FILTER,
function(array $notices) : array {
$notices[] = new Message(
__('Your PayPal settings have been resetted.', 'woocommerce-paypal-commerce-gateway'),
'success'
);
return $notices;
}
);
return true;
}
private function reset() : bool {
if (
! isset($_GET['nonce'] )
|| ! wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['nonce'])), self::NONCE)
|| ! current_user_can('manage_options')
) {
return false;
}
$this->settings->reset();
$url = remove_query_arg([
'nonce',
'ppcp-reset',
]);
$url = add_query_arg([
'ppcp-resetted' => 1,
],
$url
);
wp_redirect($url, 302);
exit;
}
public function render() {
$url = add_query_arg([
'ppcp-reset' => 1,
'nonce' => wp_create_nonce(self::NONCE),
]);
?>
<tr valign="top">
<th scope="row" class="titledesc">
</th>
<td class="forminp">
<a
class="button"
href="<?php echo esc_url($url); ?>"
><?php
esc_html_e('Reset', 'woocommerce-paypal-commerce-gateway');
?></a>
</td>
</tr>
<?php
}
}

View file

@ -26,19 +26,20 @@ class WcGateway extends WcGatewayBase
public const INTENT_META_KEY = '_ppcp_paypal_intent';
public const ORDER_ID_META_KEY = '_ppcp_paypal_order_id';
private $isSandbox = true;
private $settingsFields;
private $authorizedPayments;
private $notice;
private $orderProcessor;
private $onboardingRenderer;
private $resetGateway;
public function __construct(
SettingsFields $settingsFields,
OrderProcessor $orderProcessor,
AuthorizedPaymentsProcessor $authorizedPayments,
AuthorizeOrderActionNotice $notice,
OnboardingRenderer $onboardingRenderer
OnboardingRenderer $onboardingRenderer,
ResetGateway $resetGateway
) {
$this->orderProcessor = $orderProcessor;
@ -46,6 +47,7 @@ class WcGateway extends WcGatewayBase
$this->notice = $notice;
$this->settingsFields = $settingsFields;
$this->onboardingRenderer = $onboardingRenderer;
$this->resetGateway = $resetGateway;
$this->method_title = __('PayPal Payments', 'woocommerce-paypal-gateway');
$this->method_description = __(
@ -138,9 +140,23 @@ class WcGateway extends WcGatewayBase
$this->notice->displayMessage($displayMessage);
}
public function generate_ppcp_onboarding_html($a, $b)
public function generate_ppcp_onboarding_html() : string
{
ob_start();
$this->onboardingRenderer->render();
$content = ob_get_contents();
ob_end_clean();
return $content;
}
public function generate_ppcp_reset_html() : string
{
ob_start();
$this->resetGateway->render();
$content = ob_get_contents();
ob_end_clean();
return $content;
}
}

View file

@ -12,6 +12,11 @@ class FullyOnboardedSettings extends StartSettings implements SettingsFields
$this->gateway(),
$this->buttons(),
$this->creditCards(),
[
[
'type' => 'ppcp_reset',
],
]
);
}

View file

@ -9,10 +9,18 @@ class ProgressiveSettings extends StartSettings implements SettingsFields
public function fields(): array
{
$fields = parent::fields();
$fields[] = [
'type' => 'ppcp_onboarding',
];
return $fields;
return array_merge(
[
'onboarding' => [
'type' => 'ppcp_onboarding',
],
],
parent::fields(),
[
'reset' => [
'type' => 'ppcp_reset',
],
]
);
}
}

View file

@ -12,7 +12,7 @@ class Settings implements ContainerInterface
{
private $gateway;
public function __construct(WcGatewayInterface $gateway)
public function __construct(\WC_Payment_Gateway $gateway)
{
$this->gateway = $gateway;
}
@ -31,4 +31,23 @@ class Settings implements ContainerInterface
{
return !!$this->gateway->get_option($id);
}
public function reset() : bool
{
$fieldsToReset = [
'enabled',
'intent',
'client_id',
'client_secret',
'merchant_email',
];
foreach ($fieldsToReset as $key) {
$this->gateway->update_option($key, '');
}
return true;
}
}

View file

@ -9,4 +9,5 @@ namespace Inpsyde\PayPalCommerce\WcGateway\Settings;
interface SettingsFields
{
public function fields(): array;
}

View file

@ -48,6 +48,14 @@ class WcGatewayModule implements ModuleInterface
}
);
add_action(
'admin_init',
function() use ($container) {
$resetGateway = $container->get('wcgateway.gateway.reset');
$resetGateway->listen();
}
);
add_filter(
Repository::NOTICES_FILTER,
static function ($notices) use ($container): array {