⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 a2billing.php

📁 a2billing的agi脚本,有一些中文的注释
💻 PHP
📖 第 1 页 / 共 3 页
字号:
	$callback_uniqueid = $callback_uniqueid['data'];
	$callback_leg = $agi->get_variable("LEG");
	$callback_leg = $callback_leg['data'];
	
	// |MODEFROM=ALL-CALLBACK|TARIFF=".$A2B ->tariff;
	
	if ($callback_mode=='CID'){  
		$charge_callback = 1;
		$A2B->agiconfig['use_dnid'] = 0;
		$A2B->agiconfig['number_try'] =1;
		
	}elseif ($callback_mode=='ALL'){  
		$A2B->agiconfig['use_dnid'] = 0;
		$A2B->agiconfig['number_try'] =1;
		$A2B->agiconfig['cid_enable'] =0;
		
	}else{
		$charge_callback = 1;
		// FOR THE WEB-CALLBACK
		$A2B->agiconfig['number_try'] =1;
		$A2B->agiconfig['use_dnid'] =1;
		$A2B->agiconfig['say_balance_after_auth']=0;
		$A2B->agiconfig['cid_enable'] =0;
		$A2B->agiconfig['say_timetocall']=0;
	}
	
	$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "[CALLBACK]:[GET VARIABLE : CALLED=$called_party | CALLING=$calling_party | MODE=$callback_mode | TARIFF=$callback_tariff | CBID=$callback_uniqueid | LEG=$callback_leg]");
	
	$QUERY = "UPDATE cc_callback_spool SET agi_result='AGI PROCESSING' WHERE uniqueid='$callback_uniqueid'";
	$res = $A2B -> DBHandle -> Execute($QUERY);
	$A2B -> debug( WRITELOG, $agi, __FILE__, __LINE__, "[CALLBACK-ALL : UPDATE CALLBACK AGI_RESULT : QUERY=$QUERY]");
	
	
	/* WE START ;) */
	$A2B -> debug( WRITELOG, $agi, __FILE__, __LINE__, "[CALLBACK]:[TRY : callingcard_ivr_authenticate]");
	$cia_res = $A2B -> callingcard_ivr_authenticate($agi);	
	if ($cia_res==0){
		
		$charge_callback = 1; // EVEN FOR  ALL CALLBACK
		$callback_leg = $A2B -> username;
		
		$A2B -> debug( WRITELOG, $agi, __FILE__, __LINE__, "[CALLBACK]:[Start]");
		$A2B -> callingcard_auto_setcallerid($agi);
		
		for ($i=0;$i< $A2B->agiconfig['number_try'] ;$i++){
			
			$RateEngine->Reinit();
			$A2B-> Reinit();
			
			// DIVIDE THE AMOUNT OF CREDIT BY 2 IN ORDER TO AVOID NEGATIVE BALANCE IF THE USER USE ALL HIS CREDIT
			$orig_credit = $A2B -> credit;
			$A2B -> credit = $A2B->credit / 2;
			
			$stat_channel = $agi->channel_status($A2B-> channel);
			$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, '[CALLBACK]:[CHANNEL STATUS : '.$stat_channel["result"].' = '.$stat_channel["data"].']'."[status_channel=$status_channel]:[ORIG_CREDIT : ".$orig_credit." - CUR_CREDIT - : ".$A2B -> credit." - CREDIT MIN_CREDIT_2CALL : ".$A2B->agiconfig['min_credit_2call']."]");
			
			//if ($stat_channel["status"]!= "6" && $stat_channel["status"]!= "1"){	
			if ($stat_channel["result"]!= $status_channel && ($A2B -> CC_TESTING!=1)){
				break;
			}
			
			if( $A2B->credit < $A2B->agiconfig['min_credit_2call'] && $A2B -> typepaid==0) {
				// SAY TO THE CALLER THAT IT DEOSNT HAVE ENOUGH CREDIT TO MAKE A CALL							
				$prompt = "prepaid-no-enough-credit-stop";
				$agi-> stream_file($prompt, '#');
				$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "[CALLBACK]:[STOP STREAM FILE $prompt]");
			}
			
			if ($A2B-> callingcard_ivr_authorize($agi, $RateEngine, $i)==1){
				
				// PERFORM THE CALL	
				$result_callperf = $RateEngine->rate_engine_performcall ($agi, $A2B-> destination, $A2B);
				if (!$result_callperf) {
					$prompt="prepaid-dest-unreachable";
					$agi-> stream_file($prompt, '#');
				}
				
				// INSERT CDR  & UPDATE SYSTEM
				$RateEngine->rate_engine_updatesystem($A2B, $agi, $A2B-> destination);
				
				if ($A2B->agiconfig['say_balance_after_call']==1){		
					$A2B-> fct_say_balance ($agi, $A2B->credit);
				}
				
				$charge_callback = 1;
				if ($RateEngine->dialstatus == "ANSWER") {
					$callback_been_connected = 1;
				}
				
				/*$arr_save_a2billing['countrycode']	= $A2B-> countrycode;
				$arr_save_a2billing['subcode']		= $A2B-> subcode;
				$arr_save_a2billing['myprefix']		= $A2B-> myprefix;
				$arr_save_a2billing['ipaddress']	= $A2B-> ipaddress;
				$arr_save_a2billing['rate']			= $A2B-> rate;
				$arr_save_a2billing['destination']	= $A2B-> destination;
				$arr_save_a2billing['sip_iax_buddy']= $A2B-> sip_iax_buddy;
				
				$arr_save_rateengine['number_trunk']= $RateEngine-> number_trunk;
				$arr_save_rateengine['answeredtime']= $RateEngine-> answeredtime;
				$arr_save_rateengine['dialstatus']	= $RateEngine-> dialstatus;
				$arr_save_rateengine['usedratecard']= $RateEngine-> usedratecard;
				$arr_save_rateengine['lastcost']	= $RateEngine-> lastcost;
				$arr_save_rateengine['usedtrunk']	= $RateEngine-> usedtrunk;*/
				
				$A2B -> debug( WRITELOG, $agi, __FILE__, __LINE__, "[CALLBACK]:[a2billing end loop num_try] RateEngine->usedratecard=".$RateEngine->usedratecard);
			}
		}//END FOR
		
		if ($A2B->set_inuse==1){ 
			$A2B->callingcard_acct_start_inuse($agi,0);
		}
		
	}else{
		$A2B -> debug( WRITELOG, $agi, __FILE__, __LINE__, "[CALLBACK]:[AUTHENTICATION FAILED (cia_res:".$cia_res.")]");
	}


}elseif ($mode == 'predictivedialer'){
	
	$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, '[MODE : PREDICTIVEDIALER]');
	$A2B->agiconfig['number_try'] = 10;
	$A2B->agiconfig['use_dnid'] =1;
	$A2B->agiconfig['say_balance_after_auth']=0;
	$A2B->agiconfig['say_timetocall']=0;
	$A2B->agiconfig['cid_enable'] =0;
	
	
	$agi->answer();
	
	/* WE START ;) */	
	$cia_res = $A2B -> callingcard_ivr_authenticate($agi);
	
	if ($A2B->id_campaign<=0){
		$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "[NOT CAMPAIGN ASSOCIATE AT THIS CARD]"); 
		$cia_res=-3;		
	}
	
	$A2B -> debug( WRITELOG, $agi, __FILE__, __LINE__, "[TRY : callingcard_ivr_authenticate]");
	if ($cia_res==0){
		
		$A2B -> debug( WRITELOG, $agi, __FILE__, __LINE__, "[callingcard_acct_start_inuse]");
		$A2B->callingcard_auto_setcallerid($agi);
		//$A2B->callingcard_acct_start_inuse($agi,1);
		
		for ($i=0;$i< $A2B -> config["callback"]['nb_predictive_call'] ;$i++){
			
			$RateEngine->Reinit();
			$A2B-> Reinit();
			
			$stat_channel = $agi->channel_status($A2B-> channel);
			$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, '[CHANNEL STATUS : '.$stat_channel["result"].' = '.$stat_channel["data"].']');
			$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "[CREDIT STATUS : ".$A2B-> credit."] : [CREDIT MIN_CREDIT_2CALL : ".$A2B->agiconfig['min_credit_2call']."]");
			
			//if ($stat_channel["status"]!= "6" && $stat_channel["status"]!= "1"){	
			if ($stat_channel["result"]!= "6" && ($A2B -> CC_TESTING!=1)){
				$A2B->callingcard_acct_start_inuse($agi,0); 
				$A2B -> write_log("[STOP - EXIT]", 0);
				exit();
			}
			
			$today_date = date("Y-m-d"); // 2005-12-24			
			// DEFINE HERE THE NUMBER OF DAY THAT A PHONENUMBER FROM THE LIST WILL LAST BEFORE BE CALL AGAIN
			$days_compare = $A2B -> config["callback"]['nb_day_wait_before_retry'];			
			if ($A2B->config["database"]['dbtype'] == "postgres"){
				$UNIX_TIMESTAMP = ""; $sql_limit = " LIMIT 5 OFFSET 0";	
				$date_clause = " last_attempt < date'$today_date'- INTERVAL '$days_compare DAY' ";
				// last_attempt < date'2005-12-24'- INTERVAL '1 DAY'
			}else{		
				$UNIX_TIMESTAMP = "UNIX_TIMESTAMP"; 	$sql_limit = " LIMIT 0,5";	 
				$date_clause = " last_attempt < SUBDATE('$today_date',INTERVAL $days_compare DAY)";
			}
			$QUERY = "SELECT id, numbertodial, name  FROM cc_phonelist WHERE enable=1 AND num_trials_done<10 AND inuse=0 AND id_cc_campaign=".$A2B->id_campaign.
					" AND ( $date_clause OR num_trials_done=0) ORDER BY last_attempt DESC $sql_limit";
			
			$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, $QUERY);
			$result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
			// $A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, $result);
			
			if (!is_array($result)){
				$A2B->callingcard_acct_start_inuse($agi,0); 
				$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "[PREDICTIVEDIALER]:[NO MORE NUMBER TO CALL]");
				$A2B -> write_log("[STOP - EXIT]", 0);
				exit();
			}else{					
				$id_phonelist = $result[0][0];
				$QUERY = "UPDATE cc_phonelist SET inuse='1', id_cc_card='".$A2B->id_card."' WHERE id='".$id_phonelist."'";
				$update_result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY, 0);						
			}
			
			$A2B->dnid = $A2B-> destination = $result[0][1];
			$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "[PREDICTIVEDIALER]:[NUMBER TO DIAL -> ".$A2B-> destination."]");
			
			//cause $i is the try_num and in callingcard_ivr_authorize if the try_num is upper than 1 we prompt for destination 
			if ($A2B-> callingcard_ivr_authorize($agi, $RateEngine, 0)==1){
				// PERFORM THE CALL	
				$result_callperf = $RateEngine->rate_engine_performcall ($agi, $A2B-> destination, $A2B, 1);
				
				if (!$result_callperf) {
					$prompt="prepaid-dest-unreachable";
					//$agi->agi_exec("STREAM FILE $prompt #");
					$agi-> stream_file($prompt, '#');
				}
				
				// INSERT CDR  & UPDATE SYSTEM
				$RateEngine->rate_engine_updatesystem($A2B, $agi, $A2B-> destination);
				
				if ($A2B->agiconfig['say_balance_after_call']==1){		
					$A2B-> fct_say_balance ($agi, $A2B-> credit);
				}
				$A2B -> debug( WRITELOG, $agi, __FILE__, __LINE__, "[callingcard_acct_stop]");
			}
			$QUERY = "UPDATE cc_phonelist SET inuse='0', last_attempt=now(),  num_trials_done=num_trials_done+1, secondusedreal=secondusedreal+".$RateEngine->answeredtime." WHERE id='".$id_phonelist."'";
			$update_result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY, 0);
		}//END FOR
		if ($A2B->set_inuse==1){ 
			$A2B->callingcard_acct_start_inuse($agi,0);
		}
		
	}else{
		$A2B -> debug( WRITELOG, $agi, __FILE__, __LINE__, "[AUTHENTICATION FAILED (cia_res:".$cia_res.")]");
	}

}

// CHECK IF WE HAVE TO CHARGE CALLBACK
if ($charge_callback){
	
	// IF THE CALL HAS NOT BEEN CONNECTED CHECK IF WE CHARGE OR NOT
	if ( ($callback_been_connected==1) || ($callback_been_connected != 1) && ($A2B->agiconfig['callback_bill_1stleg_ifcall_notconnected']==1) ){
		
		/*$A2B-> countrycode = $arr_save_a2billing['countrycode'];
		$A2B-> subcode = $arr_save_a2billing['subcode'];
		$A2B-> myprefix = $arr_save_a2billing['myprefix'];
		$A2B-> ipaddress = $arr_save_a2billing['ipaddress'];
		$A2B-> rate = $arr_save_a2billing['rate'];
		$A2B-> destination = $arr_save_a2billing['destination'];
		$A2B-> sip_iax_buddy = $arr_save_a2billing['sip_iax_buddy'];
		
		$RateEngine-> number_trunk = $arr_save_rateengine['number_trunk'];
		$RateEngine-> answeredtime = $arr_save_rateengine['answeredtime'];
		$RateEngine-> dialstatus = $arr_save_rateengine['dialstatus'];
		$RateEngine-> usedratecard = $arr_save_rateengine['usedratecard'];
		$RateEngine-> lastcost = $arr_save_rateengine['lastcost'];
		$RateEngine-> usedtrunk = $arr_save_rateengine['usedtrunk'];*/
		
		//list($callback_username, $callback_usedratecard, $callback_lastcost, $callback_lastbuycost) = split(",", $callback_leg, 4);
		
		/*// MAKE THE BILLING FOR THE 1ST LEG
		if ($callback_mode=='ALL'){  
			//IF IT S ALL THE BILLING TO APPLY COME FROM $callback_tariff
			$A2B -> tariff = $callback_tariff;
		}*/
		
		$callback_username = $callback_leg;
		$A2B -> accountcode = $callback_username;
		$A2B -> agiconfig['say_balance_after_auth'] = 0;
		$A2B -> agiconfig['cid_enable'] = 0;
		$A2B -> agiconfig['say_timetocall'] = 0;
		
		$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "[CALLBACK 1ST LEG]:[INFO FOR THE 1ST LEG - callback_username=$callback_username");
		
		$A2B -> debug( WRITELOG, $agi, __FILE__, __LINE__, "[CALLBACK 1ST LEG]:[TRY : callingcard_ivr_authenticate]");
		$cia_res = $A2B -> callingcard_ivr_authenticate($agi);	
		if ($cia_res==0){
			
			$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "[CALLBACK 1ST LEG]:[MAKE BILLING FOR THE 1ST LEG - TARIFF:".$A2B -> tariff.";CALLED=$called_party]");
			$A2B->agiconfig['use_dnid'] =1;
			$A2B ->dnid = $A2B ->destination = $called_party;
			
			$resfindrate = $RateEngine->rate_engine_findrates($A2B, $called_party, $A2B -> tariff);
			
			$RateEngine-> usedratecard = 0;
			// IF FIND RATE
			if ($resfindrate!=0 && is_numeric($RateEngine->usedratecard)){														
				$res_all_calcultimeout = $RateEngine->rate_engine_all_calcultimeout($A2B, $A2B->credit);
				
				if ($res_all_calcultimeout){
					// SET CORRECTLY THE CALLTIME FOR THE 1st LEG
					$RateEngine -> answeredtime  = time() - $G_startime;
					$RateEngine -> dialstatus = 'ANSWERED';
					$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "[CALLBACK]:[RateEngine -> answeredtime=".$RateEngine -> answeredtime."]");
					
					// INSERT CDR  & UPDATE SYSTEM
					$RateEngine->rate_engine_updatesystem($A2B, $agi, $A2B-> destination, 1, 0, 1);
				}else{										
					$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "[CALLBACK 1ST LEG]:[ERROR - BILLING FOR THE 1ST LEG - rate_engine_all_calcultimeout: CALLED=$called_party]");
				}
			}else{
				$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "[CALLBACK 1ST LEG]:[ERROR - BILLING FOR THE 1ST LEG - rate_engine_findrates: CALLED=$called_party - RateEngine->usedratecard=".$RateEngine->usedratecard."]");
			}
		}else{
			$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "[CALLBACK 1ST LEG]:[ERROR - AUTHENTICATION USERNAME]");
		}
	
	}
}// END if ($charge_callback)


// END
$agi->hangup();


// SEND MAIL REMINDER WHEN CREDIT IS TOO LOW
if (isset($send_reminder) && $send_reminder == 1 && $A2B->agiconfig['send_reminder'] == 1) {
	
	if (strlen($A2B -> cardholder_email) > 5){
		$QUERY = "SELECT mailtype, fromemail, fromname, subject, messagetext, messagehtml FROM cc_templatemail WHERE mailtype='reminder' ";
		$listtemplate = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
		
		if (is_array($listtemplate)){
			list($mailtype, $from, $fromname, $subject, $messagetext, $messagehtml) = $listtemplate [0];
			$messagetext = str_replace('$name', $A2B -> cardholder_lastname, $messagetext);
			$messagetext = str_replace('$card_gen', $A2B -> username, $messagetext);
			$messagetext = str_replace('$password', $A2B -> cardholder_uipass, $messagetext);
			$messagetext = str_replace('$min_credit', $A2B->agiconfig['min_credit_2call'], $messagetext);
			
			$em_headers  = "From: ".$fromname." <".$from.">\n";		
			$em_headers .= "Reply-To: ".$from."\n";
			$em_headers .= "Return-Path: ".$from."\n";
			$em_headers .= "X-Priority: 3\n";
			
			mail($A2B -> cardholder_email, $subject, $messagetext, $em_headers);
			
			/* USE PHPMAILER
			include (dirname(__FILE__)."/libs_a2billing/mail/class.phpmailer.php");
			//  change class.phpmailer.php - hostname
			$mail = new phpmailer();
			$mail -> From     = $from;
			$mail -> FromName = $fromname;
			//$mail -> IsSendmail();
			$mail -> IsSMTP();
			$mail -> Subject  = $subject;
			$mail -> Body    = $messagetext ; //$HTML;
			//$mail -> AltBody = $messagetext;	// Plain text body (for mail clients that cannot read 	HTML)
			//$mail -> ContentType = "multipart/alternative";
			$mail->AddAddress($A2B -> cardholder_email);				
			$mail->Send();
			*/
			$A2B -> debug( WRITELOG, $agi, __FILE__, __LINE__, "[SEND-MAIL REMINDER]:[TO:".$A2B -> cardholder_email." - FROM:$from - SUBJECT:$subject]");
		}
	}
}

if ($A2B->set_inuse==1){ 
	$A2B->callingcard_acct_start_inuse($agi,0);
}

/************** END OF THE APPLICATION ****************/
$A2B -> write_log("[exit]", 0);


?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -