mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
Create order and redirect when no order
This commit is contained in:
parent
5c699c24a5
commit
a61e9303e9
10 changed files with 214 additions and 131 deletions
|
@ -9,19 +9,25 @@ declare(strict_types=1);
|
|||
|
||||
namespace WooCommerce\PayPalCommerce\WcGateway\Processor;
|
||||
|
||||
use Exception;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use WC_Order;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\OrderFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingPreferenceFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Helper\OrderHelper;
|
||||
use WooCommerce\PayPalCommerce\Button\Helper\ThreeDSecure;
|
||||
use WooCommerce\PayPalCommerce\Onboarding\Environment;
|
||||
use WooCommerce\PayPalCommerce\Session\SessionHandler;
|
||||
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
|
||||
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Exception\PayPalOrderMissingException;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
|
||||
|
@ -88,13 +94,6 @@ class OrderProcessor {
|
|||
*/
|
||||
private $settings;
|
||||
|
||||
/**
|
||||
* The last error.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $last_error = '';
|
||||
|
||||
/**
|
||||
* A logger.
|
||||
*
|
||||
|
@ -116,6 +115,27 @@ class OrderProcessor {
|
|||
*/
|
||||
private $order_helper;
|
||||
|
||||
/**
|
||||
* The PurchaseUnit factory.
|
||||
*
|
||||
* @var PurchaseUnitFactory
|
||||
*/
|
||||
private $purchase_unit_factory;
|
||||
|
||||
/**
|
||||
* The payer factory.
|
||||
*
|
||||
* @var PayerFactory
|
||||
*/
|
||||
private $payer_factory;
|
||||
|
||||
/**
|
||||
* The shipping_preference factory.
|
||||
*
|
||||
* @var ShippingPreferenceFactory
|
||||
*/
|
||||
private $shipping_preference_factory;
|
||||
|
||||
/**
|
||||
* Array to store temporary order data changes to restore after processing.
|
||||
*
|
||||
|
@ -136,6 +156,9 @@ class OrderProcessor {
|
|||
* @param Environment $environment The environment.
|
||||
* @param SubscriptionHelper $subscription_helper The subscription helper.
|
||||
* @param OrderHelper $order_helper The order helper.
|
||||
* @param PurchaseUnitFactory $purchase_unit_factory The PurchaseUnit factory.
|
||||
* @param PayerFactory $payer_factory The payer factory.
|
||||
* @param ShippingPreferenceFactory $shipping_preference_factory The shipping_preference factory.
|
||||
*/
|
||||
public function __construct(
|
||||
SessionHandler $session_handler,
|
||||
|
@ -147,7 +170,10 @@ class OrderProcessor {
|
|||
LoggerInterface $logger,
|
||||
Environment $environment,
|
||||
SubscriptionHelper $subscription_helper,
|
||||
OrderHelper $order_helper
|
||||
OrderHelper $order_helper,
|
||||
PurchaseUnitFactory $purchase_unit_factory,
|
||||
PayerFactory $payer_factory,
|
||||
ShippingPreferenceFactory $shipping_preference_factory
|
||||
) {
|
||||
|
||||
$this->session_handler = $session_handler;
|
||||
|
@ -160,60 +186,57 @@ class OrderProcessor {
|
|||
$this->logger = $logger;
|
||||
$this->subscription_helper = $subscription_helper;
|
||||
$this->order_helper = $order_helper;
|
||||
$this->purchase_unit_factory = $purchase_unit_factory;
|
||||
$this->payer_factory = $payer_factory;
|
||||
$this->shipping_preference_factory = $shipping_preference_factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes a given WooCommerce order and captured/authorizes the connected PayPal orders.
|
||||
*
|
||||
* @param \WC_Order $wc_order The WooCommerce order.
|
||||
* @param WC_Order $wc_order The WooCommerce order.
|
||||
*
|
||||
* @return bool
|
||||
* @throws PayPalOrderMissingException If no PayPal order.
|
||||
* @throws Exception If processing fails.
|
||||
*/
|
||||
public function process( \WC_Order $wc_order ): bool {
|
||||
// phpcs:ignore WordPress.Security.NonceVerification
|
||||
$order_id = $wc_order->get_meta( PayPalGateway::ORDER_ID_META_KEY ) ?: wc_clean( wp_unslash( $_POST['paypal_order_id'] ?? '' ) );
|
||||
$order = $this->session_handler->order();
|
||||
if ( ! $order && is_string( $order_id ) && $order_id ) {
|
||||
$order = $this->order_endpoint->order( $order_id );
|
||||
}
|
||||
public function process( WC_Order $wc_order ): void {
|
||||
$order = $this->session_handler->order();
|
||||
if ( ! $order ) {
|
||||
$order_id = $wc_order->get_meta( PayPalGateway::ORDER_ID_META_KEY );
|
||||
if ( ! $order_id ) {
|
||||
// phpcs:ignore WordPress.Security.NonceVerification
|
||||
$order_id = $wc_order->get_meta( PayPalGateway::ORDER_ID_META_KEY ) ?: wc_clean( wp_unslash( $_POST['paypal_order_id'] ?? '' ) );
|
||||
if ( is_string( $order_id ) && $order_id ) {
|
||||
try {
|
||||
$order = $this->order_endpoint->order( $order_id );
|
||||
} catch ( RuntimeException $exception ) {
|
||||
throw new Exception( __( 'Could not retrieve PayPal order.', 'woocommerce-paypal-payments' ) );
|
||||
}
|
||||
} else {
|
||||
$this->logger->warning(
|
||||
sprintf(
|
||||
'No PayPal order ID found in order #%d meta.',
|
||||
$wc_order->get_id()
|
||||
)
|
||||
);
|
||||
$this->last_error = __( 'Could not retrieve order. Maybe it was already completed or this browser is not supported. Please check your email or try again with a different browser.', 'woocommerce-paypal-payments' );
|
||||
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;
|
||||
throw new PayPalOrderMissingException(
|
||||
__(
|
||||
'Could not retrieve order. Maybe it was already completed or this browser is not supported. Please check your email or try again with a different browser.',
|
||||
'woocommerce-paypal-payments'
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$this->add_paypal_meta( $wc_order, $order, $this->environment );
|
||||
|
||||
$error_message = null;
|
||||
if ( $this->order_helper->contains_physical_goods( $order ) && ! $this->order_is_ready_for_process( $order ) ) {
|
||||
$error_message = __(
|
||||
'The payment is not ready for processing yet.',
|
||||
'woocommerce-paypal-payments'
|
||||
throw new Exception(
|
||||
__(
|
||||
'The payment is not ready for processing yet.',
|
||||
'woocommerce-paypal-payments'
|
||||
)
|
||||
);
|
||||
}
|
||||
if ( $error_message ) {
|
||||
$this->last_error = sprintf(
|
||||
// translators: %s is the message of the error.
|
||||
__( 'Payment error: %s', 'woocommerce-paypal-payments' ),
|
||||
$error_message
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
$order = $this->patch_order( $wc_order, $order );
|
||||
|
||||
|
@ -242,8 +265,30 @@ class OrderProcessor {
|
|||
if ( $this->capture_authorized_downloads( $order ) ) {
|
||||
$this->authorized_payments_processor->capture_authorized_payment( $wc_order );
|
||||
}
|
||||
$this->last_error = '';
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a PayPal order for the given WC order.
|
||||
*
|
||||
* @param WC_Order $wc_order The WC order.
|
||||
* @return Order
|
||||
* @throws RuntimeException If order creation fails.
|
||||
*/
|
||||
public function create_order( WC_Order $wc_order ): Order {
|
||||
$pu = $this->purchase_unit_factory->from_wc_order( $wc_order );
|
||||
$shipping_preference = $this->shipping_preference_factory->from_state( $pu, 'checkout' );
|
||||
$order = $this->order_endpoint->create(
|
||||
array( $pu ),
|
||||
$shipping_preference,
|
||||
$this->payer_factory->from_wc_order( $wc_order ),
|
||||
null,
|
||||
'',
|
||||
ApplicationContext::USER_ACTION_PAY_NOW
|
||||
);
|
||||
|
||||
$this->add_paypal_meta( $wc_order, $order, $this->environment );
|
||||
|
||||
return $order;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -280,25 +325,15 @@ class OrderProcessor {
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last error.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function last_error(): string {
|
||||
|
||||
return $this->last_error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Patches a given PayPal order with a WooCommerce order.
|
||||
*
|
||||
* @param \WC_Order $wc_order The WooCommerce order.
|
||||
* @param Order $order The PayPal order.
|
||||
* @param WC_Order $wc_order The WooCommerce order.
|
||||
* @param Order $order The PayPal order.
|
||||
*
|
||||
* @return Order
|
||||
*/
|
||||
public function patch_order( \WC_Order $wc_order, Order $order ): Order {
|
||||
public function patch_order( WC_Order $wc_order, Order $order ): Order {
|
||||
$this->apply_outbound_order_filters( $wc_order );
|
||||
$updated_order = $this->order_factory->from_wc_order( $wc_order, $order );
|
||||
$this->restore_order_from_filters( $wc_order );
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue