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

235 lines
8.8 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;
use WooCommerce\PayPalCommerce\Onboarding\State;
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;
2023-03-08 09:17:30 +01:00
use WooCommerce\PayPalCommerce\Webhooks\Handler\BillingPlanPricingChangeActivated;
use WooCommerce\PayPalCommerce\Webhooks\Handler\BillingPlanUpdated;
use WooCommerce\PayPalCommerce\Webhooks\Handler\BillingSubscriptionCancelled;
use WooCommerce\PayPalCommerce\Webhooks\Handler\CatalogProductUpdated;
2020-09-11 14:11:10 +03:00
use WooCommerce\PayPalCommerce\Webhooks\Handler\CheckoutOrderApproved;
use WooCommerce\PayPalCommerce\Webhooks\Handler\CheckoutOrderCompleted;
2022-09-12 10:35:27 +02:00
use WooCommerce\PayPalCommerce\Webhooks\Handler\CheckoutPaymentApprovalReversed;
2020-09-11 14:11:10 +03:00
use WooCommerce\PayPalCommerce\Webhooks\Handler\PaymentCaptureCompleted;
use WooCommerce\PayPalCommerce\Webhooks\Handler\PaymentCapturePending;
2020-09-11 14:11:10 +03:00
use WooCommerce\PayPalCommerce\Webhooks\Handler\PaymentCaptureRefunded;
use WooCommerce\PayPalCommerce\Webhooks\Handler\PaymentCaptureReversed;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\Webhooks\Handler\PaymentSaleCompleted;
use WooCommerce\PayPalCommerce\Webhooks\Handler\PaymentSaleRefunded;
use WooCommerce\PayPalCommerce\Webhooks\Handler\VaultPaymentTokenCreated;
use WooCommerce\PayPalCommerce\Webhooks\Handler\VaultPaymentTokenDeleted;
2021-10-13 16:57:06 +03:00
use WooCommerce\PayPalCommerce\Webhooks\Status\Assets\WebhooksStatusPageAssets;
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-30 12:52:15 +02:00
'webhook.registrar' => function( ContainerInterface $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' );
2021-11-24 11:52:14 +02:00
$last_webhook_storage = $container->get( 'webhook.last-webhook-storage' );
$logger = $container->get( 'woocommerce.logger.woocommerce' );
2020-08-27 13:10:16 +03:00
return new WebhookRegistrar(
$factory,
$endpoint,
$rest_endpoint,
2021-11-24 11:52:14 +02:00
$last_webhook_storage,
$logger
2020-08-27 13:10:16 +03:00
);
},
2021-09-30 12:52:15 +02:00
'webhook.endpoint.controller' => function( ContainerInterface $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' );
2021-11-24 11:52:14 +02:00
$last_webhook_storage = $container->get( 'webhook.last-webhook-storage' );
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,
2021-11-24 11:52:14 +02:00
$last_webhook_storage,
2020-08-27 13:10:16 +03:00
... $handler
);
},
2021-09-30 12:52:15 +02:00
'webhook.endpoint.handler' => function( ContainerInterface $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' );
2022-03-04 12:13:41 +01:00
$authorized_payments_processor = $container->get( 'wcgateway.processor.authorized-payments' );
$payment_token_factory = $container->get( 'vaulting.payment-token-factory' );
2022-11-23 17:00:04 +01:00
2020-08-27 13:10:16 +03:00
return array(
new CheckoutOrderApproved( $logger, $order_endpoint ),
new CheckoutOrderCompleted( $logger ),
2022-09-12 12:01:13 +02:00
new CheckoutPaymentApprovalReversed( $logger ),
new PaymentCaptureRefunded( $logger ),
new PaymentCaptureReversed( $logger ),
new PaymentCaptureCompleted( $logger, $order_endpoint ),
new VaultPaymentTokenCreated( $logger, $prefix, $authorized_payments_processor, $payment_token_factory ),
new VaultPaymentTokenDeleted( $logger ),
new PaymentCapturePending( $logger ),
new PaymentSaleCompleted( $logger ),
new PaymentSaleRefunded( $logger ),
new BillingSubscriptionCancelled( $logger ),
2023-03-08 09:17:30 +01:00
new BillingPlanPricingChangeActivated( $logger ),
2023-05-02 12:35:46 +02:00
new CatalogProductUpdated( $logger ),
new BillingPlanUpdated( $logger ),
2020-08-27 13:10:16 +03:00
);
},
2021-09-16 13:48:20 +03:00
2021-10-13 15:02:10 +03:00
'webhook.current' => function( ContainerInterface $container ) : ?Webhook {
2021-09-21 17:52:44 +03:00
$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;
}
},
2021-10-13 15:02:10 +03:00
'webhook.is-registered' => function( ContainerInterface $container ) : bool {
2021-09-21 17:52:44 +03:00
return $container->get( 'webhook.current' ) !== null;
},
2021-10-13 15:02:10 +03:00
'webhook.status.registered-webhooks' => function( ContainerInterface $container ) : array {
2021-09-16 13:48:20 +03:00
$endpoint = $container->get( 'api.endpoint.webhook' );
assert( $endpoint instanceof WebhookEndpoint );
2023-03-06 10:19:23 +02:00
$state = $container->get( 'onboarding.state' );
if ( $state->current_state() >= State::STATE_ONBOARDED ) {
return $endpoint->list();
}
return array();
2021-09-16 13:48:20 +03:00
},
2021-10-13 15:02:10 +03:00
'webhook.status.registered-webhooks-data' => function( ContainerInterface $container ) : array {
2021-09-16 13:48:20 +03:00
$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-10-13 15:02:10 +03:00
'webhook.status.simulation' => function( ContainerInterface $container ) : WebhookSimulation {
2021-09-22 17:13:38 +03:00
$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,
'CHECKOUT.ORDER.APPROVED',
'2.0'
2021-09-22 17:13:38 +03:00
);
},
2021-10-13 15:02:10 +03:00
'webhook.status.assets' => function( ContainerInterface $container ) : WebhooksStatusPageAssets {
2021-09-16 13:48:20 +03:00
return new WebhooksStatusPageAssets(
2022-02-17 18:51:24 +02:00
$container->get( 'webhook.module-url' ),
$container->get( 'ppcp.asset-version' ),
$container->get( 'onboarding.environment' )
2021-09-16 13:48:20 +03:00
);
},
2021-10-13 15:02:10 +03:00
'webhook.endpoint.resubscribe' => static function ( ContainerInterface $container ) : ResubscribeEndpoint {
2021-09-16 17:08:43 +03:00
$registrar = $container->get( 'webhook.registrar' );
$request_data = $container->get( 'button.request-data' );
return new ResubscribeEndpoint(
$registrar,
$request_data
);
},
2021-10-13 15:02:10 +03:00
'webhook.endpoint.simulate' => static function ( ContainerInterface $container ) : SimulateEndpoint {
2021-09-22 17:13:38 +03:00
$simulation = $container->get( 'webhook.status.simulation' );
$request_data = $container->get( 'button.request-data' );
return new SimulateEndpoint(
$simulation,
$request_data
);
},
2021-10-13 15:02:10 +03:00
'webhook.endpoint.simulation-state' => static function ( ContainerInterface $container ) : SimulationStateEndpoint {
2021-09-22 17:13:38 +03:00
$simulation = $container->get( 'webhook.status.simulation' );
return new SimulationStateEndpoint(
$simulation
);
},
2023-03-08 17:36:38 +02:00
'webhook.last-webhook-storage' => static function ( ContainerInterface $container ): WebhookEventStorage {
return new WebhookEventStorage( $container->get( 'webhook.last-webhook-storage.key' ) );
2021-11-24 11:52:14 +02:00
},
'webhook.last-webhook-storage.key' => static function ( ContainerInterface $container ): string {
return 'ppcp-last-webhook';
},
2021-10-13 15:02:10 +03:00
'webhook.module-url' => static function ( ContainerInterface $container ): string {
2021-09-16 13:48:20 +03:00
return plugins_url(
'/modules/ppcp-webhooks/',
dirname( realpath( __FILE__ ), 3 ) . '/woocommerce-paypal-payments.php'
2021-09-16 13:48:20 +03:00
);
},
2020-08-27 13:10:16 +03:00
);