Change order string payment

This commit is contained in:
Mészáros Róbert 2020-04-13 22:30:57 +03:00
parent ab822033a4
commit b655750485
4 changed files with 86 additions and 43 deletions

View file

@ -7,6 +7,7 @@ use Dhii\Data\Container\ContainerInterface;
use Inpsyde\PayPalCommerce\WcGateway\Checkout\DisableGateways;
use Inpsyde\PayPalCommerce\WcGateway\Gateway\WcGateway;
use Inpsyde\PayPalCommerce\WcGateway\Gateway\WcGatewayBase;
use Inpsyde\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
use Inpsyde\PayPalCommerce\WcGateway\Notice\ConnectAdminNotice;
use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsFields;
@ -36,7 +37,11 @@ return [
$settings = $container->get('wcgateway.settings');
return new ConnectAdminNotice($settings);
},
'wcgateway.settings.fields' => function (ContainerInterface $container) : SettingsFields {
'wcgateway.notice.authorize-order-action' =>
function (ContainerInterface $container): AuthorizeOrderActionNotice {
return new AuthorizeOrderActionNotice();
},
'wcgateway.settings.fields' => function (ContainerInterface $container): SettingsFields {
return new SettingsFields();
},
];

View file

@ -11,6 +11,7 @@ use Inpsyde\PayPalCommerce\ApiClient\Exception\RuntimeException;
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\Settings\SettingsFields;
//phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
@ -31,6 +32,7 @@ class WcGateway extends WcGatewayBase implements WcGatewayInterface
OrderFactory $orderFactory,
SettingsFields $settingsFields
) {
$this->sessionHandler = $sessionHandler;
$this->cartRepository = $cartRepository;
$this->endpoint = $endpoint;
@ -108,53 +110,28 @@ class WcGateway extends WcGatewayBase implements WcGatewayInterface
try {
$order = $this->endpoint->order($payPalOrderId);
} catch (RuntimeException $e) {
// TODO: add a notice instead
$wcOrder->add_order_note(
__(
'Unable to capture charge:',
'woocommerce-paypal-gateway'
) . ' ' . $e->getMessage()
);
}
if ($order->status()->is(OrderStatus::COMPLETED)) {
// TODO: add a notice instead
$wcOrder->add_order_note(
__(
'Order already completed',
'woocommerce-paypal-gateway'
)
);
} catch (RuntimeException $exception) {
AuthorizeOrderActionNotice::displayMessage(AuthorizeOrderActionNotice::NO_INFO);
return;
}
if (!$order->status()->is(OrderStatus::APPROVED)) {
// TODO: add a notice instead
$wcOrder->add_order_note(
__(
'Order not approved',
'woocommerce-paypal-gateway'
)
);
if ($order->status()->is(OrderStatus::COMPLETED)) {
AuthorizeOrderActionNotice::displayMessage(AuthorizeOrderActionNotice::ALREADY_AUTHORIZED);
return;
}
try {
$this->endpoint->authorize($payPalOrderId);
} catch (RuntimeException $e) {
// TODO: display admin error message
$wcOrder->add_order_note(
__(
'Unable to capture charge:',
'woocommerce-paypal-gateway'
) . ' ' . $e->getMessage()
);
} catch (RuntimeException $exception) {
AuthorizeOrderActionNotice::displayMessage(AuthorizeOrderActionNotice::FAILED);
return;
}
AuthorizeOrderActionNotice::displayMessage(AuthorizeOrderActionNotice::SUCCESS);
$wcOrder->add_order_note(
__(
'Payment authorized.',
'Payment successfully authorized.',
'woocommerce-paypal-gateway'
)
);

View file

@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Notice;
class AuthorizeOrderActionNotice
{
const NO_INFO = 81;
const ALREADY_AUTHORIZED = 82;
const FAILED = 83;
const SUCCESS = 84;
public function registerMessages(array $messages): array
{
$messages['shop_order'][self::NO_INFO] = __(
'Could not retrieve payment information. Try again.',
'woocommerce-paypal-gateway'
);
$messages['shop_order'][self::ALREADY_AUTHORIZED] = __(
'Payment was previously authorized.',
'woocommerce-paypal-gateway'
);
$messages['shop_order'][self::FAILED] = __(
'Authorization failed',
'woocommerce-paypal-gateway'
);
$messages['shop_order'][self::SUCCESS] = __(
'Authorization successful',
'woocommerce-paypal-gateway'
);
return $messages;
}
public static function displayMessage(int $messageCode): void
{
add_filter(
'redirect_post_location',
function ($location) use ($messageCode) {
return add_query_arg(
'message',
$messageCode,
$location
);
}
);
}
}

View file

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway;
@ -7,6 +8,7 @@ use Dhii\Container\ServiceProvider;
use Dhii\Modular\Module\ModuleInterface;
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 Interop\Container\ServiceProviderInterface;
use Psr\Container\ContainerInterface;
@ -17,8 +19,8 @@ class WcGatewayModule 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'
);
}
@ -27,9 +29,8 @@ class WcGatewayModule implements ModuleInterface
add_filter(
'woocommerce_payment_gateways',
function ($methods) use ($container) : array {
$methods[] = $container->get('wcgateway.gateway');
return (array) $methods;
return (array)$methods;
}
);
@ -40,7 +41,7 @@ class WcGatewayModule implements ModuleInterface
/**
* @var DisableGateways $disabler
*/
return $disabler->handler((array) $methods);
return $disabler->handler((array)$methods);
}
);
@ -57,9 +58,9 @@ class WcGatewayModule implements ModuleInterface
add_filter(
'woocommerce_order_actions',
function ($orderActions) : array {
function ($orderActions): array {
$orderActions['ppcp_authorize_order'] = __(
'Authorize PayPal Order',
'Authorize PayPal Payment',
'woocommerce-paypal-gateway'
);
return $orderActions;
@ -76,5 +77,17 @@ class WcGatewayModule implements ModuleInterface
$gateway->authorizeOrder($wcOrder);
}
);
add_filter(
'post_updated_messages',
function ($messages) use ($container) {
/**
* @var AuthorizeOrderActionNotice $authorizeOrderAction
*/
$authorizeOrderAction = $container->get('wcgateway.notice.authorize-order-action');
return $authorizeOrderAction->registerMessages($messages);
},
20
);
}
}