replace wc gateway settings with independent settings system

This commit is contained in:
David Remer 2020-07-02 09:37:07 +03:00
parent 621567da66
commit 4dadd6143f
16 changed files with 514 additions and 535 deletions

View file

@ -20,32 +20,27 @@ use Inpsyde\PayPalCommerce\WcGateway\Settings\FullyOnboardedSettings;
use Inpsyde\PayPalCommerce\WcGateway\Settings\ProgressiveSettings;
use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsFields;
use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsListener;
use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsRenderer;
use Inpsyde\PayPalCommerce\WcGateway\Settings\StartSettings;
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');
$settingsRenderer = $container->get('wcgateway.settings.render');
$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,
$settingsRenderer,
$orderProcessor,
$authorizedPayments,
$notice,
$onboardingRender,
$reset
$onboardingRender
);
},
'wcgateway.disabler' => static function (ContainerInterface $container): DisableGateways {
@ -65,19 +60,16 @@ return [
static function (ContainerInterface $container): AuthorizeOrderActionNotice {
return new AuthorizeOrderActionNotice();
},
'wcgateway.settings.fields' => static function (ContainerInterface $container): SettingsFields {
'wcgateway.settings.render' => static function (ContainerInterface $container): SettingsRenderer {
$settings = $container->get('wcgateway.settings');
$state = $container->get('onboarding.state');
/**
* @var State $state
*/
if ($state->currentState() === State::STATE_START) {
return new StartSettings();
}
$environment = $container->get('onboarding.environment');
if ($state->currentState() === State::STATE_PROGRESSIVE) {
return new ProgressiveSettings($environment);
}
return new FullyOnboardedSettings($environment);
$fields = $container->get('wcgateway.settings.fields');
return new SettingsRenderer($settings, $state, $fields);
},
'wcgateway.settings.listener' => static function (ContainerInterface $container): SettingsListener {
$settings = $container->get('wcgateway.settings');
$fields = $container->get('wcgateway.settings.fields');
return new SettingsListener($settings, $fields);
},
'wcgateway.order-processor' => static function (ContainerInterface $container): OrderProcessor {
@ -106,4 +98,260 @@ return [
$settings = $container->get('wcgateway.settings');
return new OrderTablePaymentStatusColumn($settings);
},
'wcgateway.settings.fields' => function(ContainerInterface $container) : array {
$settings = $container->get('wcgateway.settings');
$sandboxText = $settings->has('sandbox_on') && $settings->get('sandbox_on') ?
__(
'You are currently in the sandbox mode to test your installation. You can switch this, by clicking <button name="%1$s" value="%2$s">Reset</button>',
'woocommerce-paypal-commerce-gateway'
) : __(
'You are in live mode. This means, you can receive money into your account. You can switch this, by clicking <button name="%1$s" value="%2$s">Reset</button>',
'woocommerce-paypal-commerce-gateway'
);
$sandboxText = sprintf(
$sandboxText,
'save',
'reset'
);
$merchantEmailText = sprintf(
__(
'You are connected with your email address <mark>%1$s</mark>.
If you want to change this settings, please click <button name="%2$s" value="%3$s">Reset</button>',
'woocommerce-paypal-commerce-gateway'
),
$settings->has('merchant_email') ? $settings->get('merchant_email') : '',
'save',
'reset'
);
return [
'sandbox_on' => [
'title' => __('Sandbox', 'woocommerce-paypal-gateway'),
'type' => 'checkbox',
'label' => __('To test your Woocommerce installation, you can use the sandbox mode.', 'woocommerce-paypal-gateway'),
'default' => 0,
'screens' => [
State::STATE_START,
],
],
'sandbox_on_info' => [
'title' => __('Sandbox', 'woocommerce-paypal-gateway'),
'type' => 'ppcp-text',
'text' => $sandboxText,
'screens' => [
State::STATE_PROGRESSIVE,
State::STATE_ONBOARDED,
],
'hidden' => 'sandbox_on',
],
'merchant_email' => [
'title' => __('Email address', 'woocommerce-paypal-gateway'),
'type' => 'text',
'label' => __('The email address of your PayPal account.', 'woocommerce-paypal-gateway'),
'default' => '',
'screens' => [
State::STATE_START,
],
],
'merchant_email_info' => [
'title' => __('Email address', 'woocommerce-paypal-gateway'),
'type' => 'ppcp-text',
'text' => $merchantEmailText,
'screens' => [
State::STATE_PROGRESSIVE,
State::STATE_ONBOARDED,
],
'hidden' => 'merchant_email',
],
'title' => [
'title' => __('Title', 'woocommerce-paypal-gateway'),
'type' => 'text',
'description' => __(
'This controls the title which the user sees during checkout.',
'woocommerce-paypal-gateway'
),
'default' => __('PayPal', 'woocommerce-paypal-gateway'),
'desc_tip' => true,
'screens' => [
State::STATE_PROGRESSIVE,
State::STATE_ONBOARDED,
],
],
'description' => [
'title' => __('Description', 'woocommerce-paypal-gateway'),
'type' => 'text',
'desc_tip' => true,
'description' => __(
'This controls the description which the user sees during checkout.',
'woocommerce-paypal-gateway'
),
'default' => __(
'Pay via PayPal; you can pay with your credit card if you don\'t have a PayPal account.',
'woocommerce-paypal-gateway'
),
'screens' => [
State::STATE_PROGRESSIVE,
State::STATE_ONBOARDED,
],
],
'button_single_product_enabled' => [
'title' => __('Buttons on Single Product', 'woocommerce-paypal-gateway'),
'type' => 'checkbox',
'label' => __('Enable on Single Product', 'woocommerce-paypal-gateway'),
'default' => 1,
'screens' => [
State::STATE_PROGRESSIVE,
State::STATE_ONBOARDED,
],
],
'button_mini_cart_enabled' => [
'title' => __('Buttons on Mini Cart', 'woocommerce-paypal-gateway'),
'type' => 'checkbox',
'label' => __('Enable on Mini Cart', 'woocommerce-paypal-gateway'),
'default' => 1,
'screens' => [
State::STATE_PROGRESSIVE,
State::STATE_ONBOARDED,
],
],
'button_cart_enabled' => [
'title' => __('Buttons on Cart', 'woocommerce-paypal-gateway'),
'type' => 'checkbox',
'label' => __('Enable on Cart', 'woocommerce-paypal-gateway'),
'default' => 1,
'screens' => [
State::STATE_PROGRESSIVE,
State::STATE_ONBOARDED,
],
],
'button_color' => [
'title' => __('Color', 'woocommerce-paypal-gateway'),
'type' => 'select',
'class' => ['wc-enhanced-select'],
'default' => 'gold',
'desc_tip' => true,
'description' => __(
'Controls the background color of the primary button. Use "Gold" to leverage PayPal\'s recognition and preference, or change it to match your site design or aesthetic.',
'woocommerce-paypal-gateway'
),
'options' => [
'gold' => __('Gold (Recommended)', 'woocommerce-paypal-gateway'),
'blue' => __('Blue', 'woocommerce-paypal-gateway'),
'silver' => __('Silver', 'woocommerce-paypal-gateway'),
'black' => __('Black', 'woocommerce-paypal-gateway'),
],
'screens' => [
State::STATE_PROGRESSIVE,
State::STATE_ONBOARDED,
],
],
'button_shape' => [
'title' => __('Shape', 'woocommerce-paypal-gateway'),
'type' => 'select',
'class' => ['wc-enhanced-select'],
'default' => 'rect',
'desc_tip' => true,
'description' => __(
'The pill-shaped button\'s unique and powerful shape signifies PayPal in people\'s minds. Use the rectangular button as an alternative when pill-shaped buttons might pose design challenges.',
'woocommerce-paypal-gateway'
),
'options' => [
'pill' => __('Pill', 'woocommerce-paypal-gateway'),
'rect' => __('Rectangle', 'woocommerce-paypal-gateway'),
],
'screens' => [
State::STATE_PROGRESSIVE,
State::STATE_ONBOARDED,
],
],
'disable_funding' => [
'title' => __('Disable funding sources', 'woocommerce-paypal-gateway'),
'type' => 'ppcp-multiselect',
'class' => ['wc-enhanced-select'],
'default' => [],
'desc_tip' => true,
'description' => __(
'By default all possible funding sources will be shown. You can disable some sources, if you wish.',
'woocommerce-paypal-gateway'
),
'options' => [
'card' => _x('Credit or debit cards', 'Name of payment method', 'woocommerce-paypal-gateway'),
'credit' => _x('PayPal Credit', 'Name of payment method', 'woocommerce-paypal-gateway'),
'venmo' => _x('Venmo', 'Name of payment method', 'woocommerce-paypal-gateway'),
'sepa' => _x('SEPA-Lastschrift', 'Name of payment method', 'woocommerce-paypal-gateway'),
'bancontact' => _x('Bancontact', 'Name of payment method', 'woocommerce-paypal-gateway'),
'eps' => _x('eps', 'Name of payment method', 'woocommerce-paypal-gateway'),
'giropay' => _x('giropay', 'Name of payment method', 'woocommerce-paypal-gateway'),
'ideal' => _x('iDEAL', 'Name of payment method', 'woocommerce-paypal-gateway'),
'mybank' => _x('MyBank', 'Name of payment method', 'woocommerce-paypal-gateway'),
'p24' => _x('Przelewy24', 'Name of payment method', 'woocommerce-paypal-gateway'),
'sofort' => _x('Sofort', 'Name of payment method', 'woocommerce-paypal-gateway'),
],
'screens' => [
State::STATE_PROGRESSIVE,
State::STATE_ONBOARDED,
],
],
'dcc_cart_enabled' => [
'title' => __('Enable credit card on cart', 'woocommerce-paypal-gateway'),
'type' => 'checkbox',
'label' => __('Allow your customers to pay with credit card directly in your cart.', 'woocommerce-paypal-gateway'),
'default' => 1,
'screens' => [
State::STATE_ONBOARDED,
],
],
'dcc_mini_cart_enabled' => [
'title' => __('Enable credit card on mini cart', 'woocommerce-paypal-gateway'),
'type' => 'checkbox',
'label' => __('Allow your customers to pay with credit card directly in your mini cart.', 'woocommerce-paypal-gateway'),
'default' => 1,
'screens' => [
State::STATE_ONBOARDED,
],
],
'dcc_checkout_enabled' => [
'title' => __('Enable credit card on checkout', 'woocommerce-paypal-gateway'),
'type' => 'checkbox',
'label' => __('Allow your customers to pay with credit card in the checkout.', 'woocommerce-paypal-gateway'),
'default' => 1,
'screens' => [
State::STATE_ONBOARDED,
],
],
'dcc_single_product_enabled' => [
'title' => __('Enable credit card on products', 'woocommerce-paypal-gateway'),
'type' => 'checkbox',
'label' => __('Allow your customers to pay with credit card instantly on the product page.', 'woocommerce-paypal-gateway'),
'default' => 1,
'screens' => [
State::STATE_ONBOARDED,
],
],
'disable_cards' => [
'title' => __('Disable specific credid cards', 'woocommerce-paypal-gateway'),
'type' => 'ppcp-multiselect',
'class' => ['wc-enhanced-select'],
'default' => [],
'desc_tip' => true,
'description' => __(
'By default all possible credit cards will be shown. You can disable some cards, if you wish.',
'woocommerce-paypal-gateway'
),
'options' => [
'visa' => _x('Visa', 'Name of credit card', 'woocommerce-paypal-gateway'),
'mastercard' => _x('Mastercard', 'Name of credit card', 'woocommerce-paypal-gateway'),
'amex' => _x('American Express', 'Name of credit card', 'woocommerce-paypal-gateway'),
'discover' => _x('Discover', 'Name of credit card', 'woocommerce-paypal-gateway'),
'jcb' => _x('JCB', 'Name of credit card', 'woocommerce-paypal-gateway'),
'elo' => _x('Elo', 'Name of credit card', 'woocommerce-paypal-gateway'),
'hiper' => _x('Hiper', 'Name of credit card', 'woocommerce-paypal-gateway'),
],
'screens' => [
State::STATE_ONBOARDED,
],
],
];
}
];

View file

@ -1,92 +0,0 @@
<?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

@ -16,6 +16,7 @@ use Inpsyde\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
use Inpsyde\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use Inpsyde\PayPalCommerce\WcGateway\Processor\OrderProcessor;
use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsFields;
use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsRenderer;
//phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
//phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
@ -26,28 +27,25 @@ class WcGateway extends WcGatewayBase
public const INTENT_META_KEY = '_ppcp_paypal_intent';
public const ORDER_ID_META_KEY = '_ppcp_paypal_order_id';
private $settingsFields;
private $settingsRenderer;
private $authorizedPayments;
private $notice;
private $orderProcessor;
private $onboardingRenderer;
private $resetGateway;
public function __construct(
SettingsFields $settingsFields,
SettingsRenderer $settingsRenderer,
OrderProcessor $orderProcessor,
AuthorizedPaymentsProcessor $authorizedPayments,
AuthorizeOrderActionNotice $notice,
OnboardingRenderer $onboardingRenderer,
ResetGateway $resetGateway
OnboardingRenderer $onboardingRenderer
) {
$this->orderProcessor = $orderProcessor;
$this->authorizedPayments = $authorizedPayments;
$this->notice = $notice;
$this->settingsFields = $settingsFields;
$this->settingsRenderer = $settingsRenderer;
$this->onboardingRenderer = $onboardingRenderer;
$this->resetGateway = $resetGateway;
$this->method_title = __('PayPal Payments', 'woocommerce-paypal-gateway');
$this->method_description = __(
@ -74,7 +72,17 @@ class WcGateway extends WcGatewayBase
public function init_form_fields()
{
$this->form_fields = $this->settingsFields->fields();
$this->form_fields = [
'enabled' => [
'title' => __('Enable/Disable', 'woocommerce-paypal-gateway'),
'type' => 'checkbox',
'label' => __('Enable PayPal Payments', 'woocommerce-paypal-gateway'),
'default' => 'yes',
],
'ppcp' => [
'type' => 'ppcp',
],
];
}
public function process_payment($orderId): ?array
@ -140,40 +148,11 @@ class WcGateway extends WcGatewayBase
$this->notice->displayMessage($displayMessage);
}
public function generate_ppcp_onboarding_html() : string
public function generate_ppcp_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;
}
public function generate_ppcp_info_html($type, $data) : string
{
ob_start();
?>
<tr valign="top">
<th scope="row" class="titledesc">
<?php echo wp_kses_post( $data['title'] ); ?>
</th>
<td class="forminp">
<?php echo wp_kses_post( $data['text'] ); ?>
</td>
</tr>
<?php
$this->settingsRenderer->render();
$content = ob_get_contents();
ob_end_clean();
return $content;

View file

@ -1,111 +0,0 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Settings;
use Inpsyde\PayPalCommerce\Onboarding\Environment;
class FullyOnboardedSettings extends StartSettings implements SettingsFields
{
use SettingsTrait;
public function __construct(Environment $environment)
{
$this->environment = $environment;
}
public function fields(): array
{
return array_merge(
$this->gateway(),
$this->buttons(),
$this->creditCards()
);
}
private function gateway(): array
{
return array_merge(
$this->defaultFields(),
[
'intent' => [
'title' => __('Intent', 'woocommerce-paypal-gateway'),
'type' => 'select',
'class' => 'wc-enhanced-select',
'default' => 'capture',
'desc_tip' => true,
'description' => __(
'The intent to either capture payment immediately or authorize a payment for an order after order creation.',
'woocommerce-paypal-gateway'
),
'options' => [
'capture' => __('Capture', 'woocommerce-paypal-gateway'),
'authorize' => __('Authorize', 'woocommerce-paypal-gateway'),
],
],
]
);
}
private function creditCards(): array
{
return [
'credit_card_settings' => [
'title' => __('Credit Card Settings', 'woocommerce-paypal-gateway'),
'type' => 'title',
'description' => __(
'Customize the appearance of Credit Card Payments on your site.',
'woocommerce-paypal-gateway'
),
],
'dcc_cart_enabled' => [
'title' => __('Enable credit card on cart', 'woocommerce-paypal-gateway'),
'type' => 'checkbox',
'label' => __('Allow your customers to pay with credit card directly in your cart.', 'woocommerce-paypal-gateway'),
'default' => 'yes',
],
'dcc_mini_cart_enabled' => [
'title' => __('Enable credit card on mini cart', 'woocommerce-paypal-gateway'),
'type' => 'checkbox',
'label' => __('Allow your customers to pay with credit card directly in your mini cart.', 'woocommerce-paypal-gateway'),
'default' => 'yes',
],
'dcc_checkout_enabled' => [
'title' => __('Enable credit card on checkout', 'woocommerce-paypal-gateway'),
'type' => 'checkbox',
'label' => __('Allow your customers to pay with credit card in the checkout.', 'woocommerce-paypal-gateway'),
'default' => 'yes',
],
'dcc_single_product_enabled' => [
'title' => __('Enable credit card on products', 'woocommerce-paypal-gateway'),
'type' => 'checkbox',
'label' => __('Allow your customers to pay with credit card instantly on the product page.', 'woocommerce-paypal-gateway'),
'default' => 'yes',
],
'disable_cards' => [
'title' => __('Disable specific credid cards', 'woocommerce-paypal-gateway'),
'type' => 'multiselect',
'class' => 'wc-enhanced-select',
'default' => [],
'desc_tip' => true,
'description' => __(
'By default all possible credit cards will be shown. You can disable some cards, if you wish.',
'woocommerce-paypal-gateway'
),
'options' => [
'visa' => _x('Visa', 'Name of credit card', 'woocommerce-paypal-gateway'),
'mastercard' => _x('Mastercard', 'Name of credit card', 'woocommerce-paypal-gateway'),
'amex' => _x('American Express', 'Name of credit card', 'woocommerce-paypal-gateway'),
'discover' => _x('Discover', 'Name of credit card', 'woocommerce-paypal-gateway'),
'jcb' => _x('JCB', 'Name of credit card', 'woocommerce-paypal-gateway'),
'elo' => _x('Elo', 'Name of credit card', 'woocommerce-paypal-gateway'),
'hiper' => _x('Hiper', 'Name of credit card', 'woocommerce-paypal-gateway'),
],
],
];
}
}

View file

@ -1,36 +0,0 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Settings;
use Inpsyde\PayPalCommerce\Onboarding\Environment;
class ProgressiveSettings implements SettingsFields
{
use SettingsTrait;
public function __construct(Environment $environment)
{
$this->environment = $environment;
}
public function fields(): array
{
$fields = array_merge(
[
'onboarding' => [
'type' => 'ppcp_onboarding',
],
],
$this->defaultFields(),
[
'reset' => [
'type' => 'ppcp_reset',
],
]
);
return $fields;
}
}

View file

@ -10,11 +10,11 @@ use Psr\Container\ContainerInterface;
class Settings implements ContainerInterface
{
private $gateway;
public const KEY = 'woocommerce-ppcp-settings';
private $settings = [];
public function __construct(\WC_Payment_Gateway $gateway)
public function __construct()
{
$this->gateway = $gateway;
}
// phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration.NoReturnType
@ -24,18 +24,28 @@ class Settings implements ContainerInterface
if (!$this->has($id)) {
throw new NotFoundException();
}
return $this->gateway->get_option($id);
return $this->settings[$id];
}
public function has($id)
{
return !!$this->gateway->get_option($id);
$this->load();
return array_key_exists($id, $this->settings);
}
public function set($id, $value)
{
$this->load();
$this->settings[$id] = $value;
}
public function persist() {
update_option(self::KEY, $this->settings);
}
public function reset() : bool
{
$this->load();
$fieldsToReset = [
'enabled',
'intent',
@ -43,11 +53,18 @@ class Settings implements ContainerInterface
'client_secret',
'merchant_email',
];
foreach ($fieldsToReset as $key) {
$this->gateway->update_option($key, '');
foreach ($fieldsToReset as $id) {
$this->settings[$id] = null;
}
return true;
}
private function load() : bool {
if ($this->settings) {
return false;
}
$this->settings = get_option(self::KEY, []);
return true;
}
}

View file

@ -1,13 +0,0 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Settings;
//phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
interface SettingsFields
{
public function fields(): array;
}

View file

@ -0,0 +1,83 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Settings;
use Inpsyde\PayPalCommerce\Onboarding\State;
class SettingsListener
{
public const NONCE = 'ppcp-settings';
private $settings;
private $settingFields;
public function __construct(Settings $settings, array $settingFields)
{
$this->settings = $settings;
$this->settingFields = $settingFields;
}
public function listen() {
if (! $this->isValidUpdateRequest()) {
return;
}
if (isset($_POST['save']) && sanitize_text_field(wp_unslash($_POST['save'])) === 'reset') {
$this->settings->reset();
$this->settings->persist();
return;
}
$settings = [
'enabled' => isset($_POST['woocommerce_ppcp-gateway_enabled']) && absint($_POST['woocommerce_ppcp-gateway_enabled']) === 1,
];
$rawData = (isset($_POST['ppcp'])) ? (array) $_POST['ppcp'] : [];
foreach ($this->settingFields as $key => $config) {
switch ($config['type']) {
case 'checkbox':
$settings[$key] = isset($rawData[$key]);
break;
case 'text':
$settings[$key] = isset($rawData[$key]) ? sanitize_text_field($rawData[$key]) : '';
break;
case 'multiselect':
$values = isset($rawData[$key]) ? (array) $rawData[$key] : [];
$valuesToSave = [];
foreach ($values as $index => $rawValue) {
$value = sanitize_text_field($rawValue);
if (! in_array($value, $config['options'], true)) {
continue;
}
$valuesToSave[] = $value;
}
$settings[$key] = $valuesToSave;
break;
case 'select':
$settings[$key] = isset($rawData[$key]) && in_array(sanitize_text_field($rawData[$key]), $config['options'], true) ? sanitize_text_field($rawData[$key]) : null;
break;
}
}
foreach ($settings as $id => $value) {
$this->settings->set($id, $value);
}
$this->settings->persist();
return;
}
private function isValidUpdateRequest() : bool
{
if (! isset($_REQUEST['section']) || sanitize_text_field(wp_unslash($_REQUEST['section'])) !== 'ppcp-gateway') {
return false;
}
if (! current_user_can('manage_options')) {
return false;
}
if (! isset($_POST['ppcp-nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['ppcp-nonce'])), self::NONCE)) {
return false;
}
return true;
}
}

View file

@ -0,0 +1,84 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Settings;
use Inpsyde\PayPalCommerce\Onboarding\State;
use Psr\Container\ContainerInterface;
class SettingsRenderer
{
private $settings;
private $state;
private $fields;
public function __construct(
ContainerInterface $settings,
State $state,
array $fields
) {
$this->settings = $settings;
$this->state = $state;
$this->fields = $fields;
}
public function renderMultiSelect($field, $key, $config, $value) : string {
if ($config['type'] !== 'ppcp-multiselect') {
return $field;
}
$options = [];
foreach ($config['options'] as $optionKey => $optionValue) {
$selected = '';
$options[] = '<option value="' . esc_attr($optionKey) . '" ' . $selected . '>' . esc_html($optionValue) . '</option>';
}
$html = sprintf(
'<select
multiple
class="%s"
name="%s"
>%s</select>',
implode(' ', $config['class']),
$key,
implode('', $options)
);
return $html;
}
public function render() {
$nonce = wp_create_nonce(SettingsListener::NONCE);
?>
<input type="hidden" name="ppcp-nonce" value="<?php echo esc_attr($nonce); ?>">
<?php
foreach ($this->fields as $field => $config) :
if (! in_array($this->state->currentState(), $config['screens'], true)) {
continue;
}
$value = $this->settings->has($field) ? $this->settings->get($field) : null;
$id = 'ppcp[' . $field . ']';
?>
<tr valign="top">
<th>
<label
for="<?php echo esc_attr($id); ?>"
><?php echo esc_html($config['title']); ?></label>
</th>
<td><?php
if ($config['type'] === 'ppcp-text' ) {
echo wp_kses_post($config['text']);
if (isset($config['hidden'])) {
$value = $this->settings->has($config['hidden']) ? (string) $this->settings->get($config['hidden']) : '';
echo '<input type="hidden" name="ppcp[' . esc_attr($config['hidden']) . ']" value="' . esc_attr($value) . '">';
}
} else {
woocommerce_form_field($id, $config, $value);
} ?></td>
</tr>
<?php endforeach;
}
}

View file

@ -1,158 +0,0 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Settings;
use Inpsyde\PayPalCommerce\Onboarding\Environment;
trait SettingsTrait
{
/**
* @var Environment
*/
private $environment;
private function defaultFields(): array
{
$isSandbox = ($this->environment) ? $this->environment->currentEnvironmentIs(Environment::SANDBOX) : false;
$sandbox = [
'type' => 'ppcp_info',
'title' => __('Sandbox'),
'text' => ($isSandbox) ? __('You are currently in the sandbox mode. Click Reset if you want to change your mode.', 'woocommerce-paypal-commerce-gateway') : __('You are in production mode. Click Reset if you want to change your mode.', 'woocommerce-paypal-commerce-gateway'),
];
return array_merge(
[
'enabled' => [
'title' => __('Enable/Disable', 'woocommerce-paypal-gateway'),
'type' => 'checkbox',
'label' => __('Enable PayPal Payments', 'woocommerce-paypal-gateway'),
'default' => 'yes',
],
'title' => [
'title' => __('Title', 'woocommerce-paypal-gateway'),
'type' => 'text',
'description' => __(
'This controls the title which the user sees during checkout.',
'woocommerce-paypal-gateway'
),
'default' => __('PayPal', 'woocommerce-paypal-gateway'),
'desc_tip' => true,
],
'description' => [
'title' => __('Description', 'woocommerce-paypal-gateway'),
'type' => 'text',
'desc_tip' => true,
'description' => __(
'This controls the description which the user sees during checkout.',
'woocommerce-paypal-gateway'
),
'default' => __(
'Pay via PayPal; you can pay with your credit card if you don\'t have a PayPal account.',
'woocommerce-paypal-gateway'
),
],
'account_settings' => [
'title' => __('Account Settings', 'woocommerce-paypal-gateway'),
'type' => 'title',
'description' => '',
],
'sandbox_on' => $sandbox,
'reset' => [
'type' => 'ppcp_reset',
],
],
$this->buttons()
);
}
private function buttons(): array
{
return [
'button_settings' => [
'title' => __('SmartButton Settings', 'woocommerce-paypal-gateway'),
'type' => 'title',
'description' => __(
'Customize the appearance of PayPal Payments on your site.',
'woocommerce-paypal-gateway'
),
],
'button_single_product_enabled' => [
'title' => __('Buttons on Single Product', 'woocommerce-paypal-gateway'),
'type' => 'checkbox',
'label' => __('Enable on Single Product', 'woocommerce-paypal-gateway'),
'default' => 'yes',
],
'button_mini_cart_enabled' => [
'title' => __('Buttons on Mini Cart', 'woocommerce-paypal-gateway'),
'type' => 'checkbox',
'label' => __('Enable on Mini Cart', 'woocommerce-paypal-gateway'),
'default' => 'yes',
],
'button_cart_enabled' => [
'title' => __('Buttons on Cart', 'woocommerce-paypal-gateway'),
'type' => 'checkbox',
'label' => __('Enable on Cart', 'woocommerce-paypal-gateway'),
'default' => 'yes',
],
'button_color' => [
'title' => __('Color', 'woocommerce-paypal-gateway'),
'type' => 'select',
'class' => 'wc-enhanced-select',
'default' => 'gold',
'desc_tip' => true,
'description' => __(
'Controls the background color of the primary button. Use "Gold" to leverage PayPal\'s recognition and preference, or change it to match your site design or aesthetic.',
'woocommerce-paypal-gateway'
),
'options' => [
'gold' => __('Gold (Recommended)', 'woocommerce-paypal-gateway'),
'blue' => __('Blue', 'woocommerce-paypal-gateway'),
'silver' => __('Silver', 'woocommerce-paypal-gateway'),
'black' => __('Black', 'woocommerce-paypal-gateway'),
],
],
'button_shape' => [
'title' => __('Shape', 'woocommerce-paypal-gateway'),
'type' => 'select',
'class' => 'wc-enhanced-select',
'default' => 'rect',
'desc_tip' => true,
'description' => __(
'The pill-shaped button\'s unique and powerful shape signifies PayPal in people\'s minds. Use the rectangular button as an alternative when pill-shaped buttons might pose design challenges.',
'woocommerce-paypal-gateway'
),
'options' => [
'pill' => __('Pill', 'woocommerce-paypal-gateway'),
'rect' => __('Rectangle', 'woocommerce-paypal-gateway'),
],
],
'disable_funding' => [
'title' => __('Disable funding sources', 'woocommerce-paypal-gateway'),
'type' => 'multiselect',
'class' => 'wc-enhanced-select',
'default' => [],
'desc_tip' => true,
'description' => __(
'By default all possible funding sources will be shown. You can disable some sources, if you wish.',
'woocommerce-paypal-gateway'
),
'options' => [
'card' => _x('Credit or debit cards', 'Name of payment method', 'woocommerce-paypal-gateway'),
'credit' => _x('PayPal Credit', 'Name of payment method', 'woocommerce-paypal-gateway'),
'venmo' => _x('Venmo', 'Name of payment method', 'woocommerce-paypal-gateway'),
'sepa' => _x('SEPA-Lastschrift', 'Name of payment method', 'woocommerce-paypal-gateway'),
'bancontact' => _x('Bancontact', 'Name of payment method', 'woocommerce-paypal-gateway'),
'eps' => _x('eps', 'Name of payment method', 'woocommerce-paypal-gateway'),
'giropay' => _x('giropay', 'Name of payment method', 'woocommerce-paypal-gateway'),
'ideal' => _x('iDEAL', 'Name of payment method', 'woocommerce-paypal-gateway'),
'mybank' => _x('MyBank', 'Name of payment method', 'woocommerce-paypal-gateway'),
'p24' => _x('Przelewy24', 'Name of payment method', 'woocommerce-paypal-gateway'),
'sofort' => _x('Sofort', 'Name of payment method', 'woocommerce-paypal-gateway'),
],
],
];
}
}

View file

@ -1,35 +0,0 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Settings;
class StartSettings implements SettingsFields
{
use SettingsTrait;
public function fields(): array
{
return [
'merchant_email' => [
'title' => __('PayPal Email', 'woocommerce-paypal-gateway'),
'type' => 'email',
'description' => __(
'Please enter the email address with which you want to receive payments.',
'woocommerce-paypal-gateway'
),
'default' => '',
'desc_tip' => true,
],
'sandbox_on' => [
'title' => __('Enable Sandbox', 'woocommerce-paypal-gateway'),
'type' => 'checkbox',
'label' => __(
'For testing your integration, you can enable the sandbox.',
'woocommerce-paypal-gateway'
),
'default' => 'no',
],
];
}
}

View file

@ -14,6 +14,7 @@ use Inpsyde\PayPalCommerce\WcGateway\Checkout\DisableGateways;
use Inpsyde\PayPalCommerce\WcGateway\Gateway\WcGateway;
use Inpsyde\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
use Inpsyde\PayPalCommerce\WcGateway\Notice\ConnectAdminNotice;
use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsRenderer;
use Interop\Container\ServiceProviderInterface;
use Psr\Container\ContainerInterface;
@ -37,6 +38,27 @@ class WcGatewayModule implements ModuleInterface
}
);
add_action(
'woocommerce_settings_save_checkout',
static function () use ($container) {
$listener = $container->get('wcgateway.settings.listener');
$listener->listen();
}
);
add_filter(
'woocommerce_form_field',
static function ($field, $key, $args, $value) use ($container) {
$renderer = $container->get('wcgateway.settings.render');
/**
* @var SettingsRenderer $renderer
*/
return $renderer->renderMultiSelect($field, $key, $args, $value);
},
10,
4
);
add_filter(
'woocommerce_available_payment_gateways',
static function ($methods) use ($container): array {
@ -48,14 +70,6 @@ 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 {