Add settings and settings field class

This commit is contained in:
Mészáros Róbert 2020-04-09 14:02:35 +03:00
parent 8a16329c29
commit d7f2e33e60
4 changed files with 72 additions and 21 deletions

View file

@ -6,6 +6,8 @@ namespace Inpsyde\PayPalCommerce\WcGateway;
use Dhii\Data\Container\ContainerInterface;
use Inpsyde\PayPalCommerce\WcGateway\Checkout\DisableGateways;
use Inpsyde\PayPalCommerce\WcGateway\Gateway\WcGateway;
use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsFields;
return [
'wcgateway.gateway' => function (ContainerInterface $container) : WcGateway {
@ -13,10 +15,19 @@ return [
$cartRepository = $container->get('api.cart-repository');
$endpoint = $container->get('api.endpoint.order');
$orderFactory = $container->get('api.factory.order');
return new WcGateway($sessionHandler, $cartRepository, $endpoint, $orderFactory);
$settingsFields = $container->get('wcgateway.settings.fields');
return new WcGateway($sessionHandler, $cartRepository, $endpoint, $orderFactory, $settingsFields);
},
'wcgateway.disabler' => function (ContainerInterface $container) : DisableGateways {
$sessionHandler = $container->get('session.handler');
return new DisableGateways($sessionHandler);
},
'wcgateway.settings' => function (ContainerInterface $container) : Settings {
$gateway = $container->get('wcgateway.gateway');
$settingsField = $container->get('wcgateway.settings.fields');
return new Settings($gateway, $settingsField);
},
'wcgateway.settings.fields' => function (ContainerInterface $container) : SettingsFields {
return new SettingsFields();
}
];

View file

@ -9,6 +9,7 @@ use Inpsyde\PayPalCommerce\ApiClient\Entity\OrderStatus;
use Inpsyde\PayPalCommerce\ApiClient\Factory\OrderFactory;
use Inpsyde\PayPalCommerce\ApiClient\Repository\CartRepository;
use Inpsyde\PayPalCommerce\Session\SessionHandler;
use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsFields;
//phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
//phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
@ -22,18 +23,21 @@ class WcGateway extends \WC_Payment_Gateway
private $endpoint;
private $cartRepository;
private $orderFactory;
private $settingsFields;
public function __construct(
SessionHandler $sessionHandler,
CartRepository $cartRepository,
OrderEndpoint $endpoint,
OrderFactory $orderFactory
OrderFactory $orderFactory,
SettingsFields $settingsFields
) {
$this->sessionHandler = $sessionHandler;
$this->cartRepository = $cartRepository;
$this->endpoint = $endpoint;
$this->orderFactory = $orderFactory;
$this->settingsFields = $settingsFields;
$this->id = self::ID;
$this->title = __('PayPal Payments', 'woocommerce-paypal-gateway');
$this->method_title = __('PayPal Payments', 'woocommerce-paypal-gateway');
@ -44,8 +48,6 @@ class WcGateway extends \WC_Payment_Gateway
$this->init_form_fields();
$this->init_settings();
$this->isSandbox = $this->get_option('sandbox_on', 'yes') === 'yes';
add_action(
'woocommerce_update_options_payment_gateways_' . $this->id,
[
@ -57,23 +59,7 @@ class WcGateway extends \WC_Payment_Gateway
public function init_form_fields()
{
$this->form_fields = [
'enabled' => [
'title' => __('Enable/Disable', 'woocommerce-paypal-gateway'),
'type' => 'checkbox',
'label' => __('Enable PayPal Payments', 'woocommerce-paypal-gateway'),
'default' => 'yes',
],
'sandbox_on' => [
'title' => __('Enable Sandbox', 'woocommerce-paypal-gateway'),
'type' => 'checkbox',
'label' => __(
'For testing your integration, you can enable the sandbox.',
'woocommerce-paypal-gateway'
),
'default' => 'yes',
],
];
$this->form_fields = $this->settingsFields->fields();
}
public function process_payment($orderId) : ?array

View file

@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Settings;
use Inpsyde\PayPalCommerce\WcGateway\Gateway\WcGateway;
class Settings
{
private $gateway;
private $formFields;
public function __construct(WcGateway $gateway, SettingsFields $formFields)
{
$this->gateway = $gateway;
$this->formFields = $formFields;
}
// phpcs:ignore Inpsyde.CodeQuality.ReturnTypeDeclaration.NoReturnType
public function get(string $settingsKey)
{
return $this->gateway->get_option($settingsKey);
}
}

View file

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Settings;
class SettingsFields
{
public function fields(): array
{
return [
'enabled' => [
'title' => __('Enable/Disable', 'woocommerce-paypal-gateway'),
'type' => 'checkbox',
'label' => __('Enable PayPal Payments', 'woocommerce-paypal-gateway'),
'default' => 'yes',
],
'sandbox_on' => [
'title' => __('Enable Sandbox', 'woocommerce-paypal-gateway'),
'type' => 'checkbox',
'label' => __(
'For testing your integration, you can enable the sandbox.',
'woocommerce-paypal-gateway'
),
'default' => 'yes',
],
];
}
}