mirror of
https://github.com/Bigherollc/wticreatorstudio.git
synced 2026-01-16 19:05:08 -05:00
227 lines
7.8 KiB
PHP
227 lines
7.8 KiB
PHP
<?php
|
|
|
|
namespace Modules\StudioPlus\Services;
|
|
|
|
use Illuminate\Http\Client\PendingRequest;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Modules\WTIJobBoard\Entities\JobRequestProductDetails;
|
|
|
|
class ProductBarcodeService
|
|
{
|
|
private $rapidAPIUrl;
|
|
private $rapidAPIKey;
|
|
private $rapidAPIHost;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->rapidAPIUrl = config('studioplus.rapid_api_url');
|
|
$this->rapidAPIKey = config('studioplus.rapid_api_key');
|
|
$this->rapidAPIHost = config('studioplus.rapid_api_host');
|
|
}
|
|
|
|
public function makeHttpRequest(): PendingRequest
|
|
{
|
|
return Http::withHeaders([
|
|
'X-RapidAPI-Key' => $this->rapidAPIKey,
|
|
'X-RapidAPI-Host' => $this->rapidAPIHost
|
|
])
|
|
->baseUrl($this->rapidAPIUrl);
|
|
}
|
|
|
|
/*
|
|
AVAILABLE OPTIONS
|
|
- COUNTRY - String - default by US
|
|
- FILTER_BY_STAR - STRING - empty string or 1 to 5
|
|
- '' - return all reviews(default)
|
|
- 1 - return only 1 star reviews
|
|
- 2 - return only 2 star reviews
|
|
- 3 - return only 3 star reviews
|
|
- 4 - return only 4 star reviews
|
|
- 5 - return only 5 star reviews
|
|
- VARIANTS - If a product has multiple product variants, then by default you will receive reviews related to all product variants. If you need reviews only for specified ASIN then set it to 0
|
|
- 1 - reviews for all product variants (default)
|
|
- 0 - reviews only for specified product ASIN
|
|
- TOP - By default reviews will be sorted by \\"Most Recent\\" reviews, if you need to sort them by \\"Top Reviews\\" then set this value to 1
|
|
- PAGE - Number
|
|
*/
|
|
public function getASINReviews(string $asin, array $options = ['country' => 'US']): array
|
|
{
|
|
$newOptions = collect($options)->merge([
|
|
'asin' => $asin,
|
|
])
|
|
->toArray();
|
|
|
|
$response = $this->makeHttpRequest()
|
|
->get('/product/reviews', $newOptions);
|
|
|
|
if ($response->status() !== 200) {
|
|
return [];
|
|
}
|
|
|
|
return (new HttpRequestResponseService)->json($response);
|
|
}
|
|
|
|
public function getASIN(string $asin, array $options = ['country' => 'US']): array
|
|
{
|
|
$newOptions = collect($options)->merge([
|
|
'asin' => $asin,
|
|
])
|
|
->toArray();
|
|
|
|
$response = $this->makeHttpRequest()
|
|
->get('/product/details', $newOptions);
|
|
|
|
if ($response->status() !== 200) {
|
|
return [];
|
|
}
|
|
|
|
return (new HttpRequestResponseService)->json($response);
|
|
}
|
|
|
|
public function getProductDetails($barcode)
|
|
{
|
|
//lets search on product details if exist so dont we dont use all 5000 api credits
|
|
$product = JobRequestProductDetails::where('asin', $barcode)->first();
|
|
|
|
if ($product) {
|
|
/*return [
|
|
'barcode_number' => $product->asin,
|
|
'name' => $product->name,
|
|
'store' => $product->store,
|
|
'price' => $product->price,
|
|
'brand' => $product->brand,
|
|
'category' => [
|
|
'name' => $product->category
|
|
],
|
|
'total_reviews' => $product->reviews,
|
|
'image' => [
|
|
'link' => $product->image
|
|
]
|
|
];*/
|
|
|
|
return [
|
|
'asin' => $product->asin,
|
|
'name' => $product->name,
|
|
'price' => $product->price,
|
|
'brand' => $product->brand,
|
|
'category' => $product->category,
|
|
'category' => [
|
|
'name' => $product->category
|
|
],
|
|
'amazon_total_reviews' => $product->reviews,
|
|
'image' => $product->image,
|
|
'image' => [
|
|
'link' => $product->image
|
|
],
|
|
];
|
|
}
|
|
else {
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, 'https://api.barcodelookup.com/v3/products?barcode='.$barcode.'&formatted=y&key='.config('wtijobboard.barcodelookup_api'));
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
$response = json_decode(curl_exec($ch));
|
|
$err = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
if ($err) {
|
|
return false;
|
|
}
|
|
else {
|
|
if ($response == null || !isset($response->products[0])) {
|
|
return false;
|
|
}
|
|
|
|
$product = $response->products[0];
|
|
$product_name = null;
|
|
$store = null;
|
|
$price = null;
|
|
$brand = null;
|
|
$category = null;
|
|
$categories = null;
|
|
$total_ratings = null;
|
|
$image = null;
|
|
|
|
if (isset($product->title)) {
|
|
$product_name = $product->title;
|
|
}
|
|
|
|
if (isset($product->stores) && count($product->stores) > 0) {
|
|
$stores = collect($product->stores);
|
|
$latest_store = $stores->sortByDesc('last_update')->first();
|
|
$store = $latest_store->name;
|
|
$price = $latest_store->price;
|
|
}
|
|
|
|
if (isset($product->brand)) {
|
|
$brand = $product->brand;
|
|
}
|
|
|
|
if($brand == null && isset($product->manufacturer)){
|
|
$brand = $product->manufacturer;
|
|
}
|
|
|
|
if (isset($product->category) && $product->category != null) {
|
|
$categories = explode(" > ",$product->category);
|
|
if (count($categories) > 0) {
|
|
$category['name'] = end($categories);
|
|
$category['user_id'] = 1;
|
|
|
|
$category = \Modules\StudioPlus\Entities\StudioPlusCategory::firstOrCreate(
|
|
[
|
|
'name' => $category['name']
|
|
],
|
|
$category
|
|
);
|
|
}
|
|
}
|
|
|
|
if (isset($product->images[0])) {
|
|
$image = $product->images[0];
|
|
}
|
|
|
|
/*return [
|
|
'barcode_number' => $product->barcode_number,
|
|
'name' => $product_name,
|
|
'store' => $store,
|
|
'price' => $price,
|
|
'brand' => $brand,
|
|
'category' => $category,
|
|
'total_reviews' => count($product->reviews),
|
|
'image' => [
|
|
'link' => $image
|
|
]
|
|
];*/
|
|
|
|
return [
|
|
'asin' => $product->barcode_number,
|
|
'name' => $product_name,
|
|
'price' => $price,
|
|
'brand' => $brand,
|
|
'category' => $category,
|
|
'category' => [
|
|
'name' => $category
|
|
],
|
|
'amazon_total_reviews' => count($product->reviews),
|
|
'image' => $image,
|
|
'image' => [
|
|
'link' => $image
|
|
],
|
|
];
|
|
}
|
|
}
|
|
|
|
/*return [
|
|
'asin' => $asin,
|
|
'name' => $product_name,
|
|
'price' => $price,
|
|
'brand' => $brand,
|
|
'category' => $category,
|
|
'categories' => $categories,
|
|
'amazon_total_reviews' => $total_ratings,
|
|
'exists' => $existing,
|
|
'image' => $image,
|
|
//'product' => $product
|
|
];*/
|
|
}
|
|
}
|