array('url' => '/sendMessage/', 'method' => 'POST'), 'get_message_status' => array('url' => '/report/message', 'method' => 'GET'), 'get_balance' => array('url' => '/account/balance', 'method' => 'GET'), 'get_pricing' => array('url' => '/account/pricing', 'method' => 'GET') ); public $response_format = 'json'; public $message_type_option = array('7-bit' => 1, '8-bit' => 2, 'Unicode' => 3); public function __construct($api_key = null, $api_secret = null) { $this->log = new Notify_WooCoommerce_Logger(); $this->api_key = $api_key; $this->api_secret = $api_secret; $this->actual_api_url = $this->rest_base_url; } public function setApiUrl($use_domain = true) { $this->actual_api_url = ($use_domain) ? $this->rest_base_url : $this->rest_ip_address; } function sendSMS($from, $to, $message, $medium, $message_type = null, $dlr_url = null, $udh = null) { $this->log->add("360MessengerWhatsApp", "WhatsApp Sent to {$to}: {$message} "); // Send request to NotifySMS gateway // return json_encode(array( // 'messages' => array( // array( // 'status' => 0, // 'msgid' => sha1( random_bytes(128) ), // ) // ) // )); $params = array( 'phonenumber' => $to, 'text' => $message, '360notify-medium' => "wordpress_order_notification" ); return $this->invokeApi ('send_sms', $params); } public function receiveDLR($data) { $delivery_status = array(1 => 'Success', 2 => 'Failed', 3 => 'Expired'); $delivery_report_data = new stdClass(); $delivery_report_data->from = $data['notify-from']; $delivery_report_data->to = $data['notify-to']; $delivery_report_data->dlr_status = $delivery_status[$data['notify-dlr-status']]; $delivery_report_data->msgid = $data['notify-msgid']; $delivery_report_data->error_code = $data['notify-error-code']; $delivery_report_data->dlr_received_time = gmdate('Y-m-d H:i:s'); return $delivery_report_data; } public function receiveMO($data) { $mo_message = new stdClass(); $mo_message->from = $data['notify-from']; $mo_message->to = $data['notify-to']; $mo_message->keyword = $data['notify-keyword']; $mo_message->text = $data['notify-text']; $mo_message->coding = $data['notify-coding']; $mo_message->time = $data['notify-time']; if($mo_message->coding == $this->message_type_option['Unicode']) { $mo_message->keyword = $this->utf16HexToUtf8($mo_message->keyword); $mo_message->text = $this->utf16HexToUtf8($mo_message->text); } return $mo_message; } public function messageStatus($msgid) { $params = array('notify-msgid' => $msgid); return $this->invokeApi ('get_message_status', $params); } public function accountBalance() { return $this->invokeApi ('get_balance'); } public function accountPricing($mcc = null, $mnc = null) { $params = array(); if($mcc) { $params['notify-mcc'] = $mcc; } if($mnc) { $params['notify-mnc'] = $mnc; } return $this->invokeApi ('get_pricing', $params); } private function invokeApi($command, $params = array()) { if(get_option("notify_domain_reachable")) { $this->setApiUrl(true); } else { $this->setApiUrl(false); } // Get REST URL and HTTP method $command_info = $this->rest_commands[$command]; $url = $this->actual_api_url . $command_info['url'].$this->api_key; $method = $command_info['method']; $this->log->add("360MessengerWhatsApp", "Using url: {$url}"); // Build the post data $args = [ 'timeout' => 15, 'redirection' => 5, // 'blocking' => true, // 'sslverify' => false, 'headers' => [ 'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8', ], ]; if($method == 'POST') { $args['body'] = $params; $response = wp_remote_post($url, $args); } else { $query_string = http_build_query($params); $url = $url . '?' . $query_string; $response = wp_remote_get($url, $args); } if (is_wp_error($response)) { throw new Exception('HTTP request error: ' . esc_html($response->get_error_message())); } $body = wp_remote_retrieve_body($response); return $body; } private function utf16HexToUtf8($string) { if(strlen($string) % 4) { $string = '00'.$string; } $converted_string = ''; $string_length = strlen($string); for($counter = 0; $counter < $string_length; $counter += 4) { $converted_string .= "&#".hexdec(substr($string, $counter, 4)).";"; } $converted_string = mb_convert_encoding($converted_string, "UTF-8", "HTML-ENTITIES"); return $converted_string; } } ?>