<?php
/**
* Class to send SMS's using EZ4U Service
*
*/
class EZ4USmsService
{
// Account Data
var $account = "";
var $licensekey = "";
var $baseurl = "http://ziegen.dyndns.org/ez4usms/API";
/**
* Send a new SMS
* @param type $phoneNumber The destine number
* @param type $messageText Message to send
* @return boolean True if the message is sent, false otherwise
*/
public function sendMsg($entity, $customer, $messageText)
{
// Prepare the message
$mesText = str_replace("'", "", $messageText);
$msg = urlencode($mesText);
// Build the call
$url = "$this->baseurl/sendSMS.php?account=$this->account&licensekey=$this->licensekey&phoneNumber=$customer->mobile&messageText=$msg";
// Do the call
$return = file($url);
// Parse the response
$result = json_decode($return[0]);
if ($result->Result == "OK") {
$sms = new Sms();
$sms->entity_id = $entity->id;
$sms->customer_id = $customer->id;
$sms->message = $messageText;
$sms->save();
return true;
} else {
Yii::log("url: ".$url, 'error', 'EZ4USmsService::sendMsg()');
Yii::log("error: ".$result->ErrorDesc, 'error', 'EZ4USmsService::sendMsg()');
return false;
}
}
public function sendSalesMsg($entity,$messageText)
{
// Prepare the message
$mesText = str_replace("'", "", $messageText);
$msg = urlencode($mesText);
// Build the call
$url = "$this->baseurl/sendSMS.php?account=$this->account&licensekey=$this->licensekey&phoneNumber=$entity->contact_phone&messageText=$msg";
// Do the call
$return = file($url);
// Parse the response
$result = json_decode($return[0]);
if ($result->Result == "OK") {
$sms = new SmsSale();
$sms->entity_id = $entity->id;
$sms->message = $messageText;
$sms->save();
return true;
} else {
Yii::log("url: ".$url, 'error', 'EZ4USmsService::sendSalesMsg()');
Yii::log("error: ".$result->ErrorDesc, 'error', 'EZ4USmsService::sendSalesMsg()');
return false;
}
}
/**
* Get the general delivery report
*
* Max 250 returned records
*
* DeliveryStatus Codes:
* 0 – Mensagem ainda não processada
* 1 – Mensagem processada com sucesso e entregue
* 2 – Mensagem processada e em entrega (pendente)
* 3 – Mensagem processada sem sucesso (não entregue)
*
* @return array/boolean Array with the message list if OK, FALSE otherwise
*/
public function getSMSList()
{
// Build the call
$url = "$this->baseurl/getSMSList.php?account=$this->account&licensekey=$this->licensekey";
// Do the call
$return = file($url);
// Parse the response
$result = json_decode($return[0]);
if ($result->Result == "OK") {
return $result->MessageList;
} else {
Yii::log($result->ErrorDesc, 'error', 'EZ4USmsService::getSMSList()');
return false;
}
}
/**
* Get the values about credits
*
* Indicação de Sucesso de Operação com indicação de:
* Acquired - Total de Créditos Adquiridos;
* Consumed - Total de Créditos já Consumidos;
* ConsumedLandLine - Consumos Discriminados Rede Fixa;
* AvailableCreditsWithoutScheduled - Créditos Disponíveis sem contabilização de mensagens agendadas;
* Scheduled - Total Mensagens Agendadas;
* AvailableCredits - Total Créditos efectivamente Disponíves
*
* @return object/boolean Object with the result if OK, FALSE otherwise
*/
public function getAvailableCredits()
{
// Build the call
$url = "$this->baseurl/getAvailableCredits.php?account=$this->account&licensekey=$this->licensekey";
// Do the call
$return = file($url);
// Parse the response
$result = json_decode($return[0]);
if ($result->Result == "OK") {
return $result->AvailableCreditsInfo[0];
} else {
Yii::log($result->ErrorDesc, 'error', 'EZ4USmsService::getAvailableCredits()');
return false;
}
}
/**
* Send card byb sms
*
* @param type $settings The card's settings
* @param type $entity The card's entity
* @param type $customer The card's customer
* @param type $card The card to send
* @return boolean True if
*/
public function sendCardSms($settings, $entity, $customer, $card, $force = false)
{
if ($settings->sms_active || $force) {
if ($entity->smscard_balance > 0) {
include_once 'BitlyService.php';
$link = getShortUrl($card->view_url);
$messageText = Yii::t('messages', 'Thank you for joining')." ".$settings->name.". ".Yii::t('messages', 'Use this link to download')." ".$link;
if ($this->sendMsg($entity, $customer, $messageText)) {
$entity->smscard_balance--;
$entity->save();
return ['status' => 'success', 'message' => Yii::t('messages', 'SMS sent with success')];
} else {
Yii::log("Fail to send card ".$card->card_number." by SMS", 'error', 'EZ4USmsService::sendCardSms');
return ['status' => 'error', 'message' => Yii::t('messages', 'Fail to send SMS.')];
}
} else {
Yii::log("Entity ".$entity->commercial_name." has no balance to send cards by SMS", 'warning', 'EZ4USmsService::sendCardSms');
return ['status' => 'error', 'message' => Yii::t('messages', 'Fail to send SMS. No available balance.')];
}
} else {
Yii::log("Entity ".$entity->commercial_name." the option is not active", 'warning', 'EZ4USmsService::sendCardSms');
return ['status' => 'error', 'message' => Yii::t('messages', 'Fail to send SMS. The option is not active.')];
}
}
public function sendCampaignSms($entity, $customer, $msg)
{
if ($entity->smscamp_balance > 0) {
$result = $this->sendMsg($entity, $customer, $msg);
if ($result) {
$entity->smscamp_balance--;
$entity->save();
return $result;
} else {
Yii::log("Fail to send SMS campaign", 'error', 'EZ4USmsService::sendCardSms');
return false;
}
} else {
Yii::log("Entity ".$entity->commercial_name." has no balance to send SMS campaigns", 'error', 'EZ4USmsService::sendCardSms');
return false;
}
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.