Add SMS drivers for apistore, cafe24, ppurio

This commit is contained in:
Kijin Sung 2020-06-24 22:12:10 +09:00
parent d68770a3ef
commit dcbd5ff9b6
4 changed files with 367 additions and 0 deletions

View file

@ -0,0 +1,141 @@
<?php
namespace Rhymix\Framework\Drivers\SMS;
/**
* The ApiStore SMS driver.
*/
class ApiStore extends Base implements \Rhymix\Framework\Drivers\SMSInterface
{
/**
* API specifications.
*/
protected static $_spec = array(
'max_recipients' => 500,
'sms_max_length' => 90,
'sms_max_length_in_charset' => 'CP949',
'lms_supported' => true,
'lms_supported_country_codes' => array(82),
'lms_max_length' => 2000,
'lms_max_length_in_charset' => 'CP949',
'lms_subject_supported' => true,
'lms_subject_max_length' => 60,
'mms_supported' => false,
'delay_supported' => true,
);
/**
* Config keys used by this driver are stored here.
*/
protected static $_required_config = array('api_user', 'api_key');
/**
* Check if the current SMS driver is supported on this server.
*
* This method returns true on success and false on failure.
*
* @return bool
*/
public static function isSupported()
{
return true;
}
/**
* Store the last response.
*/
protected $_last_response = '';
/**
* Send a message.
*
* This method returns true on success and false on failure.
*
* @param array $messages
* @param object $original
* @return bool
*/
public function send(array $messages, \Rhymix\Framework\SMS $original)
{
$status = true;
foreach ($messages as $i => $message)
{
$data = array();
$data['send_phone'] = $message->from;
$data['dest_phone'] = implode(',', $message->to);
$data['msg_body'] = strval($message->content);
if ($message->type !== 'SMS' && $message->subject)
{
$data['subject'] = $message->subject;
}
$result = $this->_apiCall(sprintf('message/%s', strtolower($message->type)), $data);
if (!$result)
{
$message->errors[] = 'ApiStore API returned invalid response: ' . $this->_getLastResponse();
$status = false;
}
if ($result->result_message !== 'OK')
{
$message->errors[] = 'ApiStore API error: ' . $result->result_code . ' ' . $result->result_message;
}
}
return $status;
}
/**
* API call.
*
* @param string $url
* @param array $data
* @param string $method (optional)
* @return object|false
*/
protected function _apiCall(string $url, array $data, string $method = 'POST')
{
// Build the request URL.
if ($data['version'])
{
$version = $data['version'];
unset($data['version']);
}
else
{
$version = 1;
}
$url = sprintf('http://api.apistore.co.kr/ppurio/%d/%s/%s', $version, trim($url, '/'), $this->_config['api_user']);
// Set the API key in the header.
$headers = array(
'x-waple-authorization' => $this->_config['api_key'],
);
// Send the API reqeust.
if ($method === 'GET')
{
if ($data)
{
$url .= '?' . http_build_query($data);
}
$this->_last_response = \FileHandler::getRemoteResource($url, null, 5, $method, null, $headers) ?: '';
}
else
{
$this->_last_response = \FileHandler::getRemoteResource($url, $data, 5, $method, null, $headers) ?: '';
}
$result = @json_decode($this->_last_response);
return $result ?: false;
}
/**
* Fetch the last API response.
*
* @return string
*/
protected function _getLastResponse()
{
return $this->_last_response;
}
}

View file

@ -0,0 +1,116 @@
<?php
namespace Rhymix\Framework\Drivers\SMS;
/**
* The Cafe24 SMS driver.
*/
class Cafe24 extends Base implements \Rhymix\Framework\Drivers\SMSInterface
{
/**
* API specifications.
*/
protected static $_spec = array(
'max_recipients' => 1000,
'sms_max_length' => 90,
'sms_max_length_in_charset' => 'CP949',
'lms_supported' => true,
'lms_supported_country_codes' => array(82),
'lms_max_length' => 2000,
'lms_max_length_in_charset' => 'CP949',
'lms_subject_supported' => true,
'lms_subject_max_length' => 50,
'mms_supported' => false,
'delay_supported' => true,
);
/**
* Config keys used by this driver are stored here.
*/
protected static $_required_config = array('api_user', 'api_key');
/**
* Check if the current SMS driver is supported on this server.
*
* This method returns true on success and false on failure.
*
* @return bool
*/
public static function isSupported()
{
return true;
}
/**
* Send a message.
*
* This method returns true on success and false on failure.
*
* @param array $messages
* @param object $original
* @return bool
*/
public function send(array $messages, \Rhymix\Framework\SMS $original)
{
$status = true;
foreach ($messages as $i => $message)
{
// Authentication and basic information
$data = array();
$data['user_id'] = $this->_config['api_user'];
$data['secure'] = $this->_config['api_key'];
$data['nointeractive'] = 1;
if ($message->type === 'LMS')
{
$data['smsType'] = 'L';
}
// Sender and recipient
$from = explode('-', \Rhymix\Framework\Korea::formatPhoneNumber($message->from));
$data['sphone1'] = $from[0];
$data['sphone2'] = $from[1];
if (isset($from[2]))
{
$data['sphone3'] = $from[2];
}
$data['rphone'] = implode(',', array_map(function($num) {
return \Rhymix\Framework\Korea::formatPhoneNumber($num);
}, $message->to));
// Subject and content
if ($message->type === 'LMS' && $message->subject)
{
$data['subject'] = $message->subject;
}
$data['msg'] = $message->content;
// Set delay
if ($message->delay && $message->delay > time() + 600)
{
$data['rdate'] = gmdate('Ymd', $message->delay + (3600 * 9));
$data['rtime'] = gmdate('His', $message->delay + (3600 * 9));
}
// Send!
$url = 'https://sslsms.cafe24.com/sms_sender.php';
$result = \FileHandler::getRemoteResource($url, $data, 5, 'POST');
if(strval($result) === '')
{
$original->addError('Unknown API error while sending message ' . ($i + 1) . ' of ' . count($messages));
$status = false;
}
else
{
$result = explode(',', $result);
if ($result[0] !== 'success' && $result[0] !== 'reserved')
{
$original->addError('API error ' . $result[0] . ' while sending message ' . ($i + 1) . ' of ' . count($messages));
$status = false;
}
}
}
return $status;
}
}

View file

@ -0,0 +1,103 @@
<?php
namespace Rhymix\Framework\Drivers\SMS;
/**
* The Ppurio SMS driver.
*/
class Ppurio extends Base implements \Rhymix\Framework\Drivers\SMSInterface
{
/**
* API specifications.
*/
protected static $_spec = array(
'max_recipients' => 1000,
'sms_max_length' => 90,
'sms_max_length_in_charset' => 'CP949',
'lms_supported' => true,
'lms_supported_country_codes' => array(82),
'lms_max_length' => 2000,
'lms_max_length_in_charset' => 'CP949',
'lms_subject_supported' => true,
'lms_subject_max_length' => 30,
'mms_supported' => false,
'delay_supported' => true,
);
/**
* Config keys used by this driver are stored here.
*/
protected static $_required_config = array('api_user');
/**
* Check if the current SMS driver is supported on this server.
*
* This method returns true on success and false on failure.
*
* @return bool
*/
public static function isSupported()
{
return true;
}
/**
* Send a message.
*
* This method returns true on success and false on failure.
*
* @param array $messages
* @param object $original
* @return bool
*/
public function send(array $messages, \Rhymix\Framework\SMS $original)
{
$status = true;
foreach ($messages as $i => $message)
{
// Authentication and basic information
$data = array();
$data['userid'] = $this->_config['api_user'];
// Sender and recipient
$data['callback'] = preg_replace('/[^0-9]/', '', $message->from);
$data['phone'] = implode('|', array_map(function($num) {
return preg_replace('/[^0-9]/', '', $num);
}, $message->to));
// Subject and content
if ($message->type === 'LMS' && $message->subject)
{
$data['subject'] = $message->subject;
}
$data['msg'] = $message->content;
// Set delay
if ($message->delay && $message->delay > time() + 600)
{
$data['appdate'] = gmdate('YmdHis', $message->delay + (3600 * 9));
}
// Send!
$url = 'https://www.ppurio.com/api/send_utf8_json.php';
$result = \FileHandler::getRemoteResource($url, $data, 5, 'POST');
if(strval($result) === '')
{
$original->addError('Unknown API error while sending message ' . ($i + 1) . ' of ' . count($messages));
$status = false;
}
else
{
$result = @json_decode($result);
if ($result->result !== 'ok')
{
$original->addError('API error (' . $result->result . ') while sending message ' . ($i + 1) . ' of ' . count($messages));
$status = false;
}
}
}
return $status;
}
}

View file

@ -16,15 +16,22 @@ class SMSTest extends \Codeception\TestCase\Test
{
$drivers = Rhymix\Framework\SMS::getSupportedDrivers();
$this->assertTrue(isset($drivers['dummy']));
$this->assertTrue(isset($drivers['apistore']));
$this->assertTrue(isset($drivers['cafe24']));
$this->assertTrue(isset($drivers['coolsms']));
$this->assertTrue(isset($drivers['solapi']));
$this->assertTrue(isset($drivers['ppurio']));
$this->assertEquals('Dummy', $drivers['dummy']['name']);
$this->assertTrue(in_array('api_user', $drivers['apistore']['required']));
$this->assertTrue(in_array('api_user', $drivers['cafe24']['required']));
$this->assertTrue(in_array('api_key', $drivers['coolsms']['required']));
$this->assertTrue(in_array('api_user', $drivers['ppurio']['required']));
$this->assertTrue(in_array('api_key', $drivers['solapi']['required']));
$this->assertTrue($drivers['coolsms']['api_spec']['mms_supported']);
$this->assertTrue($drivers['coolsms']['api_spec']['delay_supported']);
$this->assertTrue($drivers['solapi']['api_spec']['mms_supported']);
$this->assertTrue($drivers['solapi']['api_spec']['delay_supported']);
$this->assertFalse($drivers['cafe24']['api_spec']['mms_supported']);
}
public function testSenderAndRecipients()