mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 14:57:26 +08:00
apply coding standards to session module
This commit is contained in:
parent
4cf58e613f
commit
4519c3f70f
13 changed files with 270 additions and 149 deletions
|
@ -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 ) {
|
||||
|
|
|
@ -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' )
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
<?php
|
||||
/**
|
||||
* The extensions of the session module.
|
||||
*
|
||||
* @package Inpsyde\PayPalCommerce\Session
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Session;
|
||||
|
||||
return [
|
||||
|
||||
];
|
||||
return array();
|
||||
|
|
|
@ -1,4 +1,10 @@
|
|||
<?php
|
||||
/**
|
||||
* The session module.
|
||||
*
|
||||
* @package Inpsyde\PayPalCommerce\Session
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Session;
|
||||
|
@ -6,5 +12,5 @@ namespace Inpsyde\PayPalCommerce\Session;
|
|||
use Dhii\Modular\Module\ModuleInterface;
|
||||
|
||||
return function (): ModuleInterface {
|
||||
return new SessionModule();
|
||||
return new SessionModule();
|
||||
};
|
||||
|
|
|
@ -1,4 +1,10 @@
|
|||
<?php
|
||||
/**
|
||||
* The services of the session module.
|
||||
*
|
||||
* @package Inpsyde\PayPalCommerce\Session
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Session;
|
||||
|
@ -7,27 +13,27 @@ use Dhii\Data\Container\ContainerInterface;
|
|||
use Inpsyde\PayPalCommerce\Session\Cancellation\CancelController;
|
||||
use Inpsyde\PayPalCommerce\Session\Cancellation\CancelView;
|
||||
|
||||
return [
|
||||
'session.handler' => 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' )
|
||||
);
|
||||
},
|
||||
);
|
||||
|
|
|
@ -1,46 +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
|
||||
*/
|
||||
class CancelController {
|
||||
|
||||
private $sessionHandler;
|
||||
private $view;
|
||||
public function __construct(
|
||||
SessionHandler $sessionHandler,
|
||||
CancelView $view
|
||||
) {
|
||||
/**
|
||||
* The Session handler.
|
||||
*
|
||||
* @var SessionHandler
|
||||
*/
|
||||
private $session_handler;
|
||||
|
||||
$this->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 );
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,30 +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
|
||||
*/
|
||||
class CancelView {
|
||||
|
||||
public function renderSessionCancelation(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
|
||||
/**
|
||||
* 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.',
|
||||
'woocommerce-paypal-commerce-gateway'
|
||||
),
|
||||
'<a href="'. esc_url($url) . '">',
|
||||
'</a>'
|
||||
);
|
||||
?>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
'woocommerce-paypal-commerce-gateway'
|
||||
),
|
||||
'<a href="' . esc_url( $url ) . '">',
|
||||
'</a>'
|
||||
);
|
||||
?>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,50 +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
|
||||
{
|
||||
const ID = 'ppcp';
|
||||
/**
|
||||
* Class SessionHandler
|
||||
*/
|
||||
class SessionHandler {
|
||||
|
||||
private $order;
|
||||
private $bnCode = '';
|
||||
public function order() : ?Order
|
||||
{
|
||||
return $this->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 );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,10 @@
|
|||
<?php
|
||||
/**
|
||||
* The session module.
|
||||
*
|
||||
* @package Inpsyde\PayPalCommerce\Session
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Session;
|
||||
|
@ -9,28 +15,40 @@ use Inpsyde\PayPalCommerce\Session\Cancellation\CancelController;
|
|||
use Interop\Container\ServiceProviderInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class SessionModule implements ModuleInterface
|
||||
{
|
||||
/**
|
||||
* Class SessionModule
|
||||
*/
|
||||
class SessionModule implements ModuleInterface {
|
||||
|
||||
public function setup(): ServiceProviderInterface
|
||||
{
|
||||
return new ServiceProvider(
|
||||
require __DIR__.'/../services.php',
|
||||
require __DIR__.'/../extensions.php'
|
||||
);
|
||||
}
|
||||
/**
|
||||
* Sets up the module.
|
||||
*
|
||||
* @return ServiceProviderInterface
|
||||
*/
|
||||
public function setup(): ServiceProviderInterface {
|
||||
return new ServiceProvider(
|
||||
require __DIR__ . '/../services.php',
|
||||
require __DIR__ . '/../extensions.php'
|
||||
);
|
||||
}
|
||||
|
||||
public function run(ContainerInterface $container)
|
||||
{
|
||||
add_action(
|
||||
'woocommerce_init',
|
||||
function () use ($container) {
|
||||
$controller = $container->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();
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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(),
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue