function create_cobot_user_for_email($email) { error_log("5.a.1. Trying to create Cobot user with email: " . $email); global $cobot_user_default_password; $url = 'https://www.cobot.me/api/users'; $util = new utilities(); $data = array('access_token' => $util->get_current_environment_cobot_access_token(), 'email' => $email, 'password' => $cobot_user_default_password); $result = $this->make_post_request($url, $data); $this->check_cobot_error($result); return $result; }
public function invoice_cobot_bookings() { error_log('In invoice and charge last 24 hours bookings'); $util = new utilities(); $access_token = $util->get_current_environment_cobot_access_token(); $to = date_create(); $from = date_add(date_create(), date_interval_create_from_date_string("-24 hours")); $from = date_format($from, 'Y-m-d H:i O'); $to = date_format($to, 'Y-m-d H:i O'); $query = $this->db->get("cobot_spaces"); $spaces = $query->result(); foreach ($spaces as $space) { $bookings_url = 'https://' . $space->id . '.cobot.me/api/bookings'; $bookings = $util->do_get($bookings_url, $params = array('access_token' => $access_token, 'from' => rawurlencode($from), 'to' => rawurlencode($to))); if (count($bookings) < 1) { echo " *** No bookings in the last 24 hours for space: " . $space->id . "\r\n"; error_log(" *** No bookings in the last 24 hours for space: " . $space->id); } else { error_log('Bookings for space: ' . $space->id . ' are: ' . json_encode($bookings)); } $memberships = array(); foreach ($bookings as $booking) { $booking = (array) $booking; $membership = $booking['membership']; if ($membership) { if (!isset($memberships[$membership->id])) { $memberships[$membership->id] = 1; } } } $membership_ids = array_keys($memberships); error_log('Memberships to be charged: ' . json_encode($membership_ids)); foreach ($membership_ids as $membership_id) { echo " *** Trying to generate invoice for membership id: " . $membership_id . " for space: " . $space->id . "\r\n"; error_log(" *** Trying to generate invoice for membership id: " . $membership_id . " for space: " . $space->id); $invoice_url = 'https://' . $space->id . '.cobot.me/api/memberships/' . $membership_id . '/charges_based_invoices'; $params = array(); $result = $util->do_post($invoice_url, $params, $access_token); if ($result && count($result) > 0 && !array_key_exists('error', $result)) { error_log('Invoice created with id: ' . $result['id'] . ' and number: ' . $result['invoice_number'] . ' and url: ' . $result['url'] . ' for membership id: ' . $membership_id); echo ' *** Invoice created with id: ' . $result['id'] . ' and number: ' . $result['invoice_number'] . ' and url: ' . $result['url'] . ' for membership id: ' . $membership_id . "\r\n"; $charge_url = 'https://' . $space->id . '.cobot.me/api/invoices/' . $result['invoice_number'] . '/charges'; $charge_result = $util->do_post($charge_url, array(), $access_token); echo " *** Charge made for invoice number: " . $result['invoice_number'] . "\r\n"; error_log(" *** Charge made for invoice number: " . $result['invoice_number']); } } } }
function do_get($url, $environment, $params = array()) { $util = new utilities(); $get_result = array(); $curl = curl_init(); $url = $url . '?access_token=' . $util->get_current_environment_cobot_access_token(); foreach ($params as $key => $value) { $url .= "&" . $key . "=" . $value; } error_log($url); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($curl); $result_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); if ($result_code >= 200 && $result_code < 300) { $get_result = (array) json_decode($result); } //error_log(json_encode($get_result)); return $get_result; }
function delete_webhook_subscription($subscription_url) { $util = new utilities(); $data = array('access_token' => $util->get_current_environment_cobot_access_token()); $curl = curl_init(); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE"); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_setopt($curl, CURLOPT_URL, $subscription_url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($curl); curl_close($curl); $result = (array) json_decode($result); error_log(json_encode($result)); $subdomain_start = strpos($subscription_url, '://') + 3; $subdomain_end = strpos($subscription_url, '.cobot.me/api/subscriptions/'); $subdomain = substr($subscription_url, $subdomain_start, $subdomain_end - $subdomain_start); $id_start = strpos($subscription_url, '.cobot.me/api/subscriptions/') + 28; $id = substr($subscription_url, $id_start); $sql = "DELETE FROM cobot_webhook_subscriptions WHERE space_id='" . $subdomain . "' and id='" . $id . "'"; error_log($sql); $this->db->query($sql); return $subscription_url; }
function add_rfid_tokens() { $util = new utilities(); $access_token = $util->get_current_environment_cobot_access_token(); $query = $this->db->get("cobot_spaces"); $spaces = $query->result(); foreach ($spaces as $space) { $token_url = 'https://' . $space->id . '.cobot.me/api/check_in_tokens'; $sql = "SELECT u.id as user_id, u.rfid as user_rfid, cm.id as membership_id FROM cobot_memberships cm join user u on cm.user_id = u.id where cm.space_id = '" . $space->id . "'"; error_log($sql); $query = $this->db->query($sql); $members = $query->result(); error_log(json_encode($members)); foreach ($members as $member) { $token = "test-token-" . "{$member->membership_id}"; if ($member->user_rfid) { $token = "{$member->user_rfid}"; } $params = array("membership_id" => "{$member->membership_id}", "token" => $token); $util->do_post($token_url, $params, $access_token); } } }
public function add_space_and_resources($space_id, $space = NULL) { $util = new utilities(); $main_area_resource_id = NULL; if ($space) { $main_area_resource_id = $space['main_area_id']; } $this->add_update_space($space); $resource_data = array(); $curl = curl_init(); $url = 'https://' . $space_id . '.cobot.me/api/resources'; $rdata = array('access_token' => $util->get_current_environment_cobot_access_token()); if ($rdata) { $url = sprintf("%s?%s", $url, http_build_query($rdata)); } curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($curl); $result_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); $cobot_resources = array(); if ($result_code == 200) { $result = (array) json_decode($result); foreach ($result as $resource) { $resource = (array) $resource; $resource_id = $resource['id']; if ($resource_id == $main_area_resource_id) { $sql = "UPDATE cobot_spaces SET capacity = " . $resource['capacity'] . ", rate = " . $resource['price_per_hour'] . " WHERE id = '" . $space_id . "'"; error_log($sql); $this->db->query($sql); } else { $this->add_update_resource($resource, $space_id); } } } }