首页 > 解决方案 > Users and channels are not getting created when executed inside Paypal IPN

问题描述

Issue Summary

I implemented Twilio chat service for my CodeIgniter 3 application. I added a new method and called it from Paypal IPN which will create two types of users and a private channel for one-to-one communication.

Strangely, after executing few lines of that method, execution is getting stopped. It's happening when the Paypal IPN is hit for the first time. But next time, when I make a payment and the IPN is hit again, all the code is working properly.

Recently updated to the latest version of twilio-php but the issue remains. Not sure whether I'm doing something wrong. Please suggest a solution for this.

Code Snippet

// Paypal IPN
public function paypal_ipn() {
   ...............[code]...............
   if ($this->makeChatKitRelation($check_relation_exists['company_id'], $check_relation_exists['job_seeker_id'], $check_relation_exists['channel_id'])) {
        // 1. Not entering inside this as (see comment 2 below)
        $update_data = array(
            'number_allowed' => $check_chat_allowed_count->number_allowed + $packageDetails["num_careerseeker_contact_allowed"],
            'already_availed' => $check_chat_allowed_count->already_availed + 1,
        );      
        $this->Company_career_seeker_contact_allowed_model->updateChatAllowedCount($update_data, $check_relation_exists['company_id']);
    }
    die;
}

// Function used to make chat users & channels
public function makeChatKitRelation($companyId, $userId, $channel_id) {
  $companyDetails = $this->Companybasic_model->checkChatKitStatus($companyId); // fetch user details from DB (if user doesn't exists in twilio)
  $userDetails = $this->Jobseekerbasic_model->checkChatKitStatus($userId); // fetch user details from DB (if user doesn't exists in twilio)

  require_once('vendor/autoload.php');

  $twilio = new Twilio\Rest\Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN);
  
  if ($companyDetails) { // if user doesn't exist in twilio
      $twilio->chat->v2->services(TWILIO_CHAT_SERVICE_SID)
              ->users
              ->create('company-' . $companyId);     // 2. Working till this line in the first IPN call

      $twilio->chat->v2->services(TWILIO_CHAT_SERVICE_SID)
               ->users('company-' . $companyId)
               ->update([
                   'friendlyName' => $companyDetails->company_name
               ]);
  }
  
  if ($userDetails) { // if user doesn't exist in twilio
      $twilio->chat->v2->services(TWILIO_CHAT_SERVICE_SID)
              ->users
              ->create('user-' . $userId);

      $twilio->chat->v2->services(TWILIO_CHAT_SERVICE_SID)
               ->users('user-' . $userId)
               ->update([
                   'friendlyName' => $userDetails->full_name
               ]);
  }
  
  // Fetch channel or create a new one if it doesn't exist
  try {
      $channel = $twilio->chat->v2->services(TWILIO_CHAT_SERVICE_SID)
              ->channels($channel_id)
              ->fetch();
  } catch (\Twilio\Exceptions\RestException $e) {
      $channel = $twilio->chat->v2->services(TWILIO_CHAT_SERVICE_SID)
              ->channels
              ->create([
                  'createdBy' => 'company-' . $companyId,
                  'uniqueName' => $channel_id,
                  'type' => 'private'
              ]);
      
      $twilio->chat->v2->services(TWILIO_CHAT_SERVICE_SID)
              ->channels($channel_id)
              ->members
              ->create('company-' . $companyId);
      
      
      $twilio->chat->v2->services(TWILIO_CHAT_SERVICE_SID)
              ->channels($channel_id)
              ->members
              ->create('user-' . $userId);
      
  }      
  
  return $channel->sid;
}

Technical details:

标签: phppaypalcodeigniter-3twilio-php

解决方案


推荐阅读