init
This commit is contained in:
232 files changed
+25416
No files matched your search
@@ -0,0 +1,233 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\CafeBazaar;
|
||||
|
||||
use App\Models\BazaarApiRequest;
|
||||
use App\Models\MobileApp;
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Http\Client\PendingRequest;
|
||||
use Illuminate\Http\Client\Response;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Str;
|
||||
use InvalidArgumentException;
|
||||
use Throwable;
|
||||
|
||||
class CafeBazaarClient
|
||||
{
|
||||
private const BILLING_BASE_URL = 'https://pardakht.cafebazaar.ir/devapi/v2/api';
|
||||
|
||||
private const PISHKHAN_BASE_URL = 'https://api.pishkhan.cafebazaar.ir/v1/apps/releases';
|
||||
|
||||
private const COMMENTS_BASE_URL = 'https://sandoghche.cafebazaar.ir/api/v0/reviews';
|
||||
|
||||
private const ACCOUNT_BASE_URL = 'https://account.cafebazaar.ir/api/v0';
|
||||
|
||||
public function validatePurchase(MobileApp $app, string $productId, string $purchaseToken): BazaarResponse
|
||||
{
|
||||
return $this->send($app, 'purchase.validate', 'GET', self::BILLING_BASE_URL.'/validate/'.$this->path($app->package_name).'/inapp/'.$this->path($productId).'/purchases/'.$this->path($purchaseToken).'/', request: $this->bazaarApiRequest($app));
|
||||
}
|
||||
|
||||
public function consumePurchase(MobileApp $app, string $purchaseToken): BazaarResponse
|
||||
{
|
||||
return $this->send($app, 'purchase.consume', 'POST', self::BILLING_BASE_URL.'/consume/'.$this->path($app->package_name).'/purchases/', ['token' => $purchaseToken], $this->bazaarApiRequest($app));
|
||||
}
|
||||
|
||||
public function refundPurchase(MobileApp $app, string $purchaseToken): BazaarResponse
|
||||
{
|
||||
return $this->send($app, 'purchase.refund', 'POST', self::BILLING_BASE_URL.'/refund/'.$this->path($app->package_name).'/purchases/'.$this->path($purchaseToken).'/', request: $this->bazaarApiRequest($app));
|
||||
}
|
||||
|
||||
public function subscription(MobileApp $app, string $subscriptionId, string $purchaseToken): BazaarResponse
|
||||
{
|
||||
return $this->send($app, 'subscription.show', 'GET', self::BILLING_BASE_URL.'/applications/'.$this->path($app->package_name).'/subscriptions/'.$this->path($subscriptionId).'/purchases/'.$this->path($purchaseToken).'/', request: $this->bazaarApiRequest($app));
|
||||
}
|
||||
|
||||
public function activeSubscriptions(MobileApp $app, string $purchaseToken): BazaarResponse
|
||||
{
|
||||
return $this->send($app, 'subscription.active', 'GET', self::BILLING_BASE_URL.'/applications/'.$this->path($app->package_name).'/active-subscriptions/'.$this->path($purchaseToken).'/', request: $this->bazaarApiRequest($app));
|
||||
}
|
||||
|
||||
public function cancelSubscription(MobileApp $app, string $subscriptionId, string $purchaseToken): BazaarResponse
|
||||
{
|
||||
return $this->send($app, 'subscription.cancel', 'POST', self::BILLING_BASE_URL.'/applications/'.$this->path($app->package_name).'/subscriptions/'.$this->path($subscriptionId).'/purchases/'.$this->path($purchaseToken).'/cancel/', request: $this->bazaarApiRequest($app));
|
||||
}
|
||||
|
||||
public function exchangeLoginCode(MobileApp $app, string $code): BazaarResponse
|
||||
{
|
||||
return $this->send($app, 'login.exchange', 'POST', self::ACCOUNT_BASE_URL.'/tokens', [
|
||||
'grant_type' => 'authorization_code',
|
||||
'code' => $code,
|
||||
'client_id' => $app->bazaar_login_client_id,
|
||||
'client_secret' => $app->bazaar_login_client_secret,
|
||||
], $this->request()->asForm());
|
||||
}
|
||||
|
||||
public function refreshLoginToken(MobileApp $app, string $refreshToken): BazaarResponse
|
||||
{
|
||||
return $this->send($app, 'login.refresh', 'POST', self::ACCOUNT_BASE_URL.'/tokens/refresh', [
|
||||
'grant_type' => 'refresh_token',
|
||||
'refresh_token' => $refreshToken,
|
||||
], $this->request()->asForm());
|
||||
}
|
||||
|
||||
public function userInfo(MobileApp $app, string $accessToken, string $tokenType = 'Bearer'): BazaarResponse
|
||||
{
|
||||
return $this->send($app, 'login.userinfo', 'GET', self::ACCOUNT_BASE_URL.'/userinfo', request: $this->request()->withToken($accessToken, $tokenType));
|
||||
}
|
||||
|
||||
public function reviews(MobileApp $app, array $filters): BazaarResponse
|
||||
{
|
||||
return $this->send($app, 'reviews.list', 'GET', self::COMMENTS_BASE_URL.'/', $filters, $this->commentsRequest($app));
|
||||
}
|
||||
|
||||
public function replyToReview(MobileApp $app, int $reviewId, array $reply): BazaarResponse
|
||||
{
|
||||
return $this->send($app, 'reviews.reply', 'POST', self::COMMENTS_BASE_URL.'/'.$reviewId.'/', $reply, $this->commentsRequest($app));
|
||||
}
|
||||
|
||||
public function createRelease(MobileApp $app): BazaarResponse
|
||||
{
|
||||
return $this->pishkhan($app, 'release.create', 'POST', '/');
|
||||
}
|
||||
|
||||
public function deleteRelease(MobileApp $app): BazaarResponse
|
||||
{
|
||||
return $this->pishkhan($app, 'release.delete', 'DELETE', '/');
|
||||
}
|
||||
|
||||
public function uploadApk(MobileApp $app, UploadedFile $apk, string $architecture): BazaarResponse
|
||||
{
|
||||
$request = $this->pishkhanRequest($app)
|
||||
->attach('apk', $apk->getContent(), $apk->hashName(), ['Content-Type' => 'application/vnd.android.package-archive']);
|
||||
|
||||
return $this->send($app, 'release.upload_apk', 'POST', self::PISHKHAN_BASE_URL.'/upload/', ['architecture' => $architecture], $request);
|
||||
}
|
||||
|
||||
public function uploadAab(MobileApp $app, UploadedFile $aab): BazaarResponse
|
||||
{
|
||||
$request = $this->pishkhanRequest($app)->attach('aab', $aab->getContent(), $aab->hashName());
|
||||
|
||||
return $this->send($app, 'release.upload_aab', 'POST', self::PISHKHAN_BASE_URL.'/upload-aab/', request: $request);
|
||||
}
|
||||
|
||||
public function uploadBin(MobileApp $app, UploadedFile $bin): BazaarResponse
|
||||
{
|
||||
$request = $this->pishkhanRequest($app)->attach('file', $bin->getContent(), $bin->hashName());
|
||||
|
||||
return $this->send($app, 'release.upload_bin', 'POST', self::PISHKHAN_BASE_URL.'/upload-bin/', request: $request);
|
||||
}
|
||||
|
||||
public function bundleStatus(MobileApp $app): BazaarResponse
|
||||
{
|
||||
return $this->pishkhan($app, 'release.bundle_status', 'GET', '/bundle-status/');
|
||||
}
|
||||
|
||||
public function commitRelease(MobileApp $app, array $data): BazaarResponse
|
||||
{
|
||||
return $this->pishkhan($app, 'release.commit', 'POST', '/commit/', $data);
|
||||
}
|
||||
|
||||
public function lastRelease(MobileApp $app, string $state): BazaarResponse
|
||||
{
|
||||
return $this->pishkhan($app, 'release.last_'.$state, 'GET', '/last-'.$state.'/');
|
||||
}
|
||||
|
||||
private function pishkhan(MobileApp $app, string $operation, string $method, string $path, array $data = []): BazaarResponse
|
||||
{
|
||||
return $this->send($app, $operation, $method, self::PISHKHAN_BASE_URL.$path, $data, $this->pishkhanRequest($app));
|
||||
}
|
||||
|
||||
private function bazaarApiRequest(MobileApp $app): PendingRequest
|
||||
{
|
||||
return $this->request()->withHeaders(['CAFEBAZAAR-PISHKHAN-API-SECRET' => $app->bazaar_api_secret]);
|
||||
}
|
||||
|
||||
private function pishkhanRequest(MobileApp $app): PendingRequest
|
||||
{
|
||||
return $this->bazaarApiRequest($app);
|
||||
}
|
||||
|
||||
private function commentsRequest(MobileApp $app): PendingRequest
|
||||
{
|
||||
return $this->request()->withHeaders([$app->bazaar_comments_header => $app->bazaar_comments_secret]);
|
||||
}
|
||||
|
||||
private function request(): PendingRequest
|
||||
{
|
||||
return Http::acceptJson()->connectTimeout(5)->timeout(20)->retry([100, 300], throw: false);
|
||||
}
|
||||
|
||||
private function send(MobileApp $app, string $operation, string $method, string $url, array $data = [], ?PendingRequest $request = null): BazaarResponse
|
||||
{
|
||||
$startedAt = hrtime(true);
|
||||
$request ??= $this->request();
|
||||
|
||||
try {
|
||||
$response = $this->dispatch($request, $method, $url, $data);
|
||||
$body = $this->responseBody($response);
|
||||
$this->logRequest($app, $operation, $method, $url, $response->status(), $response->successful(), $startedAt, $data, $body);
|
||||
|
||||
return new BazaarResponse($response->status(), $body);
|
||||
} catch (ConnectionException $exception) {
|
||||
$body = ['error' => 'bazaar_unavailable', 'error_description' => 'Cafe Bazaar could not be reached.'];
|
||||
$this->logRequest($app, $operation, $method, $url, 502, false, $startedAt, $data, $body, $exception);
|
||||
|
||||
return new BazaarResponse(502, $body);
|
||||
}
|
||||
}
|
||||
|
||||
private function dispatch(PendingRequest $request, string $method, string $url, array $data): Response
|
||||
{
|
||||
return match ($method) {
|
||||
'GET' => $request->get($url, $data),
|
||||
'POST' => $request->post($url, $data),
|
||||
'DELETE' => $request->delete($url, $data),
|
||||
default => throw new InvalidArgumentException("Unsupported Bazaar method [{$method}]."),
|
||||
};
|
||||
}
|
||||
|
||||
private function responseBody(Response $response): array
|
||||
{
|
||||
$body = $response->json();
|
||||
|
||||
return is_array($body) ? $body : ['raw' => Str::limit($response->body(), 2000)];
|
||||
}
|
||||
|
||||
private function logRequest(MobileApp $app, string $operation, string $method, string $url, int $statusCode, bool $successful, int $startedAt, array $requestMetadata, array $responseBody, ?Throwable $exception = null): void
|
||||
{
|
||||
BazaarApiRequest::create([
|
||||
'mobile_app_id' => $app->id,
|
||||
'operation' => $operation,
|
||||
'method' => $method,
|
||||
'url' => $this->redactUrl($url),
|
||||
'status_code' => $statusCode,
|
||||
'successful' => $successful,
|
||||
'duration_ms' => (int) ((hrtime(true) - $startedAt) / 1_000_000),
|
||||
'request_metadata' => $this->redact($requestMetadata),
|
||||
'response_body' => $this->redact($responseBody),
|
||||
'error_message' => $exception?->getMessage(),
|
||||
]);
|
||||
}
|
||||
|
||||
private function redact(array $data): array
|
||||
{
|
||||
return collect($data)->mapWithKeys(function (mixed $value, string|int $key): array {
|
||||
if (is_string($key) && Str::contains(Str::lower($key), ['token', 'secret', 'code'])) {
|
||||
return [$key => '[REDACTED]'];
|
||||
}
|
||||
|
||||
return [$key => is_array($value) ? $this->redact($value) : $value];
|
||||
})->all();
|
||||
}
|
||||
|
||||
private function redactUrl(string $url): string
|
||||
{
|
||||
return preg_replace('#/(purchases|active-subscriptions)/[^/]+#', '/$1/[REDACTED]', $url) ?? $url;
|
||||
}
|
||||
|
||||
private function path(string $value): string
|
||||
{
|
||||
return rawurlencode($value);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user