This commit is contained in:
David Remer 2020-07-02 12:48:40 +03:00
parent ded172c131
commit f09d71e888
13 changed files with 159 additions and 98 deletions

View file

@ -1,9 +1,9 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Onboarding\Endpoint;
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\LoginSeller;
use Inpsyde\PayPalCommerce\ApiClient\Repository\PartnerReferralsData;
use Inpsyde\PayPalCommerce\Button\Endpoint\EndpointInterface;
@ -24,6 +24,7 @@ class LoginSellerEndpoint implements EndpointInterface
PartnerReferralsData $partnerReferralsData,
\WC_Payment_Gateway $gateway
) {
$this->requestData = $requestData;
$this->loginSellerEndpoint = $loginSellerEndpoint;
$this->partnerReferralsData = $partnerReferralsData;

View file

@ -1,9 +1,9 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Onboarding;
use Psr\Container\ContainerInterface;
class Environment
@ -11,12 +11,6 @@ class Environment
public const PRODUCTION = 'production';
public const SANDBOX = 'sandbox';
public const VALID_ENVIRONMENTS = [
self::PRODUCTION,
self::SANDBOX,
];
public const OPTION_KEY = 'ppcp-env';
private $settings;
public function __construct(ContainerInterface $settings)
@ -24,23 +18,15 @@ class Environment
$this->settings = $settings;
}
public function currentEnvironment() : string
public function currentEnvironment(): string
{
return (
$this->settings->has('sandbox_on') && $this->settings->get('sandbox_on')
) ? self::SANDBOX : self::PRODUCTION;
}
public function currentEnvironmentIs(string $environment) : bool {
public function currentEnvironmentIs(string $environment): bool
{
return $this->currentEnvironment() === $environment;
}
public function changeEnvironmentTo(string $environment) : bool
{
if (! in_array($environment, self::VALID_ENVIRONMENTS, true)) {
return false;
}
update_option(self::OPTION_KEY, $environment);
return true;
}
}
}

View file

@ -43,7 +43,6 @@ class OnboardingModule implements ModuleInterface
]
);
add_action(
'wc_ajax_' . LoginSellerEndpoint::ENDPOINT,
static function () use ($container) {

View file

@ -1,16 +1,16 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Onboarding;
use Psr\Container\ContainerInterface;
class State
{
const STATE_START = 0;
const STATE_PROGRESSIVE = 4;
const STATE_ONBOARDED = 8;
public const STATE_START = 0;
public const STATE_PROGRESSIVE = 4;
public const STATE_ONBOARDED = 8;
private $environment;
private $settings;
@ -18,11 +18,13 @@ class State
Environment $environment,
ContainerInterface $settings
) {
$this->environment = $environment;
$this->settings = $settings;
}
public function currentState() : int {
public function currentState(): int
{
$value = self::STATE_START;
/**
* Having provided the merchant email means, we are at least
@ -40,4 +42,4 @@ class State
}
return $value;
}
}
}

View file

@ -4,26 +4,28 @@ declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway;
use Psr\Container\ContainerInterface;
return [
'api.merchant_email' => function ($container) : string {
'api.merchant_email' => static function (ContainerInterface $container): string {
$settings = $container->get('wcgateway.settings');
return $settings->has('merchant_email') ? (string) $settings->get('merchant_email') : '';
},
'api.merchant_id' => function ($container) : string {
'api.merchant_id' => static function (ContainerInterface $container): string {
$settings = $container->get('wcgateway.settings');
return $settings->has('merchant_id') ? (string) $settings->get('merchant_id') : '';
},
'api.partner_merchant_id' => static function () : string {
'api.partner_merchant_id' => static function (): string {
// ToDo: Replace with the real merchant id of platform
return 'KQ8FCM66JFGDL';
},
'api.key' => function ($container) : string {
'api.key' => static function (ContainerInterface $container): string {
$settings = $container->get('wcgateway.settings');
$key = $settings->has('client_id') ? (string) $settings->get('client_id') : '';
return $key;
},
'api.secret' => function ($container) : string {
'api.secret' => static function (ContainerInterface $container): string {
$settings = $container->get('wcgateway.settings');
return $settings->has('client_secret') ? (string) $settings->get('client_secret') : '';
},

View file

@ -99,7 +99,7 @@ return [
return new OrderTablePaymentStatusColumn($settings);
},
'wcgateway.settings.fields' => function(ContainerInterface $container) : array {
'wcgateway.settings.fields' => static function (ContainerInterface $container): array {
$settings = $container->get('wcgateway.settings');
$sandboxText = $settings->has('sandbox_on') && $settings->get('sandbox_on') ?
__(
@ -353,5 +353,5 @@ return [
],
],
];
}
},
];

View file

@ -144,11 +144,13 @@ class WcGateway extends WcGatewayBase
AuthorizedPaymentsProcessor::INACCESSIBLE => AuthorizeOrderActionNotice::NO_INFO,
AuthorizedPaymentsProcessor::NOT_FOUND => AuthorizeOrderActionNotice::NOT_FOUND,
];
$displayMessage = (isset($messageMapping[$status])) ? $messageMapping[$status] : AuthorizeOrderActionNotice::FAILED;
$displayMessage = (isset($messageMapping[$status])) ?
$messageMapping[$status]
: AuthorizeOrderActionNotice::FAILED;
$this->notice->displayMessage($displayMessage);
}
public function generate_ppcp_html() : string
public function generate_ppcp_html(): string
{
ob_start();

View file

@ -8,7 +8,7 @@ use WC_Payment_Gateway;
class WcGatewayBase extends WC_Payment_Gateway implements WcGatewayInterface
{
const ID = 'ppcp-gateway';
public const ID = 'ppcp-gateway';
public function __construct()
{

View file

@ -38,7 +38,7 @@ class OrderProcessor
$this->orderFactory = $orderFactory;
}
public function process(\WC_Order $wcOrder, $woocommerce): bool
public function process(\WC_Order $wcOrder, \WooCommerce $woocommerce): bool
{
$order = $this->sessionHandler->order();
$wcOrder->update_meta_data(WcGateway::ORDER_ID_META_KEY, $order->id());
@ -50,7 +50,7 @@ class OrderProcessor
}
if ($errorMessage) {
$this->lastError = sprintf(
// translators %s is the message of the error.
// translators: %s is the message of the error.
__('Payment error: %s', 'woocommerce-paypal-gateway'),
$errorMessage
);

View file

@ -39,11 +39,13 @@ class Settings implements ContainerInterface
$this->settings[$id] = $value;
}
public function persist() {
public function persist()
{
update_option(self::KEY, $this->settings);
}
public function reset() : bool
public function reset(): bool
{
$this->load();
$fieldsToReset = [
@ -60,7 +62,9 @@ class Settings implements ContainerInterface
return true;
}
private function load() : bool {
private function load(): bool
{
if ($this->settings) {
return false;
}

View file

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Settings;
@ -17,11 +18,17 @@ class SettingsListener
$this->settingFields = $settingFields;
}
public function listen() {
public function listen()
{
if (! $this->isValidUpdateRequest()) {
return;
}
/**
* Nonce verification is done in self::isValidUpdateRequest
*/
//phpcs:disable WordPress.Security.NonceVerification.Missing
if (isset($_POST['save']) && sanitize_text_field(wp_unslash($_POST['save'])) === 'reset') {
$this->settings->reset();
$this->settings->persist();
@ -29,9 +36,25 @@ class SettingsListener
}
$settings = [
'enabled' => isset($_POST['woocommerce_ppcp-gateway_enabled']) && absint($_POST['woocommerce_ppcp-gateway_enabled']) === 1,
'enabled' => isset($_POST['woocommerce_ppcp-gateway_enabled'])
&& absint($_POST['woocommerce_ppcp-gateway_enabled']) === 1,
];
$rawData = (isset($_POST['ppcp'])) ? (array) $_POST['ppcp'] : [];
//phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
/**
* Sanitization is done at a later stage.
*/
$rawData = (isset($_POST['ppcp'])) ? (array) wp_unslash($_POST['ppcp']) : [];
$settings = $this->retrieveSettingsFromRawData($rawData);
foreach ($settings as $id => $value) {
$this->settings->set($id, $value);
}
$this->settings->persist();
}
//phpcs:disable Inpsyde.CodeQuality.NestingLevel.MaxExceeded
private function retrieveSettingsFromRawData(array $rawData): array
{
$settings = [];
foreach ($this->settingFields as $key => $config) {
switch ($config['type']) {
case 'checkbox':
@ -53,21 +76,25 @@ class SettingsListener
$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;
$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;
return $settings;
}
//phpcs:enable Inpsyde.CodeQuality.NestingLevel.MaxExceeded
private function isValidUpdateRequest() : bool
private function isValidUpdateRequest(): bool
{
if (! isset($_REQUEST['section']) || sanitize_text_field(wp_unslash($_REQUEST['section'])) !== 'ppcp-gateway') {
if (
! isset($_REQUEST['section'])
|| sanitize_text_field(wp_unslash($_REQUEST['section'])) !== 'ppcp-gateway'
) {
return false;
}
@ -75,9 +102,15 @@ class SettingsListener
return false;
}
if (! isset($_POST['ppcp-nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['ppcp-nonce'])), self::NONCE)) {
if (
! isset($_POST['ppcp-nonce'])
|| !wp_verify_nonce(
sanitize_text_field(wp_unslash($_POST['ppcp-nonce'])),
self::NONCE
)
) {
return false;
}
return true;
}
}
}

View file

@ -1,9 +1,9 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Settings;
use Inpsyde\PayPalCommerce\Onboarding\State;
use Psr\Container\ContainerInterface;
@ -14,25 +14,31 @@ class SettingsRenderer
private $state;
private $fields;
public function __construct(
ContainerInterface $settings,
State $state,
array $fields
ContainerInterface $settings,
State $state,
array $fields
) {
$this->settings = $settings;
$this->state = $state;
$this->fields = $fields;
}
public function renderMultiSelect($field, $key, $config, $value) : string {
//phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
public function renderMultiSelect($field, $key, $config, $value): string
{
if ($config['type'] !== 'ppcp-multiselect') {
return $field;
}
$options = [];
foreach ($config['options'] as $optionKey => $optionValue) {
$selected = '';
$selected = (in_array($optionKey, $value, true)) ? 'selected="selected"' : '';
$options[] = '<option value="' . esc_attr($optionKey) . '" ' . $selected . '>' . esc_html($optionValue) . '</option>';
$options[] = '<option value="' . esc_attr($optionKey) . '" ' . $selected . '>' .
esc_html($optionValue) .
'</option>';
}
$html = sprintf(
@ -48,8 +54,10 @@ class SettingsRenderer
return $html;
}
//phpcs:enable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
public function render() {
public function render()
{
$nonce = wp_create_nonce(SettingsListener::NONCE);
?>
@ -68,23 +76,33 @@ class SettingsRenderer
for="<?php echo esc_attr($id); ?>"
><?php echo esc_html($config['title']); ?></label>
<?php if (isset($config['desc_tip']) && $config['desc_tip']) : ?>
<span class="woocommerce-help-tip" data-tip="<?php echo esc_attr($config['description']); ?>"></span>
<?php
unset($config['description']);
endif;
?>
<span
class="woocommerce-help-tip"
data-tip="<?php echo esc_attr($config['description']); ?>"
></span>
<?php unset($config['description']);
endif; ?>
</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>
$config['type'] === 'ppcp-text' ?
$this->renderText($config)
: woocommerce_form_field($id, $config, $value); ?></td>
</tr>
<?php endforeach;
}
private function renderText(array $config)
{
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) . '"
>';
}
}
}

View file

@ -30,6 +30,35 @@ class WcGatewayModule implements ModuleInterface
public function run(ContainerInterface $container)
{
$this->registerPaymentGateway($container);
$this->registerOrderFunctionality($container);
$this->registerColumns($container);
add_filter(
Repository::NOTICES_FILTER,
static function ($notices) use ($container): array {
$notice = $container->get('wcgateway.notice.connect');
/**
* @var ConnectAdminNotice $notice
*/
$connectMessage = $notice->connectMessage();
if ($connectMessage) {
$notices[] = $connectMessage;
}
$authorizeOrderAction = $container->get('wcgateway.notice.authorize-order-action');
$authorizedMessage = $authorizeOrderAction->message();
if ($authorizedMessage) {
$notices[] = $authorizedMessage;
}
return $notices;
}
);
}
private function registerPaymentGateWay(ContainerInterface $container)
{
add_filter(
'woocommerce_payment_gateways',
static function ($methods) use ($container): array {
@ -69,28 +98,10 @@ class WcGatewayModule implements ModuleInterface
return $disabler->handler((array)$methods);
}
);
}
add_filter(
Repository::NOTICES_FILTER,
static function ($notices) use ($container): array {
$notice = $container->get('wcgateway.notice.connect');
/**
* @var ConnectAdminNotice $notice
*/
$connectMessage = $notice->connectMessage();
if ($connectMessage) {
$notices[] = $connectMessage;
}
$authorizeOrderAction = $container->get('wcgateway.notice.authorize-order-action');
$authorizedMessage = $authorizeOrderAction->message();
if ($authorizedMessage) {
$notices[] = $authorizedMessage;
}
return $notices;
}
);
private function registerOrderFunctionality(ContainerInterface $container)
{
add_filter(
'woocommerce_order_actions',
static function ($orderActions): array {
@ -112,7 +123,10 @@ class WcGatewayModule implements ModuleInterface
$gateway->captureAuthorizedPayment($wcOrder);
}
);
}
private function registerColumns(ContainerInterface $container)
{
add_action(
'woocommerce_order_actions_start',
static function ($wcOrderId) use ($container) {