From d87249aa46708a07d41b5d43f443ab5c814d1f61 Mon Sep 17 00:00:00 2001 From: Brian Iyoha Date: Fri, 15 Jun 2018 17:08:03 +0100 Subject: [PATCH 01/19] initial commit --- AfricasTalkingGateway.php | 626 ++++++++++++++++++++++++++++++++++++++ index.php | 29 ++ 2 files changed, 655 insertions(+) create mode 100644 AfricasTalkingGateway.php create mode 100644 index.php diff --git a/AfricasTalkingGateway.php b/AfricasTalkingGateway.php new file mode 100644 index 0000000..ddf7efa --- /dev/null +++ b/AfricasTalkingGateway.php @@ -0,0 +1,626 @@ + + + AFRICAStALKING SMS GATEWAY CLASS IS A FREE SOFTWARE IE. CAN BE MODIFIED AND/OR REDISTRIBUTED + UNDER THE TERMS OF GNU GENERAL PUBLIC LICENCES AS PUBLISHED BY THE + FREE SOFTWARE FOUNDATION VERSION 3 OR ANY LATER VERSION + + THE CLASS IS DISTRIBUTED ON 'AS IS' BASIS WITHOUT ANY WARRANTY, INCLUDING BUT NOT LIMITED TO + THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE + OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +*/ + +class AfricasTalkingGatewayException extends Exception {} + +class AfricasTalkingGateway +{ + protected $_username; + protected $_apiKey; + + protected $_requestBody; + protected $_requestUrl; + + protected $_responseBody; + protected $_responseInfo; + + //Turn this on if you run into problems. It will print the raw HTTP response from our server + const Debug = false; + + const HTTP_CODE_OK = 200; + const HTTP_CODE_CREATED = 201; + + public function __construct($username_, $apiKey_) + { + $this->_username = $username_; + $this->_apiKey = $apiKey_; + + if ($username_ === 'sandbox') { + $this->_environment = 'sandbox'; + } else { + $this->_environment = 'production'; + } + + $this->_requestBody = null; + $this->_requestUrl = null; + + $this->_responseBody = null; + $this->_responseInfo = null; + } + + + //Messaging methods + public function sendMessage($to_, $message_, $from_ = null, $bulkSMSMode_ = 1, Array $options_ = array()) + { + if ( strlen($to_) == 0 || strlen($message_) == 0 ) { + throw new AfricasTalkingGatewayException('Please supply both to and message parameters'); + } + + $params = array( + 'username' => $this->_username, + 'to' => $to_, + 'message' => $message_, + ); + + if ( $from_ !== null ) { + $params['from'] = $from_; + $params['bulkSMSMode'] = $bulkSMSMode_; + } + + //This contains a list of parameters that can be passed in $options_ parameter + if ( count($options_) > 0 ) { + $allowedKeys = array ( + 'enqueue', + 'keyword', + 'linkId', + 'retryDurationInHours' + ); + + //Check whether data has been passed in options_ parameter + foreach ( $options_ as $key => $value ) { + if ( in_array($key, $allowedKeys) && strlen($value) > 0 ) { + $params[$key] = $value; + } else { + throw new AfricasTalkingGatewayException("Invalid key in options array: [$key]"); + } + } + } + + $this->_requestUrl = $this->getSendSmsUrl(); + $this->_requestBody = http_build_query($params, '', '&'); + + $this->executePOST(); + + if ( $this->_responseInfo['http_code'] == self::HTTP_CODE_CREATED ) { + $responseObject = json_decode($this->_responseBody); + if(count($responseObject->SMSMessageData->Recipients) > 0) + return $responseObject->SMSMessageData->Recipients; + + throw new AfricasTalkingGatewayException($responseObject->SMSMessageData->Message); + } + + throw new AfricasTalkingGatewayException($this->_responseBody); + } + + + public function fetchMessages($lastReceivedId_) + { + $username = $this->_username; + $this->_requestUrl = $this->getSendSmsUrl().'?username='.$username.'&lastReceivedId='. intval($lastReceivedId_); + + $this->executeGet(); + + if ( $this->_responseInfo['http_code'] == self::HTTP_CODE_OK ) { + $responseObject = json_decode($this->_responseBody); + return $responseObject->SMSMessageData->Messages; + } + + throw new AfricasTalkingGatewayException($this->_responseBody); + } + + + //Subscription methods + public function createSubscription($phoneNumber_, $shortCode_, $keyword_, $checkoutToken_) + { + + if ( strlen($phoneNumber_) == 0 || strlen($shortCode_) == 0 || strlen($keyword_) == 0 || strlen($checkoutToken_) == 0 ) { + throw new AfricasTalkingGatewayException('Please supply phoneNumber, shortCode, keyword and checkoutToken'); + } + + $params = array( + 'username' => $this->_username, + 'phoneNumber' => $phoneNumber_, + 'shortCode' => $shortCode_, + 'keyword' => $keyword_, + 'checkoutToken' => $checkoutToken_, + ); + + $this->_requestUrl = $this->getSubscriptionUrl("/create"); + $this->_requestBody = http_build_query($params, '', '&'); + + $this->executePOST(); + + if ( $this->_responseInfo['http_code'] != self::HTTP_CODE_CREATED ) + throw new AfricasTalkingGatewayException($this->_responseBody); + + return json_decode($this->_responseBody); + } + + public function deleteSubscription($phoneNumber_, $shortCode_, $keyword_) + { + if ( strlen($phoneNumber_) == 0 || strlen($shortCode_) == 0 || strlen($keyword_) == 0 ) { + throw new AfricasTalkingGatewayException('Please supply phoneNumber, shortCode and keyword'); + } + + $params = array( + 'username' => $this->_username, + 'phoneNumber' => $phoneNumber_, + 'shortCode' => $shortCode_, + 'keyword' => $keyword_ + ); + + $this->_requestUrl = $this->getSubscriptionUrl("/delete"); + $this->_requestBody = http_build_query($params, '', '&'); + + $this->executePOST(); + + if ( $this->_responseInfo['http_code'] != self::HTTP_CODE_CREATED ) + throw new AfricasTalkingGatewayException($this->_responseBody); + + return json_decode($this->_responseBody); + + } + + public function fetchPremiumSubscriptions($shortCode_, $keyword_, $lastReceivedId_ = 0) + { + $params = '?username='.$this->_username.'&shortCode='.$shortCode_; + $params .= '&keyword='.$keyword_.'&lastReceivedId='.intval($lastReceivedId_); + $this->_requestUrl = $this->getSubscriptionUrl($params); + + $this->executeGet(); + + if ( $this->_responseInfo['http_code'] == self::HTTP_CODE_OK ) { + $responseObject = json_decode($this->_responseBody); + return $responseObject->responses; + } + + throw new AfricasTalkingGatewayException($this->_responseBody); + } + + + //Call methods + public function call($from_, $to_) + { + if ( strlen($from_) == 0 || strlen($to_) == 0 ) { + throw new AfricasTalkingGatewayException('Please supply both from and to parameters'); + } + + $params = array( + 'username' => $this->_username, + 'from' => $from_, + 'to' => $to_ + ); + + $this->_requestUrl = $this->getVoiceUrl() . "/call"; + $this->_requestBody = http_build_query($params, '', '&'); + + $this->executePOST(); + + if(($responseObject = json_decode($this->_responseBody)) !== null) { + if(strtoupper(trim($responseObject->errorMessage)) == "NONE") { + return $responseObject->entries; + } + throw new AfricasTalkingGatewayException($responseObject->errorMessage); + } + else + throw new AfricasTalkingGatewayException($this->_responseBody); + } + + public function getNumQueuedCalls($phoneNumber_, $queueName = null) + { + $this->_requestUrl = $this->getVoiceUrl() . "/queueStatus"; + $params = array( + "username" => $this->_username, + "phoneNumbers" => $phoneNumber_ + ); + if($queueName !== null) + $params['queueName'] = $queueName; + $this->_requestBody = http_build_query($params, '', '&'); + $this->executePOST(); + + if(($responseObject = json_decode($this->_responseBody)) !== null) { + if(strtoupper(trim($responseObject->errorMessage)) == "NONE") + return $responseObject->entries; + throw new AfricasTalkingGatewayException($responseObject->ErrorMessage); + } + + throw new AfricasTalkingGatewayException($this->_responseBody); + } + + + public function uploadMediaFile($url_, $phoneNumber_) + { + $params = array( + "username" => $this->_username, + "url" => $url_, + "phoneNumber" => $phoneNumber_ + ); + + $this->_requestBody = http_build_query($params, '', '&'); + $this->_requestUrl = $this->getVoiceUrl() . "/mediaUpload"; + + $this->executePOST(); + } + + + //Airtime method + public function sendAirtime($recipients) + { + $params = array( + "username" => $this->_username, + "recipients" => $recipients + ); + $this->_requestUrl = $this->getAirtimeUrl("/send"); + $this->_requestBody = http_build_query($params, '', '&'); + + $this->executePOST(); + + if($this->_responseInfo['http_code'] == self::HTTP_CODE_CREATED) { + $responseObject = json_decode($this->_responseBody); + if(count($responseObject->responses) > 0) + return $responseObject->responses; + + throw new AfricasTalkingGatewayException($responseObject->errorMessage); + } + + throw new AfricasTalkingGatewayException($this->_responseBody); + } + + // Payments + public function bankPaymentCheckoutCharge($productName_, + $bankAccount_, + $currencyCode_, + $amount_, + $narration_, + $metadata_) { + $this->_requestBody = json_encode(array("username" => $this->_username, + "productName" => $productName_, + "bankAccount" => $bankAccount_, + "currencyCode" => $currencyCode_, + "amount" => $amount_, + "narration" => $narration_, + "metadata" => $metadata_)); + $this->_requestUrl = $this->getBankPaymentCheckoutChargeUrl(); + + $this->executeJsonPOST(); + if($this->_responseInfo['http_code'] == self::HTTP_CODE_CREATED) { + $response = json_decode($this->_responseBody); + if ( $response->status == "PendingValidation") return $response->transactionId; + else throw new AfricasTalkingGatewayException($response->description); + } + throw new AfricasTalkingGatewayException($this->_responseBody); + } + + public function bankPaymentCheckoutValidation($transactionId_, + $otp_) { + $this->_requestBody = json_encode(array("username" => $this->_username, + "transactionId" => $transactionId_, + "otp" => $otp_)); + + $this->_requestUrl = $this->getBankPaymentCheckoutValidationUrl(); + + $this->executeJsonPOST(); + if($this->_responseInfo['http_code'] == self::HTTP_CODE_CREATED) { + $response = json_decode($this->_responseBody); + if ( $response->status == "Success") return; + else throw new AfricasTalkingGatewayException($response->description); + } + throw new AfricasTalkingGatewayException($this->_responseBody); + + } + + public function bankPaymentTransfer($productName_, + $recipients_) { + $this->_requestBody = json_encode(array("username" => $this->_username, + "productName" => $productName_, + "recipients" => $recipients_)); + $this->_requestUrl = $this->getBankPaymentTransferUrl(); + + $this->executeJsonPOST(); + + if($this->_responseInfo['http_code'] == self::HTTP_CODE_CREATED) { + $response = json_decode($this->_responseBody); + if ( $response->status == "Success") return $response->transactionId; + else throw new AfricasTalkingGatewayException($response->description); + } + throw new AfricasTalkingGatewayException($this->_responseBody); + } + + + public function cardPaymentCheckoutCharge($productName_, + $paymentCard_, + $currencyCode_, + $amount_, + $narration_, + $metadata_) { + + $this->_requestBody = json_encode(array("username" => $this->_username, + "productName" => $productName_, + "paymentCard" => $paymentCard_, + "currencyCode" => $currencyCode_, + "amount" => $amount_, + "narration" => $narration_, + "metadata" => $metadata_)); + $this->_requestUrl = $this->getCardPaymentCheckoutChargeUrl(); + + $this->executeJsonPOST(); + if($this->_responseInfo['http_code'] == self::HTTP_CODE_CREATED) { + $response = json_decode($this->_responseBody); + if ( $response->status == "PendingValidation") return $response->transactionId; + else throw new AfricasTalkingGatewayException($response->description); + } + throw new AfricasTalkingGatewayException($this->_responseBody); + } + + public function cardPaymentCheckoutChargeWithToken($productName_, + $checkoutToken_, + $currencyCode_, + $amount_, + $narration_, + $metadata_) { + $this->_requestBody = json_encode(array("username" => $this->_username, + "productName" => $productName_, + "checkoutToken" => $checkoutToken_, + "currencyCode" => $currencyCode_, + "amount" => $amount_, + "narration" => $narration_, + "metadata" => $metadata_)); + $this->_requestUrl = $this->getCardPaymentCheckoutChargeUrl(); + + $this->executeJsonPOST(); + if($this->_responseInfo['http_code'] == self::HTTP_CODE_CREATED) { + $response = json_decode($this->_responseBody); + if ( $response->status == "Success") return $response->transactionId; + else throw new AfricasTalkingGatewayException($response->description); + } + throw new AfricasTalkingGatewayException($this->_responseBody); + } + + public function cardPaymentCheckoutValidation($transactionId_, + $otp_) { + $this->_requestBody = json_encode(array("username" => $this->_username, + "transactionId" => $transactionId_, + "otp" => $otp_)); + + $this->_requestUrl = $this->getCardPaymentCheckoutValidationUrl(); + + $this->executeJsonPOST(); + if($this->_responseInfo['http_code'] == self::HTTP_CODE_CREATED) { + $response = json_decode($this->_responseBody); + if ( $response->status == "Success") return $response->checkoutToken; + else throw new AfricasTalkingGatewayException($response->description); + } + throw new AfricasTalkingGatewayException($this->_responseBody); + + } + + + public function initiateMobilePaymentCheckout($productName_, + $phoneNumber_, + $currencyCode_, + $amount_, + $metadata_) { + $this->_requestBody = json_encode(array("username" => $this->_username, + "productName" => $productName_, + "phoneNumber" => $phoneNumber_, + "currencyCode" => $currencyCode_, + "amount" => $amount_, + "metadata" => $metadata_)); + $this->_requestUrl = $this->getMobilePaymentCheckoutUrl(); + + $this->executeJsonPOST(); + if($this->_responseInfo['http_code'] == self::HTTP_CODE_CREATED) { + $response = json_decode($this->_responseBody); + if ( $response->status == "PendingConfirmation") return $response->transactionId; + else throw new AfricasTalkingGatewayException($response->description); + } + throw new AfricasTalkingGatewayException($this->_responseBody); + } + + public function mobilePaymentB2CRequest($productName_, + $recipients_) { + $this->_requestBody = json_encode(array("username" => $this->_username, + "productName" => $productName_, + "recipients" => $recipients_)); + $this->_requestUrl = $this->getMobilePaymentB2CUrl(); + + $this->executeJsonPOST(); + if($this->_responseInfo['http_code'] == self::HTTP_CODE_CREATED) { + $response = json_decode($this->_responseBody); + $entries = $response->entries; + if (count($entries) > 0) return $entries; + else throw new AfricasTalkingGatewayException($response->errorMessage); + } + throw new AfricasTalkingGatewayException($this->_responseBody); + } + + public function mobilePaymentB2BRequest($productName_, $providerData_, $currencyCode_, $amount_, $metadata_) { + if(!isset($providerData_['provider']) || strlen($providerData_['provider']) == 0) + throw new AfricasTalkingGatewayException("Missing field provider"); + + if(!isset($providerData_['destinationChannel']) || strlen($providerData_['destinationChannel']) == 0) + throw new AfricasTalkingGatewayException("Missing field destinationChannel"); + + if(!isset($providerData_['destinationAccount']) || strlen($providerData_['destinationAccount']) == 0) + throw new AfricasTalkingGatewayException("Missing field destinationAccount"); + + if(!isset($providerData_['transferType']) || strlen($providerData_['transferType']) == 0) + throw new AfricasTalkingGatewayException("Missing field transferType"); + + $params = array("username" => $this->_username, + "productName" => $productName_, + "currencyCode" => $currencyCode_, + "amount"=>$amount_, + 'provider' => $providerData_['provider'], + 'destinationChannel' => $providerData_['destinationChannel'], + 'destinationAccount' => $providerData_['destinationAccount'], + 'transferType' => $providerData_['transferType'], + 'metadata' => $metadata_); + + $this->_requestBody = json_encode($params); + $this->_requestUrl = $this->getMobilePaymentB2BUrl(); + + $this->executeJsonPOST(); + if($this->_responseInfo['http_code'] == self::HTTP_CODE_CREATED) { + $response = json_decode($this->_responseBody); + return $response; + } + throw new AfricasTalkingGatewayException($this->_responseBody); + } + + //User info method + public function getUserData() + { + $username = $this->_username; + $this->_requestUrl = $this->getUserDataUrl('?username='.$username); + $this->executeGet(); + + if ( $this->_responseInfo['http_code'] == self::HTTP_CODE_OK ) { + $responseObject = json_decode($this->_responseBody); + return $responseObject->UserData; + } + + throw new AfricasTalkingGatewayException($this->_responseBody); + } + + private function executeGet () + { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Accept: application/json', + 'apikey: ' . $this->_apiKey)); + $this->doExecute($ch); + } + + private function executePost () + { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_requestBody); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Accept: application/json', + 'apikey: ' . $this->_apiKey)); + + $this->doExecute($ch); + } + + private function executeJsonPost () + { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_requestBody); + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', + 'Content-Length: ' . strlen($this->_requestBody), + 'apikey: ' . $this->_apiKey)); + $this->doExecute($ch); + } + + private function doExecute (&$curlHandle_) + { + try { + + $this->setCurlOpts($curlHandle_); + $responseBody = curl_exec($curlHandle_); + + if ( self::Debug ) { + echo "Full response: ". print_r($responseBody, true)."\n"; + } + + $this->_responseInfo = curl_getinfo($curlHandle_); + + $this->_responseBody = $responseBody; + curl_close($curlHandle_); + } + + catch(Exeption $e) { + curl_close($curlHandle_); + throw $e; + } + } + + private function setCurlOpts (&$curlHandle_) + { + curl_setopt($curlHandle_, CURLOPT_TIMEOUT, 60); + curl_setopt($curlHandle_, CURLOPT_SSL_VERIFYPEER, FALSE); + curl_setopt($curlHandle_, CURLOPT_URL, $this->_requestUrl); + curl_setopt($curlHandle_, CURLOPT_RETURNTRANSFER, true); + } + + private function getApiHost() { + return ($this->_environment == 'sandbox') ? 'https://api.sandbox.africastalking.com' : 'https://api.africastalking.com'; + } + + private function getPaymentHost() { + return ($this->_environment == 'sandbox') ? 'https://payments.sandbox.africastalking.com' : 'https://payments.africastalking.com'; + } + + private function getVoiceHost() { + return ($this->_environment == 'sandbox') ? 'https://voice.sandbox.africastalking.com' : 'https://voice.africastalking.com'; + } + + private function getSendSmsUrl($extension_ = "") { + return $this->getApiHost().'/version1/messaging'.$extension_; + } + + private function getVoiceUrl() { + return $this->getVoiceHost(); + } + + private function getUserDataUrl($extension_) { + return $this->getApiHost().'/version1/user'.$extension_; + } + + private function getSubscriptionUrl($extension_) { + return $this->getApiHost().'/version1/subscription'.$extension_; + } + + private function getAirtimeUrl($extension_) { + return $this->getApiHost().'/version1/airtime'.$extension_; + } + + private function getBankPaymentCheckoutChargeUrl() { + return $this->getPaymentHost().'/bank/checkout/charge'; + } + + private function getBankPaymentCheckoutValidationUrl() { + return $this->getPaymentHost().'/bank/checkout/validate'; + } + + private function getBankPaymentTransferUrl() { + return $this->getPaymentHost().'/bank/transfer'; + } + + private function getCardPaymentCheckoutChargeUrl() { + return $this->getPaymentHost().'/card/checkout/charge'; + } + + private function getCardPaymentCheckoutValidationUrl() { + return $this->getPaymentHost().'/card/checkout/validate'; + } + + private function getMobilePaymentCheckoutUrl() { + return $this->getPaymentHost().'/mobile/checkout/request'; + } + + private function getMobilePaymentB2CUrl() { + return $this->getPaymentHost().'/mobile/b2c/request'; + } + + private function getMobilePaymentB2BUrl() { + return $this->getPaymentHost().'/mobile/b2b/request'; + } +} diff --git a/index.php b/index.php new file mode 100644 index 0000000..10e62c4 --- /dev/null +++ b/index.php @@ -0,0 +1,29 @@ +sendMessage($recipient, $message, $from); + foreach ($results as $result) { + if ($result->status === "Success") { + echo "

Message succesfully sent to $result->number

Cost: $result->cost

"; + } + + } +} catch (AfricasTalkingGatewayException $e) { + echo "

Message failed to send to $recipient. See below message for error details or try again

Error:". $e->getMessage()."
"; + + +} From 2674fdcd3b4ee644c50f3ab63a705a475befb80e Mon Sep 17 00:00:00 2001 From: Brian Iyoha Date: Fri, 15 Jun 2018 17:11:25 +0100 Subject: [PATCH 02/19] add composer.json file --- composer.json | 1 + 1 file changed, 1 insertion(+) create mode 100644 composer.json diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/composer.json @@ -0,0 +1 @@ +{} From 62c971838fabea2cd0805946c049ed132b917b6a Mon Sep 17 00:00:00 2001 From: Brian Iyoha Date: Fri, 15 Jun 2018 17:15:32 +0100 Subject: [PATCH 03/19] initalize composer --- .gitignore | 1 + composer.json | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57872d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor/ diff --git a/composer.json b/composer.json index 0967ef4..f192fe7 100644 --- a/composer.json +++ b/composer.json @@ -1 +1,12 @@ -{} +{ + "name": "codearcher/code-challenge-uni-ben1606", + "require": { + "ext-curl": "^7.2" + }, + "authors": [ + { + "name": "Brian Iyoha", + "email": "brian98ehis@gmail.com" + } + ] +} From eee7e2ad5207ff32510e0779e9a14eb599666ac4 Mon Sep 17 00:00:00 2001 From: Brian Iyoha Date: Fri, 15 Jun 2018 17:35:21 +0100 Subject: [PATCH 04/19] setting up heroku --- index.php | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 index.php diff --git a/index.php b/index.php deleted file mode 100644 index 10e62c4..0000000 --- a/index.php +++ /dev/null @@ -1,29 +0,0 @@ -sendMessage($recipient, $message, $from); - foreach ($results as $result) { - if ($result->status === "Success") { - echo "

Message succesfully sent to $result->number

Cost: $result->cost

"; - } - - } -} catch (AfricasTalkingGatewayException $e) { - echo "

Message failed to send to $recipient. See below message for error details or try again

Error:". $e->getMessage()."
"; - - -} From f1fff32112157662ef295e5ea8b722b82cf62b90 Mon Sep 17 00:00:00 2001 From: Brian Iyoha Date: Fri, 15 Jun 2018 17:35:37 +0100 Subject: [PATCH 05/19] setting up heroku --- public/index.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 public/index.php diff --git a/public/index.php b/public/index.php new file mode 100644 index 0000000..10e62c4 --- /dev/null +++ b/public/index.php @@ -0,0 +1,29 @@ +sendMessage($recipient, $message, $from); + foreach ($results as $result) { + if ($result->status === "Success") { + echo "

Message succesfully sent to $result->number

Cost: $result->cost

"; + } + + } +} catch (AfricasTalkingGatewayException $e) { + echo "

Message failed to send to $recipient. See below message for error details or try again

Error:". $e->getMessage()."
"; + + +} From 947c8c1261327dbabefd048fa8308c2f0380eafa Mon Sep 17 00:00:00 2001 From: Brian Iyoha Date: Fri, 15 Jun 2018 17:36:35 +0100 Subject: [PATCH 06/19] Revert "initalize composer" This reverts commit 62c971838fabea2cd0805946c049ed132b917b6a. --- .gitignore | 1 - composer.json | 13 +------------ 2 files changed, 1 insertion(+), 13 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 57872d0..0000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/vendor/ diff --git a/composer.json b/composer.json index f192fe7..0967ef4 100644 --- a/composer.json +++ b/composer.json @@ -1,12 +1 @@ -{ - "name": "codearcher/code-challenge-uni-ben1606", - "require": { - "ext-curl": "^7.2" - }, - "authors": [ - { - "name": "Brian Iyoha", - "email": "brian98ehis@gmail.com" - } - ] -} +{} From 410ee055d51e424d454217ab244298aac31a044a Mon Sep 17 00:00:00 2001 From: Brian Iyoha Date: Fri, 15 Jun 2018 17:37:22 +0100 Subject: [PATCH 07/19] Revert "setting up heroku" This reverts commit eee7e2ad5207ff32510e0779e9a14eb599666ac4. --- index.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 index.php diff --git a/index.php b/index.php new file mode 100644 index 0000000..10e62c4 --- /dev/null +++ b/index.php @@ -0,0 +1,29 @@ +sendMessage($recipient, $message, $from); + foreach ($results as $result) { + if ($result->status === "Success") { + echo "

Message succesfully sent to $result->number

Cost: $result->cost

"; + } + + } +} catch (AfricasTalkingGatewayException $e) { + echo "

Message failed to send to $recipient. See below message for error details or try again

Error:". $e->getMessage()."
"; + + +} From 7d462a54f98bcec412b2b10548d2e577b80c9083 Mon Sep 17 00:00:00 2001 From: Brian Iyoha Date: Fri, 15 Jun 2018 17:37:31 +0100 Subject: [PATCH 08/19] Revert "setting up heroku" This reverts commit f1fff32112157662ef295e5ea8b722b82cf62b90. --- public/index.php | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 public/index.php diff --git a/public/index.php b/public/index.php deleted file mode 100644 index 10e62c4..0000000 --- a/public/index.php +++ /dev/null @@ -1,29 +0,0 @@ -sendMessage($recipient, $message, $from); - foreach ($results as $result) { - if ($result->status === "Success") { - echo "

Message succesfully sent to $result->number

Cost: $result->cost

"; - } - - } -} catch (AfricasTalkingGatewayException $e) { - echo "

Message failed to send to $recipient. See below message for error details or try again

Error:". $e->getMessage()."
"; - - -} From 9fda224ad9a4836042db960e1811ca1129a94438 Mon Sep 17 00:00:00 2001 From: Brian Iyoha Date: Fri, 15 Jun 2018 17:55:41 +0100 Subject: [PATCH 09/19] check for proper request method --- index.php | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/index.php b/index.php index 10e62c4..0cad7f1 100644 --- a/index.php +++ b/index.php @@ -1,29 +1,36 @@ This is a simple implementation of a two-way sms code challenge"); +} else if ($request === 'POST') { -$username = "sandbox"; -$apikey = "e213988d4ac0a2a16604c7c796b64d613ad5269a13c1c8074c9457060d9b0a94"; -$recipient = "+2347031362606"; + require_once 'AfricasTalkingGateway.php'; + $to = $_POST['to']; + $username = getenv('AT_USERNAME'); + $apikey = getenv('AT_APIKEY'); -$message = "I am a fisherman. I sleep all day and work all night!"; + $recipient = trim($to); -$from = "6996"; + $message = "I am a fisherman. I sleep all day and work all night!"; -$gateway = new AfricasTalkingGateway($username, $apikey); + $from = "6996"; -try -{ + $gateway = new AfricasTalkingGateway($username, $apikey); + + try + { + + $results = $gateway->sendMessage($recipient, $message, $from); + foreach ($results as $result) { + if ($result->status === "Success") { + echo "

Message succesfully sent to $result->number

Cost: $result->cost

"; + } - $results = $gateway->sendMessage($recipient, $message, $from); - foreach ($results as $result) { - if ($result->status === "Success") { - echo "

Message succesfully sent to $result->number

Cost: $result->cost

"; } + } catch (AfricasTalkingGatewayException $e) { + echo "

Message failed to send to $recipient. See below message for error details or try again

Error:" . $e->getMessage() . "
"; } -} catch (AfricasTalkingGatewayException $e) { - echo "

Message failed to send to $recipient. See below message for error details or try again

Error:". $e->getMessage()."
"; - } From ae47ca287efe6bf27ae8a4a9eb939b8bd727b729 Mon Sep 17 00:00:00 2001 From: Brian Iyoha Date: Fri, 15 Jun 2018 18:43:54 +0100 Subject: [PATCH 10/19] updated to oop --- index.php | 63 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 20 deletions(-) diff --git a/index.php b/index.php index 0cad7f1..0678e1d 100644 --- a/index.php +++ b/index.php @@ -1,36 +1,59 @@ This is a simple implementation of a two-way sms code challenge"); -} else if ($request === 'POST') { +class TwoWaySms +{ + public function sendSms() + { + require_once 'AfricasTalkingGateway.php'; + $rawData = $this->retrieveJsonPostData(); - require_once 'AfricasTalkingGateway.php'; - $to = $_POST['to']; - $username = getenv('AT_USERNAME'); - $apikey = getenv('AT_APIKEY'); + $username = getenv('AT_USERNAME'); + $apikey = getenv('AT_APIKEY'); - $recipient = trim($to); + $recipient = trim($rawData->to); + + $message = "I am a fisherman. I sleep all day and work all night!"; - $message = "I am a fisherman. I sleep all day and work all night!"; + $from = "6996"; - $from = "6996"; + $gateway = new AfricasTalkingGateway($username, $apikey); - $gateway = new AfricasTalkingGateway($username, $apikey); + try + { - try - { + $results = $gateway->sendMessage($recipient, $message, $from); + foreach ($results as $result) { + if ($result->status === "Success") { + echo "

Message succesfully sent to $result->number

Cost: $result->cost

"; + } - $results = $gateway->sendMessage($recipient, $message, $from); - foreach ($results as $result) { - if ($result->status === "Success") { - echo "

Message succesfully sent to $result->number

Cost: $result->cost

"; } + } catch (AfricasTalkingGatewayException $e) { + echo "

Message failed to send to $recipient. See below message for error details or try again

Error:" . $e->getMessage() . "
"; } - } catch (AfricasTalkingGatewayException $e) { - echo "

Message failed to send to $recipient. See below message for error details or try again

Error:" . $e->getMessage() . "
"; + } + /** + * Returns the JSON encoded POST data, if any, as an object. + * + * @return Object|null + */ + private function retrieveJsonPostData() + { + // get the raw POST data + $rawData = file_get_contents("php://input"); + + // this returns null if not valid json + return json_decode($rawData); } +} + +$request = $_SERVER['REQUEST_METHOD']; +if ($request === 'GET') { + print("

This is a simple implementation of a two-way sms code challenge

"); +} else if ($request === 'POST') { + $sms = new TwoWaySms(); + $sms->sendSms(); } From 907fd622031e191005482408f48c46111f175386 Mon Sep 17 00:00:00 2001 From: Brian Iyoha Date: Fri, 15 Jun 2018 22:02:39 +0100 Subject: [PATCH 11/19] [WIP] bug fix --- index.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/index.php b/index.php index 0678e1d..17ca8b2 100644 --- a/index.php +++ b/index.php @@ -4,14 +4,17 @@ class TwoWaySms { public function sendSms() { + error_log("hello, this is a test!"); + require_once 'AfricasTalkingGateway.php'; $rawData = $this->retrieveJsonPostData(); + error_log("RAW DATA $rawData"); $username = getenv('AT_USERNAME'); $apikey = getenv('AT_APIKEY'); $recipient = trim($rawData->to); - + $message = "I am a fisherman. I sleep all day and work all night!"; $from = "6996"; @@ -50,6 +53,8 @@ private function retrieveJsonPostData() } $request = $_SERVER['REQUEST_METHOD']; +error_log($request); + if ($request === 'GET') { print("

This is a simple implementation of a two-way sms code challenge

"); } else if ($request === 'POST') { From 52fc99118081b7b357ac40bc51391807af3ba776 Mon Sep 17 00:00:00 2001 From: Brian Iyoha Date: Fri, 15 Jun 2018 22:04:44 +0100 Subject: [PATCH 12/19] [WIP] bug fix --- index.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.php b/index.php index 17ca8b2..52fb4da 100644 --- a/index.php +++ b/index.php @@ -9,6 +9,8 @@ public function sendSms() require_once 'AfricasTalkingGateway.php'; $rawData = $this->retrieveJsonPostData(); error_log("RAW DATA $rawData"); + error_log("REQUEST $_REQUEST"); + error_log("REQUEST $_POST"); $username = getenv('AT_USERNAME'); $apikey = getenv('AT_APIKEY'); From c77e39512908e77cebae65f887779c08fd5f3f3c Mon Sep 17 00:00:00 2001 From: Brian Iyoha Date: Fri, 15 Jun 2018 22:07:42 +0100 Subject: [PATCH 13/19] [WIP] bug fix --- index.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/index.php b/index.php index 52fb4da..1fe742d 100644 --- a/index.php +++ b/index.php @@ -9,8 +9,9 @@ public function sendSms() require_once 'AfricasTalkingGateway.php'; $rawData = $this->retrieveJsonPostData(); error_log("RAW DATA $rawData"); - error_log("REQUEST $_REQUEST"); - error_log("REQUEST $_POST"); + error_log("REQUEST" .var_dump($_REQUEST).""); + error_log("POST". var_dump($_POST).""); + error_log("GET". var_dump($_GET).""); $username = getenv('AT_USERNAME'); $apikey = getenv('AT_APIKEY'); From b69b4be5c48acde53397d22e1badfcf30739ab84 Mon Sep 17 00:00:00 2001 From: Brian Iyoha Date: Fri, 15 Jun 2018 22:10:42 +0100 Subject: [PATCH 14/19] [WIP] bug fix --- index.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.php b/index.php index 1fe742d..b907e15 100644 --- a/index.php +++ b/index.php @@ -8,15 +8,15 @@ public function sendSms() require_once 'AfricasTalkingGateway.php'; $rawData = $this->retrieveJsonPostData(); - error_log("RAW DATA $rawData"); - error_log("REQUEST" .var_dump($_REQUEST).""); + error_log($rawData); + error_log("REQUEST" .print_r($_REQUEST).""); error_log("POST". var_dump($_POST).""); error_log("GET". var_dump($_GET).""); $username = getenv('AT_USERNAME'); $apikey = getenv('AT_APIKEY'); - $recipient = trim($rawData->to); + $recipient = trim($_POST['to']); $message = "I am a fisherman. I sleep all day and work all night!"; From aa2ea4dad43be2443ce91f00d2a6f9e22958be31 Mon Sep 17 00:00:00 2001 From: Brian Iyoha Date: Fri, 15 Jun 2018 22:20:19 +0100 Subject: [PATCH 15/19] [WIP] bug fix --- index.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/index.php b/index.php index b907e15..f8deb26 100644 --- a/index.php +++ b/index.php @@ -8,10 +8,9 @@ public function sendSms() require_once 'AfricasTalkingGateway.php'; $rawData = $this->retrieveJsonPostData(); - error_log($rawData); - error_log("REQUEST" .print_r($_REQUEST).""); - error_log("POST". var_dump($_POST).""); - error_log("GET". var_dump($_GET).""); + error_log(print_r($rawData,true)); + error_log("REQUEST" .print_r($_REQUEST,true).""); + error_log("POST". print_r($_POST,true).""); $username = getenv('AT_USERNAME'); $apikey = getenv('AT_APIKEY'); From bd2252330250cd48167dd9f10d79cd6367c7f499 Mon Sep 17 00:00:00 2001 From: Brian Iyoha Date: Fri, 15 Jun 2018 22:29:01 +0100 Subject: [PATCH 16/19] [WIP] possible bug fix --- index.php | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/index.php b/index.php index f8deb26..082f267 100644 --- a/index.php +++ b/index.php @@ -2,7 +2,7 @@ class TwoWaySms { - public function sendSms() + public function sendSms($to) { error_log("hello, this is a test!"); @@ -15,7 +15,7 @@ public function sendSms() $username = getenv('AT_USERNAME'); $apikey = getenv('AT_APIKEY'); - $recipient = trim($_POST['to']); + $recipient = trim($to); $message = "I am a fisherman. I sleep all day and work all night!"; @@ -38,20 +38,6 @@ public function sendSms() } } - - /** - * Returns the JSON encoded POST data, if any, as an object. - * - * @return Object|null - */ - private function retrieveJsonPostData() - { - // get the raw POST data - $rawData = file_get_contents("php://input"); - - // this returns null if not valid json - return json_decode($rawData); - } } $request = $_SERVER['REQUEST_METHOD']; @@ -61,6 +47,9 @@ private function retrieveJsonPostData() print("

This is a simple implementation of a two-way sms code challenge

"); } else if ($request === 'POST') { $sms = new TwoWaySms(); - - $sms->sendSms(); + $from = $_POST['from']; + $sms->sendSms($from); } + + + From 85c1dbbba6a9a43ec713b1cea5e0133328bfa56e Mon Sep 17 00:00:00 2001 From: Brian Iyoha Date: Fri, 15 Jun 2018 22:31:56 +0100 Subject: [PATCH 17/19] [WIP: bug fix] removed undefined method --- index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.php b/index.php index 082f267..b91dfff 100644 --- a/index.php +++ b/index.php @@ -7,7 +7,7 @@ public function sendSms($to) error_log("hello, this is a test!"); require_once 'AfricasTalkingGateway.php'; - $rawData = $this->retrieveJsonPostData(); + error_log(print_r($rawData,true)); error_log("REQUEST" .print_r($_REQUEST,true).""); error_log("POST". print_r($_POST,true).""); From f9c3ee2f57ce81dcdcca8b6dcbc81e6d1e51dbe6 Mon Sep 17 00:00:00 2001 From: Brian Iyoha Date: Fri, 15 Jun 2018 22:39:21 +0100 Subject: [PATCH 18/19] code clean up --- index.php | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/index.php b/index.php index b91dfff..d1320ab 100644 --- a/index.php +++ b/index.php @@ -2,15 +2,14 @@ class TwoWaySms { + + /** + * send sms response back to the sender + * @param number $to Phone number to send a response to + */ public function sendSms($to) { - error_log("hello, this is a test!"); - require_once 'AfricasTalkingGateway.php'; - - error_log(print_r($rawData,true)); - error_log("REQUEST" .print_r($_REQUEST,true).""); - error_log("POST". print_r($_POST,true).""); $username = getenv('AT_USERNAME'); $apikey = getenv('AT_APIKEY'); @@ -41,7 +40,6 @@ public function sendSms($to) } $request = $_SERVER['REQUEST_METHOD']; -error_log($request); if ($request === 'GET') { print("

This is a simple implementation of a two-way sms code challenge

"); @@ -50,6 +48,3 @@ public function sendSms($to) $from = $_POST['from']; $sms->sendSms($from); } - - - From c72626af96746a2b054aa0f399dbee5cb7d192ca Mon Sep 17 00:00:00 2001 From: Brian Iyoha Date: Fri, 15 Jun 2018 22:53:26 +0100 Subject: [PATCH 19/19] update readme --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index e82b086..2000b65 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,14 @@ You can participate on as many challenges as you wish: ## Code Challenge Bounty: - Airtime and Swag for the first 30 successful participants +## Usage: +- Open the [AfricasTalking Sandbox Simulator](https://simulator.africastalking.com:1517/). +- Enter a test phone number on the text field provided. +- Click on launch button to open a simulator +- Select the SMS option +- Create and send a message of any context to 6996 +- You will receive a response shortly after, if your message was sent succesfully + ## Task In this code challenge you should create a 2-way SMS app that can be accessed on the Sandbox. The 2-Way SMS App should allow users to send an SMS from their simulator to your short code, and get a simple reply.