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\Entity\Webhook;
|
2021-09-21 17:52:44 +03:00
|
|
|
use WooCommerce\PayPalCommerce\ApiClient\Factory\WebhookFactory;
|
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;
|
2023-04-13 15:24:21 +02:00
|
|
|
use WooCommerce\PayPalCommerce\Webhooks\Handler\BillingPlanUpdated;
|
2023-03-07 10:35:39 +01:00
|
|
|
use WooCommerce\PayPalCommerce\Webhooks\Handler\BillingSubscriptionCancelled;
|
2023-04-13 11:55:50 +02:00
|
|
|
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;
|
2022-07-06 10:35:53 +02:00
|
|
|
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;
|
2022-11-09 10:11:31 +02:00
|
|
|
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
2023-02-08 11:09:16 +01:00
|
|
|
use WooCommerce\PayPalCommerce\Webhooks\Handler\PaymentSaleCompleted;
|
2023-05-10 16:59:03 +02:00
|
|
|
use WooCommerce\PayPalCommerce\Webhooks\Handler\PaymentSaleRefunded;
|
2021-12-20 09:43:36 +01:00
|
|
|
use WooCommerce\PayPalCommerce\Webhooks\Handler\VaultPaymentTokenCreated;
|
2022-11-24 15:42:58 +01:00
|
|
|
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' );
|
2021-09-13 11:37:21 +03:00
|
|
|
$logger = $container->get( 'woocommerce.logger.woocommerce' );
|
2020-08-27 13:10:16 +03:00
|
|
|
return new WebhookRegistrar(
|
|
|
|
$factory,
|
|
|
|
$endpoint,
|
2021-09-13 11:37:21 +03:00
|
|
|
$rest_endpoint,
|
2021-11-24 11:52:14 +02:00
|
|
|
$last_webhook_storage,
|
2021-09-13 11:37:21 +03:00
|
|
|
$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-07-09 08:29:17 +03:00
|
|
|
|
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' );
|
2023-01-02 12:50:13 +01:00
|
|
|
$payment_token_factory = $container->get( 'vaulting.payment-token-factory' );
|
2023-09-04 16:33:04 +02:00
|
|
|
$payment_token_helper = $container->get( 'vaulting.payment-token-helper' );
|
2023-08-04 17:51:32 +01:00
|
|
|
$refund_fees_updater = $container->get( 'wcgateway.helper.refund-fees-updater' );
|
2022-11-23 17:00:04 +01:00
|
|
|
|
2020-08-27 13:10:16 +03:00
|
|
|
return array(
|
2023-06-14 12:44:06 +03:00
|
|
|
new CheckoutOrderApproved(
|
|
|
|
$logger,
|
|
|
|
$order_endpoint,
|
|
|
|
$container->get( 'session.handler' ),
|
|
|
|
$container->get( 'wcgateway.funding-source.renderer' ),
|
|
|
|
$container->get( 'wcgateway.order-processor' )
|
|
|
|
),
|
2023-06-08 16:00:21 +03:00
|
|
|
new CheckoutOrderCompleted( $logger ),
|
2022-09-12 12:01:13 +02:00
|
|
|
new CheckoutPaymentApprovalReversed( $logger ),
|
2023-08-04 17:51:32 +01:00
|
|
|
new PaymentCaptureRefunded( $logger, $refund_fees_updater ),
|
2023-06-08 16:00:21 +03:00
|
|
|
new PaymentCaptureReversed( $logger ),
|
|
|
|
new PaymentCaptureCompleted( $logger, $order_endpoint ),
|
2023-09-04 16:33:04 +02:00
|
|
|
new VaultPaymentTokenCreated( $logger, $prefix, $authorized_payments_processor, $payment_token_factory, $payment_token_helper ),
|
2022-11-24 15:42:58 +01:00
|
|
|
new VaultPaymentTokenDeleted( $logger ),
|
2022-07-06 10:35:53 +02:00
|
|
|
new PaymentCapturePending( $logger ),
|
2024-09-13 15:35:13 +02:00
|
|
|
new PaymentSaleCompleted( $logger, $container->get( 'paypal-subscriptions.renewal-handler' ) ),
|
2023-08-04 17:51:32 +01:00
|
|
|
new PaymentSaleRefunded( $logger, $refund_fees_updater ),
|
2023-03-07 10:35:39 +01:00
|
|
|
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-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,
|
2021-09-24 17:36:51 +03:00
|
|
|
'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' ),
|
2023-03-10 10:39:13 +02:00
|
|
|
$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/',
|
2022-01-14 16:35:13 +02:00
|
|
|
dirname( realpath( __FILE__ ), 3 ) . '/woocommerce-paypal-payments.php'
|
2021-09-16 13:48:20 +03:00
|
|
|
);
|
|
|
|
},
|
2020-08-27 13:10:16 +03:00
|
|
|
);
|