optionsProvider = $optionsProvider; $this->packetSubmitter = $packetSubmitter; $this->orderRepository = $orderRepository; } /** * Registers listeners. * * @return void */ public function register(): void { if ( $this->optionsProvider->isPacketAutoSubmissionEnabled() === false ) { return; } add_action( self::HOOK_NAME_HANDLE_EVENT, [ $this, 'handleEvent' ], 10, 2 ); $mappedEvents = $this->optionsProvider->getPacketAutoSubmissionMappedUniqueEvents(); foreach ( $mappedEvents as $mappedEvent ) { if ( $mappedEvent === self::EVENT_ON_ORDER_COMPLETED ) { add_action( 'woocommerce_order_status_completed', function ( int $orderId ): void { $this->handleEvent( self::EVENT_ON_ORDER_COMPLETED, $orderId ); } ); continue; } if ( $mappedEvent === self::EVENT_ON_ORDER_PROCESSING ) { add_action( 'woocommerce_order_status_processing', function ( int $orderId ): void { $this->handleEvent( self::EVENT_ON_ORDER_PROCESSING, $orderId ); } ); } } } /** * Handle event. * * @param string $event Event. * @param int $orderId WC Order. * * @return void */ public function handleEvent( string $event, int $orderId ): void { if ( $this->optionsProvider->isPacketAutoSubmissionEnabled() === false ) { return; } $wcOrder = $this->orderRepository->getWcOrderById( $orderId ); assert( $wcOrder !== null, 'WC order has to be present' ); $paymentGateway = wc_get_payment_gateway_by_order( $wcOrder ); if ( ! $paymentGateway instanceof WC_Payment_Gateway || array_key_exists( $paymentGateway->id, PaymentGatewayHelper::getAvailablePaymentGateways() ) === false ) { return; } $mappingEventForGateway = $this->optionsProvider->getPacketAutoSubmissionEventForPaymentGateway( $this->optionsProvider->sanitizePaymentGatewayId( $paymentGateway->id ) ); if ( $mappingEventForGateway === null || $mappingEventForGateway !== $event ) { return; } $this->packetSubmitter->submitPacket( $wcOrder ); } /** * Handles event async. * * @param string $event Event. * @param int $orderId Order ID. * * @return void */ public function handleEventAsync( string $event, int $orderId ): void { if ( $this->optionsProvider->isPacketAutoSubmissionEnabled() === false ) { return; } as_schedule_single_action( time(), self::HOOK_NAME_HANDLE_EVENT, [ $event, $orderId, is_admin() === false ] ); } }