woocommerce-paypal-payments/modules/ppcp-webhooks/services.php

192 lines
6.3 KiB
PHP
Raw Normal View History

2020-07-06 11:04:06 +03:00
<?php
2020-08-27 13:10:16 +03:00
/**
* The webhook module services.
*
2020-09-11 14:11:10 +03:00
* @package WooCommerce\PayPalCommerce\Webhooks
2020-08-27 13:10:16 +03:00
*/
2020-07-06 11:04:06 +03:00
declare(strict_types=1);
2020-09-11 14:11:10 +03:00
namespace WooCommerce\PayPalCommerce\Webhooks;
2020-07-06 11:04:06 +03:00
2021-09-16 13:48:20 +03:00
use Exception;
2021-09-21 17:52:44 +03:00
use Psr\Log\LoggerInterface;
2021-09-16 13:48:20 +03:00
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\WebhookEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Webhook;
2021-09-21 17:52:44 +03:00
use WooCommerce\PayPalCommerce\ApiClient\Factory\WebhookFactory;
2021-09-16 13:48:20 +03:00
use WooCommerce\PayPalCommerce\WcGateway\Assets\WebhooksStatusPageAssets;
2021-09-16 17:08:43 +03:00
use WooCommerce\PayPalCommerce\Webhooks\Endpoint\ResubscribeEndpoint;
2021-09-22 17:13:38 +03:00
use WooCommerce\PayPalCommerce\Webhooks\Endpoint\SimulateEndpoint;
use WooCommerce\PayPalCommerce\Webhooks\Endpoint\SimulationStateEndpoint;
2020-09-11 14:11:10 +03:00
use WooCommerce\PayPalCommerce\Webhooks\Handler\CheckoutOrderApproved;
use WooCommerce\PayPalCommerce\Webhooks\Handler\CheckoutOrderCompleted;
use WooCommerce\PayPalCommerce\Webhooks\Handler\PaymentCaptureCompleted;
use WooCommerce\PayPalCommerce\Webhooks\Handler\PaymentCaptureRefunded;
use WooCommerce\PayPalCommerce\Webhooks\Handler\PaymentCaptureReversed;
2020-07-06 11:04:06 +03:00
use Psr\Container\ContainerInterface;
2021-09-22 17:13:38 +03:00
use WooCommerce\PayPalCommerce\Webhooks\Status\WebhookSimulation;
2020-07-06 11:04:06 +03:00
2020-08-27 13:10:16 +03:00
return array(
2020-07-06 11:04:06 +03:00
2021-09-16 13:48:20 +03:00
'webhook.registrar' => function( $container ) : WebhookRegistrar {
2020-08-27 13:10:16 +03:00
$factory = $container->get( 'api.factory.webhook' );
$endpoint = $container->get( 'api.endpoint.webhook' );
$rest_endpoint = $container->get( 'webhook.endpoint.controller' );
$logger = $container->get( 'woocommerce.logger.woocommerce' );
2020-08-27 13:10:16 +03:00
return new WebhookRegistrar(
$factory,
$endpoint,
$rest_endpoint,
$logger
2020-08-27 13:10:16 +03:00
);
},
2021-09-16 13:48:20 +03:00
'webhook.endpoint.controller' => function( $container ) : IncomingWebhookEndpoint {
2020-08-27 13:10:16 +03:00
$webhook_endpoint = $container->get( 'api.endpoint.webhook' );
2021-09-21 17:52:44 +03:00
$webhook = $container->get( 'webhook.current' );
2020-08-27 13:10:16 +03:00
$handler = $container->get( 'webhook.endpoint.handler' );
$logger = $container->get( 'woocommerce.logger.woocommerce' );
$verify_request = ! defined( 'PAYPAL_WEBHOOK_REQUEST_VERIFICATION' ) || PAYPAL_WEBHOOK_REQUEST_VERIFICATION;
2021-09-22 17:13:38 +03:00
$webhook_event_factory = $container->get( 'api.factory.webhook-event' );
$simulation = $container->get( 'webhook.status.simulation' );
2020-08-27 13:10:16 +03:00
return new IncomingWebhookEndpoint(
$webhook_endpoint,
2021-09-21 17:52:44 +03:00
$webhook,
2020-08-27 13:10:16 +03:00
$logger,
$verify_request,
2021-09-22 17:13:38 +03:00
$webhook_event_factory,
$simulation,
2020-08-27 13:10:16 +03:00
... $handler
);
},
2021-09-16 13:48:20 +03:00
'webhook.endpoint.handler' => function( $container ) : array {
2020-08-27 13:10:16 +03:00
$logger = $container->get( 'woocommerce.logger.woocommerce' );
$prefix = $container->get( 'api.prefix' );
$order_endpoint = $container->get( 'api.endpoint.order' );
return array(
new CheckoutOrderApproved( $logger, $prefix, $order_endpoint ),
new CheckoutOrderCompleted( $logger, $prefix ),
new PaymentCaptureRefunded( $logger, $prefix ),
new PaymentCaptureReversed( $logger, $prefix ),
new PaymentCaptureCompleted( $logger, $prefix ),
);
},
2021-09-16 13:48:20 +03:00
2021-09-21 17:52:44 +03:00
'webhook.current' => function( $container ) : ?Webhook {
$data = (array) get_option( WebhookRegistrar::KEY, array() );
if ( empty( $data ) ) {
return null;
}
$factory = $container->get( 'api.factory.webhook' );
assert( $factory instanceof WebhookFactory );
try {
return $factory->from_array( $data );
} catch ( Exception $exception ) {
$logger = $container->get( 'woocommerce.logger.woocommerce' );
assert( $logger instanceof LoggerInterface );
$logger->error( 'Failed to parse the stored webhook data: ' . $exception->getMessage() );
return null;
}
},
'webhook.is-registered' => function( $container ) : bool {
return $container->get( 'webhook.current' ) !== null;
},
2021-09-16 13:48:20 +03:00
'webhook.status.registered-webhooks' => function( $container ) : array {
$endpoint = $container->get( 'api.endpoint.webhook' );
assert( $endpoint instanceof WebhookEndpoint );
return $endpoint->list();
},
'webhook.status.registered-webhooks-data' => function( $container ) : array {
$empty_placeholder = __( 'No webhooks found.', 'woocommerce-paypal-payments' );
$webhooks = array();
try {
$webhooks = $container->get( 'webhook.status.registered-webhooks' );
} catch ( Exception $exception ) {
$empty_placeholder = sprintf(
'<span class="error">%s</span>',
__( 'Failed to load webhooks.', 'woocommerce-paypal-payments' )
);
}
return array(
'headers' => array(
__( 'URL', 'woocommerce-paypal-payments' ),
__( 'Tracked events', 'woocommerce-paypal-payments' ),
),
'data' => array_map(
function ( Webhook $webhook ): array {
return array(
esc_html( $webhook->url() ),
implode(
',<br/>',
array_map(
'esc_html',
$webhook->humanfriendly_event_names()
)
),
);
},
$webhooks
),
'empty_placeholder' => $empty_placeholder,
);
},
2021-09-22 17:13:38 +03:00
'webhook.status.simulation' => function( $container ) : WebhookSimulation {
$webhook_endpoint = $container->get( 'api.endpoint.webhook' );
$webhook = $container->get( 'webhook.current' );
return new WebhookSimulation(
$webhook_endpoint,
2021-09-23 10:27:48 +03:00
$webhook,
'PAYMENT.AUTHORIZATION.CREATED'
2021-09-22 17:13:38 +03:00
);
},
2021-09-16 13:48:20 +03:00
'webhook.status.assets' => function( $container ) : WebhooksStatusPageAssets {
return new WebhooksStatusPageAssets(
$container->get( 'webhook.module-url' )
);
},
2021-09-16 17:08:43 +03:00
'webhook.endpoint.resubscribe' => static function ( $container ) : ResubscribeEndpoint {
$registrar = $container->get( 'webhook.registrar' );
$request_data = $container->get( 'button.request-data' );
return new ResubscribeEndpoint(
$registrar,
$request_data
);
},
2021-09-22 17:13:38 +03:00
'webhook.endpoint.simulate' => static function ( $container ) : SimulateEndpoint {
$simulation = $container->get( 'webhook.status.simulation' );
$request_data = $container->get( 'button.request-data' );
return new SimulateEndpoint(
$simulation,
$request_data
);
},
'webhook.endpoint.simulation-state' => static function ( $container ) : SimulationStateEndpoint {
$simulation = $container->get( 'webhook.status.simulation' );
return new SimulationStateEndpoint(
$simulation
);
},
2021-09-16 13:48:20 +03:00
'webhook.module-url' => static function ( $container ): string {
return plugins_url(
'/modules/ppcp-webhooks/',
dirname( __FILE__, 3 ) . '/woocommerce-paypal-payments.php'
);
},
2020-08-27 13:10:16 +03:00
);