mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-01 07:02:48 +08:00
move module.local to module
This commit is contained in:
parent
c443e4053c
commit
f8e82bdfaf
217 changed files with 8 additions and 2 deletions
12
modules/ppcp-session/extensions.php
Normal file
12
modules/ppcp-session/extensions.php
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
/**
|
||||
* The extensions of the session module.
|
||||
*
|
||||
* @package Inpsyde\PayPalCommerce\Session
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Session;
|
||||
|
||||
return array();
|
16
modules/ppcp-session/module.php
Normal file
16
modules/ppcp-session/module.php
Normal file
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
/**
|
||||
* The session module.
|
||||
*
|
||||
* @package Inpsyde\PayPalCommerce\Session
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Session;
|
||||
|
||||
use Dhii\Modular\Module\ModuleInterface;
|
||||
|
||||
return function (): ModuleInterface {
|
||||
return new SessionModule();
|
||||
};
|
39
modules/ppcp-session/services.php
Normal file
39
modules/ppcp-session/services.php
Normal file
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
/**
|
||||
* The services of the session module.
|
||||
*
|
||||
* @package Inpsyde\PayPalCommerce\Session
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Session;
|
||||
|
||||
use Dhii\Data\Container\ContainerInterface;
|
||||
use Inpsyde\PayPalCommerce\Session\Cancellation\CancelController;
|
||||
use Inpsyde\PayPalCommerce\Session\Cancellation\CancelView;
|
||||
|
||||
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;
|
||||
}
|
||||
$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' )
|
||||
);
|
||||
},
|
||||
);
|
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
/**
|
||||
* Controlls the cancel mechanism to step out of the PayPal order session.
|
||||
*
|
||||
* @package Inpsyde\PayPalCommerce\Session\Cancellation
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Session\Cancellation;
|
||||
|
||||
use Inpsyde\PayPalCommerce\Session\SessionHandler;
|
||||
|
||||
/**
|
||||
* Class CancelController
|
||||
*/
|
||||
class CancelController {
|
||||
|
||||
/**
|
||||
* The Session handler.
|
||||
*
|
||||
* @var SessionHandler
|
||||
*/
|
||||
private $session_handler;
|
||||
|
||||
/**
|
||||
* The view.
|
||||
*
|
||||
* @var CancelView
|
||||
*/
|
||||
private $view;
|
||||
|
||||
/**
|
||||
* CancelController constructor.
|
||||
*
|
||||
* @param SessionHandler $session_handler The session handler.
|
||||
* @param CancelView $view The view object.
|
||||
*/
|
||||
public function __construct(
|
||||
SessionHandler $session_handler,
|
||||
CancelView $view
|
||||
) {
|
||||
|
||||
$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 );
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
42
modules/ppcp-session/src/Cancellation/class-cancelview.php
Normal file
42
modules/ppcp-session/src/Cancellation/class-cancelview.php
Normal file
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
/**
|
||||
* Renders the cancel view for the order on the checkout.
|
||||
*
|
||||
* @package Inpsyde\PayPalCommerce\Session\Cancellation
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Session\Cancellation;
|
||||
|
||||
/**
|
||||
* Class CancelView
|
||||
*/
|
||||
class CancelView {
|
||||
|
||||
/**
|
||||
* Renders the cancel link.
|
||||
*
|
||||
* @param string $url The URL.
|
||||
*/
|
||||
public function render_session_cancellation( string $url ) {
|
||||
?>
|
||||
<p id="ppcp-cancel"
|
||||
class="has-text-align-center ppcp-cancel"
|
||||
>
|
||||
<?php
|
||||
printf(
|
||||
// translators: the placeholders are html tags for a link.
|
||||
esc_html__(
|
||||
'You are currently paying with PayPal. If you want to cancel
|
||||
this process, please click %1$shere%2$s.',
|
||||
'paypal-for-woocommerce'
|
||||
),
|
||||
'<a href="' . esc_url( $url ) . '">',
|
||||
'</a>'
|
||||
);
|
||||
?>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
}
|
97
modules/ppcp-session/src/class-sessionhandler.php
Normal file
97
modules/ppcp-session/src/class-sessionhandler.php
Normal file
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
/**
|
||||
* The Session Handler.
|
||||
*
|
||||
* @package Inpsyde\PayPalCommerce\Session
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Session;
|
||||
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\Order;
|
||||
|
||||
/**
|
||||
* Class SessionHandler
|
||||
*/
|
||||
class SessionHandler {
|
||||
|
||||
const ID = 'ppcp';
|
||||
|
||||
/**
|
||||
* The Order.
|
||||
*
|
||||
* @var Order|null
|
||||
*/
|
||||
private $order;
|
||||
|
||||
/**
|
||||
* The BN Code.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $bn_code = '';
|
||||
|
||||
/**
|
||||
* Returns the order.
|
||||
*
|
||||
* @return Order|null
|
||||
*/
|
||||
public function order() : ?Order {
|
||||
return $this->order;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 );
|
||||
}
|
||||
}
|
54
modules/ppcp-session/src/class-sessionmodule.php
Normal file
54
modules/ppcp-session/src/class-sessionmodule.php
Normal file
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
/**
|
||||
* The session module.
|
||||
*
|
||||
* @package Inpsyde\PayPalCommerce\Session
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Session;
|
||||
|
||||
use Dhii\Container\ServiceProvider;
|
||||
use Dhii\Modular\Module\ModuleInterface;
|
||||
use Inpsyde\PayPalCommerce\Session\Cancellation\CancelController;
|
||||
use Interop\Container\ServiceProviderInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Class SessionModule
|
||||
*/
|
||||
class SessionModule implements ModuleInterface {
|
||||
|
||||
/**
|
||||
* Sets up the module.
|
||||
*
|
||||
* @return ServiceProviderInterface
|
||||
*/
|
||||
public function setup(): ServiceProviderInterface {
|
||||
return new ServiceProvider(
|
||||
require __DIR__ . '/../services.php',
|
||||
require __DIR__ . '/../extensions.php'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue