import_CSV($file_path); $create_count = 0; foreach ($api_products as $key => $api_product) { // return $api_product['operator_reference']; $locate_operator = Operator::where('type', 'dtone')->where('title', $api_product['operator_reference'])->get()->first(); if ($locate_operator) { $locate_product = Product::where('category', 'webmartbd')->where('vivape_id', ('VPPRWM' . $api_product['id']))->get()->first(); if (!$locate_product) { $create_product = Product::create([ 'vivape_id' => 'VPPRWM' . ($api_product['id'] ?? ($key + 1)), 'sku' => $api_product['id'] ?? ($key + 1), 'routes' => $api_product['routes'] ?? '', 'priority_routes' => $api_product['priority_routes'] ?? '', 'hold_routes' => $api_product['hold_routes'] ?? '', 'cost' => $api_product['average_cost'] ?? ($key + 1), 'price' => $api_product['max_amount_eur'] ?? ($key + 1), 'price_min' => $api_product['min_amount_eur'] ?? ($key + 1) ?? ($key + 1), 'price_max' => $api_product['max_amount_eur'] ?? ($key + 1) ?? ($key + 1), 'price_index' => $product_index_webmart, 'price_index_min' => $product_index_min_webmart, 'price_index_max' => $product_index_max_webmart, 'profit_index' => $api_product['margin_percentage'] / 100, 'profit_index_min' => $api_product['margin_percentage'] / 100, 'profit_index_max' => $api_product['margin_percentage'] / 100, 'commission_amount' => 0, 'commission_index' => 0.2, 'commission_index_min' => 0.1, 'commission_index_max' => 0.3, 'commission_type' => 'percentage', 'discount_amount' => 0, 'discount_index' => 0, 'discount_type' => 0, 'discount_media' => $api_product['discount_media'] ?? ($key + 1), 'discount_code' => $api_product['discount_code'] ?? ($key + 1), 'starts_from' => '2023-01-01', 'ends_on' => '2024-12-31', // 'inactivity_starts' => '', // 'inactivity_ends' => '', 'date_of_joining' => date('Y-m-d'), 'user_id' => Auth::user()->id, 'operator_id' => $locate_operator->id, 'api_provider_id' => $api_product['api_provider_id'] ?? ($key + 1), 'country_id' => $api_product['country_id'], 'state_id' => $api_product['state_id'] ?? ($key + 1), 'city_id' => $api_product['city_id'] ?? ($key + 1), 'area_id' => $api_product['area_id'] ?? ($key + 1), 'address' => $api_product['address'] ?? ($key + 1), 'identity' => $api_product['identity'] ?? ($key + 1), 'identity_type' => $api_product['identity_type'] ?? ($key + 1), 'identity_attachment' => $api_product['identity_attachment'] ?? ($key + 1), 'parent_type_id' => $api_product['parent_type_id'] ?? ($key + 1), 'parent_category_id' => $api_product['parent_category_id'] ?? ($key + 1), 'parent_status_id' => $api_product['parent_status_id'] ?? ($key + 1), 'referance' => $api_product['denomination_type'] ?? 'reference', 'code' => $api_product['id_new'] ?? ($key + 1), 'title' => $api_product['package_title'], 'name' => $api_product['package_title'], 'level' => $api_product['level'] ?? ($key + 1), 'index' => $api_product['index'] ?? ($key + 1), 'index_min' => $api_product['index_min'] ?? ($key + 1), 'index_max' => $api_product['index_max'] ?? ($key + 1), 'status' => $api_product['status'] ?? 'Active', 'type' => $api_product['type'] ?? ($key + 1), 'category' => $api_product['category'] ?? 'webmartbd', 'ranking' => $api_product['min_amount'] ?? ($key + 1), 'priority' => $api_product['max_amount'] ?? ($key + 1), 'details' => json_encode($api_product), 'note' => 'BDT', 'description' => json_encode($api_product), 'paremeters' => $api_product['max_amount'] ?? ($key + 1), 'attachment' => $api_product['tags'] ?? ($key + 1), 'image' => '', 'created_by' => Auth::user()->id ?? ($key + 1), 'updated_by' => Auth::user()->id ?? ($key + 1), ]); $create_count++; } } } return $create_count . ' Webmart products fetched successfully'; } // //import CSV function // function import_CSV($filename, $delimiter = ',') // { // if (!file_exists($filename) || !is_readable($filename)) // return false; // $header = null; // $data = array(); // if (($handle = fopen($filename, 'r')) !== false) { // while (($row = fgetcsv($handle, 1000, $delimiter)) !== false) { // if (!$header) // $header = $row; // else // $data[] = array_combine($header, $row); // } // fclose($handle); // } // return $data; // } public function execute_request($raw_phone, $amount, $unique_id) { session_start(); $token = bin2hex(openssl_random_pseudo_bytes(16)); $_SESSION['token'] = $token; $processed_phone = self::analyze_phone_number($raw_phone); $json = [ "method" => "execute_request", "tag_name" => $processed_phone["tag_name"] . '', "opt_id" => $processed_phone["opt_id"] . '', "number" => $processed_phone["processed_phone"] . '', "prefix" => $processed_phone["prefix"] . '', "amount" => $amount . '', "type_id" => "1", "uniq_id" => $unique_id, "api_pass" => $this->api_pass, "api_string" => $this->api_string, ]; // $url = "http://webmartbd.uk/api/communication/public_api.php"; // Use Laravel HTTP client for the request $response = Http::withOptions(['verify' => false])->post($this->url, $json); if ($response->successful()) { $obj = $response->json(); if (trim($obj['status']) == "Success") { // Handle success return $obj['status']; } else { echo $obj['reason']; } WebmartTransaction::create([ 'type' => 'request', 'category' => 'web', 'api_provider' => 'webmart', 'status' => $obj['status'], 'parameters' => json_encode($json), 'note' => 'from server', 'request' => json_encode($json), 'response' => json_encode($obj), 'created_by' => Auth::user()->id, 'updated_by' => Auth::user()->id, ]); } else { // Handle HTTP request failure echo $response->body(); } return $response; } public function execute_request_un($raw_phone, $amount, $unique_id) { // session_start(); // $token = bin2hex(openssl_random_pseudo_bytes(16)); // $_SESSION['token'] = $token; $processed_phone = self::analyze_phone_number($raw_phone); $json = [ "method" => "execute_request", "tag_name" => $processed_phone["tag_name"] . '', "opt_id" => $processed_phone["opt_id"] . '', "number" => $processed_phone["processed_phone"] . '', "prefix" => $processed_phone["prefix"] . '', "amount" => $amount . '', "type_id" => "1", "uniq_id" => 'VP' . $unique_id, "api_pass" => $this->api_pass, "api_string" => $this->api_string, ]; // $json = json_encode($json); return view('webmartbd',compact('json','processed_phone')); $url = "http://webmartbd.uk/api/communication/public_api.php"; $curl = curl_init($url); curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $json); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); $rsl_api = curl_exec($curl); if (curl_errno($curl)) { return curl_error($curl); } curl_close($curl); if ($rsl_api != null) { $obj = json_decode($rsl_api); if (trim($obj->{'status'}) == "Success") { return $obj->{'status'}; } else { return $obj->{'reason'}; } } } public function analyze_phone_number($phone) { $phone_without_country_code = self::removeCountryCode($phone); $first_3_chars = substr($phone_without_country_code, 0, 3); $operator_id = 0; $operator_name = ''; $number_prefix = $first_3_chars; if ($first_3_chars == '017') { $operator_id = 1; $operator_name = 'grameen'; } elseif ($first_3_chars == '013') { $operator_id = 1; $operator_name = 'grameen'; } elseif ($first_3_chars == '019') { $operator_id = 2; $operator_name = 'banglalink'; } elseif ($first_3_chars == '014') { $operator_id = 2; $operator_name = 'banglalink'; } elseif ($first_3_chars == '018') { $operator_id = 3; $operator_name = 'robi'; } elseif ($first_3_chars == '016') { $operator_id = 4; $operator_name = 'airtel'; } elseif ($first_3_chars == '015') { $operator_id = 5; $operator_name = 'teletalk'; } $returnData = []; $returnData['tag_name'] = $operator_name; $returnData['opt_id'] = $operator_id; $returnData['prefix'] = $number_prefix; $returnData['processed_phone'] = $phone_without_country_code; return $returnData; } public function removeCountryCode($phone) { // Check if the country code is present if (strpos($phone, "+88") === 0) { // Remove the country code $phone_without_country_code = substr($phone, 3); return $phone_without_country_code; } else { // Country code not found, return the original phone number return $phone; } } }