';
}
add_action( 'airalo_sync_products', 'airalo_sync_products_function', 10, 2 );
function airalo_sync_products_function() {
$product_syncer = new \Airalo\Admin\Syncers\ProductSyncer();
$product_syncer->handle();
}
add_filter( 'woocommerce_add_to_cart_validation', 'airalo_validate_cart_item_quantity', 10, 3 );
function airalo_validate_cart_item_quantity( $passed, $product_id, $quantity ) {
return ( new OrderValidator() )->handle( $passed, $product_id, $quantity );
}
add_action( 'woocommerce_thankyou', 'airalo_submit_order', 10, 1 );
function airalo_submit_order( $order ) {
try {
$order = wc_get_order( $order );
if ( ! $order ) {
return;
}
$items = $order->get_items();
$order_items = new \Airalo\Admin\OrderItem( $items );
$airalo_order_items = $order_items->get_airalo_order_items();
// skip if no airalo order items
if ( empty( $airalo_order_items ) ) {
return;
}
// Only provision once payment is confirmed. Asynchronous/redirect
// gateways (Trustly, Bizum, direct bank transfer, ...) reach the
// thank-you page while the order is still pending/on-hold; those are
// provisioned later via the order-status hooks once payment completes.
if ( ! $order->is_paid() ) {
return;
}
( new AiraloOrder() )->handle( $order );
} catch ( \Throwable $ex ) {
if ( $order instanceof \WC_Order ) {
try {
$order->update_status( 'on-hold', 'Error while submitting order to Airalo. Error: ' . $ex->getMessage() );
} catch ( \Throwable $inner ) {
error_log( '[Airalo] Failed to update order status in thankyou handler: ' . $inner->getMessage() );
}
}
error_log( '[Airalo] woocommerce_thankyou handler failed: ' . $ex->getMessage() );
}
}
add_action( 'woocommerce_order_status_completed', 'airalo_admin_order_on_status' );
add_action( 'woocommerce_order_status_processing', 'airalo_admin_order_on_status' );
function airalo_admin_order_on_status( $order_id ) {
try {
$order = wc_get_order( $order_id );
if ( ! $order ) {
return;
}
// Provision on payment-confirmed status transitions for all orders (both admin-created and frontend).
// This is the path that covers asynchronous/redirect gateways, which are not yet paid when the
// thank-you page fires.
// Double-provisioning is prevented by the cache guard below and AiraloOrder::handle()'s idempotency check.
if ( Cached::get( function () {
// do nothing, just check if cache for this order exists
}, $order->get_id() )
) {
return;
}
( new AiraloOrder() )->handle( $order );
} catch ( \Throwable $ex ) {
error_log( '[Airalo] order status handler failed: ' . $ex->getMessage() );
}
}
/**
* Show an admin notice when Airalo API credentials are missing so the
* misconfiguration is surfaced before it impacts checkout.
*/
add_action( 'admin_notices', 'airalo_missing_credentials_notice' );
function airalo_missing_credentials_notice() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
try {
$client = new AiraloClient( new Option() );
if ( $client->has_credentials() ) {
return;
}
} catch ( \Throwable $ex ) {
error_log( '[Airalo] Failed to check credentials for admin notice: ' . $ex->getMessage() );
return;
}
echo '
Airalo: '
. esc_html__( 'API credentials (Client ID and/or Client Secret) are not configured. Airalo orders will not be processed until they are set in the plugin settings.', 'airalo' )
. '
';
}
/**
* Show an admin notice when a PHP extension the plugin depends on is missing.
*
* These are hard requirements: without them credential storage (openssl),
* API authentication (sodium), or every API call (curl) fails, and checkout
* provisioning breaks with an opaque "invalid credentials" style error.
* Surfacing the missing extension here makes the root cause obvious instead
* of hiding it behind a generic failure.
*/
add_action( 'admin_notices', 'airalo_missing_php_extensions_notice' );
function airalo_missing_php_extensions_notice() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$required = [ 'curl', 'openssl', 'sodium', 'json' ];
$missing = [];
foreach ( $required as $extension ) {
if ( ! extension_loaded( $extension ) ) {
$missing[] = $extension;
}
}
if ( empty( $missing ) ) {
return;
}
echo '
Airalo: '
. esc_html__( 'The following required PHP extensions are not enabled:', 'airalo' )
. ' ' . esc_html( implode( ', ', $missing ) ) . '. '
. esc_html__( 'The plugin cannot work without those extensions. Please ask your host to enable them.', 'airalo' )
. '