mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 13:44:42 +08:00
Refactor shipping_preference
Extract its determination to a separate class, also remove useless CartRepository and use single PU to avoid confusion.
This commit is contained in:
parent
67cdf9be8e
commit
fff2570124
20 changed files with 245 additions and 264 deletions
|
@ -23,7 +23,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
|||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Repository\CartRepository;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingPreferenceFactory;
|
||||
use WooCommerce\PayPalCommerce\Button\Helper\EarlyOrderHandler;
|
||||
use WooCommerce\PayPalCommerce\Session\SessionHandler;
|
||||
use WooCommerce\PayPalCommerce\Subscription\FreeTrialHandlerTrait;
|
||||
|
@ -48,13 +48,6 @@ class CreateOrderEndpoint implements EndpointInterface {
|
|||
*/
|
||||
private $request_data;
|
||||
|
||||
/**
|
||||
* The cart repository.
|
||||
*
|
||||
* @var CartRepository
|
||||
*/
|
||||
private $cart_repository;
|
||||
|
||||
/**
|
||||
* The PurchaseUnit factory.
|
||||
*
|
||||
|
@ -62,6 +55,13 @@ class CreateOrderEndpoint implements EndpointInterface {
|
|||
*/
|
||||
private $purchase_unit_factory;
|
||||
|
||||
/**
|
||||
* The shipping_preference factory.
|
||||
*
|
||||
* @var ShippingPreferenceFactory
|
||||
*/
|
||||
private $shipping_preference_factory;
|
||||
|
||||
/**
|
||||
* The order endpoint.
|
||||
*
|
||||
|
@ -105,11 +105,11 @@ class CreateOrderEndpoint implements EndpointInterface {
|
|||
private $parsed_request_data;
|
||||
|
||||
/**
|
||||
* The array of purchase units for order.
|
||||
* The purchase unit for order.
|
||||
*
|
||||
* @var PurchaseUnit[]
|
||||
* @var PurchaseUnit|null
|
||||
*/
|
||||
private $purchase_units;
|
||||
private $purchase_unit;
|
||||
|
||||
/**
|
||||
* Whether a new user must be registered during checkout.
|
||||
|
@ -128,21 +128,21 @@ class CreateOrderEndpoint implements EndpointInterface {
|
|||
/**
|
||||
* CreateOrderEndpoint constructor.
|
||||
*
|
||||
* @param RequestData $request_data The RequestData object.
|
||||
* @param CartRepository $cart_repository The CartRepository object.
|
||||
* @param PurchaseUnitFactory $purchase_unit_factory The Purchaseunit factory.
|
||||
* @param OrderEndpoint $order_endpoint The OrderEndpoint object.
|
||||
* @param PayerFactory $payer_factory The PayerFactory object.
|
||||
* @param SessionHandler $session_handler The SessionHandler object.
|
||||
* @param Settings $settings The Settings object.
|
||||
* @param EarlyOrderHandler $early_order_handler The EarlyOrderHandler object.
|
||||
* @param bool $registration_needed Whether a new user must be registered during checkout.
|
||||
* @param LoggerInterface $logger The logger.
|
||||
* @param RequestData $request_data The RequestData object.
|
||||
* @param PurchaseUnitFactory $purchase_unit_factory The PurchaseUnit factory.
|
||||
* @param ShippingPreferenceFactory $shipping_preference_factory The shipping_preference factory.
|
||||
* @param OrderEndpoint $order_endpoint The OrderEndpoint object.
|
||||
* @param PayerFactory $payer_factory The PayerFactory object.
|
||||
* @param SessionHandler $session_handler The SessionHandler object.
|
||||
* @param Settings $settings The Settings object.
|
||||
* @param EarlyOrderHandler $early_order_handler The EarlyOrderHandler object.
|
||||
* @param bool $registration_needed Whether a new user must be registered during checkout.
|
||||
* @param LoggerInterface $logger The logger.
|
||||
*/
|
||||
public function __construct(
|
||||
RequestData $request_data,
|
||||
CartRepository $cart_repository,
|
||||
PurchaseUnitFactory $purchase_unit_factory,
|
||||
ShippingPreferenceFactory $shipping_preference_factory,
|
||||
OrderEndpoint $order_endpoint,
|
||||
PayerFactory $payer_factory,
|
||||
SessionHandler $session_handler,
|
||||
|
@ -152,16 +152,16 @@ class CreateOrderEndpoint implements EndpointInterface {
|
|||
LoggerInterface $logger
|
||||
) {
|
||||
|
||||
$this->request_data = $request_data;
|
||||
$this->cart_repository = $cart_repository;
|
||||
$this->purchase_unit_factory = $purchase_unit_factory;
|
||||
$this->api_endpoint = $order_endpoint;
|
||||
$this->payer_factory = $payer_factory;
|
||||
$this->session_handler = $session_handler;
|
||||
$this->settings = $settings;
|
||||
$this->early_order_handler = $early_order_handler;
|
||||
$this->registration_needed = $registration_needed;
|
||||
$this->logger = $logger;
|
||||
$this->request_data = $request_data;
|
||||
$this->purchase_unit_factory = $purchase_unit_factory;
|
||||
$this->shipping_preference_factory = $shipping_preference_factory;
|
||||
$this->api_endpoint = $order_endpoint;
|
||||
$this->payer_factory = $payer_factory;
|
||||
$this->session_handler = $session_handler;
|
||||
$this->settings = $settings;
|
||||
$this->early_order_handler = $early_order_handler;
|
||||
$this->registration_needed = $registration_needed;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -198,9 +198,9 @@ class CreateOrderEndpoint implements EndpointInterface {
|
|||
)
|
||||
);
|
||||
}
|
||||
$this->purchase_units = array( $this->purchase_unit_factory->from_wc_order( $wc_order ) );
|
||||
$this->purchase_unit = $this->purchase_unit_factory->from_wc_order( $wc_order );
|
||||
} else {
|
||||
$this->purchase_units = $this->cart_repository->all();
|
||||
$this->purchase_unit = $this->purchase_unit_factory->from_wc_cart();
|
||||
|
||||
// The cart does not have any info about payment method, so we must handle free trial here.
|
||||
if ( (
|
||||
|
@ -209,10 +209,10 @@ class CreateOrderEndpoint implements EndpointInterface {
|
|||
)
|
||||
&& $this->is_free_trial_cart()
|
||||
) {
|
||||
$this->purchase_units[0]->set_amount(
|
||||
$this->purchase_unit->set_amount(
|
||||
new Amount(
|
||||
new Money( 1.0, $this->purchase_units[0]->amount()->currency_code() ),
|
||||
$this->purchase_units[0]->amount()->breakdown()
|
||||
new Money( 1.0, $this->purchase_unit->amount()->currency_code() ),
|
||||
$this->purchase_unit->amount()->breakdown()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
@ -329,17 +329,21 @@ class CreateOrderEndpoint implements EndpointInterface {
|
|||
* phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
|
||||
*/
|
||||
private function create_paypal_order( \WC_Order $wc_order = null ): Order {
|
||||
$needs_shipping = WC()->cart instanceof \WC_Cart && WC()->cart->needs_shipping();
|
||||
$shipping_address_is_fix = $needs_shipping && 'checkout' === $this->parsed_request_data['context'];
|
||||
assert( $this->purchase_unit instanceof PurchaseUnit );
|
||||
|
||||
$shipping_preference = $this->shipping_preference_factory->from_state(
|
||||
$this->purchase_unit,
|
||||
$this->parsed_request_data['context'],
|
||||
WC()->cart
|
||||
);
|
||||
|
||||
try {
|
||||
return $this->api_endpoint->create(
|
||||
$this->purchase_units,
|
||||
array( $this->purchase_unit ),
|
||||
$shipping_preference,
|
||||
$this->payer( $this->parsed_request_data, $wc_order ),
|
||||
null,
|
||||
$this->payment_method(),
|
||||
'',
|
||||
$shipping_address_is_fix
|
||||
$this->payment_method()
|
||||
);
|
||||
} catch ( PayPalApiException $exception ) {
|
||||
// Looks like currently there is no proper way to validate the shipping address for PayPal,
|
||||
|
@ -354,17 +358,14 @@ class CreateOrderEndpoint implements EndpointInterface {
|
|||
) ) {
|
||||
$this->logger->info( 'Invalid shipping address for order creation, retrying without it.' );
|
||||
|
||||
foreach ( $this->purchase_units as $purchase_unit ) {
|
||||
$purchase_unit->set_shipping( null );
|
||||
}
|
||||
$this->purchase_unit->set_shipping( null );
|
||||
|
||||
return $this->api_endpoint->create(
|
||||
$this->purchase_units,
|
||||
array( $this->purchase_unit ),
|
||||
$shipping_preference,
|
||||
$this->payer( $this->parsed_request_data, $wc_order ),
|
||||
null,
|
||||
$this->payment_method(),
|
||||
'',
|
||||
$shipping_address_is_fix
|
||||
$this->payment_method()
|
||||
);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue