Ejemplo n.º 1
0
 /**
  * Request a registration code from WhatsApp.
  *
  * @param Identity $identity
  * @param string   $method      Accepts only 'sms' or 'voice' as a value.
  * @param string   $carrier     Carrier name
  * @param string   $countryCode ISO Country Code, 2 Digit.
  * @param string   $langCode    ISO 639-1 Language Code: two-letter codes.
  *
  * @return object
  *                An object with server response.
  *                - status: Status of the request (sent/fail).
  *                - length: Registration code lenght.
  *                - method: Used method.
  *                - reason: Reason of the status (e.g. too_recent/missing_param/bad_param).
  *                - param: The missing_param/bad_param.
  *                - retry_after: Waiting time before requesting a new code.
  *
  * @throws RuntimeException
  */
 public function codeRequest(Identity $identity, $method = 'sms', $carrier = "T-Mobile5", $countryCode = null, $langCode = null)
 {
     $phone = $identity->getPhone();
     if ($countryCode == null && $phone->getIso3166() != '') {
         $countryCode = $phone->getIso3166();
     }
     if ($countryCode == null) {
         $countryCode = 'US';
     }
     if ($langCode == null && $phone->getIso639() != '') {
         $langCode = $phone->getIso639();
     }
     if ($langCode == null) {
         $langCode = 'en';
     }
     if (null !== $carrier) {
         $mnc = $this->detectMnc(strtolower($countryCode), $carrier);
     } else {
         $mnc = $phone->getMcc();
     }
     // Build the token.
     $token = $this->generateRequestToken($phone->getPhone());
     // Build the url.
     $host = 'https://' . Client::WHATSAPP_REQUEST_HOST;
     $query = ['in' => $phone->getPhone(), 'cc' => $phone->getCc(), 'id' => $identity->getIdentityToken(), 'lg' => $langCode, 'lc' => $countryCode, 'sim_mcc' => $phone->getMcc(), 'sim_mnc' => $mnc, 'method' => $method, 'token' => $token];
     $response = $this->getResponse($host, $query);
     if ($response['status'] != 'sent' && $response['status'] != 'ok') {
         if (isset($response['reason']) && $response['reason'] == "too_recent") {
             $minutes = round($response['retry_after'] / 60);
             throw new RuntimeException("Code already sent. Retry after {$minutes} minutes.");
         } else {
             throw new RuntimeException('There was a problem trying to request the code.');
         }
     }
     return $response;
 }