You are here

public function PoptinController::poptinSignup in Website Popups, Email Popup, Exit-Intent Popup, and Contact Forms – Poptin 8

Implements poptinSignup().

1 string reference to 'PoptinController::poptinSignup'
poptin.routing.yml in ./poptin.routing.yml
poptin.routing.yml

File

src/Controller/PoptinController.php, line 164

Class

PoptinController
Provides route responses for the Poptin module.

Namespace

Drupal\poptin\Controller

Code

public function poptinSignup(Request $request) {
  $token = $request->request
    ->get('csrftoken');
  if (!$this
    ->poptinCheckToken($token)) {
    $tmp_arr = [
      'mssg' => "Invalid csrf token",
      'status' => 0,
      'token' => $this
        ->poptinGetToken(),
    ];
    die(Json::encode($tmp_arr));
  }
  $email_id = $request->request
    ->get('email');
  $api_url = "https://app.popt.in/api/marketplace/";
  $url = $api_url . "register";
  $dataurl = "email=" . $email_id . "&marketplace=drupal8";
  $curl = curl_init();
  curl_setopt_array($curl, [
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => $dataurl,
    CURLOPT_HTTPHEADER => [
      "cache-control: no-cache",
      "content-type: application/x-www-form-urlencoded",
      "postman-token: 16ba048a-499c-06c8-517c-cea2abb11945",
    ],
  ]);
  $response = curl_exec($curl);
  $err = curl_error($curl);
  if ($err) {
    echo "cURL Error #:" . $err;
  }
  else {
    $result = json_decode($response);
    if (!empty($result) && $result->success == 1) {
      $tmp_arr = [
        'user_id' => $result->user_id,
        'client_id' => $result->client_id,
        'token' => $result->token,
      ];
      \Drupal::database()
        ->insert('poptin')
        ->fields($tmp_arr)
        ->execute();
      $return_mssg = [
        'mssg' => "User Registered successfully",
        'status' => 1,
        'token' => $this
          ->poptinGetToken(),
      ];
    }
    else {
      $return_mssg = [
        'mssg' => $result->message,
        'status' => 0,
        'token' => $this
          ->poptinGetToken(),
      ];
    }
  }
  echo Json::encode($return_mssg);
  die;
}