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);
return [

View file

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

View file

@ -19,13 +19,13 @@ use Inpsyde\PayPalCommerce\Button\Endpoint\RequestData;
use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException;
return [
'admin-notices.renderer' => function(ContainerInterface $container) : RendererInterface {
'admin-notices.renderer' => static function (ContainerInterface $container): RendererInterface {
$repository = $container->get('admin-notices.repository');
return new Renderer($repository);
},
'admin-notices.repository' => function(ContainerInterface $container) : RepositoryInterface {
'admin-notices.repository' => static function (ContainerInterface $container): RepositoryInterface {
return new Repository();
}
},
];

View file

@ -1,9 +1,9 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\AdminNotices\Entity;
class Message
{
@ -17,17 +17,17 @@ class Message
$this->dismissable = $dismissable;
}
public function message() : string
public function message(): string
{
return $this->message;
}
public function type() : string
public function type(): string
{
return $this->type;
}
public function isDismissable() : bool
public function isDismissable(): bool
{
return $this->dismissable;
}

View file

@ -1,11 +1,11 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\AdminNotices\Renderer;
interface RendererInterface
{
public function render() : bool;
public function render(): bool;
}

View file

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\AdminNotices\Repository;
@ -11,5 +12,5 @@ interface RepositoryInterface
/**
* @return Message[]
*/
public function currentMessages() : array;
public function currentMessages(): array;
}

View file

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
return [

View file

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

View file

@ -15,7 +15,7 @@ use Inpsyde\PayPalCommerce\Button\Endpoint\RequestData;
use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException;
return [
'button.smart-button' => function (ContainerInterface $container): SmartButtonInterface {
'button.smart-button' => static function (ContainerInterface $container): SmartButtonInterface {
$settings = $container->get('wcgateway.settings');
$payeeRepository = $container->get('api.repository.payee');
if (wc_string_to_bool($settings->get('enabled'))) {
@ -28,16 +28,16 @@ return [
}
return new DisabledSmartButton();
},
'button.url' => function (ContainerInterface $container): string {
'button.url' => static function (ContainerInterface $container): string {
return plugins_url(
'/modules/ppcp-button/',
dirname(__FILE__, 3) . '/woocommerce-paypal-commerce-gateway.php'
);
},
'button.request-data' => function (ContainerInterface $container): RequestData {
'button.request-data' => static function (ContainerInterface $container): RequestData {
return new RequestData();
},
'button.endpoint.change-cart' => function (ContainerInterface $container): ChangeCartEndpoint {
'button.endpoint.change-cart' => static function (ContainerInterface $container): ChangeCartEndpoint {
if (!\WC()->cart) {
throw new RuntimeException('cant initialize endpoint at this moment');
}
@ -47,14 +47,14 @@ return [
$repository = $container->get('api.repository.cart');
return new ChangeCartEndpoint($cart, $shipping, $requestData, $repository);
},
'button.endpoint.create-order' => function (ContainerInterface $container): CreateOrderEndpoint {
'button.endpoint.create-order' => static function (ContainerInterface $container): CreateOrderEndpoint {
$requestData = $container->get('button.request-data');
$repository = $container->get('api.repository.cart');
$apiClient = $container->get('api.endpoint.order');
$payerFactory = $container->get('api.factory.payer');
return new CreateOrderEndpoint($requestData, $repository, $apiClient, $payerFactory);
},
'button.endpoint.approve-order' => function (ContainerInterface $container): ApproveOrderEndpoint {
'button.endpoint.approve-order' => static function (ContainerInterface $container): ApproveOrderEndpoint {
$requestData = $container->get('button.request-data');
$apiClient = $container->get('api.endpoint.order');
$sessionHandler = $container->get('session.handler');

View file

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

View file

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Button\Assets;
@ -32,7 +33,7 @@ class SmartButton implements SmartButtonInterface
public function renderWrapper(): bool
{
$renderer = function () {
$renderer = static function () {
echo '<div id="ppc-button"></div>';
};
if (is_cart() && wc_string_to_bool($this->settings->get('button_cart_enabled'))) {
@ -52,7 +53,7 @@ class SmartButton implements SmartButtonInterface
if (wc_string_to_bool($this->settings->get('button_mini_cart_enabled'))) {
add_action(
'woocommerce_widget_shopping_cart_after_buttons',
function () {
static function () {
echo '<p id="ppc-button-minicart" class="woocommerce-mini-cart__buttons buttons"></p>';
},
30
@ -70,7 +71,10 @@ class SmartButton implements SmartButtonInterface
{
wp_enqueue_script(
'paypal-smart-button',
$this->moduleUrl . '/assets/js/button.js'
$this->moduleUrl . '/assets/js/button.js',
['jquery'],
1,
true
);
wp_localize_script(
@ -81,7 +85,7 @@ class SmartButton implements SmartButtonInterface
return true;
}
private function localizeScript() : array
private function localizeScript(): array
{
$localize = [
'redirect' => wc_get_checkout_url(),
@ -117,7 +121,9 @@ class SmartButton implements SmartButtonInterface
return $localize;
}
private function payerData() : ?array {
private function payerData(): ?array
{
$customer = WC()->customer;
if (! is_user_logged_in() || ! is_a($customer, \WC_Customer::class)) {
return null;
@ -145,7 +151,7 @@ class SmartButton implements SmartButtonInterface
];
}
private function url() : string
private function url(): string
{
$params = [
//ToDo: Add the correct client id, toggle when settings is set to sandbox

View file

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

View file

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Button;
@ -19,8 +20,8 @@ class ButtonModule implements ModuleInterface
public function setup(): ServiceProviderInterface
{
return new ServiceProvider(
require __DIR__.'/../services.php',
require __DIR__.'/../extensions.php'
require __DIR__ . '/../services.php',
require __DIR__ . '/../extensions.php'
);
}
@ -34,7 +35,7 @@ class ButtonModule implements ModuleInterface
*/
add_action(
'wp',
function () use ($container) {
static function () use ($container) {
if (is_admin()) {
return;
}
@ -42,7 +43,7 @@ class ButtonModule implements ModuleInterface
$smartButton->renderWrapper();
}
);
add_action('wp_enqueue_scripts', function () use ($container) {
add_action('wp_enqueue_scripts', static function () use ($container) {
$smartButton = $container->get('button.smart-button');
$smartButton->enqueue();
@ -50,7 +51,7 @@ class ButtonModule implements ModuleInterface
add_action(
'wc_ajax_' . ChangeCartEndpoint::ENDPOINT,
function () use ($container) {
static function () use ($container) {
$endpoint = $container->get('button.endpoint.change-cart');
/**
* @var ChangeCartEndpoint $endpoint
@ -61,7 +62,7 @@ class ButtonModule implements ModuleInterface
add_action(
'wc_ajax_' . ApproveOrderEndpoint::ENDPOINT,
function () use ($container) {
static function () use ($container) {
$endpoint = $container->get('button.endpoint.approve-order');
/**
* @var ChangeCartEndpoint $endpoint
@ -72,7 +73,7 @@ class ButtonModule implements ModuleInterface
add_action(
'wc_ajax_' . CreateOrderEndpoint::ENDPOINT,
function () use ($container) {
static function () use ($container) {
$endpoint = $container->get('button.endpoint.create-order');
/**
* @var ChangeCartEndpoint $endpoint

View file

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

View file

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Button\Endpoint;
@ -30,17 +31,18 @@ class ChangeCartEndpoint implements EndpointInterface
$this->repository = $repository;
}
public static function nonce() : string
public static function nonce(): string
{
return self::ENDPOINT;
}
public function handleRequest() : bool
public function handleRequest(): bool
{
try {
$data = $this->requestData->readRequest($this->nonce());
if (! isset($data['products'])
if (
! isset($data['products'])
|| ! is_array($data['products'])
) {
wp_send_json_error(
@ -95,7 +97,7 @@ class ChangeCartEndpoint implements EndpointInterface
if (count($errors)) {
$message = array_reduce(
$errors,
function (string $add, array $error) : string {
static function (string $add, array $error): string {
return $add . $error['notice'] . ' ';
},
''
@ -114,7 +116,7 @@ class ChangeCartEndpoint implements EndpointInterface
}
}
private function addProduct(\WC_Product $product, int $quantity) : bool
private function addProduct(\WC_Product $product, int $quantity): bool
{
return false !== $this->cart->add_to_cart($product->get_id(), $quantity);
}
@ -123,7 +125,7 @@ class ChangeCartEndpoint implements EndpointInterface
\WC_Product $product,
int $quantity,
array $postVariations
) : bool {
): bool {
foreach ($postVariations as $key => $value) {
$variations[$value['name']] = $value['value'];
@ -136,10 +138,10 @@ class ChangeCartEndpoint implements EndpointInterface
return false !== WC()->cart->add_to_cart($product->get_id(), $quantity, $variationId, $variations);
}
private function generatePurchaseUnits() : array
private function generatePurchaseUnits(): array
{
return array_map(
function (PurchaseUnit $lineItem) : array {
static function (PurchaseUnit $lineItem): array {
return $lineItem->toArray();
},
$this->repository->all()

View file

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Button\Endpoint;
@ -30,12 +31,12 @@ class CreateOrderEndpoint implements EndpointInterface
$this->payerFactory = $payerFactory;
}
public static function nonce() : string
public static function nonce(): string
{
return self::ENDPOINT;
}
public function handleRequest() : bool
public function handleRequest(): bool
{
try {
$data = $this->requestData->readRequest($this->nonce());
@ -46,7 +47,7 @@ class CreateOrderEndpoint implements EndpointInterface
// make sure the phone number contains only numbers and is max 14. chars long.
$number = $data['payer']['phone']['phone_number']['national_number'];
$number = preg_replace("/[^0-9]/", "", $number);
$number = substr($number,0,14);
$number = substr($number, 0, 14);
$data['payer']['phone']['phone_number']['national_number'] = $number;
}
$payer = $this->payerFactory->fromPayPalResponse(json_decode(json_encode($data['payer'])));

View file

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Button\Endpoint;
@ -6,7 +7,7 @@ namespace Inpsyde\PayPalCommerce\Button\Endpoint;
interface EndpointInterface
{
public static function nonce() : string;
public static function nonce(): string;
public function handleRequest() : bool;
public function handleRequest(): bool;
}

View file

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Button\Endpoint;
@ -8,11 +9,12 @@ use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException;
class RequestData
{
public function readRequest(string $nonce) : array
public function readRequest(string $nonce): array
{
$stream = file_get_contents('php://input');
$json = json_decode($stream, true);
if (! isset($json['nonce'])
if (
! isset($json['nonce'])
|| !wp_verify_nonce($json['nonce'], $nonce)
) {
throw new RuntimeException(
@ -23,7 +25,7 @@ class RequestData
return $this->sanitize($json);
}
private function sanitize(array $assocArray) : array
private function sanitize(array $assocArray): array
{
$data = [];
foreach ((array) $assocArray as $rawKey => $rawValue) {

View file

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

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
*/