mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-08-30 05:00:51 +08:00
Fix coding styles, psalm
This commit is contained in:
parent
b3f858d4b3
commit
cc0e4db916
11 changed files with 226 additions and 178 deletions
|
@ -50,6 +50,27 @@ namespace Vendidero\Germanized\Shipments {
|
|||
|
||||
public function add_note( $note, $added_by_user = false ) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of items within this shipment.
|
||||
*
|
||||
* @return ShipmentItem[]
|
||||
*/
|
||||
public function get_items() {
|
||||
}
|
||||
}
|
||||
|
||||
class ShipmentItem extends WC_Data {
|
||||
|
||||
/**
|
||||
* Get order ID this meta belongs to.
|
||||
*
|
||||
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
|
||||
* @return int
|
||||
*/
|
||||
public function get_order_item_id( $context = 'view' ) {
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace WooCommerce\PayPalCommerce\Compat;
|
||||
|
||||
use Vendidero\Germanized\Shipments\ShipmentItem;
|
||||
use WooCommerce\PayPalCommerce\OrderTracking\Shipment\ShipmentFactoryInterface;
|
||||
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
|
||||
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
|
||||
|
@ -49,11 +50,7 @@ class CompatModule implements ModuleInterface {
|
|||
|
||||
$this->initialize_ppec_compat_layer( $c );
|
||||
$this->fix_site_ground_optimizer_compatibility( $c );
|
||||
|
||||
$tracking_enabled = $c->get( 'order-tracking.is-module-enabled' );
|
||||
if ( $tracking_enabled ) {
|
||||
//$this->initialize_tracking_compat_layer( $c );
|
||||
}
|
||||
$this->initialize_tracking_compat_layer( $c );
|
||||
|
||||
$asset_loader = $c->get( 'compat.assets' );
|
||||
assert( $asset_loader instanceof CompatAssets );
|
||||
|
@ -165,6 +162,7 @@ class CompatModule implements ModuleInterface {
|
|||
}
|
||||
|
||||
$wc_order = $shipment->get_order();
|
||||
|
||||
if ( ! is_a( $wc_order, WC_Order::class ) ) {
|
||||
return;
|
||||
}
|
||||
|
@ -174,7 +172,7 @@ class CompatModule implements ModuleInterface {
|
|||
$tracking_number = $shipment->get_tracking_id();
|
||||
$carrier = $shipment->get_shipping_provider();
|
||||
$items = array_map(
|
||||
function ( $item ) {
|
||||
function ( ShipmentItem $item ): int {
|
||||
return $item->get_order_item_id();
|
||||
},
|
||||
$shipment->get_items()
|
||||
|
@ -238,7 +236,7 @@ class CompatModule implements ModuleInterface {
|
|||
return;
|
||||
}
|
||||
|
||||
$order_id = (int) wc_clean( wp_unslash( $_POST['order_id'] ?? 0 ) );
|
||||
$order_id = (int) wc_clean( wp_unslash( $_POST['order_id'] ?? '' ) );
|
||||
$wc_order = wc_get_order( $order_id );
|
||||
if ( ! is_a( $wc_order, WC_Order::class ) ) {
|
||||
return;
|
||||
|
@ -247,8 +245,10 @@ class CompatModule implements ModuleInterface {
|
|||
$transaction_id = $wc_order->get_transaction_id();
|
||||
$tracking_number = wc_clean( wp_unslash( $_POST['tracking_number'] ?? '' ) );
|
||||
$carrier = wc_clean( wp_unslash( $_POST['tracking_provider'] ?? '' ) );
|
||||
$carrier_other = wc_clean( wp_unslash( $_POST['custom_tracking_provider'] ?? '' ) );
|
||||
$carrier = $carrier ?: $carrier_other ?: '';
|
||||
|
||||
if ( ! $tracking_number || ! $carrier || ! $transaction_id ) {
|
||||
if ( ! $tracking_number || ! is_string( $tracking_number ) || ! $carrier || ! is_string( $carrier ) || ! $transaction_id ) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -592,7 +592,6 @@ namespace WooCommerce\PayPalCommerce\OrderTracking;
|
|||
'WHISTL_SFTP' => _x( 'Whistl', 'Name of carrier', 'woocommerce-paypal-payments' ),
|
||||
'WINESHIPPING_WEBHOOK' => _x( 'Wineshipping', 'Name of carrier', 'woocommerce-paypal-payments' ),
|
||||
'WISH_EMAIL_PUSH' => _x( 'Wish', 'Name of carrier', 'woocommerce-paypal-payments' ),
|
||||
'WOOYOUNG_LOGISTICS_SFTP' => _x( 'WOO YOUNG LOG', 'Name of carrier', 'woocommerce-paypal-payments' ),
|
||||
'WOOYOUNG_LOGISTICS_SFTP' => _x( 'WOO YOUNG LOGISTICS CO., LTD.', 'Name of carrier', 'woocommerce-paypal-payments' ),
|
||||
'WORLDCOURIER' => _x( 'World Courier', 'Name of carrier', 'woocommerce-paypal-payments' ),
|
||||
'WORLDNET' => _x( 'Worldnet Logistics', 'Name of carrier', 'woocommerce-paypal-payments' ),
|
||||
|
|
|
@ -16,7 +16,7 @@ document.addEventListener(
|
|||
|
||||
function toggleLineItemsSelectbox() {
|
||||
const selectContainer = document.getElementById('items-select-container');
|
||||
includeAllItemsCheckbox.addEventListener('change', function(){
|
||||
includeAllItemsCheckbox?.addEventListener('change', function(){
|
||||
selectContainer.style.display = includeAllItemsCheckbox.checked ? 'none' : 'block';
|
||||
})
|
||||
}
|
||||
|
|
|
@ -35,7 +35,8 @@ return array(
|
|||
$container->get( 'woocommerce.logger.woocommerce' ),
|
||||
$container->get( 'button.request-data' ),
|
||||
$container->get( 'order-tracking.shipment.factory' ),
|
||||
$container->get( 'order-tracking.allowed-shipping-statuses' )
|
||||
$container->get( 'order-tracking.allowed-shipping-statuses' ),
|
||||
$container->get( 'order-tracking.is-merchant-country-us' )
|
||||
);
|
||||
},
|
||||
'order-tracking.module.url' => static function ( ContainerInterface $container ): string {
|
||||
|
@ -53,7 +54,8 @@ return array(
|
|||
return new MetaBoxRenderer(
|
||||
$container->get( 'order-tracking.allowed-shipping-statuses' ),
|
||||
$container->get( 'order-tracking.available-carriers' ),
|
||||
$container->get( 'order-tracking.endpoint.controller' )
|
||||
$container->get( 'order-tracking.endpoint.controller' ),
|
||||
$container->get( 'order-tracking.is-merchant-country-us' )
|
||||
);
|
||||
},
|
||||
'order-tracking.allowed-shipping-statuses' => static function ( ContainerInterface $container ): array {
|
||||
|
@ -113,4 +115,8 @@ return array(
|
|||
|
||||
return $is_tracking_available && apply_filters( 'woocommerce_paypal_payments_shipment_tracking_enabled', true );
|
||||
},
|
||||
|
||||
'order-tracking.is-merchant-country-us' => static function ( ContainerInterface $container ): bool {
|
||||
return $container->get( 'api.shop.country' ) === 'US';
|
||||
},
|
||||
);
|
||||
|
|
|
@ -84,7 +84,7 @@ class OrderEditPageAssets {
|
|||
'tracking_info' => array(
|
||||
'endpoint' => \WC_AJAX::get_endpoint( OrderTrackingEndpoint::ENDPOINT ),
|
||||
'nonce' => wp_create_nonce( OrderTrackingEndpoint::nonce() ),
|
||||
'url' => admin_url( 'admin-ajax.php' )
|
||||
'url' => admin_url( 'admin-ajax.php' ),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
|
|
@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\OrderTracking\Endpoint;
|
|||
|
||||
use Exception;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use stdClass;
|
||||
use WC_Order;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\RequestTrait;
|
||||
|
@ -32,9 +33,8 @@ use WooCommerce\PayPalCommerce\WcGateway\Processor\TransactionIdHandlingTrait;
|
|||
* status: SupportedStatuses,
|
||||
* tracking_number: string,
|
||||
* carrier: string,
|
||||
* items?: string,
|
||||
* items?: list<int>,
|
||||
* carrier_name_other?: string,
|
||||
* order_id?: int
|
||||
* }
|
||||
* Class OrderTrackingEndpoint
|
||||
*/
|
||||
|
@ -86,6 +86,13 @@ class OrderTrackingEndpoint {
|
|||
*/
|
||||
protected $allowed_statuses;
|
||||
|
||||
/**
|
||||
* Whether new API should be used.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $should_use_new_api;
|
||||
|
||||
/**
|
||||
* PartnersEndpoint constructor.
|
||||
*
|
||||
|
@ -95,6 +102,7 @@ class OrderTrackingEndpoint {
|
|||
* @param RequestData $request_data The Request data.
|
||||
* @param ShipmentFactoryInterface $shipment_factory The ShipmentFactory.
|
||||
* @param string[] $allowed_statuses Allowed shipping statuses.
|
||||
* @param bool $should_use_new_api Whether new API should be used.
|
||||
*/
|
||||
public function __construct(
|
||||
string $host,
|
||||
|
@ -102,14 +110,16 @@ class OrderTrackingEndpoint {
|
|||
LoggerInterface $logger,
|
||||
RequestData $request_data,
|
||||
ShipmentFactoryInterface $shipment_factory,
|
||||
array $allowed_statuses
|
||||
array $allowed_statuses,
|
||||
bool $should_use_new_api
|
||||
) {
|
||||
$this->host = $host;
|
||||
$this->bearer = $bearer;
|
||||
$this->logger = $logger;
|
||||
$this->request_data = $request_data;
|
||||
$this->shipment_factory = $shipment_factory;
|
||||
$this->allowed_statuses = $allowed_statuses;
|
||||
$this->host = $host;
|
||||
$this->bearer = $bearer;
|
||||
$this->logger = $logger;
|
||||
$this->request_data = $request_data;
|
||||
$this->shipment_factory = $shipment_factory;
|
||||
$this->allowed_statuses = $allowed_statuses;
|
||||
$this->should_use_new_api = $should_use_new_api;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -160,64 +170,33 @@ class OrderTrackingEndpoint {
|
|||
* @throws RuntimeException If problem adding.
|
||||
*/
|
||||
public function add_tracking_information( ShipmentInterface $shipment, int $order_id ) : void {
|
||||
$wc_order = wc_get_order( $order_id );
|
||||
$paypal_order_id = $wc_order->get_meta( PayPalGateway::ORDER_ID_META_KEY );
|
||||
$host = trailingslashit( $this->host );
|
||||
$url = "{$host}v2/checkout/orders/{$paypal_order_id}/track";
|
||||
$shipment_data = $shipment->to_array( $order_id );
|
||||
$wc_order = wc_get_order( $order_id );
|
||||
if ( ! $wc_order instanceof WC_Order ) {
|
||||
return;
|
||||
}
|
||||
|
||||
unset( $shipment_data['transaction_id'] );
|
||||
$shipment_data['capture_id'] = $shipment->transaction_id();
|
||||
$shipment_request_data = $this->generate_request_data( $wc_order, $shipment );
|
||||
|
||||
$args = array(
|
||||
'method' => 'POST',
|
||||
'headers' => $this->request_headers(),
|
||||
'body' => wp_json_encode( (array) apply_filters( 'woocommerce_paypal_payments_tracking_data_before_sending', $shipment_data, $order_id ) ),
|
||||
);
|
||||
$url = $shipment_request_data['url'] ?? '';
|
||||
$args = $shipment_request_data['args'] ?? array();
|
||||
|
||||
do_action( 'woocommerce_paypal_payments_before_tracking_is_added', $order_id, $shipment_data );
|
||||
if ( ! $url || empty( $args ) ) {
|
||||
$this->throw_runtime_exception( $shipment_request_data, 'create' );
|
||||
}
|
||||
|
||||
$response = $this->request( $url, $args );
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
$error = new RuntimeException(
|
||||
'Could not create order tracking information.'
|
||||
$args = array(
|
||||
'args' => $args,
|
||||
'response' => $response,
|
||||
);
|
||||
$this->logger->log(
|
||||
'warning',
|
||||
$error->getMessage(),
|
||||
array(
|
||||
'args' => $args,
|
||||
'response' => $response,
|
||||
)
|
||||
);
|
||||
throw $error;
|
||||
$this->throw_runtime_exception( $args, 'create' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Need to ignore Method WP_Error::offsetGet does not exist
|
||||
*
|
||||
* @psalm-suppress UndefinedMethod
|
||||
*/
|
||||
$json = json_decode( $response['body'] );
|
||||
$status_code = (int) wp_remote_retrieve_response_code( $response );
|
||||
if ( 201 !== $status_code ) {
|
||||
$error = new PayPalApiException(
|
||||
$json,
|
||||
$status_code
|
||||
);
|
||||
$this->logger->log(
|
||||
'warning',
|
||||
sprintf(
|
||||
'Failed to create order tracking information. PayPal API response: %1$s',
|
||||
$error->getMessage()
|
||||
),
|
||||
array(
|
||||
'args' => $args,
|
||||
'response' => $response,
|
||||
)
|
||||
);
|
||||
throw $error;
|
||||
if ( 201 !== $status_code && ! is_wp_error( $response ) ) {
|
||||
$this->throw_paypal_api_exception( $status_code, $args, $response, 'create' );
|
||||
}
|
||||
|
||||
$this->save_tracking_metadata( $wc_order, $shipment->tracking_number(), array_keys( $shipment->line_items() ) );
|
||||
|
@ -237,7 +216,7 @@ class OrderTrackingEndpoint {
|
|||
$host = trailingslashit( $this->host );
|
||||
$tracker_id = $this->find_tracker_id( $shipment->transaction_id(), $shipment->tracking_number() );
|
||||
$url = "{$host}v1/shipping/trackers/{$tracker_id}";
|
||||
$shipment_data = $shipment->to_array( $order_id );
|
||||
$shipment_data = $shipment->to_array();
|
||||
|
||||
$args = array(
|
||||
'method' => 'PUT',
|
||||
|
@ -250,44 +229,16 @@ class OrderTrackingEndpoint {
|
|||
$response = $this->request( $url, $args );
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
$error = new RuntimeException(
|
||||
'Could not update order tracking information.'
|
||||
$args = array(
|
||||
'args' => $args,
|
||||
'response' => $response,
|
||||
);
|
||||
$this->logger->log(
|
||||
'warning',
|
||||
$error->getMessage(),
|
||||
array(
|
||||
'args' => $args,
|
||||
'response' => $response,
|
||||
)
|
||||
);
|
||||
throw $error;
|
||||
$this->throw_runtime_exception( $args, 'update' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Need to ignore Method WP_Error::offsetGet does not exist
|
||||
*
|
||||
* @psalm-suppress UndefinedMethod
|
||||
*/
|
||||
$json = json_decode( $response['body'] );
|
||||
$status_code = (int) wp_remote_retrieve_response_code( $response );
|
||||
if ( 204 !== $status_code ) {
|
||||
$error = new PayPalApiException(
|
||||
$json,
|
||||
$status_code
|
||||
);
|
||||
$this->logger->log(
|
||||
'warning',
|
||||
sprintf(
|
||||
'Failed to update the order tracking information. PayPal API response: %1$s',
|
||||
$error->getMessage()
|
||||
),
|
||||
array(
|
||||
'args' => $args,
|
||||
'response' => $response,
|
||||
)
|
||||
);
|
||||
throw $error;
|
||||
if ( 204 !== $status_code && ! is_wp_error( $response ) ) {
|
||||
$this->throw_paypal_api_exception( $status_code, $args, $response, 'update' );
|
||||
}
|
||||
|
||||
do_action( 'woocommerce_paypal_payments_after_tracking_is_updated', $order_id, $response );
|
||||
|
@ -303,7 +254,11 @@ class OrderTrackingEndpoint {
|
|||
* @throws RuntimeException If problem getting.
|
||||
*/
|
||||
public function get_tracking_information( int $wc_order_id, string $tracking_number ) : ?ShipmentInterface {
|
||||
$wc_order = wc_get_order( $wc_order_id );
|
||||
$wc_order = wc_get_order( $wc_order_id );
|
||||
if ( ! $wc_order instanceof WC_Order ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$host = trailingslashit( $this->host );
|
||||
$tracker_id = $this->find_tracker_id( $wc_order->get_transaction_id(), $tracking_number );
|
||||
$url = "{$host}v1/shipping/trackers/{$tracker_id}";
|
||||
|
@ -316,18 +271,11 @@ class OrderTrackingEndpoint {
|
|||
$response = $this->request( $url, $args );
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
$error = new RuntimeException(
|
||||
'Could not fetch the tracking information.'
|
||||
$args = array(
|
||||
'args' => $args,
|
||||
'response' => $response,
|
||||
);
|
||||
$this->logger->log(
|
||||
'warning',
|
||||
$error->getMessage(),
|
||||
array(
|
||||
'args' => $args,
|
||||
'response' => $response,
|
||||
)
|
||||
);
|
||||
throw $error;
|
||||
$this->throw_runtime_exception( $args, 'fetch' );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -346,15 +294,18 @@ class OrderTrackingEndpoint {
|
|||
}
|
||||
|
||||
/**
|
||||
* Gets the tracking information of a given order.
|
||||
* Gets the list of shipments of a given order.
|
||||
*
|
||||
* @param int $wc_order_id The order ID.
|
||||
* @return array|null The tracking information.
|
||||
* @psalm-return TrackingInfo|null
|
||||
* @return ShipmentInterface[] The list of shipments.
|
||||
* @throws RuntimeException If problem getting.
|
||||
*/
|
||||
public function list_tracking_information( int $wc_order_id ) : ?array {
|
||||
$wc_order = wc_get_order( $wc_order_id );
|
||||
$wc_order = wc_get_order( $wc_order_id );
|
||||
if ( ! $wc_order instanceof WC_Order ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$host = trailingslashit( $this->host );
|
||||
$transaction_id = $wc_order->get_transaction_id();
|
||||
$url = "{$host}v1/shipping/trackers?transaction_id={$transaction_id}";
|
||||
|
@ -367,18 +318,11 @@ class OrderTrackingEndpoint {
|
|||
$response = $this->request( $url, $args );
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
$error = new RuntimeException(
|
||||
'Could not fetch the tracking information.'
|
||||
$args = array(
|
||||
'args' => $args,
|
||||
'response' => $response,
|
||||
);
|
||||
$this->logger->log(
|
||||
'warning',
|
||||
$error->getMessage(),
|
||||
array(
|
||||
'args' => $args,
|
||||
'response' => $response,
|
||||
)
|
||||
);
|
||||
throw $error;
|
||||
$this->throw_runtime_exception( $args, 'fetch' );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -455,8 +399,7 @@ class OrderTrackingEndpoint {
|
|||
/**
|
||||
* Validates the requested tracking info.
|
||||
*
|
||||
* @param array $tracking_info A map of tracking information keys to values.
|
||||
* @psalm-param TrackingInfo $tracking_info
|
||||
* @param array<string, mixed> $tracking_info A map of tracking information keys to values.
|
||||
* @return void
|
||||
* @throws RuntimeException If validation failed.
|
||||
*/
|
||||
|
@ -526,4 +469,88 @@ class OrderTrackingEndpoint {
|
|||
$wc_order->update_meta_data( OrderTrackingModule::PPCP_TRACKING_INFO_META_NAME, $tracking_meta );
|
||||
$wc_order->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the request data.
|
||||
*
|
||||
* @param WC_Order $wc_order The WC order.
|
||||
* @param ShipmentInterface $shipment The shipment.
|
||||
* @return array
|
||||
*/
|
||||
protected function generate_request_data( WC_Order $wc_order, ShipmentInterface $shipment ): array {
|
||||
$paypal_order_id = $wc_order->get_meta( PayPalGateway::ORDER_ID_META_KEY );
|
||||
$host = trailingslashit( $this->host );
|
||||
$shipment_data = $shipment->to_array();
|
||||
|
||||
$old_api_data = $shipment_data;
|
||||
unset( $old_api_data['items'] );
|
||||
$request_shipment_data = array( 'trackers' => array( $old_api_data ) );
|
||||
|
||||
if ( $this->should_use_new_api ) {
|
||||
unset( $shipment_data['transaction_id'] );
|
||||
$shipment_data['capture_id'] = $shipment->transaction_id();
|
||||
$request_shipment_data = $shipment_data;
|
||||
}
|
||||
|
||||
$url = $this->should_use_new_api ? "{$host}v2/checkout/orders/{$paypal_order_id}/track" : "{$host}v1/shipping/trackers";
|
||||
$args = array(
|
||||
'method' => 'POST',
|
||||
'headers' => $this->request_headers(),
|
||||
'body' => wp_json_encode( (array) apply_filters( 'woocommerce_paypal_payments_tracking_data_before_sending', $request_shipment_data, $wc_order->get_id() ) ),
|
||||
);
|
||||
|
||||
return array(
|
||||
'url' => $url,
|
||||
'args' => $args,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws PayPal APi exception and logs the error message with given arguments.
|
||||
*
|
||||
* @param int $status_code The response status code.
|
||||
* @param array<string, mixed> $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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace WooCommerce\PayPalCommerce\OrderTracking;
|
||||
|
||||
use WC_Order;
|
||||
use WooCommerce\PayPalCommerce\OrderTracking\Endpoint\OrderTrackingEndpoint;
|
||||
use WooCommerce\PayPalCommerce\OrderTracking\Shipment\ShipmentInterface;
|
||||
use WP_Post;
|
||||
|
@ -46,6 +47,13 @@ class MetaBoxRenderer {
|
|||
*/
|
||||
protected $order_tracking_endpoint;
|
||||
|
||||
/**
|
||||
* Whether new API should be used.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $should_use_new_api;
|
||||
|
||||
/**
|
||||
* MetaBoxRenderer constructor.
|
||||
*
|
||||
|
@ -53,16 +61,19 @@ class MetaBoxRenderer {
|
|||
* @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(
|
||||
array $allowed_statuses,
|
||||
array $carriers,
|
||||
OrderTrackingEndpoint $order_tracking_endpoint
|
||||
OrderTrackingEndpoint $order_tracking_endpoint,
|
||||
bool $should_use_new_api
|
||||
) {
|
||||
|
||||
$this->allowed_statuses = $allowed_statuses;
|
||||
$this->carriers = $carriers;
|
||||
$this->order_tracking_endpoint = $order_tracking_endpoint;
|
||||
$this->should_use_new_api = $should_use_new_api;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -71,8 +82,12 @@ class MetaBoxRenderer {
|
|||
* @param WP_Post $post The post object.
|
||||
*/
|
||||
public function render( WP_Post $post ): void {
|
||||
$wc_order_id = $post->ID;
|
||||
$wc_order = wc_get_order( $wc_order_id );
|
||||
$wc_order_id = $post->ID;
|
||||
$wc_order = wc_get_order( $wc_order_id );
|
||||
if ( ! $wc_order instanceof WC_Order ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$transaction_id = $wc_order->get_transaction_id() ?: '';
|
||||
$order_items = $wc_order->get_items();
|
||||
$order_item_count = ! empty( $order_items ) ? count( $order_items ) : 0;
|
||||
|
@ -91,7 +106,7 @@ class MetaBoxRenderer {
|
|||
<label for="ppcp-tracking-transaction_id"><?php echo esc_html__( 'Transaction ID', 'woocommerce-paypal-payments' ); ?></label>
|
||||
<input type="text" disabled class="ppcp-tracking-transaction_id disabled" id="ppcp-tracking-transaction_id" name="ppcp-tracking[transaction_id]" value="<?php echo esc_attr( $transaction_id ); ?>" />
|
||||
</p>
|
||||
<?php if ( $order_item_count > 1 ) : ?>
|
||||
<?php if ( $order_item_count > 1 && $this->should_use_new_api ) : ?>
|
||||
<p>
|
||||
<label for="include-all-items"><?php echo esc_html__( 'Include All Products', 'woocommerce-paypal-payments' ); ?></label>
|
||||
<input type="checkbox" id="include-all-items" checked>
|
||||
|
@ -99,7 +114,7 @@ class MetaBoxRenderer {
|
|||
<label for="ppcp-tracking-items"><?php echo esc_html__( 'Select items for this shipment', 'woocommerce-paypal-payments' ); ?></label>
|
||||
<select multiple class="wc-enhanced-select ppcp-tracking-items" id="ppcp-tracking-items" name="ppcp-tracking[items]">
|
||||
<?php foreach ( $order_items as $item ) : ?>
|
||||
<option value="<?php echo esc_attr( $item->get_id() ); ?>"><?php echo esc_html( $item->get_name() ); ?></option>
|
||||
<option value="<?php echo intval( $item->get_id() ); ?>"><?php echo esc_html( $item->get_name() ); ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</div>
|
||||
|
@ -140,7 +155,7 @@ class MetaBoxRenderer {
|
|||
<label for="ppcp-tracking-carrier_name_other"><?php echo esc_html__( 'Carrier Name*', 'woocommerce-paypal-payments' ); ?></label>
|
||||
<input type="text" class="ppcp-tracking-carrier_name_other" id="ppcp-tracking-carrier_name_other" name="ppcp-tracking[carrier_name_other]" />
|
||||
</p>
|
||||
<input type="hidden" class="ppcp-tracking-order_id" name="ppcp-tracking[order_id]" value="<?php echo esc_html( $wc_order_id ); ?>"/>
|
||||
<input type="hidden" class="ppcp-tracking-order_id" name="ppcp-tracking[order_id]" value="<?php echo intval( $wc_order_id ); ?>"/>
|
||||
<p><button type="button" class="button submit_tracking_info"><?php echo esc_html__( 'Add Shipment', 'woocommerce-paypal-payments' ); ?></button></p>
|
||||
</div>
|
||||
<div class="ppcp-tracking-column shipments">
|
||||
|
|
|
@ -49,6 +49,11 @@ class OrderTrackingModule implements ModuleInterface {
|
|||
public function run( ContainerInterface $c ): void {
|
||||
$tracking_enabled = $c->get( 'order-tracking.is-module-enabled' );
|
||||
|
||||
$endpoint = $c->get( 'order-tracking.endpoint.controller' );
|
||||
assert( $endpoint instanceof OrderTrackingEndpoint );
|
||||
|
||||
add_action( 'wc_ajax_' . OrderTrackingEndpoint::ENDPOINT, array( $endpoint, 'handle_request' ) );
|
||||
|
||||
if ( ! $tracking_enabled ) {
|
||||
return;
|
||||
}
|
||||
|
@ -56,15 +61,11 @@ class OrderTrackingModule implements ModuleInterface {
|
|||
$asset_loader = $c->get( 'order-tracking.assets' );
|
||||
assert( $asset_loader instanceof OrderEditPageAssets );
|
||||
|
||||
$endpoint = $c->get( 'order-tracking.endpoint.controller' );
|
||||
assert( $endpoint instanceof OrderTrackingEndpoint );
|
||||
|
||||
$logger = $c->get( 'woocommerce.logger.woocommerce' );
|
||||
assert( $logger instanceof LoggerInterface );
|
||||
|
||||
add_action( 'init', array( $asset_loader, 'register' ) );
|
||||
add_action( 'admin_enqueue_scripts', array( $asset_loader, 'enqueue' ) );
|
||||
add_action( 'wc_ajax_' . OrderTrackingEndpoint::ENDPOINT, array( $endpoint, 'handle_request' ) );
|
||||
|
||||
$meta_box_renderer = $c->get( 'order-tracking.meta-box.renderer' );
|
||||
add_action(
|
||||
|
@ -75,38 +76,5 @@ class OrderTrackingModule implements ModuleInterface {
|
|||
10,
|
||||
2
|
||||
);
|
||||
|
||||
add_action(
|
||||
'woocommerce_order_status_completed',
|
||||
static function( int $order_id ) use ( $endpoint, $logger ) {
|
||||
$tracking_information = $endpoint->get_tracking_information( $order_id );
|
||||
|
||||
if ( $tracking_information ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$wc_order = wc_get_order( $order_id );
|
||||
if ( ! is_a( $wc_order, WC_Order::class ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$transaction_id = $wc_order->get_transaction_id();
|
||||
if ( empty( $transaction_id ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$tracking_data = array(
|
||||
'transaction_id' => $transaction_id,
|
||||
'status' => 'SHIPPED',
|
||||
);
|
||||
|
||||
try {
|
||||
$endpoint->add_tracking_information( $tracking_data, $order_id );
|
||||
} catch ( Exception $exception ) {
|
||||
$logger->error( "Couldn't create tracking information: " . $exception->getMessage() );
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace WooCommerce\PayPalCommerce\OrderTracking\Shipment;
|
||||
|
||||
use WC_Order;
|
||||
use WC_Order_Item_Product;
|
||||
use WC_Product;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Item;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
|
||||
|
@ -135,7 +137,11 @@ class Shipment implements ShipmentInterface {
|
|||
* {@inheritDoc}
|
||||
*/
|
||||
public function line_items(): array {
|
||||
$wc_order = wc_get_order( $this->wc_order_id );
|
||||
$wc_order = wc_get_order( $this->wc_order_id );
|
||||
if ( ! $wc_order instanceof WC_Order ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$wc_order_items = $wc_order->get_items();
|
||||
$tracking_meta = $wc_order->get_meta( OrderTrackingModule::PPCP_TRACKING_INFO_META_NAME );
|
||||
$saved_line_items = $tracking_meta[ $this->tracking_number() ] ?? array();
|
||||
|
@ -143,6 +149,7 @@ class Shipment implements ShipmentInterface {
|
|||
|
||||
$tracking_items = array();
|
||||
foreach ( $wc_order_items as $item ) {
|
||||
assert( $item instanceof WC_Order_Item_Product );
|
||||
if ( ! empty( $line_items ) && ! in_array( $item->get_id(), $line_items, true ) ) {
|
||||
continue;
|
||||
}
|
||||
|
@ -242,6 +249,11 @@ class Shipment implements ShipmentInterface {
|
|||
* @return void
|
||||
*/
|
||||
protected function render_shipment_line_item_info(): void {
|
||||
$line_items = $this->line_items();
|
||||
if ( empty( $line_items ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$format = '<p><strong>%1$s</strong> <span>%2$s</span></p>';
|
||||
$order_items_info = array();
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@ namespace WooCommerce\PayPalCommerce\OrderTracking\Shipment;
|
|||
* tracking_number: string,
|
||||
* status: string,
|
||||
* carrier: string,
|
||||
* line_items: array<LineItemMap>,
|
||||
* items: array<LineItemMap>,
|
||||
* carrier_name_other?: string,
|
||||
* }
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue