mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 10:55:00 +08:00
Stop searching for prefix in custom IDs
we are not creating such IDs for 2 years already
This commit is contained in:
parent
eb4418931a
commit
8979c9e605
14 changed files with 26 additions and 104 deletions
|
@ -184,8 +184,7 @@ return array(
|
|||
$state = $container->get( 'onboarding.state' );
|
||||
$order_processor = $container->get( 'wcgateway.order-processor' );
|
||||
$session_handler = $container->get( 'session.handler' );
|
||||
$prefix = $container->get( 'api.prefix' );
|
||||
return new EarlyOrderHandler( $state, $order_processor, $session_handler, $prefix );
|
||||
return new EarlyOrderHandler( $state, $order_processor, $session_handler );
|
||||
},
|
||||
'button.endpoint.approve-order' => static function ( ContainerInterface $container ): ApproveOrderEndpoint {
|
||||
$request_data = $container->get( 'button.request-data' );
|
||||
|
|
|
@ -15,15 +15,12 @@ use WooCommerce\PayPalCommerce\Onboarding\State;
|
|||
use WooCommerce\PayPalCommerce\Session\SessionHandler;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor;
|
||||
use WooCommerce\PayPalCommerce\Webhooks\Handler\PrefixTrait;
|
||||
|
||||
/**
|
||||
* Class EarlyOrderHandler
|
||||
*/
|
||||
class EarlyOrderHandler {
|
||||
|
||||
use PrefixTrait;
|
||||
|
||||
/**
|
||||
* The State.
|
||||
*
|
||||
|
@ -51,19 +48,16 @@ class EarlyOrderHandler {
|
|||
* @param State $state The State.
|
||||
* @param OrderProcessor $order_processor The Order Processor.
|
||||
* @param SessionHandler $session_handler The Session Handler.
|
||||
* @param string $prefix The Prefix.
|
||||
*/
|
||||
public function __construct(
|
||||
State $state,
|
||||
OrderProcessor $order_processor,
|
||||
SessionHandler $session_handler,
|
||||
string $prefix
|
||||
SessionHandler $session_handler
|
||||
) {
|
||||
|
||||
$this->state = $state;
|
||||
$this->order_processor = $order_processor;
|
||||
$this->session_handler = $session_handler;
|
||||
$this->prefix = $prefix;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -101,7 +95,7 @@ class EarlyOrderHandler {
|
|||
$order_id = false;
|
||||
foreach ( $order->purchase_units() as $purchase_unit ) {
|
||||
if ( $purchase_unit->custom_id() === sanitize_text_field( wp_unslash( $_REQUEST['ppcp-resume-order'] ) ) ) {
|
||||
$order_id = (int) $this->sanitize_custom_id( $purchase_unit->custom_id() );
|
||||
$order_id = (int) $purchase_unit->custom_id();
|
||||
}
|
||||
}
|
||||
if ( $order_id === $resume_order_id ) {
|
||||
|
|
|
@ -938,11 +938,9 @@ return array(
|
|||
'wcgateway.endpoint.return-url' => static function ( ContainerInterface $container ) : ReturnUrlEndpoint {
|
||||
$gateway = $container->get( 'wcgateway.paypal-gateway' );
|
||||
$endpoint = $container->get( 'api.endpoint.order' );
|
||||
$prefix = $container->get( 'api.prefix' );
|
||||
return new ReturnUrlEndpoint(
|
||||
$gateway,
|
||||
$endpoint,
|
||||
$prefix,
|
||||
$container->get( 'session.handler' ),
|
||||
$container->get( 'woocommerce.logger.woocommerce' )
|
||||
);
|
||||
|
|
|
@ -15,14 +15,12 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
|
|||
use WooCommerce\PayPalCommerce\Session\SessionHandler;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\OXXO\OXXOGateway;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||
use WooCommerce\PayPalCommerce\Webhooks\Handler\PrefixTrait;
|
||||
|
||||
/**
|
||||
* Class ReturnUrlEndpoint
|
||||
*/
|
||||
class ReturnUrlEndpoint {
|
||||
|
||||
use PrefixTrait;
|
||||
const ENDPOINT = 'ppc-return-url';
|
||||
|
||||
/**
|
||||
|
@ -58,20 +56,17 @@ class ReturnUrlEndpoint {
|
|||
*
|
||||
* @param PayPalGateway $gateway The PayPal Gateway.
|
||||
* @param OrderEndpoint $order_endpoint The Order Endpoint.
|
||||
* @param string $prefix The prefix.
|
||||
* @param SessionHandler $session_handler The session handler.
|
||||
* @param LoggerInterface $logger The logger.
|
||||
*/
|
||||
public function __construct(
|
||||
PayPalGateway $gateway,
|
||||
OrderEndpoint $order_endpoint,
|
||||
string $prefix,
|
||||
SessionHandler $session_handler,
|
||||
LoggerInterface $logger
|
||||
) {
|
||||
$this->gateway = $gateway;
|
||||
$this->order_endpoint = $order_endpoint;
|
||||
$this->prefix = $prefix;
|
||||
$this->session_handler = $session_handler;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
@ -90,7 +85,7 @@ class ReturnUrlEndpoint {
|
|||
// phpcs:enable WordPress.Security.NonceVerification.Recommended
|
||||
$order = $this->order_endpoint->order( $token );
|
||||
|
||||
$wc_order_id = $this->sanitize_custom_id( $order->purchase_units()[0]->custom_id() );
|
||||
$wc_order_id = (int) $order->purchase_units()[0]->custom_id();
|
||||
if ( ! $wc_order_id ) {
|
||||
// We cannot finish processing here without WC order, but at least go into the continuation mode.
|
||||
if ( $order->status()->is( OrderStatus::APPROVED )
|
||||
|
|
|
@ -82,12 +82,12 @@ return array(
|
|||
$payment_token_factory = $container->get( 'vaulting.payment-token-factory' );
|
||||
|
||||
return array(
|
||||
new CheckoutOrderApproved( $logger, $prefix, $order_endpoint ),
|
||||
new CheckoutOrderCompleted( $logger, $prefix ),
|
||||
new CheckoutOrderApproved( $logger, $order_endpoint ),
|
||||
new CheckoutOrderCompleted( $logger ),
|
||||
new CheckoutPaymentApprovalReversed( $logger ),
|
||||
new PaymentCaptureRefunded( $logger, $prefix ),
|
||||
new PaymentCaptureReversed( $logger, $prefix ),
|
||||
new PaymentCaptureCompleted( $logger, $prefix, $order_endpoint ),
|
||||
new PaymentCaptureRefunded( $logger ),
|
||||
new PaymentCaptureReversed( $logger ),
|
||||
new PaymentCaptureCompleted( $logger, $order_endpoint ),
|
||||
new VaultPaymentTokenCreated( $logger, $prefix, $authorized_payments_processor, $payment_token_factory ),
|
||||
new VaultPaymentTokenDeleted( $logger ),
|
||||
new PaymentCapturePending( $logger ),
|
||||
|
|
|
@ -20,7 +20,7 @@ use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\PayUponInvoiceGa
|
|||
*/
|
||||
class CheckoutOrderApproved implements RequestHandler {
|
||||
|
||||
use PrefixTrait;
|
||||
use RequestHandlerTrait;
|
||||
|
||||
/**
|
||||
* The logger.
|
||||
|
@ -40,12 +40,10 @@ class CheckoutOrderApproved implements RequestHandler {
|
|||
* CheckoutOrderApproved constructor.
|
||||
*
|
||||
* @param LoggerInterface $logger The logger.
|
||||
* @param string $prefix The prefix.
|
||||
* @param OrderEndpoint $order_endpoint The order endpoint.
|
||||
*/
|
||||
public function __construct( LoggerInterface $logger, string $prefix, OrderEndpoint $order_endpoint ) {
|
||||
public function __construct( LoggerInterface $logger, OrderEndpoint $order_endpoint ) {
|
||||
$this->logger = $logger;
|
||||
$this->prefix = $prefix;
|
||||
$this->order_endpoint = $order_endpoint;
|
||||
}
|
||||
|
||||
|
@ -160,13 +158,7 @@ class CheckoutOrderApproved implements RequestHandler {
|
|||
return rest_ensure_response( $response );
|
||||
}
|
||||
|
||||
$wc_order_ids = array_map(
|
||||
array(
|
||||
$this,
|
||||
'sanitize_custom_id',
|
||||
),
|
||||
$custom_ids
|
||||
);
|
||||
$wc_order_ids = $custom_ids;
|
||||
$args = array(
|
||||
'post__in' => $wc_order_ids,
|
||||
'limit' => -1,
|
||||
|
|
|
@ -19,7 +19,7 @@ use WP_REST_Response;
|
|||
*/
|
||||
class CheckoutOrderCompleted implements RequestHandler {
|
||||
|
||||
use PrefixTrait, RequestHandlerTrait;
|
||||
use RequestHandlerTrait;
|
||||
|
||||
/**
|
||||
* The logger.
|
||||
|
@ -32,11 +32,9 @@ class CheckoutOrderCompleted implements RequestHandler {
|
|||
* CheckoutOrderCompleted constructor.
|
||||
*
|
||||
* @param LoggerInterface $logger The logger.
|
||||
* @param string $prefix The prefix.
|
||||
*/
|
||||
public function __construct( LoggerInterface $logger, string $prefix ) {
|
||||
public function __construct( LoggerInterface $logger ) {
|
||||
$this->logger = $logger;
|
||||
$this->prefix = $prefix;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -18,7 +18,7 @@ use WP_REST_Response;
|
|||
*/
|
||||
class CheckoutPaymentApprovalReversed implements RequestHandler {
|
||||
|
||||
use RequestHandlerTrait, PrefixTrait;
|
||||
use RequestHandlerTrait;
|
||||
|
||||
/**
|
||||
* The logger.
|
||||
|
|
|
@ -22,7 +22,7 @@ use WP_REST_Response;
|
|||
*/
|
||||
class PaymentCaptureCompleted implements RequestHandler {
|
||||
|
||||
use PrefixTrait, TransactionIdHandlingTrait;
|
||||
use TransactionIdHandlingTrait;
|
||||
|
||||
/**
|
||||
* The logger.
|
||||
|
@ -42,16 +42,13 @@ class PaymentCaptureCompleted implements RequestHandler {
|
|||
* PaymentCaptureCompleted constructor.
|
||||
*
|
||||
* @param LoggerInterface $logger The logger.
|
||||
* @param string $prefix The prefix.
|
||||
* @param OrderEndpoint $order_endpoint The order endpoint.
|
||||
*/
|
||||
public function __construct(
|
||||
LoggerInterface $logger,
|
||||
string $prefix,
|
||||
OrderEndpoint $order_endpoint
|
||||
) {
|
||||
$this->logger = $logger;
|
||||
$this->prefix = $prefix;
|
||||
$this->order_endpoint = $order_endpoint;
|
||||
}
|
||||
|
||||
|
@ -95,8 +92,7 @@ class PaymentCaptureCompleted implements RequestHandler {
|
|||
return new WP_REST_Response( $response );
|
||||
}
|
||||
|
||||
$wc_order_id = isset( $resource['custom_id'] ) ?
|
||||
$this->sanitize_custom_id( (string) $resource['custom_id'] ) : 0;
|
||||
$wc_order_id = isset( $resource['custom_id'] ) ? (string) $resource['custom_id'] : 0;
|
||||
if ( ! $wc_order_id ) {
|
||||
$message = sprintf( 'No order for webhook event %s was found.', $webhook_id );
|
||||
$this->logger->warning( $message, array( 'request' => $request ) );
|
||||
|
|
|
@ -18,7 +18,7 @@ use WP_REST_Response;
|
|||
*/
|
||||
class PaymentCapturePending implements RequestHandler {
|
||||
|
||||
use PrefixTrait;
|
||||
use RequestHandlerTrait;
|
||||
|
||||
/**
|
||||
* The logger.
|
||||
|
@ -68,7 +68,7 @@ class PaymentCapturePending implements RequestHandler {
|
|||
public function handle_request( WP_REST_Request $request ): WP_REST_Response {
|
||||
$response = array( 'success' => false );
|
||||
$order_id = $request['resource'] !== null && isset( $request['resource']['custom_id'] )
|
||||
? $this->sanitize_custom_id( $request['resource']['custom_id'] )
|
||||
? $request['resource']['custom_id']
|
||||
: 0;
|
||||
if ( ! $order_id ) {
|
||||
$message = sprintf(
|
||||
|
|
|
@ -20,7 +20,7 @@ use WP_REST_Response;
|
|||
*/
|
||||
class PaymentCaptureRefunded implements RequestHandler {
|
||||
|
||||
use PrefixTrait, TransactionIdHandlingTrait, RefundMetaTrait;
|
||||
use TransactionIdHandlingTrait, RefundMetaTrait;
|
||||
|
||||
/**
|
||||
* The logger.
|
||||
|
@ -33,11 +33,9 @@ class PaymentCaptureRefunded implements RequestHandler {
|
|||
* PaymentCaptureRefunded constructor.
|
||||
*
|
||||
* @param LoggerInterface $logger The logger.
|
||||
* @param string $prefix The prefix.
|
||||
*/
|
||||
public function __construct( LoggerInterface $logger, string $prefix ) {
|
||||
public function __construct( LoggerInterface $logger ) {
|
||||
$this->logger = $logger;
|
||||
$this->prefix = $prefix;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -70,7 +68,7 @@ class PaymentCaptureRefunded implements RequestHandler {
|
|||
public function handle_request( WP_REST_Request $request ): WP_REST_Response {
|
||||
$response = array( 'success' => false );
|
||||
$order_id = isset( $request['resource']['custom_id'] ) ?
|
||||
$this->sanitize_custom_id( $request['resource']['custom_id'] ) : 0;
|
||||
$request['resource']['custom_id'] : 0;
|
||||
$refund_id = (string) ( $request['resource']['id'] ?? '' );
|
||||
if ( ! $order_id ) {
|
||||
$message = sprintf(
|
||||
|
|
|
@ -19,7 +19,7 @@ use Psr\Log\LoggerInterface;
|
|||
*/
|
||||
class PaymentCaptureReversed implements RequestHandler {
|
||||
|
||||
use PrefixTrait;
|
||||
use RequestHandlerTrait;
|
||||
|
||||
/**
|
||||
* The logger.
|
||||
|
@ -32,11 +32,9 @@ class PaymentCaptureReversed implements RequestHandler {
|
|||
* PaymentCaptureReversed constructor.
|
||||
*
|
||||
* @param LoggerInterface $logger The logger.
|
||||
* @param string $prefix The prefix.
|
||||
*/
|
||||
public function __construct( LoggerInterface $logger, string $prefix ) {
|
||||
public function __construct( LoggerInterface $logger ) {
|
||||
$this->logger = $logger;
|
||||
$this->prefix = $prefix;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -73,7 +71,7 @@ class PaymentCaptureReversed implements RequestHandler {
|
|||
public function handle_request( \WP_REST_Request $request ): \WP_REST_Response {
|
||||
$response = array( 'success' => false );
|
||||
$order_id = isset( $request['resource']['custom_id'] ) ?
|
||||
$this->sanitize_custom_id( $request['resource']['custom_id'] ) : 0;
|
||||
$request['resource']['custom_id'] : 0;
|
||||
if ( ! $order_id ) {
|
||||
$message = sprintf(
|
||||
// translators: %s is the PayPal webhook Id.
|
||||
|
|
|
@ -1,40 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Trait which helps to remove the prefix of IDs.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\Webhooks\Handler
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\Webhooks\Handler;
|
||||
|
||||
/**
|
||||
* Trait PrefixTrait
|
||||
*/
|
||||
trait PrefixTrait {
|
||||
|
||||
|
||||
/**
|
||||
* The prefix.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $prefix = '';
|
||||
|
||||
/**
|
||||
* Removes the prefix from a given Id.
|
||||
*
|
||||
* @param string $custom_id The custom id.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
private function sanitize_custom_id( string $custom_id ): int {
|
||||
|
||||
$id = $custom_id;
|
||||
if ( strlen( $this->prefix ) > 0 && 0 === strpos( $id, $this->prefix ) ) {
|
||||
$id = substr( $id, strlen( $this->prefix ) );
|
||||
}
|
||||
return (int) $id;
|
||||
}
|
||||
}
|
|
@ -45,13 +45,7 @@ trait RequestHandlerTrait {
|
|||
* @return WC_Order[]
|
||||
*/
|
||||
protected function get_wc_orders_from_custom_ids( array $custom_ids ): array {
|
||||
$order_ids = array_map(
|
||||
array(
|
||||
$this,
|
||||
'sanitize_custom_id',
|
||||
),
|
||||
$custom_ids
|
||||
);
|
||||
$order_ids = $custom_ids;
|
||||
$args = array(
|
||||
'post__in' => $order_ids,
|
||||
'limit' => -1,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue