/**
  * @param $data array eg: [['mobile'=>'0912345678', 'smsContent'=>'恭喜中奖了'], ...]
  * @param $smsName string eg: 'cny_winners'
  * @param $smsRecord MongoId
  */
 public static function sendSms($data, $smsName, $smsRecordId, $accountId)
 {
     BulkSmsRecord::updateProcessById($smsRecordId, 1);
     // 正在發送
     try {
         if (!empty($data)) {
             foreach ($data as $sms) {
                 $mobile = self::processSmsMobile($accountId, $sms['mobile']);
                 $response = MessageUtil::sendMobileMessage($mobile, $sms['smsContent'], $accountId);
                 BulkSmsLog::createSmsLog($sms['mobile'], $sms['smsContent'], $response, $smsRecordId, $accountId);
                 if (!$response) {
                     LogUtil::error(['message' => '群發簡訊失敗', 'mobile' => $mobile, 'SMSContent' => $sms['smsContent']], 'bulkSms');
                     BulkSmsFailed::createSmsFailed($sms['mobile'], $sms['smsContent'], $smsRecordId, $accountId);
                 }
                 unset($response, $mobile);
             }
             BulkSmsRecord::updateProcessById($smsRecordId, 2);
             // 發送完成
         }
     } catch (\Exception $e) {
         LogUtil::error(['message' => 'EarlyBirdSms發送失敗', 'error' => $e], 'earlybird');
         BulkSmsRecord::updateProcessById($smsRecordId, 3);
         // 發送故障
         throw $e;
     }
 }
 public function perform()
 {
     $args = $this->args;
     if (empty($args['data']) || empty($args['accountId']) || empty($args['modelContent']) || empty($args['smsBatch'])) {
         ResqueUtil::log(['status' => 'fail to send sms', 'message' => 'missing params', 'args' => $args]);
         LogUtil::error(['message' => 'missing params in job', 'args' => $args], 'Sms');
     }
     $data = $args['data'];
     $accountId = $args['accountId'];
     $modelContent = $args['modelContent'];
     $smsBatch = $args['smsBatch'];
     $failureCount = 0;
     $successCount = 0;
     $totalCount = 0;
     try {
         if (!empty($data)) {
             foreach ($data as $sms) {
                 if ($sms['mobile'] != '') {
                     $response = MessageUtil::sendMobileMessage($sms['mobile'], $sms['content'], $accountId);
                     // $response = MessageUtil::sendMobileMessage($sms['mobile'], $sms['content']);
                     BulkSmsLog::createSmsLog($sms['mobile'], $sms['content'], $response, $smsBatch, $accountId);
                     if (!$response) {
                         $failureCount++;
                         LogUtil::error(['message' => '群發簡訊失敗', 'mobile' => $sms['mobile'], 'SMSContent' => $sms['content']], 'bulkSms');
                         BulkSmsFailed::createSmsFailed($sms['mobile'], $sms['content'], $smsBatch, $accountId);
                     } else {
                         $successCount++;
                         LogUtil::error(['message' => '群發簡訊成功', 'mobile' => $sms['mobile'], 'SMSContent' => $sms['content']], 'bulkSms');
                         BulkSmsSuccess::createSmsSuccess($sms['mobile'], $sms['content'], $smsBatch, $accountId);
                     }
                     unset($response);
                 } else {
                     LogUtil::error(date('Y-m-d h:i:s') . '号码为空.');
                     $failureCount++;
                 }
             }
             $totalCount = $successCount + $failureCount;
             //record result
             $SmsResultModel = new SmsResultModel();
             $SmsResultModel->successRecord = $successCount;
             $SmsResultModel->failureRecord = $failureCount;
             $SmsResultModel->totalRecord = $totalCount;
             $SmsResultModel->smsBatch = $smsBatch;
             $SmsResultModel->accountId = $accountId;
             $SmsResultModel->modelContent = $modelContent;
             $SmsResultModel->save();
         }
     } catch (\Exception $e) {
         LogUtil::error(['message' => 'Sms發送失敗', 'error' => $e], 'sms');
         throw $e;
     }
 }
 public function actionSendTestSms()
 {
     $params = $this->getQuery();
     if (empty($params) || empty($params['testSms'])) {
         throw new BadRequestHttpException('Failed! Mobile number or content is null.');
     }
     $response = MessageUtil::sendMobileMessage($params['testMobile'], $params['testSms'], $this->getAccountId());
     Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
     if ($response) {
         return ['code' => 200];
     } else {
         LogUtil::error(['message' => 'EarlyBird測試簡訊發送失敗', 'mobile' => $params['testMobile'], 'content' => $params['testSms']], 'earlybird');
         return ['code' => 1000];
     }
 }
 public function actionSendRedemptionMessage()
 {
     $params = $this->getParams();
     $name = MessageTemplate::getTemplateName($params['type']);
     $mobileParams = MessageTemplate::getMobileVar($params);
     $accountId = $this->getAccountId();
     $specialParams = MessageTemplate::getProductList($params);
     $template = MessageTemplate::getMobileTemplate($name, $accountId, $mobileParams, $specialParams);
     $params['memberId'] = new \MongoId($params['memberId']);
     $member = Member::getMemberInfo($params['memberId'], 'tel');
     if (!empty($member['tel']) && !empty($template)) {
         return MessageUtil::sendMobileMessage($member['tel'], $template);
     } else {
         return false;
     }
 }
 /**
  * when crate a staff successful,and send sms fail,we need to delete the staff
  */
 public function actionCreate()
 {
     $params = $this->getParams();
     if (empty($params['phone']) || empty($params['channel']['channelId']) || empty($params['badge']) || empty($params['storeId'])) {
         throw new BadRequestHttpException('params missing');
     }
     $accountId = $this->getAccountId();
     $params['accountId'] = $accountId;
     $existsEmpID = Staff::checkUnique($params['badge'], $accountId);
     if ($existsEmpID) {
         throw new InvalidParameterException(['badge' => Yii::t("store", "badge_exists")]);
     }
     $storeId = $params['storeId'];
     $params['storeId'] = new \MongoId($storeId);
     if (false === Staff::checkPhone($params['storeId'], $params['phone'])) {
         throw new InvalidParameterException(['phone' => Yii::t("store", 'phone_exists')]);
     }
     $data = Staff::setQrcodeParam($params['channel']['channelId']);
     $params = array_merge($params, $data);
     $params['salt'] = StringUtil::rndString(6, 1);
     $staff = new Staff();
     $staff->load($params, '');
     $result = 'success';
     if ($staff->save()) {
         if (!empty($params['useWebhook'])) {
             $eventData = ['type' => Webhook::EVENT_STAFF_CREATED, 'store_id' => $storeId, 'staff_id' => (string) $staff->_id, 'phone' => $params['phone'], 'badge' => $params['badge'], 'channel' => ['id' => $params['channel']['channelId'], 'name' => $params['channel']['channelName'], 'type' => $params['channel']['channelType']], 'origin' => Member::PORTAL, 'account_id' => (string) $accountId, 'created_at' => MongodbUtil::MongoDate2String($staff->createdAt, \DateTime::ATOM)];
             Yii::$app->webhook->triggerEvent($eventData);
         } else {
             //send mobile message
             $template = Staff::getMobileTemplate($accountId);
             $status = MessageUtil::sendMobileMessage($params['phone'], $template);
             if (false === $status) {
                 $result = 'fail';
                 //delete the staff
                 Staff::getCollection()->remove(['_id' => $staff->_id]);
                 LogUtil::error(['message' => 'Faild to send message', 'template' => $template, 'params' => $params], 'staff');
             }
         }
     } else {
         throw new ServerErrorHttpException(Yii::t('store', 'fail_to_create'));
     }
     return ['result' => $result];
 }
 /**
  * Send mobile captcha.
  *
  * <b>Request Type</b>: POST<br/><br/>
  * <b>Request Endpoint</b>:http://{server-domain}/api/mobile/send-captcha<br/><br/>
  * <b>Response Content-type</b>: application/json<br/><br/>
  * <b>Summary</b>: This api is used for send mobile captcha.
  * <br/><br/>
  *
  * <b>Request Params</b>:<br/>
  *     mobile: string, phone number<br/>
  *     unionId: string<br/>
  *     language: 'zh_cn' or 'en_us', This param is just for update mobile<br/>
  *     <br/><br/>
  *
  * <b>Response Params:</b><br/>
  *     message: OK or Fail
  *     data: string, if success, It is verification code<br/>
  *     <br/><br/>
  *
  * <br/><br/>
  *
  * <b>Response Example</b>:<br/>
  * <pre>
  * {
  *  "message": "OK",
  *  "data": "456787"
  * }
  * </pre>
  */
 public function actionSendCaptcha()
 {
     $params = $this->getParams();
     if (empty($params['type']) || empty($params['mobile']) || empty($params['codeId']) || empty($params['code'])) {
         throw new BadRequestHttpException('Missing params');
     }
     $type = $params['type'];
     $mobile = $params['mobile'];
     if (in_array($type, [self::CAPTCHA_TYPE_COMPANY_INFO, self::CAPTCHA_TYPE_EXCHANGE])) {
         $params['accountId'] = $this->getAccountId();
     } else {
         if (!in_array($type, [self::CAPTCHA_TYPE_BIND, self::CAPTCHA_TYPE_SIGNUP])) {
             throw new BadRequestHttpException('Invalid type');
         }
     }
     $this->attachBehavior('CaptchaBehavior', new CaptchaBehavior());
     $companyInfo = $this->{$type}($params);
     $company = $companyInfo['company'] === null ? self::DEFAULT_COMPANY : $companyInfo['company'];
     $accountId = $companyInfo['accountId'];
     //limit captcha send by ip
     $ip = Yii::$app->request->userIp;
     $captcha = Captcha::getByIP($ip);
     $now = time();
     if (!empty($captcha)) {
         $sendTimeInt = MongodbUtil::MongoDate2TimeStamp($captcha->createdAt);
         $nextTime = $sendTimeInt + Yii::$app->params['captcha_send_interval'];
         if ($nextTime > $now) {
             throw new InvalidParameterException(['phone' => Yii::t('common', 'send_too_frequently')]);
         } else {
             $captcha->isExpired = true;
             $captcha->save();
         }
     }
     //get random string, length = 6, charlist = '0123456789'
     $code = StringUtil::rndString(6, 0, '0123456789');
     $text = str_replace('#code#', $code, Yii::$app->params['mobile_message_text']);
     $text = str_replace('#company#', $company, $text);
     $captcha = new Captcha();
     $captcha->ip = $ip;
     $captcha->code = $code;
     $captcha->mobile = $mobile;
     $captcha->isExpired = false;
     $captcha->accountId = $accountId;
     if (MessageUtil::sendMobileMessage($mobile, $text) && $captcha->save()) {
         MessageUtil::recoreMessageCount('omni_record_message_' . $type);
         $result = ['message' => 'OK', 'data' => ''];
     } else {
         $result = ['message' => 'Error', 'data' => 'unknow error'];
     }
     return $result;
 }
 private static function _sendSms($members, $smsTag, $smsRecordId, $accountId)
 {
     try {
         if (!empty($members) && count($members) > 0) {
             for ($i = 0; $i < count($members); $i++) {
                 $member = $members[$i];
                 $sms = self::getSms($member, $smsTag, $accountId);
                 if (!empty($sms) && !empty($sms['mobile'])) {
                     $response = MessageUtil::sendMobileMessage($sms['mobile'], $sms['smsContent'], $accountId);
                     EarlyBirdSmsDetail::createSmsDetail($sms['mobile'], $sms['smsContent'], $response, $smsRecordId, $accountId);
                     if (!$response) {
                         LogUtil::error(['message' => 'EarlyBirdSms發送失敗', 'mobile' => $sms['mobile'], 'name' => $sms['name']], 'earlybird');
                         EarlyBirdSmsFailed::createSmsFailed($sms['mobile'], $sms['smsContent'], $smsRecordId, $accountId);
                     }
                     unset($sms, $member, $response);
                 }
             }
         }
         return true;
     } catch (\Exception $e) {
         LogUtil::error(['message' => 'EarlyBirdSms發送失敗', 'error' => $e], 'earlybird');
         EarlyBirdSmsRecord::updateProcessById($smsRecordId, 3);
         // 發送故障
         throw $e;
     }
 }
 public function actionCnyPromotion()
 {
     $params = $this->getParams('data', null);
     $accountId = new \MongoId($params['account_id']);
     if (empty($params) || empty($params['member_id']) || empty($params['type']) || empty($params['score']) || empty($accountId)) {
         throw new BadRequestHttpException("params are missing.");
     }
     if ($params['type'] != 'promotion_code_redeemed') {
         Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
         return ['message' => 'Not promotion_code_redeemed'];
     }
     //get memberInfo
     $member = Member::findByPk(new \MongoId($params['member_id']));
     $memberInfo = ['addScore' => $params['score'], 'id' => new \MongoId($params['member_id']), 'score' => $member->score];
     if (!empty($member->properties)) {
         foreach ($member->properties as $propertie) {
             if ($propertie['name'] == 'name') {
                 $memberInfo['name'] = $propertie['value'];
             }
             if ($propertie['name'] == 'tel') {
                 $memberInfo['mobile'] = $propertie['value'];
             }
         }
     }
     unset($member, $params);
     // get CNY Info
     $activity = Activity::findOne(['name' => 'cny', 'accountId' => $accountId]);
     if (empty($activity)) {
         throw new ServerErrorHttpException("Get CNY information failed or No CNY");
     }
     $needPoints = $activity->luckyDrawInfo['needPoints'];
     $drawDates = $activity->luckyDrawInfo['drawDate'];
     $conditionForOdds = ['member.id' => $memberInfo['id'], 'redeemTime' => ['$gte' => $activity->startDate, '$lte' => $activity->endDate]];
     unset($activity);
     // get day
     // sort($drawDates);
     // $currentDate = new \mongoDate();
     // $targetDate = null;
     // $day = 0;
     // foreach ($drawDates as $drawDate) {
     //     if ($drawDate > $currentDate) {
     //         $targetDate = $drawDate;
     //         break;
     //     }
     // }
     // if (!empty($targetDate)) {
     //     $offsetTime = MongodbUtil::MongoDate2msTimeStamp($targetDate) - MongodbUtil::MongoDate2msTimeStamp($currentDate);
     //     $day = ceil($offsetTime / (1000 * 60 * 60 * 24));
     //     unset($drawDates, $currentDate, $targetDate);
     // }
     // get odds
     $oddsCount = 0;
     $checkDouble = [];
     $canDouble = false;
     $redeemRecords = CampaignLog::find()->where($conditionForOdds)->all();
     if (!empty($redeemRecords)) {
         foreach ($redeemRecords as $redeemRecord) {
             $product = $redeemRecord['productName'];
             $oddsCount += $redeemRecord['member']['scoreAdded'];
             if (!$canDouble) {
                 if ($product == '2015 雞粉2.2kg' || $product == '2015 雞粉1.1kg' || $product == '2016 康寶雞粉 1.1KG' || $product == '2016 康寶雞粉 2.2KG') {
                     $checkDouble['chickenPowder'] = true;
                 }
                 if ($product == '2015 鮮雞汁' || $product == '2016 康寶濃縮鮮雞汁') {
                     $checkDouble['chickenJuice'] = true;
                 }
                 if ($product == '2015 鰹魚粉1kg' || $product == '2015 鰹魚粉1.5kg' || $product == '2016 康寶鰹魚粉 1KG' || $product == '2016 康寶鰹魚粉 1.5KG') {
                     $checkDouble['fishmeal'] = true;
                 }
                 if (count($checkDouble) == 3) {
                     $canDouble = true;
                 }
             }
             unset($product);
         }
     }
     $oddsCount = intval($oddsCount / $needPoints);
     if ($canDouble) {
         $oddsCount = $oddsCount * 2;
     }
     unset($checkDouble, $canDouble, $needPoints, $redeemRecords, $conditionForOdds);
     //$memberInfo:id,name,mobile,addScore,score; $day; $oddsCount
     $mobile = BulkSmsUtil::processSmsMobile($accountId, $memberInfo['mobile']);
     $smsContent = null;
     $currentDate = MongodbUtil::MongoDate2msTimeStamp(new \mongoDate());
     $topPrizeDate = MongodbUtil::MongoDate2msTimeStamp(new \MongoDate(strtotime("2016-02-29 00:00:00")));
     $offsetDay = ceil(($topPrizeDate - $currentDate) / (1000 * 60 * 60 * 24));
     if ($offsetDay > 10) {
         // 還沒倒計時
         $smsContent = $memberInfo['name'] . '您好,您郵寄的點數已入點完成,此次共入點' . $memberInfo['addScore'] . '點,您目前點數為' . $memberInfo['score'] . '點。恭喜您同時累積活動『年年好味不能沒有你』' . $oddsCount . '次抽獎機會,累積點數越多,中獎機會越大,詳細活動辦法請見http://bit.ly/1P4yZEA';
     } elseif ($offsetDay > 3 && $offsetDay < 11) {
         $smsContent = $memberInfo['name'] . '您好,您郵寄的點數已入點完成,此次共入點' . $memberInfo['addScore'] . '點,您目前點數為' . $memberInfo['score'] . '點。恭喜您同時累積活動『年年好味不能沒有你』' . $oddsCount . '次抽獎機會,距離30萬元紅包抽獎,只剩' . $offsetDay . '天,詳細活動辦法請見http://bit.ly/1P4yZEA';
     } elseif ($offsetDay >= 0 && $offsetDay <= 3) {
         $smsContent = $memberInfo['name'] . '您好,您郵寄的點數已入點完成,此次共入點' . $memberInfo['addScore'] . '點,您目前點數為' . $memberInfo['score'] . '點。恭喜您同時累積活動『年年好味不能沒有你』' . $oddsCount . '次抽獎機會,距離30萬元紅包抽獎,倒數' . abs($offsetDay) . '天,詳細活動辦法請見http://bit.ly/1P4yZEA';
     }
     Smslog::createSmsLog($mobile, $smsContent, 'CNY webhook SMS', 'sending', $accountId);
     MessageUtil::sendMobileMessage($mobile, $smsContent, $accountId);
     Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
     return ['mobile' => $mobile, 'smsContent' => $smsContent];
 }