Files
wticreatorstudio/Modules/PixelMe/Services/CommonService.php
T
2024-02-12 22:54:20 -05:00

279 lines
9.9 KiB
PHP

<?php
namespace Modules\PixelMe\Services;
use GuzzleHttp\Client;
use Modules\PixelMe\Entities\UserMeta;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Config;
class CommonService
{
protected $base_url = 'https://app.pixelme.me/api';
protected $bearer_token = ''; //'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6Im1hdEB3aGF0dG9vbHNpbnNpZGUuY29tIiwidXNlci1pZCI6IjY0MDIxYWMzMWNjM2NhMDAwODI2YzJlYSJ9.SPybzqUa9uH93L8kAsfzf2hLkSH0z8nTskXN_NQzX-4';
protected $account_id = '64021ac31cc3ca000826c2ed';
public function __construct()
{
$this->bearer_token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6Im1hdEB3aGF0dG9vbHNpbnNpZGUuY29tIiwidXNlci1pZCI6IjY0MDIxYWMzMWNjM2NhMDAwODI2YzJlYSJ9.SPybzqUa9uH93L8kAsfzf2hLkSH0z8nTskXN_NQzX-4'; //Config::get('PIXELME_TOKEN');
}
public function connectAmazon() {
Log::channel('pixelme')->info('Connecting to Amazon.');
$base_url = 'https://www.amazon.com/ap/oa';
$client_id = 'amzn1.application-oa2-client.9be562139317498d9aa657067331f22d';
$scope = 'advertising::campaign_management%20profile';
$response_type = 'code';
$redirect_uri = urlencode('https://wticreatorstudio.com/pixelme/amazon/auth-request');
return redirect("{$base_url}?client_id={$client_id}&scope={$scope}&response_type={$response_type}&redirect_uri={$redirect_uri}");
}
public function createWorkspace($name)
{
Log::channel('pixelme')->info('Creating workspace.');
$client = new Client();
$bearer_token = $this->bearer_token;
$account_id = $this->account_id;
$org_id = '64021ac31cc3ca000826c2ec';
Log::channel('pixelme')->info('Sending request to PixelMe');
$response = $client->request('POST', "{$this->base_url}/account?account_id={$account_id}", [
'headers' => [
'Accept' => 'application/json',
'Authorization' => "Bearer {$bearer_token}"
],
'json' => [
'org_id' => $org_id,
'name' => $name
]
]);
Log::channel('pixelme')->info('Request response status code: ' . $response->getStatusCode());
if ($response->getStatusCode() === 200) {
return json_decode($response->getBody(), true);
}
return null;
}
public function validateAmazonAuth($code, $amazon_marketplace_id)
{
Log::channel('pixelme')->info('Validating Amazon auth');
$client = new Client();
$bearer_token = $this->bearer_token;
$response = $client->request('POST', "{$this->base_url}/product-providers/amazon", [
'headers' => [
'Accept' => 'application/json',
'Authorization' => "Bearer {$bearer_token}"
],
'json' => [
'code' => $code,
'amazon_marketplace_id' => $amazon_marketplace_id,
'redirect_url' => urlencode('https://wticreatorstudio.com/pixelme/amazon/auth-request')
]
]);
Log::channel('pixelme')->info('Response status code: ' . $response->getStatusCode());
if ($response->getStatusCode() === 200) {
$body = json_decode($response->getBody(), true);
Log::channel('pixelme')->info('Response:');
Log::channel('pixelme')->info($body);
return $body;
}
return null;
}
public function listProfiles($account_id, $provider)
{
Log::channel('pixelme')->info('Sending list profiles request for account ID #' . $account_id);
$client = new Client();
$bearer_token = $this->bearer_token;
$response = $client->request('POST', "{$this->base_url}/product-providers/-/profiles?account_id={$account_id}", [
'headers' => [
'Accept' => 'application/json',
'Authorization' => "Bearer {$bearer_token}"
],
'json' => $provider
]);
Log::channel('pixelme')->info('Response status code: ' . $response->getStatusCode());
if ($response->getStatusCode() === 200) {
$body = json_decode($response->getBody(), true);
Log::channel('pixelme')->info('Response:');
Log::channel('pixelme')->info($body);
return $body;
}
return null;
}
public function createProductProvider($account_id, $provider)
{
Log::channel('pixelme')->info('Sending create product provider request for account ID #' . $account_id);
$client = new Client();
$bearer_token = $this->bearer_token;
$response = $client->request('POST', "{$this->base_url}/product-providers?account_id={$account_id}", [
'headers' => [
'Accept' => 'application/json',
'Authorization' => "Bearer {$bearer_token}"
],
'json' => $provider
]);
Log::channel('pixelme')->info('Response status code: ' . $response->getStatusCode());
if ($response->getStatusCode() === 200) {
$body = json_decode($response->getBody(), true);
Log::channel('pixelme')->info('Response:');
Log::channel('pixelme')->info($body);
return $body;
}
return null;
}
public function getProducts() {
$user_id = auth()->id();
$workspace_meta = UserMeta::where('user_id', $user_id)->where('name', 'workspace')->first();
if ($workspace_meta) {
$workspace = json_decode($workspace_meta->meta);
$client = new Client();
$bearer_token = $this->bearer_token;
$account_id = $workspace->account->id;
$response = $client->request('GET', "{$this->base_url}/product-providers/-/products", [
'headers' => [
'Accept' => 'application/json',
'Authorization' => "Bearer {$bearer_token}"
],
'query' => [
'account_id' => $account_id,
'from' => '2023-03-24T00:00:00.000Z',
'to' => '2023-05-23T03:06:20.016Z'
]
]);
if ($response->getStatusCode() === 200) {
return json_decode($response->getBody(), true);
}
}
return null;
}
public function getProviders() {
$user_id = auth()->id();
$workspace_meta = UserMeta::where('user_id', $user_id)->where('name', 'workspace')->first();
if ($workspace_meta) {
$workspace = json_decode($workspace_meta->meta);
$client = new Client();
$bearer_token = $this->bearer_token;
$account_id = $workspace->account->id;
$response = $client->request('GET', "{$this->base_url}/product-providers", [
'headers' => [
'Accept' => 'application/json',
'Authorization' => "Bearer {$bearer_token}"
],
'query' => [
'account_id' => $account_id
]
]);
if ($response->getStatusCode() === 200) {
return json_decode($response->getBody(), true);
}
}
return null;
}
public function getCampaigns($data) {
$user_id = auth()->id();
$workspace_meta = UserMeta::where('user_id', $user_id)->where('name', 'workspace')->first();
if ($workspace_meta) {
$workspace = json_decode($workspace_meta->meta);
$client = new Client();
$bearer_token = $this->bearer_token;
$account_id = $workspace->account->id;
$asin = $data['asin'];
$provider_id = $data['provider'];
$from = $data['from'];
$to = $data['to'];
$url = "{$this->base_url}/product-providers/{$provider_id}/products/{$asin}/campaigns";
$response = $client->request('GET', $url, [
'headers' => [
'Accept' => 'application/json',
'Authorization' => "Bearer {$bearer_token}"
],
'query' => [
'account_id' => $account_id,
'from' => $from,
'to' => $to
]
]);
if ($response->getStatusCode() === 200) {
return json_decode($response->getBody(), true);
}
}
return null;
}
public function getMetrics($data) {
$user_id = auth()->id();
$workspace_meta = UserMeta::where('user_id', $user_id)->where('name', 'workspace')->first();
if ($workspace_meta) {
$workspace = json_decode($workspace_meta->meta);
$client = new Client();
$bearer_token = $this->bearer_token;
$account_id = $workspace->account->id;
$asin = $data['asin'];
$provider_id = $data['provider'];
$from = $data['from'];
$to = $data['to'];
$url = "{$this->base_url}/product-providers/{$provider_id}/products/{$asin}/metrics";
$response = $client->request('GET', $url, [
'headers' => [
'Accept' => 'application/json',
'Authorization' => "Bearer {$bearer_token}"
],
'query' => [
'account_id' => $account_id,
'from' => $from,
'to' => $to
]
]);
if ($response->getStatusCode() === 200) {
return json_decode($response->getBody(), true);
}
}
return null;
}
}