/** * @param PhoneNumber $number * @param Message $message * @param \DateTime $schedule * * @throws \SMSKrank\Exceptions\GatewayException * * @return float | null Message fee, if available. Null otherwise */ public function send(PhoneNumber $number, Message $message, \DateTime $schedule = null) { $mailer = new PHPMailer(); $mailer->IsSMTP(); $mailer->Port = $this->port; $mailer->Host = $this->host; if ($this->user || $this->password) { $mailer->SMTPAuth = true; $mailer->Username = $this->user; $mailer->Password = $this->password; } $mailer->CharSet = "UTF-8"; $mailer->From = $this->from; $mailer->Subject = "SMS-Krank Debug Gateway"; if (empty($this->to)) { throw new GatewayException('No recipients'); } foreach ($this->to as $to) { if (!$mailer->AddAddress($to)) { throw new GatewayException("Invalid recipient: '{$to}'"); } } $nl = "\r\n"; $mailer->Body .= "Phone: " . $number->getNumber() . $nl; $mailer->Body .= "Message: " . $message->getText() . $nl; $mailer->Body .= "Scheduled: " . ($schedule ? $schedule->format(\DateTime::ISO8601) : 'immediately'); if (!$mailer->Send()) { throw new GatewayException("Failed to send"); } return null; }
public function send(PhoneNumber $number, Message $message, \DateTime $schedule = null) { $args = array('key' => $this->public_key, 'text' => $message->getText(), 'phone' => $number->getNumber(), 'sender' => $this->sender); if ($schedule) { // NOTE: this gate traits date timezone as your location provided in cabinet $args['datetime'] = $schedule->format("Y-m-d H:i:s"); } $json = $this->call('sendSMS', $args); if (!isset($json['price'])) { throw new GatewayException('Bad response'); } return (double) $json['price']; // price amount is set in user prefs }
public function send(PhoneNumber $number, Message $message, \DateTime $schedule = null) { if ($this->balance - $this->price < 0) { throw new GatewayException("You don't have enough money on your balance"); } $template = $this->format; $template = str_replace('{name}', $this->name, $template); $template = str_replace('{phone}', $number->getNumber(), $template); $template = str_replace('{schedule}', $schedule ? 'at ' . $schedule->format(\DateTime::ISO8601) : 'immediately', $template); // message may contain any data, even template placeholders, so process it aat last $template = str_replace('{message}', $message->getText(), $template); echo $template; $this->balance -= $this->price; return $this->price; }
public function __construct($phone_number, array $calling_codes, array $geo, array $props) { parent::__construct($phone_number); $this->codes = $calling_codes; $this->geo = $geo; $this->props = $props; }
/** * @param PhoneNumber $number * @param Message $message * @param \DateTime $schedule * * @throws \SMSKrank\Exceptions\GatewayException * * @return float | null Message fee, if available. Null otherwise */ public function send(PhoneNumber $number, Message $message, \DateTime $schedule = null) { $args = array('operation' => 'send', 'login' => $this->login, 'password' => $this->password, 'msisdn' => $number->getNumber(), 'shortcode' => $this->sender, 'text' => $message->getText()); $url = 'http://http.a1smsmarket.ru:8000/send?' . http_build_query($args); $res = file_get_contents($url); if (strpos($res, ':')) { list(, $error) = explode(':', $res); throw new GatewayException(trim($error)); } $id = (int) $res; // retrieve message id if ($id < 1) { throw new GatewayException('Bad response'); } return null; }
public function send(PhoneNumber $number, Message $message, \DateTime $schedule = null) { $args = array('login' => $this->login, 'password' => $this->password, 'txt' => $message->getText(), 'to' => $number->getNumber(), 'onlydelivery' => 1); if ($schedule) { // NOTE: this gate traits date timezone as your location provided in cabinet $args['dateTimeSend'] = $schedule->format("Y-m-d H:i:s"); } // response as JSON (no respect to HTTP status code, btw, very nice. curl is useless here) // https://lcab.sms-uslugi.ru/lcabApi/sendSms.php $res = file_get_contents("https://lcab.sms-uslugi.ru/lcabApi/sendSms.php?" . http_build_query($args)); $json = json_decode($res, true); if (!is_array($json) || !isset($json['code'])) { throw new GatewayException('Bad response'); } // TODO: return message(s) cost return $json['code'] == 1; }
public function send(PhoneNumber $number, Message $message, \DateTime $schedule = null) { if ($number instanceof PhoneNumberDetailed) { $detailed_phone_number = $number; } else { $detailed_phone_number = $this->directory->getPhoneNumberDetailed($number->getNumber()); } try { $gateways = $this->maps_loader->get($detailed_phone_number->getGeo('country_alpha2')); } catch (LoaderException $e) { throw new ExchangeException("Failed to send message (unable to load country)"); } catch (PhoneNumberDetailedException $e) { throw new ExchangeException("Failed to send message (phone number is not supported)"); } $price = false; foreach ($gateways as $gate_name => $required_props) { if (null != $required_props) { // required props are array with at least one property // all properties should exist in phone number and have the same value $phone_props = $detailed_phone_number->getProp() + array('geo' => $detailed_phone_number->getGeo(), 'code' => $detailed_phone_number->getCode()); // TODO: support OR, NOT for nested props, for now I don't need this and it is a bit complicated piece of code // NOTE: we use AND logic for comparison, so to use gate phone should have all required props with the same values $intersection = $this->array_intersect_assoc_recursive($required_props, $phone_props); if ($required_props !== $intersection) { continue; // no match, try the next one } } try { $gate = $this->gateway_factory->getGateway($gate_name); $price = $gate->send($detailed_phone_number, $message, $schedule); break; } catch (LoaderException $e) { } catch (GatewayException $e) { } } if (false === $price) { // when price for sent message is not available it should be set to null throw new ExchangeException("Failed to send message (unable to pick working gate)"); } return $price; }
public function send(PhoneNumber $number, Message $message, \DateTime $schedule = null) { if ($this->file_name == '/dev/null') { // add null device support for non-*NIX users return null; } $template = $this->format; $schedule = $schedule ? 'at ' . $schedule->format(\DateTime::ISO8601) : 'immediately'; $now = new \DateTime(); $template = str_replace('{datetime}', $now->format(\DateTime::ISO8601), $template); $template = str_replace('{phone}', $number->getNumber(), $template); $template = str_replace('{schedule}', $schedule, $template); // message may contain any data, even template placeholders, so process it aat last $template = str_replace('{message}', $message->getText(), $template); $fh = fopen($this->file_name, 'w+'); if (!$fh) { throw new GatewayException("Failed to open file '{$this->file_name}'"); } fwrite($fh, $template . PHP_EOL); fclose($fh); return null; }
/** * @cover \SMSKrank\PhoneNumber::__construct * @cover \SMSKrank\PhoneNumber::getNumber */ public function testGetNumber() { $number = '12345'; $object = new PhoneNumber($number); $this->assertEquals($number, $object->getNumber()); }