Fixes https://linear.app/chatwoot/issue/CW-5692/whatsapp-es-numbers-stuck-in-pending-due-to-premature-registration ### Problem Multiple customers reported that their WhatsApp numbers remain stuck in **Pending** in WhatsApp Manager even after successful onboarding. - Our system triggers a **registration call** (`/<PHONE_NUMBER_ID>/register`) as soon as the number is OTP verified. - In many cases, Meta hasn’t finished **display name review/provisioning**, so the call fails with: ``` code: 100, error_subcode: 2388001 error_user_title: "Cannot Create Certificate" error_user_msg: "Your display name could not be processed. Please edit your display name and try again." ``` - This leaves the number stuck in Pending, no messaging can start until we manually retry registration. - Some customers have reported being stuck in this state for **7+ days**. ### Root cause - We only check `code_verification_status = VERIFIED` before attempting registration. - We **don’t wait** for display name provisioning (`name_status` / `platform_type`) to be complete. - As a result, registration fails prematurely and the number never transitions out of Pending. ### Solution #### 1. Health Status Monitoring - Build a backend service to fetch **real-time health data** from Graph API: - `code_verification_status` - `name_status` / `display_name_status` - `platform_type` - `throughput.level` - `messaging_limit_tier` - `quality_rating` - Expose health data via API (`/api/v1/accounts/:account_id/inboxes/:id/health`). - Display this in the UI as an **Account Health tab** with clear badges and direct links to WhatsApp Manager. #### 2. Smarter Registration Logic - Update `WebhookSetupService` to include a **dual-condition check**: - Register if: 1. Phone is **not verified**, OR 2. Phone is **verified but provisioning incomplete** (`platform_type = NOT_APPLICABLE`, `throughput.level = NOT_APPLICABLE`). - Skip registration if number is already provisioned. - Retry registration automatically when stuck. - Provide a UI banner with complete registration button so customers can retry without manual support. ### Screenshot <img width="2292" height="1344" alt="CleanShot 2025-09-30 at 16 01 03@2x" src="https://github.com/user-attachments/assets/1c417d2a-b11c-475e-b092-3c5671ee59a7" /> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com>
85 lines
2.6 KiB
Ruby
85 lines
2.6 KiB
Ruby
class Whatsapp::EmbeddedSignupService
|
|
def initialize(account:, params:, inbox_id: nil)
|
|
@account = account
|
|
@code = params[:code]
|
|
@business_id = params[:business_id]
|
|
@waba_id = params[:waba_id]
|
|
@phone_number_id = params[:phone_number_id]
|
|
@inbox_id = inbox_id
|
|
end
|
|
|
|
def perform
|
|
validate_parameters!
|
|
|
|
access_token = exchange_code_for_token
|
|
phone_info = fetch_phone_info(access_token)
|
|
validate_token_access(access_token)
|
|
|
|
channel = create_or_reauthorize_channel(access_token, phone_info)
|
|
channel.setup_webhooks
|
|
check_channel_health_and_prompt_reauth(channel)
|
|
channel
|
|
|
|
rescue StandardError => e
|
|
Rails.logger.error("[WHATSAPP] Embedded signup failed: #{e.message}")
|
|
raise e
|
|
end
|
|
|
|
private
|
|
|
|
def exchange_code_for_token
|
|
Whatsapp::TokenExchangeService.new(@code).perform
|
|
end
|
|
|
|
def fetch_phone_info(access_token)
|
|
Whatsapp::PhoneInfoService.new(@waba_id, @phone_number_id, access_token).perform
|
|
end
|
|
|
|
def validate_token_access(access_token)
|
|
Whatsapp::TokenValidationService.new(access_token, @waba_id).perform
|
|
end
|
|
|
|
def create_or_reauthorize_channel(access_token, phone_info)
|
|
if @inbox_id.present?
|
|
Whatsapp::ReauthorizationService.new(
|
|
account: @account,
|
|
inbox_id: @inbox_id,
|
|
phone_number_id: @phone_number_id,
|
|
business_id: @business_id
|
|
).perform(access_token, phone_info)
|
|
else
|
|
waba_info = { waba_id: @waba_id, business_name: phone_info[:business_name] }
|
|
Whatsapp::ChannelCreationService.new(@account, waba_info, phone_info, access_token).perform
|
|
end
|
|
end
|
|
|
|
def check_channel_health_and_prompt_reauth(channel)
|
|
health_data = Whatsapp::HealthService.new(channel).fetch_health_status
|
|
return unless health_data
|
|
|
|
if channel_in_pending_state?(health_data)
|
|
channel.prompt_reauthorization!
|
|
else
|
|
Rails.logger.info "[WHATSAPP] Channel #{channel.phone_number} health check passed"
|
|
end
|
|
rescue StandardError => e
|
|
Rails.logger.error "[WHATSAPP] Health check failed for channel #{channel.phone_number}: #{e.message}"
|
|
end
|
|
|
|
def channel_in_pending_state?(health_data)
|
|
health_data[:platform_type] == 'NOT_APPLICABLE' ||
|
|
health_data.dig(:throughput, 'level') == 'NOT_APPLICABLE'
|
|
end
|
|
|
|
def validate_parameters!
|
|
missing_params = []
|
|
missing_params << 'code' if @code.blank?
|
|
missing_params << 'business_id' if @business_id.blank?
|
|
missing_params << 'waba_id' if @waba_id.blank?
|
|
|
|
return if missing_params.empty?
|
|
|
|
raise ArgumentError, "Required parameters are missing: #{missing_params.join(', ')}"
|
|
end
|
|
end
|