$args The arguments.
+ * @param array $response The request response.
+ * @param string $message_part The part of the message.
+ * @return void
+ *
+ * @throws PayPalApiException PayPal APi exception.
+ */
+ protected function throw_paypal_api_exception( int $status_code, array $args, array $response, string $message_part ): void {
+ $error = new PayPalApiException(
+ json_decode( $response['body'] ),
+ $status_code
+ );
+ $this->logger->log(
+ 'warning',
+ sprintf(
+ "Failed to {$message_part} order tracking information. PayPal API response: %s",
+ $error->getMessage()
+ ),
+ array(
+ 'args' => $args,
+ 'response' => $response,
+ )
+ );
+ throw $error;
+ }
+
+ /**
+ * Throws the exception && logs the error message with given arguments.
+ *
+ * @param array $args The arguments.
+ * @param string $message_part The part of the message.
+ * @return void
+ *
+ * @throws RuntimeException The exception.
+ */
+ protected function throw_runtime_exception( array $args, string $message_part ): void {
+ $error = new RuntimeException( "Could not {$message_part} the order tracking information." );
+ $this->logger->log(
+ 'warning',
+ $error->getMessage(),
+ $args
+ );
+ throw $error;
+ }
}
diff --git a/modules/ppcp-order-tracking/src/MetaBoxRenderer.php b/modules/ppcp-order-tracking/src/MetaBoxRenderer.php
index b04f0cff4..d14fa8cc6 100644
--- a/modules/ppcp-order-tracking/src/MetaBoxRenderer.php
+++ b/modules/ppcp-order-tracking/src/MetaBoxRenderer.php
@@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\OrderTracking;
use WC_Order;
use WooCommerce\PayPalCommerce\OrderTracking\Endpoint\OrderTrackingEndpoint;
+use WooCommerce\PayPalCommerce\OrderTracking\Shipment\ShipmentInterface;
use WP_Post;
/**
@@ -24,15 +25,6 @@ use WP_Post;
*/
class MetaBoxRenderer {
- public const NAME_PREFIX = 'ppcp-tracking';
-
- /**
- * The OrderTrackingEndpoint.
- *
- * @var OrderTrackingEndpoint
- */
- protected $order_tracking_endpoint;
-
/**
* Allowed shipping statuses.
*
@@ -48,85 +40,136 @@ class MetaBoxRenderer {
*/
protected $carriers;
+ /**
+ * The order tracking endpoint.
+ *
+ * @var OrderTrackingEndpoint
+ */
+ protected $order_tracking_endpoint;
+
+ /**
+ * Whether new API should be used.
+ *
+ * @var bool
+ */
+ protected $should_use_new_api;
+
/**
* MetaBoxRenderer constructor.
*
- * @param OrderTrackingEndpoint $order_tracking_endpoint The OrderTrackingEndpoint.
* @param string[] $allowed_statuses Allowed shipping statuses.
* @param array $carriers Available shipping carriers.
* @psalm-param Carriers $carriers
+ * @param OrderTrackingEndpoint $order_tracking_endpoint The order tracking endpoint.
+ * @param bool $should_use_new_api Whether new API should be used.
*/
public function __construct(
- OrderTrackingEndpoint $order_tracking_endpoint,
array $allowed_statuses,
- array $carriers
+ array $carriers,
+ OrderTrackingEndpoint $order_tracking_endpoint,
+ bool $should_use_new_api
) {
- $this->order_tracking_endpoint = $order_tracking_endpoint;
$this->allowed_statuses = $allowed_statuses;
$this->carriers = $carriers;
+ $this->order_tracking_endpoint = $order_tracking_endpoint;
+ $this->should_use_new_api = $should_use_new_api;
}
/**
* Renders the order tracking MetaBox.
*
* @param mixed $post_or_order_object Either WP_Post or WC_Order when COT is data source.
- *
- * @return void
*/
public function render( $post_or_order_object ): void {
$wc_order = ( $post_or_order_object instanceof WP_Post ) ? wc_get_order( $post_or_order_object->ID ) : $post_or_order_object;
- if ( ! is_a( $wc_order, WC_Order::class ) ) {
+ if ( ! $wc_order instanceof WC_Order ) {
return;
}
- $tracking_info = $this->order_tracking_endpoint->get_tracking_information( $wc_order->get_id() );
+ $transaction_id = $wc_order->get_transaction_id() ?: '';
+ $order_items = $wc_order->get_items();
+ $order_item_count = ! empty( $order_items ) ? count( $order_items ) : 0;
- $transaction_id = $tracking_info['transaction_id'] ?? $wc_order->get_transaction_id() ?: '';
- $tracking_number = $tracking_info['tracking_number'] ?? '';
- $status_value = $tracking_info['status'] ?? 'SHIPPED';
- $carrier_value = $tracking_info['carrier'] ?? '';
-
- $carriers = (array) apply_filters( 'woocommerce_paypal_payments_tracking_carriers', $this->carriers, $wc_order->get_id() );
- $statuses = (array) apply_filters( 'woocommerce_paypal_payments_tracking_statuses', $this->allowed_statuses, $wc_order->get_id() );
- $tracking_number = (string) apply_filters( 'woocommerce_paypal_payments_tracking_number', $tracking_number, $wc_order->get_id() );
-
- $action = ! $tracking_info ? 'create' : 'update';
+ /**
+ * The shipments
+ *
+ * @var ShipmentInterface[] $shipments
+ */
+ $shipments = $this->order_tracking_endpoint->list_tracking_information( $wc_order->get_id() ) ?? array();
?>
-
-
-
-
-
-
-
-
-
-
-
-
-