woocommerce-paypal-payments/modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php

317 lines
8.2 KiB
PHP
Raw Normal View History

2020-08-28 08:13:45 +03:00
<?php
/**
* Processes orders for the gateways.
*
2020-09-11 14:11:10 +03:00
* @package WooCommerce\PayPalCommerce\WcGateway\Processor
2020-08-28 08:13:45 +03:00
*/
declare(strict_types=1);
2020-09-11 14:11:10 +03:00
namespace WooCommerce\PayPalCommerce\WcGateway\Processor;
use Psr\Log\LoggerInterface;
2022-11-28 12:34:28 +01:00
use WC_Order;
2020-09-11 14:11:10 +03:00
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
2022-11-28 12:34:28 +01:00
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
2020-09-11 14:11:10 +03:00
use WooCommerce\PayPalCommerce\ApiClient\Factory\OrderFactory;
use WooCommerce\PayPalCommerce\ApiClient\Helper\OrderHelper;
2020-09-11 14:11:10 +03:00
use WooCommerce\PayPalCommerce\Button\Helper\ThreeDSecure;
2021-10-06 17:44:41 +03:00
use WooCommerce\PayPalCommerce\Onboarding\Environment;
2020-09-11 14:11:10 +03:00
use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
2020-09-11 14:11:10 +03:00
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
2020-08-28 08:13:45 +03:00
/**
* Class OrderProcessor
*/
class OrderProcessor {
2021-02-22 13:47:32 +02:00
2021-10-14 18:33:16 +03:00
use OrderMetaTrait, PaymentsStatusHandlingTrait, TransactionIdHandlingTrait;
2021-10-06 17:44:41 +03:00
2021-02-22 13:47:32 +02:00
/**
2021-10-06 17:44:41 +03:00
* The environment.
2021-02-22 13:47:32 +02:00
*
2021-10-06 17:44:41 +03:00
* @var Environment
2021-02-22 13:47:32 +02:00
*/
2021-10-06 17:44:41 +03:00
protected $environment;
2020-08-28 08:13:45 +03:00
/**
* The payment token repository.
*
* @var PaymentTokenRepository
*/
protected $payment_token_repository;
2020-08-28 08:13:45 +03:00
/**
* The Session Handler.
*
* @var SessionHandler
*/
private $session_handler;
/**
* The Order Endpoint.
*
* @var OrderEndpoint
*/
private $order_endpoint;
/**
* The Order Factory.
*
* @var OrderFactory
*/
private $order_factory;
/**
* The helper for 3d secure.
*
* @var ThreeDSecure
*/
private $threed_secure;
/**
* The processor for authorized payments.
*
* @var AuthorizedPaymentsProcessor
*/
private $authorized_payments_processor;
/**
* The settings.
*
* @var Settings
*/
private $settings;
/**
* The last error.
*
* @var string
*/
private $last_error = '';
2021-02-22 14:40:02 +02:00
/**
2021-02-22 14:40:02 +02:00
* A logger.
*
* @var LoggerInterface
*/
private $logger;
2020-08-28 08:13:45 +03:00
/**
2022-03-04 12:13:41 +01:00
* The subscription helper.
*
* @var SubscriptionHelper
*/
private $subscription_helper;
/**
* The order helper.
*
* @var OrderHelper
*/
private $order_helper;
2021-02-22 13:47:32 +02:00
/**
* OrderProcessor constructor.
*
* @param SessionHandler $session_handler The Session Handler.
* @param OrderEndpoint $order_endpoint The Order Endpoint.
* @param OrderFactory $order_factory The Order Factory.
* @param ThreeDSecure $three_d_secure The ThreeDSecure Helper.
* @param AuthorizedPaymentsProcessor $authorized_payments_processor The Authorized Payments Processor.
* @param Settings $settings The Settings.
* @param LoggerInterface $logger A logger service.
2021-10-06 17:44:41 +03:00
* @param Environment $environment The environment.
2022-03-04 12:13:41 +01:00
* @param SubscriptionHelper $subscription_helper The subscription helper.
* @param OrderHelper $order_helper The order helper.
2021-02-22 13:47:32 +02:00
*/
2020-08-28 08:13:45 +03:00
public function __construct(
SessionHandler $session_handler,
OrderEndpoint $order_endpoint,
OrderFactory $order_factory,
ThreeDSecure $three_d_secure,
AuthorizedPaymentsProcessor $authorized_payments_processor,
Settings $settings,
LoggerInterface $logger,
Environment $environment,
SubscriptionHelper $subscription_helper,
OrderHelper $order_helper
2020-08-28 08:13:45 +03:00
) {
$this->session_handler = $session_handler;
$this->order_endpoint = $order_endpoint;
$this->order_factory = $order_factory;
$this->threed_secure = $three_d_secure;
$this->authorized_payments_processor = $authorized_payments_processor;
$this->settings = $settings;
2021-10-06 17:44:41 +03:00
$this->environment = $environment;
$this->logger = $logger;
2022-03-04 12:13:41 +01:00
$this->subscription_helper = $subscription_helper;
$this->order_helper = $order_helper;
2021-02-22 13:47:32 +02:00
}
2020-08-28 08:13:45 +03:00
/**
2020-09-03 07:05:50 +03:00
* Processes a given WooCommerce order and captured/authorizes the connected PayPal orders.
2020-08-28 08:13:45 +03:00
*
2021-03-17 13:59:18 +02:00
* @param \WC_Order $wc_order The WooCommerce order.
2020-08-28 08:13:45 +03:00
*
* @return bool
*/
2022-11-28 12:34:28 +01:00
public function process( WC_Order $wc_order ): bool {
$order = $this->session_handler->order();
if ( ! $order ) {
2022-11-28 12:34:28 +01:00
$order_id = $wc_order->get_meta( PayPalGateway::ORDER_ID_META_KEY );
if ( ! $order_id ) {
2022-11-28 12:49:34 +01:00
$this->logger->warning( "No PayPal order ID found in order #{$wc_order->get_id()} meta." );
2022-12-05 11:21:05 +01:00
$this->last_error = __( 'Could not retrieve order. This browser may not be supported. Please try again with a different browser.', 'woocommerce-paypal-payments' );
2022-11-28 12:34:28 +01:00
return false;
}
try {
$order = $this->order_endpoint->order( $order_id );
} catch ( RuntimeException $exception ) {
$this->last_error = __( 'Could not retrieve PayPal order.', 'woocommerce-paypal-payments' );
return false;
}
}
2021-10-06 17:44:41 +03:00
$this->add_paypal_meta( $wc_order, $order, $this->environment );
2020-08-28 08:13:45 +03:00
$error_message = null;
2022-06-14 10:59:47 +02:00
if ( $this->order_helper->contains_physical_goods( $order ) && ! $this->order_is_ready_for_process( $order ) ) {
2020-08-28 08:13:45 +03:00
$error_message = __(
'The payment is not ready for processing yet.',
'woocommerce-paypal-payments'
2020-08-28 08:13:45 +03:00
);
}
if ( $error_message ) {
$this->last_error = sprintf(
// translators: %s is the message of the error.
__( 'Payment error: %s', 'woocommerce-paypal-payments' ),
2020-08-28 08:13:45 +03:00
$error_message
);
return false;
}
$order = $this->patch_order( $wc_order, $order );
2021-10-06 17:44:41 +03:00
2020-08-28 08:13:45 +03:00
if ( $order->intent() === 'CAPTURE' ) {
$order = $this->order_endpoint->capture( $order );
}
if ( $order->intent() === 'AUTHORIZE' ) {
$order = $this->order_endpoint->authorize( $order );
$wc_order->update_meta_data( AuthorizedPaymentsProcessor::CAPTURED_META_KEY, 'false' );
2022-03-04 12:13:41 +01:00
if ( $this->subscription_helper->has_subscription( $wc_order->get_id() ) ) {
$wc_order->update_meta_data( '_ppcp_captured_vault_webhook', 'false' );
}
2020-08-28 08:13:45 +03:00
}
2021-02-22 13:47:32 +02:00
$transaction_id = $this->get_paypal_order_transaction_id( $order );
2021-02-19 13:54:41 +02:00
2021-10-14 16:34:03 +03:00
if ( $transaction_id ) {
$this->update_transaction_id( $transaction_id, $wc_order );
2021-02-22 13:47:32 +02:00
}
2021-02-19 13:54:41 +02:00
$this->handle_new_order_status( $order, $wc_order );
2020-08-28 08:13:45 +03:00
if ( $this->capture_authorized_downloads( $order ) ) {
$this->authorized_payments_processor->capture_authorized_payment( $wc_order );
2020-08-28 08:13:45 +03:00
}
$this->last_error = '';
return true;
}
/**
* Returns if an order should be captured immediately.
*
* @param Order $order The PayPal order.
*
* @return bool
*/
private function capture_authorized_downloads( Order $order ): bool {
if (
! $this->settings->has( 'capture_for_virtual_only' )
|| ! $this->settings->get( 'capture_for_virtual_only' )
) {
return false;
}
if ( $order->intent() === 'CAPTURE' ) {
return false;
}
/**
* We fetch the order again as the authorize endpoint (from which the Order derives)
* drops the item's category, making it impossible to check, if purchase units contain
* physical goods.
*/
$order = $this->order_endpoint->order( $order->id() );
2020-09-01 09:00:45 +03:00
foreach ( $order->purchase_units() as $unit ) {
if ( $unit->contains_physical_goods() ) {
2020-08-28 08:13:45 +03:00
return false;
}
}
return true;
}
/**
* Returns the last error.
*
* @return string
*/
public function last_error(): string {
return $this->last_error;
}
/**
2020-09-03 07:05:50 +03:00
* Patches a given PayPal order with a WooCommerce order.
2020-08-28 08:13:45 +03:00
*
2020-09-03 07:05:50 +03:00
* @param \WC_Order $wc_order The WooCommerce order.
2020-08-28 08:13:45 +03:00
* @param Order $order The PayPal order.
*
* @return Order
*/
public function patch_order( \WC_Order $wc_order, Order $order ): Order {
2020-09-01 09:00:45 +03:00
$updated_order = $this->order_factory->from_wc_order( $wc_order, $order );
$order = $this->order_endpoint->patch_order_with( $order, $updated_order );
2020-08-28 08:13:45 +03:00
return $order;
}
/**
* Whether a given order is ready for processing.
2020-08-28 08:13:45 +03:00
*
* @param Order $order The order.
*
* @return bool
*/
private function order_is_ready_for_process( Order $order ): bool {
2020-08-28 08:13:45 +03:00
if ( $order->status()->is( OrderStatus::APPROVED ) || $order->status()->is( OrderStatus::CREATED ) ) {
2020-08-28 08:13:45 +03:00
return true;
}
2020-09-01 09:00:45 +03:00
if ( ! $order->payment_source() || ! $order->payment_source()->card() ) {
2020-08-28 08:13:45 +03:00
return false;
}
return in_array(
2020-08-31 11:12:46 +03:00
$this->threed_secure->proceed_with_order( $order ),
2020-08-28 08:13:45 +03:00
array(
ThreeDSecure::NO_DECISION,
ThreeDSecure::PROCCEED,
),
true
);
}
}