get_id(), 1 ) ); } } /** * Job handler; also called directly for the "Cancel shipment" button. * * @param int $order_id Order ID. * @param int $attempt Attempt number. * @return bool True when the shipment was cancelled. */ public static function run( $order_id, $attempt = 1 ) { $order = wc_get_order( $order_id ); if ( ! $order instanceof \WC_Order || OrderMeta::CREATED !== OrderMeta::state( $order ) ) { return false; } $awb = (string) $order->get_meta( OrderMeta::AWB ); try { ( new AbsClient( Credentials::get() ) )->patch( 'shipments/cancel', array( 'awbs' => array( $awb ), 'reason' => 'CANCEL_BY_CUSTOMER', ) ); } catch ( AbsError $e ) { if ( in_array( $e->type(), array( AbsError::RETRYABLE, AbsError::UNKNOWN_OUTCOME ), true ) && $attempt < self::MAX_RETRIES ) { Queue::later( self::HOOK, array( $order->get_id(), $attempt + 1 ), Queue::retry_delay( $attempt ) ); return false; } $order->update_meta_data( OrderMeta::ERROR, $e->getMessage() ); $order->save(); $order->add_order_note( /* translators: 1: AWB, 2: reason from ABS. */ sprintf( __( 'ABS Courier shipment %1$s could not be cancelled: %2$s Contact ABS Courier support if it should not be delivered.', 'abs-courier' ), $awb, $e->getMessage() ) ); return false; } OrderMeta::set_state( $order, OrderMeta::CANCELED ); /* translators: %s: AWB. */ $order->add_order_note( sprintf( __( 'ABS Courier shipment %s was cancelled.', 'abs-courier' ), $awb ) ); return true; } }