diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index a2cb466f1..ed2d8d5fa 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -67,6 +67,8 @@ class DashboardController < ActionController::Base FB_APP_ID: GlobalConfigService.load('FB_APP_ID', ''), INSTAGRAM_APP_ID: GlobalConfigService.load('INSTAGRAM_APP_ID', ''), FACEBOOK_API_VERSION: GlobalConfigService.load('FACEBOOK_API_VERSION', 'v17.0'), + WHATSAPP_APP_ID: GlobalConfigService.load('WHATSAPP_APP_ID', ''), + WHATSAPP_CONFIGURATION_ID: GlobalConfigService.load('WHATSAPP_CONFIGURATION_ID', ''), IS_ENTERPRISE: ChatwootApp.enterprise?, AZURE_APP_ID: GlobalConfigService.load('AZURE_APP_ID', ''), GIT_SHA: GIT_HASH diff --git a/app/controllers/whatsapp/embedded_controller.rb b/app/controllers/whatsapp/embedded_controller.rb new file mode 100644 index 000000000..c0a1ead64 --- /dev/null +++ b/app/controllers/whatsapp/embedded_controller.rb @@ -0,0 +1,79 @@ +class Whatsapp::EmbeddedController < ApplicationController + include EnsureCurrentAccountHelper + + before_action :authenticate_user! + before_action :current_account + before_action :check_authorization + + def new + # Configuration endpoint for embedded signup initialization + render json: { + status: 'ready', + app_id: GlobalConfigService.load('WHATSAPP_APP_ID', ''), + config_id: GlobalConfigService.load('WHATSAPP_CONFIGURATION_ID', ''), + partner_id: GlobalConfigService.load('WHATSAPP_PARTNER_ID', ''), + graph_api_version: GlobalConfigService.load('WHATSAPP_GRAPH_API_VERSION', 'v21.0') + } + end + + def embedded_signup + # Complete embedded signup using auth code + business info from frontend + validate_authorization_code! + return if performed? + + validate_required_parameters! + return if performed? + + channel = process_signup + @inbox = channel.inbox + + # Return the inbox object using the standard jbuilder template + render 'embedded_signup', formats: [:json] + rescue StandardError => e + handle_signup_error(e) + end + + private + + def validate_authorization_code! + return if params[:code].present? + + render json: { + error: 'Missing authorization code', + message: 'Authorization code is required for embedded signup' + }, status: :bad_request + end + + def validate_required_parameters! + return if params[:business_id].present? && params[:waba_id].present? + + render json: { + error: 'Missing required parameters: business_id and waba_id are required' + }, status: :bad_request + end + + def process_signup + service = Whatsapp::EmbeddedSignupService.new( + account: Current.account, + code: params[:code], + business_id: params[:business_id], + waba_id: params[:waba_id], + phone_number_id: params[:phone_number_id] + ) + + service.perform + end + + def handle_signup_error(error) + Rails.logger.error("WhatsApp embedded signup processing error: #{error.message}") + Rails.logger.error(error.backtrace.join("\n")) + render json: { + error: 'Internal server error', + message: error.message + }, status: :internal_server_error + end + + def check_authorization + authorize(Current.account, :update?) + end +end diff --git a/app/javascript/dashboard/i18n/locale/en/inboxMgmt.json b/app/javascript/dashboard/i18n/locale/en/inboxMgmt.json index cc0fe4b33..d2effe6fb 100644 --- a/app/javascript/dashboard/i18n/locale/en/inboxMgmt.json +++ b/app/javascript/dashboard/i18n/locale/en/inboxMgmt.json @@ -222,6 +222,7 @@ "DESC": "Start supporting your customers via WhatsApp.", "PROVIDERS": { "LABEL": "API Provider", + "WHATSAPP_EMBEDDED": "WhatsApp Business (Embedded Signup)", "TWILIO": "Twilio", "WHATSAPP_CLOUD": "WhatsApp Cloud", "360_DIALOG": "360Dialog" @@ -264,6 +265,28 @@ "WEBHOOK_VERIFICATION_TOKEN": "Webhook Verification Token" }, "SUBMIT_BUTTON": "Create WhatsApp Channel", + "EMBEDDED_SIGNUP": { + "TITLE": "Quick Setup with Meta", + "DESC": "Connect your WhatsApp Business Account directly through Meta's secure signup flow.", + "BENEFITS": { + "TITLE": "Benefits of Embedded Signup:", + "EASY_SETUP": "One-click setup with no manual configuration required", + "SECURE_AUTH": "Secure OAuth-based authentication with Meta", + "AUTO_CONFIG": "Automatic webhook and phone number configuration" + }, + "SUBMIT_BUTTON": "Connect with WhatsApp Business", + "AUTH_PROCESSING": "Authenticating with Meta...", + "WAITING_FOR_BUSINESS_INFO": "Please complete business setup in the Meta window...", + "PROCESSING": "Setting up your WhatsApp Business Account...", + "PROCESSING_DESC": "Please wait while we configure your WhatsApp Business Account. This may take a few moments.", + "LOADING_SDK": "Loading Facebook SDK...", + "CANCELLED": "WhatsApp signup was cancelled", + "STEP_AUTH": "Step 1: Authenticating with Meta", + "STEP_BUSINESS": "Step 2: Selecting business account", + "STEP_CREATING": "Step 3: Creating WhatsApp channel", + "SUCCESS_TITLE": "WhatsApp Business Account Connected!", + "SUCCESS_DESC": "Your WhatsApp Business Account has been successfully connected. Please provide a name for your inbox to complete the setup." + }, "API": { "ERROR_MESSAGE": "We were not able to save the WhatsApp channel" } diff --git a/app/javascript/dashboard/routes/dashboard/settings/inbox/channels/CloudWhatsapp.vue b/app/javascript/dashboard/routes/dashboard/settings/inbox/channels/CloudWhatsapp.vue index 91f1ec9e9..2f6e58d95 100644 --- a/app/javascript/dashboard/routes/dashboard/settings/inbox/channels/CloudWhatsapp.vue +++ b/app/javascript/dashboard/routes/dashboard/settings/inbox/channels/CloudWhatsapp.vue @@ -1,12 +1,10 @@ diff --git a/app/javascript/dashboard/routes/dashboard/settings/inbox/channels/Whatsapp.vue b/app/javascript/dashboard/routes/dashboard/settings/inbox/channels/Whatsapp.vue index 68a9d8bcd..9fdef416a 100644 --- a/app/javascript/dashboard/routes/dashboard/settings/inbox/channels/Whatsapp.vue +++ b/app/javascript/dashboard/routes/dashboard/settings/inbox/channels/Whatsapp.vue @@ -3,6 +3,7 @@ import PageHeader from '../../SettingsSubPageHeader.vue'; import Twilio from './Twilio.vue'; import ThreeSixtyDialogWhatsapp from './360DialogWhatsapp.vue'; import CloudWhatsapp from './CloudWhatsapp.vue'; +import WhatsappEmbeddedSignup from './WhatsappEmbeddedSignup.vue'; export default { components: { @@ -10,10 +11,11 @@ export default { Twilio, ThreeSixtyDialogWhatsapp, CloudWhatsapp, + WhatsappEmbeddedSignup, }, data() { return { - provider: 'whatsapp_cloud', + provider: 'whatsapp_embedded', }; }, }; @@ -31,6 +33,9 @@ export default {