Exemplo n.º 1
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $notification_queue = Lead::where('notify', '=', 1)->get();
     foreach ($notification_queue as $lead) {
         if ($lead) {
             if (!empty($lead->form->integration) && !empty($lead->form->integration_id)) {
                 $integration = new $lead->form->integration->class($lead->id, json_decode($lead->form->integration_options, true));
             }
         }
         if (!empty($lead->form->notification_emails) && !empty($lead->form->notification_subject)) {
             $emails = explode(',', $lead->form->notification_emails);
             if (is_array($emails)) {
                 // Internal e-mail notification
                 try {
                     Mail::send('ZLeader::lead.email-internal-notification', ['lead' => $lead], function ($message) use($emails, $lead) {
                         $message->from(config('ZLeader.notification_sender_email_address'), $lead->form->area->company->name);
                         $message->subject($lead->form->notification_subject);
                         $reply_email = trim($lead->getValueByKey('email'));
                         $message->replyTo($reply_email);
                         foreach ($emails as $email) {
                             if (!empty(trim($email))) {
                                 $message->to(trim($email));
                             }
                         }
                     });
                 } catch (\Exception $e) {
                     Log::error($e->getMessage());
                 }
             }
         }
         try {
             // User e-mail notification
             $email = trim($lead->getValueByKey('email'));
             if (!empty($email) && !empty($lead->form->user_notification_subject)) {
                 Mail::send('ZLeader::lead.email-user-notification', ['lead' => $lead], function ($message) use($email, $lead) {
                     $message->from(config('ZLeader.notification_sender_email_address'), $lead->form->area->company->name);
                     $message->subject($lead->form->user_notification_subject);
                     $message->to($email);
                 });
             }
         } catch (\Exception $e) {
             Log::error($e->getMessage());
         }
         $lead->notify = 0;
         $lead->save();
     }
 }
Exemplo n.º 2
0
 public function store(Request $request)
 {
     $access_token = env('FACEBOOK_LEADGEN_ACCESS_TOKEN', 'token');
     Log::info('New Webhook:');
     if (is_array($request->entry)) {
         foreach ($request->entry[0]['changes'] as $change_data) {
             if ($change_data['field'] == 'leadgen') {
                 $leadgen = $change_data['value'];
                 $lead_exists = Lead::where('fb_leadgen_id', '=', $leadgen['leadgen_id'])->count();
                 if ($lead_exists == 0) {
                     Log::info('New Lead!');
                     try {
                         $response = Facebook::get('/' . $leadgen['leadgen_id'], $access_token);
                         $lead_data = $response->getDecodedBody();
                         //Log::info('Lead values:');
                         //Log::info($lead_data);
                         $leadgen['lead_values'] = $lead_data['field_data'];
                     } catch (Exception $e) {
                         Log::error($e->getMessage());
                     }
                     try {
                         $response = Facebook::get('/' . $leadgen['form_id'], $access_token);
                         $form_data = $response->getDecodedBody();
                         //Log::info('Form data:');
                         //Log::info($form_data);
                         $leadgen['form_name'] = $form_data['name'];
                     } catch (Exception $e) {
                         Log::error($e->getMessage());
                     }
                     if (!empty($leadgen['ad_id'])) {
                         try {
                             $response = Facebook::get('/' . $leadgen['ad_id'] . '?fields=name,adset_id', $access_token);
                             $ad_data = $response->getDecodedBody();
                             //Log::info('Ad data:');
                             //Log::info($ad_data);
                             $leadgen['ad_name'] = $ad_data['name'];
                         } catch (Exception $e) {
                             Log::error($e->getMessage());
                         }
                         try {
                             $response = Facebook::get('/' . $ad_data['adset_id'] . '?fields=name', $access_token);
                             $adset_data = $response->getDecodedBody();
                             //Log::info('Ad data:');
                             //Log::info($ad_data);
                             $leadgen['adset_name'] = $adset_data['name'];
                         } catch (Exception $e) {
                             Log::error($e->getMessage());
                         }
                     }
                     Log::info($leadgen);
                     $form_name_exploded = explode(' ', $leadgen['form_name']);
                     $form_integration_prefix = array_shift($form_name_exploded);
                     $form = Form::where('fb_integration_prefix', 'like', $form_integration_prefix)->first();
                     Log::info($form);
                     if ($form) {
                         $lead = new Lead();
                         $lead->form_id = $form->id;
                         $lead->fb_leadgen_id = $leadgen['leadgen_id'];
                         $lead->utm_source = 'facebook';
                         $lead->utm_medium = 'cpc';
                         $lead->utm_content = 'leadgen';
                         if (!empty($leadgen['ad_name'])) {
                             $lead->utm_term = $leadgen['ad_name'];
                         }
                         if ($request->utm_content) {
                             $lead->utm_content = $leadgen['adset_name'];
                         }
                         $lead->save();
                         foreach ($leadgen['lead_values'] as $field) {
                             if ($field['name'] == 'email') {
                                 $validator = Validator::make(['email' => $field['values'][0]], ['email' => 'email']);
                                 if (!$validator->fails()) {
                                     $leadValue = new LeadValue();
                                     $leadValue->lead_id = $lead->id;
                                     $leadValue->key = $field['name'];
                                     $leadValue->value = $field['values'][0];
                                     $leadValue->save();
                                 }
                             } else {
                                 $leadValue = new LeadValue();
                                 $leadValue->lead_id = $lead->id;
                                 $leadValue->key = $field['name'];
                                 $leadValue->value = $field['values'][0];
                                 $leadValue->save();
                             }
                         }
                         $form_name_fields = explode(',', implode(' ', $form_name_exploded));
                         if (is_array($form_name_fields)) {
                             foreach ($form_name_fields as $fields) {
                                 $field_data = explode('=', $fields);
                                 if (isset($field_data[0]) && isset($field_data[1])) {
                                     $data_key = trim($field_data[0]);
                                     $aux = explode(' ', $field_data[1]);
                                     $data_value = trim(array_shift($aux));
                                     if (!empty($data_key) && !empty($data_value)) {
                                         $leadValue = new LeadValue();
                                         $leadValue->lead_id = $lead->id;
                                         $leadValue->key = $data_key;
                                         $leadValue->value = $data_value;
                                         $leadValue->save();
                                     }
                                 }
                             }
                         }
                         Log::info('Lead saved');
                     } else {
                         Log::warning('Form "' . $form_integration_prefix . '" not found!');
                         switch ($leadgen['form_name']) {
                             default:
                                 Log::warning('Unknown form, reporting...');
                                 $company_name = 'Leadgen';
                                 $emails = 'andres@zephia.com.ar,cristian@zephia.com.ar';
                         }
                         Log::info('Sending "' . $leadgen['form_name'] . '" to ' . $emails);
                         $emails = explode(',', $emails);
                         Mail::send('ZLeader::lead.email-webhook-failed', ['lead' => $leadgen], function ($message) use($emails, $company_name) {
                             $message->from(config('ZLeader.notification_sender_email_address'), $company_name);
                             $message->subject('Nueva consulta de Facebook leadgen');
                             //$message->replyTo($address);
                             foreach ($emails as $email) {
                                 $message->to($email);
                             }
                         });
                         abort(404);
                     }
                 } else {
                     Log::warning('Lead exists!');
                 }
             }
         }
     }
     return $request->hub_challenge;
 }