Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 183 |
|
0.00% |
0 / 12 |
CRAP | |
0.00% |
0 / 1 |
| LoyaltyResponse | |
0.00% |
0 / 183 |
|
0.00% |
0 / 12 |
2352 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| receiveSMS | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
110 | |||
| sendSMS | |
0.00% |
0 / 57 |
|
0.00% |
0 / 1 |
240 | |||
| optInCustomer | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| optOutCustomer | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| checkBalance | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| sendSMSByShortName | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| filterMessage | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| sendRedemptionSMS | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
20 | |||
| sendEarnedSMS | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
6 | |||
| sendBalanceSMS | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| addTextRecord | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BuyerKiosk\Core\Loyalty; |
| 4 | |
| 5 | use BuyerKiosk\Core\Store; |
| 6 | |
| 7 | class LoyaltyResponse extends LoyaltyCustomer { |
| 8 | |
| 9 | public function __construct($loyaltyID) { |
| 10 | parent::__construct($loyaltyID); |
| 11 | } |
| 12 | public function receiveSMS($body, $typeNum, $from) { |
| 13 | $log = new \KLogger($_ENV['LOG_DIR']."response.log", \KLogger::DEBUG); |
| 14 | $body = trim(strtolower($body)); |
| 15 | $log->LogDebug("Body Text: ".$body); |
| 16 | $lastText = (int)$this->getLastText(); |
| 17 | $log->LogDebug("Last Text: ".(int)$this->getLastText()); |
| 18 | //error_log("Receive Body: ".$body); |
| 19 | // Above all else if customer ever sends us "STOP" we will immediately opt them out and stop sending promotional texts. |
| 20 | $stopCommandsArray = ["stop","stopall","unsubscribe","cancel","end","quit"]; |
| 21 | if(in_array($body, $stopCommandsArray)) { |
| 22 | $this->optOutCustomer(); |
| 23 | } else { |
| 24 | if($lastText == 0) { |
| 25 | if($body == "no") { |
| 26 | $this->optOutCustomer(); |
| 27 | } else { |
| 28 | $this->sendSMSByShortName("optIn"); |
| 29 | } |
| 30 | return; |
| 31 | //Last text was the opt in message, so we are looking for a YES or NO here |
| 32 | } else if($lastText == 1 || $lastText == 10) { |
| 33 | if($body == "yes") { |
| 34 | $this->optInCustomer(); |
| 35 | } else if($body == "no") { |
| 36 | $this->optOutCustomer(); |
| 37 | } else { |
| 38 | $this->sendSMSByShortName("wrongInputOptIn"); |
| 39 | } |
| 40 | return; |
| 41 | } |
| 42 | if($body == "balance") { |
| 43 | $log->LogDebug("We Have a Balance Request"); |
| 44 | //$this->sendBalanceSMS(); |
| 45 | return; |
| 46 | } |
| 47 | if($body == "help") { |
| 48 | $this->sendSMSByShortName("help"); |
| 49 | return; |
| 50 | } |
| 51 | $this->sendSMSByShortName("invalidInput"); |
| 52 | } |
| 53 | |
| 54 | } |
| 55 | public function sendSMS($messageID, $vars = null) { |
| 56 | $messageID = (int)$messageID; |
| 57 | if(!($messageID == 1 || $messageID == 10 || $messageID == 9 || $messageID == 5 || $messageID == 8) && $this->getOptValue() == 0) { |
| 58 | //error_log($messageID); |
| 59 | return false; |
| 60 | } |
| 61 | $message = new LoyaltyMessage($messageID); |
| 62 | |
| 63 | $store = new Store(); |
| 64 | $store->createStore($this->getOriginStore()); |
| 65 | |
| 66 | $messageBody = $this->filterMessage($message->getMessage(), $store->getTypeNum(), $vars); |
| 67 | |
| 68 | //error_log("Nexmo!: ".$this->getNexmo()); |
| 69 | if(1) { |
| 70 | $postfields = array( |
| 71 | 'api_key'=>'67e71b29', |
| 72 | 'api_secret'=>'47f48c40b1586e71', |
| 73 | 'from' => '91904', |
| 74 | 'text' => $messageBody, |
| 75 | 'to' => "+1".$this->phone |
| 76 | ); |
| 77 | error_log($messageBody); |
| 78 | //error_log($store->getTypeNum()); |
| 79 | error_log($this->phone); |
| 80 | |
| 81 | $ch = curl_init(); |
| 82 | curl_setopt($ch, CURLOPT_URL, 'https://rest.nexmo.com/sms/json'); |
| 83 | curl_setopt($ch, CURLOPT_POST, 1); |
| 84 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 85 | curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields)); |
| 86 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); // On dev server only! |
| 87 | //$result = curl_exec($ch); |
| 88 | error_log("Result: ". print_r( $result, true ) ); |
| 89 | //error_log("Error: ".curl_error($ch)); |
| 90 | curl_close($ch); |
| 91 | |
| 92 | if($messageID == 1 || $messageID == 10) { |
| 93 | error_log("Set Opt-Sent Date"); |
| 94 | $this->setOptSent(date("Y-m-d H:i:s")); |
| 95 | } |
| 96 | |
| 97 | //$this->addTextRecord($messageBody, $store->getTypeNum()); |
| 98 | if($messageID !== 8) { |
| 99 | $this->setLastText($messageID); |
| 100 | $this->setLastTextTimestamp(date("Y-m-d H:i:s")); |
| 101 | } |
| 102 | $client = new \Predis\Client($_ENV['REDIS_URL']); |
| 103 | $this->update(); |
| 104 | return true; |
| 105 | } else { |
| 106 | $AccountSid = "ACa0d8f40468057e6bc520846fba454f4c"; |
| 107 | $AuthToken = "af23ab5c293dbff72fe9308a701ad92e"; |
| 108 | |
| 109 | $message = new LoyaltyMessage($messageID); |
| 110 | $client = new \Twilio\Rest\Client($AccountSid, $AuthToken); |
| 111 | |
| 112 | error_log("Message ID: ".$messageID); |
| 113 | //If messageID is 1 or 10 we have an opt-in message |
| 114 | if($messageID == 1 || $messageID == 10) { |
| 115 | error_log("Set Opt-Sent Date"); |
| 116 | $this->setOptSent(date("Y-m-d H:i:s")); |
| 117 | } |
| 118 | try { |
| 119 | $client->messages->create( |
| 120 | // the number you'd like to send the message to |
| 121 | '+1'.$this->getPhone(), |
| 122 | array( |
| 123 | // A Twilio phone number you purchased at twilio.com/console |
| 124 | 'from' => $store->getTwilioPhone(), |
| 125 | // the body of the text message you'd like to send |
| 126 | 'body' => $messageBody |
| 127 | ) |
| 128 | ); |
| 129 | $this->addTextRecord($messageBody, $store->getTypeNum()); |
| 130 | |
| 131 | if($messageID !== 8) { |
| 132 | $this->setLastText($messageID); |
| 133 | $this->setLastTextTimestamp(date("Y-m-d H:i:s")); |
| 134 | } |
| 135 | $this->update(); |
| 136 | return true; |
| 137 | } catch (\Twilio\Exceptions\RestException $e) { |
| 138 | //error_log($e->getMessage()); |
| 139 | return false; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | public function optInCustomer() { |
| 144 | $date = date("Y-m-d H:i:s"); |
| 145 | $this->setOptValue(1); |
| 146 | $this->setOptReceived($date); |
| 147 | $this->update(); |
| 148 | $this->sendSMSByShortName("afterOptIn"); |
| 149 | |
| 150 | } |
| 151 | public function optOutCustomer() { |
| 152 | $date = date("Y-m-d H:i:s"); |
| 153 | $this->setOptValue(0); |
| 154 | $this->setOptReceived($date); |
| 155 | $this->update(); |
| 156 | $this->sendSMSByShortName("optedOut"); |
| 157 | } |
| 158 | public function checkBalance() { |
| 159 | |
| 160 | } |
| 161 | public function sendSMSByShortName($shortName, $vars = null) { |
| 162 | $message = new LoyaltyMessage(NULL, $shortName); |
| 163 | $messageID = $message->getMessageID(); |
| 164 | //error_log("messageID: ".$messageID); |
| 165 | if(isset($messageID)) { |
| 166 | if($this->sendSMS($messageID, $vars)) { |
| 167 | return true; |
| 168 | } |
| 169 | } |
| 170 | return false; |
| 171 | } |
| 172 | public function filterMessage($message, $origin, $vars = null) { |
| 173 | $store = new Store(); |
| 174 | $store->createStore($origin); |
| 175 | if(!isset($vars)) { |
| 176 | $vars = array ( |
| 177 | "%company%" => $store->getCompanyName(), |
| 178 | "%store%" => $store->getCity(), |
| 179 | "%store_phone" => formatPhoneNumber($store->getPhone()), |
| 180 | "%store_number%" => $store->getStoreNum(), |
| 181 | "%first_name%" => $this->getFirstName(), |
| 182 | "%last_name%" => $this->getLastName(), |
| 183 | "%birthday%" => $this->getBirthday(), |
| 184 | ); |
| 185 | } |
| 186 | |
| 187 | foreach ($vars as $key => $value) { |
| 188 | $message = str_replace($key, $value, $message); |
| 189 | } |
| 190 | //error_log($message); |
| 191 | return $message; |
| 192 | } |
| 193 | public function sendRedemptionSMS($points, $typeNum) { |
| 194 | $store = new Store(); |
| 195 | if(!$store->createStore($typeNum)) { |
| 196 | return false; |
| 197 | } |
| 198 | $loyaltyConfig = $store->getLoyaltyConfig(); |
| 199 | if($loyaltyConfig->type == 1) { |
| 200 | $discount = round($points / $loyaltyConfig->p2d,2, PHP_ROUND_HALF_DOWN); |
| 201 | $discount = "$".$discount." off"; |
| 202 | } else { |
| 203 | if($loyaltyConfig->percentOff > 0) { |
| 204 | $discount = $loyaltyConfig->percentOff."% off"; |
| 205 | } else { |
| 206 | $discount = "$".$loyaltyConfig->dollarsOff." off"; |
| 207 | } |
| 208 | } |
| 209 | $loyaltyData = new LoyaltyData($this->loyaltyID,$typeNum); |
| 210 | $rewardBalance = $loyaltyData->getRewardPoints(); |
| 211 | $vars = array( |
| 212 | "%company%" => $store->getCompanyName(), |
| 213 | "%store%" => $store->getCity(), |
| 214 | "%first_name%" => $this->getFirstName(), |
| 215 | "%points%" => $points, |
| 216 | "%discount%" => $discount, |
| 217 | "%balance%" => $rewardBalance['remainingPoints'] |
| 218 | ); |
| 219 | |
| 220 | $this->sendSMSByShortName("pointsRedeemed", $vars); |
| 221 | return true; |
| 222 | } |
| 223 | public function sendEarnedSMS($points, $sale, $typeNum) { |
| 224 | $store = new Store(); |
| 225 | if(!$store->createStore($typeNum)) { |
| 226 | return false; |
| 227 | } |
| 228 | $loyaltyData = new LoyaltyData($this->loyaltyID,$typeNum); |
| 229 | $rewardBalance = $loyaltyData->getRewardPoints(); |
| 230 | $vars = array( |
| 231 | "%company%" => $store->getCompanyName(), |
| 232 | "%store%" => $store->getCity(), |
| 233 | "%first_name%" => $this->getFirstName(), |
| 234 | "%points%" => $points, |
| 235 | "%balance%" => $rewardBalance['remainingPoints'], |
| 236 | "%discount_message%" => $rewardBalance['discountMessages']['nextReward'] |
| 237 | ); |
| 238 | $this->sendSMSByShortName("pointsEarned", $vars); |
| 239 | return true; |
| 240 | } |
| 241 | public function sendBalanceSMS($typeNum = null) { |
| 242 | if(!isset($typeNum)) { |
| 243 | $typeNum = $this->getOriginStore(); |
| 244 | } |
| 245 | $store = new Store(); |
| 246 | $store->createStore($typeNum); |
| 247 | $loyaltyData = new LoyaltyData($this->loyaltyID,$typeNum); |
| 248 | $rewardBalance = $loyaltyData->getRewardPoints(); |
| 249 | $vars = array( |
| 250 | "%company%" => $store->getCompanyName(), |
| 251 | "%store%" => $store->getCity(), |
| 252 | "%first_name%" => (strlen($this->getFirstName()) > 0 ? $this->getFirstName() : ""), |
| 253 | "%balance%" => $rewardBalance['remainingPoints'], |
| 254 | "%discount_message%" => $rewardBalance['discountMessages']['nextReward'] |
| 255 | ); |
| 256 | //error_log(print_r($vars, TRUE)); |
| 257 | $this->sendSMSByShortName("balance", $vars); |
| 258 | return true; |
| 259 | |
| 260 | } |
| 261 | public function addTextRecord($message, $typeNum = null) { |
| 262 | if(!isset($typeNum)) { |
| 263 | $typeNum = $this->getOriginStore(); |
| 264 | } |
| 265 | $store = new Store(); |
| 266 | $store->createStore($typeNum); |
| 267 | $db = dbConnectByName($store->getDbName()); |
| 268 | |
| 269 | $customerID = $this->getLoyaltyID(); |
| 270 | |
| 271 | try { |
| 272 | $stmt = $db->prepare("INSERT INTO texts (timestamp, customerID, type, message) VALUES (CURRENT_TIMESTAMP , :customerID, 1, :message)"); |
| 273 | $stmt->bindParam(":customerID", $customerID); |
| 274 | $stmt->bindParam(":message", $message); |
| 275 | if($stmt->execute()) { |
| 276 | return true; |
| 277 | } |
| 278 | return false; |
| 279 | } catch (\PDOException $e) { |
| 280 | return false; |
| 281 | } |
| 282 | } |
| 283 | } |