diff --git a/modules.local/ppcp-button/src/Endpoint/class-approveorderendpoint.php b/modules.local/ppcp-button/src/Endpoint/class-approveorderendpoint.php index ce1fd2863..dfa7a9fad 100644 --- a/modules.local/ppcp-button/src/Endpoint/class-approveorderendpoint.php +++ b/modules.local/ppcp-button/src/Endpoint/class-approveorderendpoint.php @@ -127,7 +127,7 @@ class ApproveOrderEndpoint implements EndpointInterface { ) ); } - $this->session_handler->replaceOrder( $order ); + $this->session_handler->replace_order( $order ); wp_send_json_success( $order ); } @@ -141,7 +141,7 @@ class ApproveOrderEndpoint implements EndpointInterface { ); } - $this->session_handler->replaceOrder( $order ); + $this->session_handler->replace_order( $order ); wp_send_json_success( $order ); return true; } catch ( \RuntimeException $error ) { diff --git a/modules.local/ppcp-button/src/Endpoint/class-createorderendpoint.php b/modules.local/ppcp-button/src/Endpoint/class-createorderendpoint.php index b882b9ab5..4bcd7ed12 100644 --- a/modules.local/ppcp-button/src/Endpoint/class-createorderendpoint.php +++ b/modules.local/ppcp-button/src/Endpoint/class-createorderendpoint.php @@ -145,7 +145,7 @@ class CreateOrderEndpoint implements EndpointInterface { } $bn_code = isset( $data['bn_code'] ) ? (string) $data['bn_code'] : ''; if ( $bn_code ) { - $this->session_handler->replaceBnCode( $bn_code ); + $this->session_handler->replace_bn_code( $bn_code ); $this->api_endpoint->with_bn_code( $bn_code ); } $payee_preferred = $this->settings->has( 'payee_preferred' ) diff --git a/modules.local/ppcp-session/extensions.php b/modules.local/ppcp-session/extensions.php index 280e6c73c..8c1b048a6 100644 --- a/modules.local/ppcp-session/extensions.php +++ b/modules.local/ppcp-session/extensions.php @@ -1,8 +1,12 @@ function (ContainerInterface $container) : SessionHandler { +return array( + 'session.handler' => function ( ContainerInterface $container ) : SessionHandler { - if (is_null(WC()->session)) { - return new SessionHandler(); - } - $result = WC()->session->get(SessionHandler::ID); - if (is_a($result, SessionHandler::class)) { - return $result; - } - $sessionHandler = new SessionHandler(); - WC()->session->set(SessionHandler::ID, $sessionHandler); - return $sessionHandler; - }, - 'session.cancellation.view' => function (ContainerInterface $container) : CancelView { - return new CancelView(); - }, - 'session.cancellation.controller' => function (ContainerInterface $container) : CancelController { - return new CancelController( - $container->get('session.handler'), - $container->get('session.cancellation.view') - ); - }, -]; + if ( is_null( WC()->session ) ) { + return new SessionHandler(); + } + $result = WC()->session->get( SessionHandler::ID ); + if ( is_a( $result, SessionHandler::class ) ) { + return $result; + } + $session_handler = new SessionHandler(); + WC()->session->set( SessionHandler::ID, $session_handler ); + return $session_handler; + }, + 'session.cancellation.view' => function ( ContainerInterface $container ) : CancelView { + return new CancelView(); + }, + 'session.cancellation.controller' => function ( ContainerInterface $container ) : CancelController { + return new CancelController( + $container->get( 'session.handler' ), + $container->get( 'session.cancellation.view' ) + ); + }, +); diff --git a/modules.local/ppcp-session/src/Cancellation/class-cancelcontroller.php b/modules.local/ppcp-session/src/Cancellation/class-cancelcontroller.php index 89ee3eb79..2d912ddb9 100644 --- a/modules.local/ppcp-session/src/Cancellation/class-cancelcontroller.php +++ b/modules.local/ppcp-session/src/Cancellation/class-cancelcontroller.php @@ -1,46 +1,74 @@ view = $view; - $this->sessionHandler = $sessionHandler; - } + /** + * The view. + * + * @var CancelView + */ + private $view; - public function run() - { - $paramName = 'ppcp-cancel'; - $nonce = 'ppcp-cancel-' . get_current_user_id(); - if (isset($_GET[$paramName]) && // Input var ok. - wp_verify_nonce( - sanitize_text_field(wp_unslash($_GET[$paramName])), // Input var ok. - $nonce - ) - ) { // Input var ok. - $this->sessionHandler->destroySessionData(); - } - if (! $this->sessionHandler->order()) { - return; - } + /** + * CancelController constructor. + * + * @param SessionHandler $session_handler The session handler. + * @param CancelView $view The view object. + */ + public function __construct( + SessionHandler $session_handler, + CancelView $view + ) { - $url = add_query_arg([$paramName => wp_create_nonce($nonce)], wc_get_checkout_url()); - add_action( - 'woocommerce_review_order_after_submit', - function () use ($url) { - $this->view->renderSessionCancelation($url); - } - ); - } + $this->view = $view; + $this->session_handler = $session_handler; + } + + /** + * Runs the controller. + */ + public function run() { + $param_name = 'ppcp-cancel'; + $nonce = 'ppcp-cancel-' . get_current_user_id(); + if ( isset( $_GET[ $param_name ] ) && // Input var ok. + wp_verify_nonce( + sanitize_text_field( wp_unslash( $_GET[ $param_name ] ) ), // Input var ok. + $nonce + ) + ) { // Input var ok. + $this->session_handler->destroy_session_data(); + } + if ( ! $this->session_handler->order() ) { + return; + } + + $url = add_query_arg( array( $param_name => wp_create_nonce( $nonce ) ), wc_get_checkout_url() ); + add_action( + 'woocommerce_review_order_after_submit', + function () use ( $url ) { + $this->view->render_session_cancellation( $url ); + } + ); + } } diff --git a/modules.local/ppcp-session/src/Cancellation/class-cancelview.php b/modules.local/ppcp-session/src/Cancellation/class-cancelview.php index 4f4ec6027..d93d95803 100644 --- a/modules.local/ppcp-session/src/Cancellation/class-cancelview.php +++ b/modules.local/ppcp-session/src/Cancellation/class-cancelview.php @@ -1,30 +1,42 @@ -
- +
+ ', - '' - ); - ?> -
- ', + '' + ); + ?> + + order; - } + const ID = 'ppcp'; - public function replaceOrder(Order $order) : SessionHandler - { - $this->order = $order; - $this->storeSession(); - return $this; - } + /** + * The Order. + * + * @var Order|null + */ + private $order; - public function bnCode() : string - { - return $this->bnCode; - } + /** + * The BN Code. + * + * @var string + */ + private $bn_code = ''; - public function replaceBnCode(string $bnCode) : SessionHandler - { - $this->bnCode = $bnCode; - $this->storeSession(); - return $this; - } + /** + * Returns the order. + * + * @return Order|null + */ + public function order() : ?Order { + return $this->order; + } - public function destroySessionData() : SessionHandler - { - $this->order = null; - $this->bnCode = ''; - $this->storeSession(); - return $this; - } + /** + * Replaces the current order. + * + * @param Order $order The new order. + * + * @return SessionHandler + */ + public function replace_order( Order $order ) : SessionHandler { + $this->order = $order; + $this->store_session(); + return $this; + } - private function storeSession() - { - WC()->session->set(self::ID, $this); - } + /** + * Returns the BN Code. + * + * @return string + */ + public function bn_code() : string { + return $this->bn_code; + } + + /** + * Replaces the BN Code. + * + * @param string $bn_code The new BN Code. + * + * @return SessionHandler + */ + public function replace_bn_code( string $bn_code ) : SessionHandler { + $this->bn_code = $bn_code; + $this->store_session(); + return $this; + } + + /** + * Destroys the session data. + * + * @return SessionHandler + */ + public function destroy_session_data() : SessionHandler { + $this->order = null; + $this->bn_code = ''; + $this->store_session(); + return $this; + } + + /** + * Stores the session. + */ + private function store_session() { + WC()->session->set( self::ID, $this ); + } } diff --git a/modules.local/ppcp-session/src/class-sessionmodule.php b/modules.local/ppcp-session/src/class-sessionmodule.php index 0f398b39a..d36b30df0 100644 --- a/modules.local/ppcp-session/src/class-sessionmodule.php +++ b/modules.local/ppcp-session/src/class-sessionmodule.php @@ -1,4 +1,10 @@ get('session.cancellation.controller'); - /** - * @var CancelController $controller - */ - $controller->run(); - } - ); - } + /** + * Run the module. + * + * @param ContainerInterface $container The container. + */ + public function run( ContainerInterface $container ) { + add_action( + 'woocommerce_init', + function () use ( $container ) { + $controller = $container->get( 'session.cancellation.controller' ); + /** + * The Cancel controller. + * + * @var CancelController $controller + */ + $controller->run(); + } + ); + } } diff --git a/modules.local/ppcp-wc-gateway/extensions.php b/modules.local/ppcp-wc-gateway/extensions.php index d52216bb2..be3b4e051 100644 --- a/modules.local/ppcp-wc-gateway/extensions.php +++ b/modules.local/ppcp-wc-gateway/extensions.php @@ -54,7 +54,7 @@ return array( * @var SessionHandler $session_handler */ $session_handler = $container->get( 'session.handler' ); - $bn_code = $session_handler->bnCode(); + $bn_code = $session_handler->bn_code(); /** * The settings. diff --git a/modules.local/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php b/modules.local/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php index 4a98d93e4..719d3b959 100644 --- a/modules.local/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php +++ b/modules.local/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php @@ -210,7 +210,7 @@ class PayPalGateway extends \WC_Payment_Gateway { ); } - $this->session_handler->destroySessionData(); + $this->session_handler->destroy_session_data(); } wc_add_notice( $this->order_processor->last_error(), diff --git a/modules.local/ppcp-wc-gateway/src/Processor/class-orderprocessor.php b/modules.local/ppcp-wc-gateway/src/Processor/class-orderprocessor.php index 012c470eb..2061b2a73 100644 --- a/modules.local/ppcp-wc-gateway/src/Processor/class-orderprocessor.php +++ b/modules.local/ppcp-wc-gateway/src/Processor/class-orderprocessor.php @@ -179,7 +179,7 @@ class OrderProcessor { $wc_order->update_status( 'processing' ); } $woocommerce->cart->empty_cart(); - $this->session_handler->destroySessionData(); + $this->session_handler->destroy_session_data(); $this->last_error = ''; return true; } diff --git a/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php b/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php index c1cfcd187..adeda92f7 100644 --- a/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php +++ b/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php @@ -49,7 +49,7 @@ class OrderProcessorTest extends TestCase ->expects('order') ->andReturn($currentOrder); $sessionHandler - ->expects('destroySessionData'); + ->expects('destroy_session_data'); $cartRepository = Mockery::mock(CartRepository::class); $orderEndpoint = Mockery::mock(OrderEndpoint::class); $orderEndpoint @@ -142,7 +142,7 @@ class OrderProcessorTest extends TestCase ->expects('order') ->andReturn($currentOrder); $sessionHandler - ->expects('destroySessionData'); + ->expects('destroy_session_data'); $cartRepository = Mockery::mock(CartRepository::class); $orderEndpoint = Mockery::mock(OrderEndpoint::class); $orderEndpoint