mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 10:55:00 +08:00
phpcs
This commit is contained in:
parent
21cfd7c091
commit
e448932988
8 changed files with 60 additions and 39 deletions
|
@ -1,4 +1,5 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Webhooks\Handler;
|
||||
|
@ -24,23 +25,23 @@ class CheckoutOrderCompleted implements RequestHandler
|
|||
return in_array($request['event_type'], $this->eventTypes(), true);
|
||||
}
|
||||
|
||||
// phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
|
||||
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response
|
||||
{
|
||||
$response = ['success' => false];
|
||||
$orderIds = array_filter(
|
||||
array_map(
|
||||
function(array $purchaseUnit) : string {
|
||||
static function (array $purchaseUnit): string {
|
||||
return isset($purchaseUnit['custom_id']) ? (string) $purchaseUnit['custom_id'] : '';
|
||||
},
|
||||
isset($request['resource']['purchase_units']) ? (array) $request['resource']['purchase_units'] : []
|
||||
),
|
||||
function(string $orderId) : bool {
|
||||
static function (string $orderId): bool {
|
||||
return ! empty($orderId);
|
||||
}
|
||||
);
|
||||
|
||||
if (empty($orderIds)) {
|
||||
|
||||
$message = sprintf(
|
||||
// translators: %s is the PayPal webhook Id.
|
||||
__('No order for webhook event %s was found.', 'woocommerce-paypal-commerce-gateway'),
|
||||
|
@ -92,7 +93,11 @@ class CheckoutOrderCompleted implements RequestHandler
|
|||
);
|
||||
$this->logger->log(
|
||||
'info',
|
||||
__('Order ' . $wcOrder->get_id() . ' has been updated through PayPal' , 'woocommerce-paypal-commerce-gateway'),
|
||||
sprintf(
|
||||
// translators: %s is the order ID.
|
||||
__('Order %s has been updated through PayPal', 'woocommerce-paypal-commerce-gateway'),
|
||||
(string) $wcOrder->get_id()
|
||||
),
|
||||
[
|
||||
'request' => $request,
|
||||
'order' => $wcOrder,
|
||||
|
@ -102,4 +107,5 @@ class CheckoutOrderCompleted implements RequestHandler
|
|||
$response['success'] = true;
|
||||
return rest_ensure_response($response);
|
||||
}
|
||||
}
|
||||
// phpcs:enable Inpsyde.CodeQuality.FunctionLength.TooLong
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Webhooks\Handler;
|
||||
|
@ -24,6 +25,7 @@ class PaymentCaptureRefunded implements RequestHandler
|
|||
return in_array($request['event_type'], $this->eventTypes(), true);
|
||||
}
|
||||
|
||||
// phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
|
||||
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response
|
||||
{
|
||||
$response = ['success' => false];
|
||||
|
@ -68,13 +70,16 @@ class PaymentCaptureRefunded implements RequestHandler
|
|||
*/
|
||||
$refund = wc_create_refund([
|
||||
'order_id' => $wcOrder->get_id(),
|
||||
'amount' => $request['resource']['amount']['value']
|
||||
'amount' => $request['resource']['amount']['value'],
|
||||
]);
|
||||
if (is_wp_error($refund)) {
|
||||
|
||||
$this->logger->log(
|
||||
'warning',
|
||||
__('Order ' . $wcOrder->get_id() . ' could not be refunded' , 'woocommerce-paypal-commerce-gateway'),
|
||||
sprintf(
|
||||
// translators: %s is the order id.
|
||||
__('Order %s could not be refunded', 'woocommerce-paypal-commerce-gateway'),
|
||||
(string) $wcOrder->get_id()
|
||||
),
|
||||
[
|
||||
'request' => $request,
|
||||
'error' => $refund,
|
||||
|
@ -88,8 +93,8 @@ class PaymentCaptureRefunded implements RequestHandler
|
|||
$this->logger->log(
|
||||
'info',
|
||||
sprintf(
|
||||
//translators: %1$s is the order id %2$s is the amount which has been refunded.
|
||||
__('Order %1$s has been refunded with %2$s through PayPal' , 'woocommerce-paypal-commerce-gateway'),
|
||||
// translators: %1$s is the order id %2$s is the amount which has been refunded.
|
||||
__('Order %1$s has been refunded with %2$s through PayPal', 'woocommerce-paypal-commerce-gateway'),
|
||||
(string) $wcOrder->get_id(),
|
||||
(string) $refund->get_amount()
|
||||
),
|
||||
|
@ -101,4 +106,5 @@ class PaymentCaptureRefunded implements RequestHandler
|
|||
$response['success'] = true;
|
||||
return rest_ensure_response($response);
|
||||
}
|
||||
}
|
||||
// phpcs:enable Inpsyde.CodeQuality.FunctionLength.TooLong
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Webhooks\Handler;
|
||||
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class PaymentCaptureReversed implements RequestHandler
|
||||
|
@ -29,6 +29,7 @@ class PaymentCaptureReversed implements RequestHandler
|
|||
return in_array($request['event_type'], $this->eventTypes(), true);
|
||||
}
|
||||
|
||||
// phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
|
||||
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response
|
||||
{
|
||||
$response = ['success' => false];
|
||||
|
@ -74,12 +75,12 @@ class PaymentCaptureReversed implements RequestHandler
|
|||
$response['success'] = (bool) $wcOrder->update_status('cancelled');
|
||||
|
||||
$message = $response['success'] ? sprintf(
|
||||
//translators: %1$s is the order id.
|
||||
__('Order %1$s has been cancelled through PayPal' , 'woocommerce-paypal-commerce-gateway'),
|
||||
// translators: %1$s is the order id.
|
||||
__('Order %1$s has been cancelled through PayPal', 'woocommerce-paypal-commerce-gateway'),
|
||||
(string) $wcOrder->get_id()
|
||||
) : sprintf(
|
||||
//translators: %1$s is the order id.
|
||||
__('Failed to cancel order %1$s through PayPal' , 'woocommerce-paypal-commerce-gateway'),
|
||||
// translators: %1$s is the order id.
|
||||
__('Failed to cancel order %1$s through PayPal', 'woocommerce-paypal-commerce-gateway'),
|
||||
(string) $wcOrder->get_id()
|
||||
);
|
||||
$this->logger->log(
|
||||
|
@ -92,4 +93,5 @@ class PaymentCaptureReversed implements RequestHandler
|
|||
);
|
||||
return rest_ensure_response($response);
|
||||
}
|
||||
}
|
||||
// phpcs:enable Inpsyde.CodeQuality.FunctionLength.TooLong
|
||||
}
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Webhooks\Handler;
|
||||
|
||||
|
||||
interface RequestHandler
|
||||
{
|
||||
|
||||
public function eventTypes() : array;
|
||||
public function eventTypes(): array;
|
||||
|
||||
public function responsibleForRequest(\WP_REST_Request $request) : bool;
|
||||
public function responsibleForRequest(\WP_REST_Request $request): bool;
|
||||
|
||||
public function handleRequest(\WP_REST_Request $request) : \WP_REST_Response;
|
||||
}
|
||||
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response;
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Webhooks;
|
||||
|
||||
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\WebhookEndpoint;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Factory\WebhookFactory;
|
||||
|
@ -32,7 +32,7 @@ class IncomingWebhookEndpoint
|
|||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function register() : bool
|
||||
public function register(): bool
|
||||
{
|
||||
return (bool) register_rest_route(
|
||||
self::NAMESPACE,
|
||||
|
@ -54,7 +54,8 @@ class IncomingWebhookEndpoint
|
|||
);
|
||||
}
|
||||
|
||||
public function verifyRequest() : bool {
|
||||
public function verifyRequest(): bool
|
||||
{
|
||||
try {
|
||||
$data = (array) get_option(WebhookRegistrar::KEY, []);
|
||||
$webhook = $this->webhookFactory->fromArray($data);
|
||||
|
@ -64,7 +65,8 @@ class IncomingWebhookEndpoint
|
|||
}
|
||||
}
|
||||
|
||||
public function handleRequest(\WP_REST_Request $request) : \WP_REST_Response {
|
||||
public function handleRequest(\WP_REST_Request $request): \WP_REST_Response
|
||||
{
|
||||
|
||||
foreach ($this->handlers as $handler) {
|
||||
if ($handler->responsibleForRequest($request)) {
|
||||
|
@ -72,8 +74,9 @@ class IncomingWebhookEndpoint
|
|||
$this->logger->log(
|
||||
'info',
|
||||
sprintf(
|
||||
// translators: %s is the event type
|
||||
__('Webhook has been handled by %s', 'woocommerce-paypal-commerce-gateway'),
|
||||
($handler->eventTypes())?current($handler->eventTypes()):''
|
||||
($handler->eventTypes()) ? current($handler->eventTypes()) : ''
|
||||
),
|
||||
[
|
||||
'request' => $request,
|
||||
|
@ -88,11 +91,13 @@ class IncomingWebhookEndpoint
|
|||
return rest_ensure_response($response);
|
||||
}
|
||||
|
||||
public function url() : string {
|
||||
return str_replace('http', 'https', rest_url(self::NAMESPACE . '/' . self::ROUTE));
|
||||
public function url(): string
|
||||
{
|
||||
return rest_url(self::NAMESPACE . '/' . self::ROUTE);
|
||||
}
|
||||
|
||||
public function handledEventTypes() : array {
|
||||
public function handledEventTypes(): array
|
||||
{
|
||||
$eventTypes = [];
|
||||
foreach ($this->handlers as $handler) {
|
||||
$eventTypes = array_merge($eventTypes, $handler->eventTypes());
|
||||
|
|
|
@ -27,7 +27,7 @@ class WebhookModule implements ModuleInterface
|
|||
{
|
||||
add_action(
|
||||
'rest_api_init',
|
||||
function() use ($container) {
|
||||
static function () use ($container) {
|
||||
$endpoint = $container->get('webhook.endpoint.controller');
|
||||
/**
|
||||
* @var IncomingWebhookEndpoint $endpoint
|
||||
|
@ -38,7 +38,7 @@ class WebhookModule implements ModuleInterface
|
|||
|
||||
add_action(
|
||||
WebhookRegistrar::EVENT_HOOK,
|
||||
function() use ($container) {
|
||||
static function () use ($container) {
|
||||
$registrar = $container->get('webhook.registrar');
|
||||
$registrar->register();
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ class WebhookModule implements ModuleInterface
|
|||
|
||||
add_action(
|
||||
'woocommerce-paypal-commerce-gateway.deactivate',
|
||||
function() use ($container) {
|
||||
static function () use ($container) {
|
||||
$registrar = $container->get('webhook.registrar');
|
||||
$registrar->unregister();
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Webhooks;
|
||||
|
||||
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\WebhookEndpoint;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Factory\WebhookFactory;
|
||||
|
@ -22,12 +22,13 @@ class WebhookRegistrar
|
|||
WebhookEndpoint $endpoint,
|
||||
IncomingWebhookEndpoint $restEndpoint
|
||||
) {
|
||||
|
||||
$this->webhookFactory = $webhookFactory;
|
||||
$this->endpoint = $endpoint;
|
||||
$this->restEndpoint = $restEndpoint;
|
||||
}
|
||||
|
||||
public function register() : bool
|
||||
public function register(): bool
|
||||
{
|
||||
$webhook = $this->webhookFactory->forUrlAndEvents(
|
||||
$this->restEndpoint->url(),
|
||||
|
@ -36,7 +37,7 @@ class WebhookRegistrar
|
|||
|
||||
try {
|
||||
$created = $this->endpoint->create($webhook);
|
||||
if(empty($created->id())) {
|
||||
if (empty($created->id())) {
|
||||
return false;
|
||||
}
|
||||
update_option(
|
||||
|
@ -53,7 +54,8 @@ class WebhookRegistrar
|
|||
}
|
||||
}
|
||||
|
||||
public function unregister() : bool {
|
||||
public function unregister(): bool
|
||||
{
|
||||
$data = (array) get_option(self::KEY, []);
|
||||
if (! $data) {
|
||||
return false;
|
||||
|
@ -70,4 +72,4 @@ class WebhookRegistrar
|
|||
}
|
||||
return $success;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue