You are here

public function MiniorangeOAuthClientCustomer::validateOtp in OAuth2 Login 8

Validate OTP.

File

src/MiniorangeOAuthClientCustomer.php, line 253
Contains miniOrange Customer class.

Class

MiniorangeOAuthClientCustomer

Namespace

Drupal\oauth2_login

Code

public function validateOtp($transaction_id) {
  if (!Utilities::isCurlInstalled()) {
    return json_encode(array(
      "status" => 'CURL_ERROR',
      "statusMessage" => '<a href="http://php.net/manual/en/curl.installation.php">PHP cURL extension</a> is not installed or disabled.',
    ));
  }
  $url = MiniorangeOAuthClientConstants::BASE_URL . '/moas/api/auth/validate';
  $ch = \curl_init($url);
  $customer_key = $this->defaultCustomerId;
  $api_key = $this->defaultCustomerApiKey;
  $current_time_in_millis = round(microtime(TRUE) * 1000);
  $string_to_hash = $customer_key . $current_time_in_millis . $api_key;
  $hash_value = hash("sha512", $string_to_hash);
  $customer_key_header = "Customer-Key: " . $customer_key;
  $timestamp_header = "Timestamp: " . $current_time_in_millis;
  $authorization_header = "Authorization: " . $hash_value;
  $fields = array(
    'txId' => $transaction_id,
    'token' => $this->otpToken,
  );
  $field_string = json_encode($fields);
  \curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  \curl_setopt($ch, CURLOPT_ENCODING, "");
  \curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  \curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
  \curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  \curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
  \curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    $customer_key_header,
    $timestamp_header,
    $authorization_header,
  ));
  \curl_setopt($ch, CURLOPT_POST, TRUE);
  \curl_setopt($ch, CURLOPT_POSTFIELDS, $field_string);
  $content = curl_exec($ch);
  if (\curl_errno($ch)) {
    $error = array(
      '%method' => 'validateOtp',
      '%file' => 'customer_setup.php',
      '%error' => curl_error($ch),
    );
    \Drupal::logger('oauth2_login')
      ->notice($error);
  }
  \curl_close($ch);
  return $content;
}