codestyle

This commit is contained in:
David Remer 2020-04-28 12:31:12 +03:00
parent 6793d7217a
commit 9dc3c073d2
35 changed files with 205 additions and 144 deletions

View file

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway;

View file

@ -1,10 +1,11 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway;
use Dhii\Modular\Module\ModuleInterface;
return function (): ModuleInterface {
return static function (): ModuleInterface {
return new WcGatewayModule();
};

View file

@ -19,10 +19,10 @@ use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsFields;
return [
'wcgateway.gateway.base' => function (ContainerInterface $container) : WcGatewayBase {
'wcgateway.gateway.base' => static function (ContainerInterface $container): WcGatewayBase {
return new WcGatewayBase();
},
'wcgateway.gateway' => function (ContainerInterface $container) : WcGateway {
'wcgateway.gateway' => static function (ContainerInterface $container): WcGateway {
$sessionHandler = $container->get('session.handler');
$cartRepository = $container->get('api.repository.cart');
// TODO eventuall get rid of the endpoints as the processor is sufficient
@ -31,6 +31,7 @@ return [
$orderFactory = $container->get('api.factory.order');
$settingsFields = $container->get('wcgateway.settings.fields');
$processor = $container->get('wcgateway.processor');
$notice = $container->get('wcgateway.notice.authorize-order-action');
return new WcGateway(
$sessionHandler,
$cartRepository,
@ -38,43 +39,44 @@ return [
$paymentsEndpoint,
$orderFactory,
$settingsFields,
$processor
$processor,
$notice
);
},
'wcgateway.disabler' => function (ContainerInterface $container) : DisableGateways {
'wcgateway.disabler' => static function (ContainerInterface $container): DisableGateways {
$sessionHandler = $container->get('session.handler');
return new DisableGateways($sessionHandler);
},
'wcgateway.settings' => function (ContainerInterface $container) : Settings {
'wcgateway.settings' => static function (ContainerInterface $container): Settings {
$gateway = $container->get('wcgateway.gateway.base');
$settingsField = $container->get('wcgateway.settings.fields');
return new Settings($gateway, $settingsField);
},
'wcgateway.notice.connect' => function (ContainerInterface $container) : ConnectAdminNotice {
'wcgateway.notice.connect' => static function (ContainerInterface $container): ConnectAdminNotice {
$settings = $container->get('wcgateway.settings');
return new ConnectAdminNotice($settings);
},
'wcgateway.notice.authorize-order-action' =>
function (ContainerInterface $container): AuthorizeOrderActionNotice {
static function (ContainerInterface $container): AuthorizeOrderActionNotice {
return new AuthorizeOrderActionNotice();
},
'wcgateway.settings.fields' => function (ContainerInterface $container): SettingsFields {
'wcgateway.settings.fields' => static function (ContainerInterface $container): SettingsFields {
return new SettingsFields();
},
'wcgateway.processor' => function (ContainerInterface $container): Processor {
'wcgateway.processor' => static function (ContainerInterface $container): Processor {
$authorizedPaymentsProcessor = $container->get('wcgateway.processor.authorized-payments');
return new Processor($authorizedPaymentsProcessor);
},
'wcgateway.processor.authorized-payments' => function (ContainerInterface $container): AuthorizedPaymentsProcessor {
'wcgateway.processor.authorized-payments' => static function (ContainerInterface $container): AuthorizedPaymentsProcessor {
$orderEndpoint = $container->get('api.endpoint.order');
$paymentsEndpoint = $container->get('api.endpoint.payments');
return new AuthorizedPaymentsProcessor($orderEndpoint, $paymentsEndpoint);
},
'wcgateway.admin.order-payment-status' => function(ContainerInterface $container): PaymentStatusOrderDetail {
'wcgateway.admin.order-payment-status' => static function (ContainerInterface $container): PaymentStatusOrderDetail {
return new PaymentStatusOrderDetail();
},
'wcgateway.admin.orders-payment-status-column' => function(ContainerInterface $container): OrderTablePaymentStatusColumn {
'wcgateway.admin.orders-payment-status-column' => static function (ContainerInterface $container): OrderTablePaymentStatusColumn {
$settings = $container->get('wcgateway.settings');
return new OrderTablePaymentStatusColumn($settings);
}
},
];

View file

@ -4,14 +4,15 @@ declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Admin;
use Inpsyde\PayPalCommerce\WcGateway\Gateway\WcGateway;
use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
class OrderTablePaymentStatusColumn
{
const COLUMN_KEY = 'ppcp_payment_status';
const INTENT = 'authorize';
const AFTER_COLUMN_KEY = 'order_status';
protected $settings;
private const COLUMN_KEY = 'ppcp_payment_status';
private const INTENT = 'authorize';
private const AFTER_COLUMN_KEY = 'order_status';
private $settings;
public function __construct(Settings $settings)
{
@ -48,31 +49,36 @@ class OrderTablePaymentStatusColumn
return;
}
if ($this->isCaptured($wcOrderId)) {
$wcOrder = wc_get_order($wcOrderId);
if (! is_a($wcOrder, \WC_Order::class) || ! $this->renderForOrder($wcOrder)) {
return;
}
if ($this->isCaptured($wcOrder)) {
$this->renderCompletedStatus();
} else {
$this->renderIncompletedStatus();
}
}
protected function isCaptured(int $wcOrderId): bool
private function renderForOrder(\WC_Order $order): bool
{
$wcOrder = wc_get_order($wcOrderId);
$captured = $wcOrder->get_meta('_ppcp_paypal_captured');
if (!empty($captured) && wc_string_to_bool($captured)) {
return true;
}
return false;
return !empty($order->get_meta(WcGateway::CAPTURED_META_KEY));
}
protected function renderCompletedStatus()
private function isCaptured(\WC_Order $wcOrder): bool
{
$captured = $wcOrder->get_meta(WcGateway::CAPTURED_META_KEY);
return wc_string_to_bool($captured);
}
private function renderCompletedStatus()
{
echo '<span class="dashicons dashicons-yes"></span>';
}
protected function renderIncompletedStatus()
private function renderIncompletedStatus()
{
printf(
'<mark class="onbackorder">%s</mark>',

View file

@ -4,13 +4,15 @@ declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Admin;
use Inpsyde\PayPalCommerce\WcGateway\Gateway\WcGateway;
class PaymentStatusOrderDetail
{
public function render(int $wcOrderId)
{
$wcOrder = new \WC_Order($wcOrderId);
$intent = $wcOrder->get_meta('_ppcp_paypal_intent');
$captured = $wcOrder->get_meta('_ppcp_paypal_captured');
$intent = $wcOrder->get_meta(WcGateway::INTENT_META_KEY);
$captured = $wcOrder->get_meta(WcGateway::CAPTURED_META_KEY);
if (strcasecmp($intent, 'AUTHORIZE') !== 0) {
return;
@ -23,8 +25,14 @@ class PaymentStatusOrderDetail
printf(
// @phpcs:ignore Inpsyde.CodeQuality.LineLength.TooLong
'<li class="wide"><p><mark class="order-status status-on-hold"><span>%1$s</span></mark></p><p>%2$s</p></li>',
esc_html__('Not captured', 'woocommerce-paypal-gateway'),
esc_html__('To capture the payment select capture action from the list below.', 'woocommerce-paypal-gateway'),
esc_html__(
'Not captured',
'woocommerce-paypal-gateway'
),
esc_html__(
'To capture the payment select capture action from the list below.',
'woocommerce-paypal-gateway'
),
);
}
}

View file

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Checkout;
@ -15,7 +16,7 @@ class DisableGateways
$this->sessionHandler = $sessionHandler;
}
public function handler(array $methods) : array
public function handler(array $methods): array
{
if (! $this->needsToDisableGateways()) {
return $methods;
@ -24,7 +25,7 @@ class DisableGateways
return [WcGateway::ID => $methods[WcGateway::ID]];
}
private function needsToDisableGateways() : bool
private function needsToDisableGateways(): bool
{
return $this->sessionHandler->order() !== null;
}

View file

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Exception;

View file

@ -12,6 +12,7 @@ use Inpsyde\PayPalCommerce\ApiClient\Factory\OrderFactory;
use Inpsyde\PayPalCommerce\ApiClient\Repository\CartRepository;
use Inpsyde\PayPalCommerce\Session\SessionHandler;
use Inpsyde\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
use Inpsyde\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use Inpsyde\PayPalCommerce\WcGateway\Processor\Processor;
use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsFields;
@ -19,6 +20,11 @@ use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsFields;
//phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
class WcGateway extends WcGatewayBase implements WcGatewayInterface
{
public const CAPTURED_META_KEY = '_ppcp_paypal_captured';
public const INTENT_META_KEY = '_ppcp_paypal_intent';
public const ORDER_ID_META_KEY = '_ppcp_paypal_order_id';
private $isSandbox = true;
private $sessionHandler;
private $orderEndpoint;
@ -27,6 +33,7 @@ class WcGateway extends WcGatewayBase implements WcGatewayInterface
private $settingsFields;
private $paymentsEndpoint;
private $processor;
private $notice;
public function __construct(
SessionHandler $sessionHandler,
@ -35,8 +42,10 @@ class WcGateway extends WcGatewayBase implements WcGatewayInterface
PaymentsEndpoint $paymentsEndpoint,
OrderFactory $orderFactory,
SettingsFields $settingsFields,
Processor $processor
Processor $processor,
AuthorizeOrderActionNotice $notice
) {
$this->sessionHandler = $sessionHandler;
$this->cartRepository = $cartRepository;
$this->orderEndpoint = $orderEndpoint;
@ -44,6 +53,7 @@ class WcGateway extends WcGatewayBase implements WcGatewayInterface
$this->orderFactory = $orderFactory;
$this->settingsFields = $settingsFields;
$this->processor = $processor;
$this->notice = $notice;
$this->method_title = __('PayPal Payments', 'woocommerce-paypal-gateway');
$this->method_description = __(
@ -79,8 +89,8 @@ class WcGateway extends WcGatewayBase implements WcGatewayInterface
$wcOrder = new \WC_Order($orderId);
$order = $this->sessionHandler->order();
$wcOrder->update_meta_data('_ppcp_paypal_order_id', $order->id());
$wcOrder->update_meta_data('_ppcp_paypal_intent', $order->intent());
$wcOrder->update_meta_data(self::ORDER_ID_META_KEY, $order->id());
$wcOrder->update_meta_data(self::INTENT_META_KEY, $order->intent());
$errorMessage = null;
if (!$order || !$order->status()->is(OrderStatus::APPROVED)) {
@ -92,7 +102,7 @@ class WcGateway extends WcGatewayBase implements WcGatewayInterface
__('Payment error: %s', 'woocommerce-paypal-gateway'),
$errorMessage
);
wc_add_notice( $notice, 'error');
wc_add_notice($notice, 'error');
return null;
}
@ -103,6 +113,7 @@ class WcGateway extends WcGatewayBase implements WcGatewayInterface
if ($order->intent() === 'AUTHORIZE') {
$order = $this->orderEndpoint->authorize($order);
$wcOrder->update_meta_data(self::CAPTURED_META_KEY, 'false');
}
$wcOrder->update_status('on-hold', __('Awaiting payment.', 'woocommerce-paypal-gateway'));
@ -122,14 +133,14 @@ class WcGateway extends WcGatewayBase implements WcGatewayInterface
{
$result = $this->processor->authorizedPayments()->process($wcOrder);
if ($result === 'INACCESSIBLE') {
AuthorizeOrderActionNotice::displayMessage(AuthorizeOrderActionNotice::NO_INFO);
if ($result === AuthorizedPaymentsProcessor::INACCESSIBLE) {
$this->notice->displayMessage(AuthorizeOrderActionNotice::NO_INFO);
}
if ($result === 'NOT_FOUND') {
AuthorizeOrderActionNotice::displayMessage(AuthorizeOrderActionNotice::NOT_FOUND);
if ($result === AuthorizedPaymentsProcessor::NOT_FOUND) {
$this->notice->displayMessage(AuthorizeOrderActionNotice::NOT_FOUND);
}
if ($result === 'ALREADY_CAPTURED') {
if ($result === AuthorizedPaymentsProcessor::ALREADY_CAPTURED) {
if ($wcOrder->get_status() === 'on-hold') {
$wcOrder->add_order_note(
__(
@ -138,19 +149,19 @@ class WcGateway extends WcGatewayBase implements WcGatewayInterface
)
);
$wcOrder->update_status('processing');
$wcOrder->update_meta_data('_ppcp_paypal_captured', 'true');
$wcOrder->update_meta_data(self::CAPTURED_META_KEY, 'true');
// TODO investigate why save has to be called
$wcOrder->save();
}
AuthorizeOrderActionNotice::displayMessage(AuthorizeOrderActionNotice::ALREADY_CAPTURED);
$this->notice->displayMessage(AuthorizeOrderActionNotice::ALREADY_CAPTURED);
}
if ($result === 'FAILED') {
AuthorizeOrderActionNotice::displayMessage(AuthorizeOrderActionNotice::FAILED);
if ($result === AuthorizedPaymentsProcessor::FAILED) {
$this->notice->displayMessage(AuthorizeOrderActionNotice::FAILED);
}
if ($result === 'SUCCESSFUL') {
if ($result === AuthorizedPaymentsProcessor::SUCCESSFUL) {
$wcOrder->add_order_note(
__(
'Payment successfully captured.',
@ -159,11 +170,11 @@ class WcGateway extends WcGatewayBase implements WcGatewayInterface
);
$wcOrder->update_status('processing');
$wcOrder->update_meta_data('_ppcp_paypal_captured', 'true');
$wcOrder->update_meta_data(self::CAPTURED_META_KEY, 'true');
// TODO investigate why save has to be called
$wcOrder->save();
AuthorizeOrderActionNotice::displayMessage(AuthorizeOrderActionNotice::SUCCESS);
$this->notice->displayMessage(AuthorizeOrderActionNotice::SUCCESS);
}
}

View file

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Gateway;

View file

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Gateway;

View file

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Notice;
@ -7,16 +8,18 @@ use Inpsyde\PayPalCommerce\AdminNotices\Entity\Message;
class AuthorizeOrderActionNotice
{
const QUERY_PARAM = 'ppcp-authorized-message';
public const QUERY_PARAM = 'ppcp-authorized-message';
const NO_INFO = 81;
const ALREADY_CAPTURED = 82;
const FAILED = 83;
const SUCCESS = 84;
const NOT_FOUND = 85;
public const NO_INFO = 81;
public const ALREADY_CAPTURED = 82;
public const FAILED = 83;
public const SUCCESS = 84;
public const NOT_FOUND = 85;
public function message() : ?Message {
$message = $this->getMessage();
public function message(): ?Message
{
$message = $this->currentMessage();
if (! $message) {
return null;
}
@ -24,7 +27,7 @@ class AuthorizeOrderActionNotice
return new Message($message['message'], $message['type']);
}
public function getMessage(): array
private function currentMessage(): array
{
$messages[self::NO_INFO] = [
'message' => __(
@ -62,18 +65,20 @@ class AuthorizeOrderActionNotice
'type' => 'success',
];
if (! isset($_GET['ppcp-message'])) {
//phpcs:disable WordPress.Security.NonceVerification.Recommended
if (! isset($_GET[self::QUERY_PARAM])) { // Input ok.
return [];
}
$messageId = absint($_GET[self::QUERY_PARAM]);
$messageId = absint($_GET[self::QUERY_PARAM]); // Input ok.
//phpcs:enable WordPress.Security.NonceVerification.Recommended
return (isset($messages[$messageId])) ? $messages[$messageId] : [];
}
public static function displayMessage(int $messageCode): void
public function displayMessage(int $messageCode): void
{
add_filter(
'redirect_post_location',
function ($location) use ($messageCode) {
static function ($location) use ($messageCode) {
return add_query_arg(
self::QUERY_PARAM,
$messageCode,

View file

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Notice;
@ -15,7 +16,7 @@ class ConnectAdminNotice
$this->settings = $settings;
}
public function connectMessage() : ?Message
public function connectMessage(): ?Message
{
if (!$this->shouldDisplay()) {
return null;
@ -31,7 +32,7 @@ class ConnectAdminNotice
// TODO: find a better way to get the url
admin_url('admin.php?page=wc-settings&tab=checkout&section=ppcp-gateway')
);
return new Message( $message, 'warning');
return new Message($message, 'warning');
}
protected function shouldDisplay(): bool

View file

@ -10,6 +10,7 @@ use Inpsyde\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
use Inpsyde\PayPalCommerce\ApiClient\Entity\Authorization;
use Inpsyde\PayPalCommerce\ApiClient\Entity\AuthorizationStatus;
use Inpsyde\PayPalCommerce\ApiClient\Entity\Order;
use Inpsyde\PayPalCommerce\WcGateway\Gateway\WcGateway;
class AuthorizedPaymentsProcessor
{
@ -25,16 +26,15 @@ class AuthorizedPaymentsProcessor
OrderEndpoint $orderEndpoint,
PaymentsEndpoint $paymentsEndpoint
) {
$this->orderEndpoint = $orderEndpoint;
$this->paymentsEndpoint = $paymentsEndpoint;
}
public function process(\WC_Order $wcOrder): string
{
$orderId = $this->getPayPalOrderId($wcOrder);
try {
$order = $this->getCurrentOrderInfo($orderId);
$order = $this->payPalOrderFromWcOrder($wcOrder);
} catch (Exception $exception) {
if ($exception->getCode() === 404) {
return self::NOT_FOUND;
@ -42,7 +42,7 @@ class AuthorizedPaymentsProcessor
return self::INACCESSIBLE;
}
$authorizations = $this->getAllAuthorizations($order);
$authorizations = $this->allAuthorizations($order);
if (!$this->areAuthorizationToCapture(...$authorizations)) {
return self::ALREADY_CAPTURED;
@ -57,17 +57,13 @@ class AuthorizedPaymentsProcessor
return self::SUCCESSFUL;
}
protected function getPayPalOrderId(\WC_Order $wcOrder): string
{
return $wcOrder->get_meta('_ppcp_paypal_order_id');
}
protected function getCurrentOrderInfo(string $orderId): Order
protected function payPalOrderFromWcOrder(\WC_Order $wcOrder): Order
{
$orderId = $wcOrder->get_meta(WcGateway::ORDER_ID_META_KEY);
return $this->orderEndpoint->order($orderId);
}
protected function getAllAuthorizations(Order $order): array
protected function allAuthorizations(Order $order): array
{
$authorizations = [];
foreach ($order->purchaseUnits() as $purchaseUnit) {
@ -102,7 +98,7 @@ class AuthorizedPaymentsProcessor
{
return array_filter(
$authorizations,
function (Authorization $authorization) {
static function (Authorization $authorization): bool {
return $authorization->status()->is(AuthorizationStatus::CREATED);
}
);
@ -115,7 +111,7 @@ class AuthorizedPaymentsProcessor
{
return array_filter(
$authorizations,
function (Authorization $authorization) {
static function (Authorization $authorization): bool {
return $authorization->status()->is(AuthorizationStatus::CAPTURED);
}
);

View file

@ -13,7 +13,9 @@ class Processor
$this->authorizedPaymentsProcessor = $authorizedPaymentsProcessor;
}
public function authorizedPayments() {
public function authorizedPayments(): AuthorizedPaymentsProcessor
{
return $this->authorizedPaymentsProcessor;
}
}

View file

@ -4,6 +4,8 @@ declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Settings;
//phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
class SettingsFields
{
public function fields(): array
@ -15,7 +17,7 @@ class SettingsFields
);
}
protected function gateway()
protected function gateway(): array
{
return [
'enabled' => [
@ -65,7 +67,7 @@ class SettingsFields
];
}
protected function account()
private function account(): array
{
return [
'account_settings' => [
@ -85,7 +87,7 @@ class SettingsFields
];
}
protected function buttons()
private function buttons(): array
{
return [
'button_settings' => [
@ -168,8 +170,8 @@ class SettingsFields
'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

@ -31,7 +31,7 @@ class WcGatewayModule implements ModuleInterface
{
add_filter(
'woocommerce_payment_gateways',
function ($methods) use ($container) : array {
static function ($methods) use ($container): array {
$methods[] = $container->get('wcgateway.gateway');
return (array)$methods;
}
@ -39,7 +39,7 @@ class WcGatewayModule implements ModuleInterface
add_filter(
'woocommerce_available_payment_gateways',
function ($methods) use ($container) : array {
static function ($methods) use ($container): array {
$disabler = $container->get('wcgateway.disabler');
/**
* @var DisableGateways $disabler
@ -50,7 +50,7 @@ class WcGatewayModule implements ModuleInterface
add_filter(
Repository::NOTICES_FILTER,
function ($notices) use ($container) : array {
static function ($notices) use ($container): array {
$notice = $container->get('wcgateway.notice.connect');
/**
* @var ConnectAdminNotice $notice
@ -71,7 +71,7 @@ class WcGatewayModule implements ModuleInterface
add_filter(
'woocommerce_order_actions',
function ($orderActions): array {
static function ($orderActions): array {
$orderActions['ppcp_authorize_order'] = __(
'Capture authorized PayPal payment',
'woocommerce-paypal-gateway'
@ -82,7 +82,7 @@ class WcGatewayModule implements ModuleInterface
add_action(
'woocommerce_order_action_ppcp_authorize_order',
function (\WC_Order $wcOrder) use ($container) {
static function (\WC_Order $wcOrder) use ($container) {
/**
* @var WcGateway $gateway
*/
@ -93,7 +93,7 @@ class WcGatewayModule implements ModuleInterface
add_action(
'woocommerce_order_actions_start',
function ($wcOrderId) use ($container) {
static function ($wcOrderId) use ($container) {
/**
* @var PaymentStatusOrderDetail $class
*/
@ -104,7 +104,7 @@ class WcGatewayModule implements ModuleInterface
add_filter(
'manage_edit-shop_order_columns',
function ($columns) use ($container) {
static function ($columns) use ($container) {
/**
* @var OrderTablePaymentStatusColumn $paymentStatusColumn
*/
@ -115,7 +115,7 @@ class WcGatewayModule implements ModuleInterface
add_action(
'manage_shop_order_posts_custom_column',
function ($column, $wcOrderId) use ($container) {
static function ($column, $wcOrderId) use ($container) {
/**
* @var OrderTablePaymentStatusColumn $paymentStatusColumn
*/