## Reference https://github.com/chatwoot/chatwoot/pull/12149#issuecomment-3178108388 ## Description setup_webhook was done before the save, and hence the meta webhook validation might fail because of a race condition where the facebook validation is done before we saving the entry to the database. ## Type of change Please delete options that are not relevant. - [ ] Bug fix (non-breaking change which fixes an issue) ## How Has This Been Tested? - New inbox creation, webhook validation - Existing inbox update, webhook validation - <img width="614" height="674" alt="image" src="https://github.com/user-attachments/assets/be223945-deed-475a-82e5-3ae9c54a13fa" /> ## Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my code - [ ] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules --------- Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
66 lines
1.9 KiB
Ruby
66 lines
1.9 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
|
|
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 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
|