feat: get status route
This commit is contained in:
@@ -365,7 +365,7 @@ public function profile()
|
||||
'sessions' => $sessions
|
||||
]);
|
||||
}
|
||||
public function status(Request $request)
|
||||
public function status(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'package_name' => 'string|required'
|
||||
@@ -378,8 +378,6 @@ public function status(Request $request)
|
||||
}
|
||||
|
||||
$user = auth()->user();
|
||||
$user->load(['response', 'plans']);
|
||||
|
||||
if ($user->mobile == config('approo.admin_mobile')) {
|
||||
$user['products'] = Product::where('package_name_id', $packageName->id)->get();
|
||||
} else {
|
||||
@@ -400,7 +398,7 @@ public function status(Request $request)
|
||||
// }
|
||||
$user['products'] = $user->products()
|
||||
->where('package_name_id', $packageName->id)
|
||||
->where(fn ($q) => $q->where('user_product.expire_at', '>', now()->addHour())->orWhere('user_product.expire_at', null))
|
||||
->where(fn($q) => $q->where('user_product.expire_at', '>', now()->addHour())->orWhere('user_product.expire_at', null))
|
||||
->get();
|
||||
|
||||
$user['package_name'] = $user->packageNames()
|
||||
|
||||
+4
-4
@@ -93,10 +93,10 @@ public function plans()
|
||||
{
|
||||
return $this->hasMany(Plan::class);
|
||||
}
|
||||
public function response()
|
||||
{
|
||||
return $this->hasOne(UserResponse::class);
|
||||
}
|
||||
// public function response()
|
||||
// {
|
||||
// return $this->hasOne(UserResponse::class);
|
||||
// }
|
||||
|
||||
public function breathingExercises()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Product;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class CafeService
|
||||
{
|
||||
public function __construct(private $cafeConfig)
|
||||
{
|
||||
}
|
||||
|
||||
private function getToken()
|
||||
{
|
||||
if ($this->cafeConfig->token_refreshed_at > now()->subMinute(50)) {
|
||||
return $this->cafeConfig->access_token;
|
||||
}
|
||||
$response = Http::asForm()->post('https://pardakht.cafebazaar.ir/devapi/v2/auth/token/', [
|
||||
'grant_type' => 'refresh_token',
|
||||
'client_id' => $this->cafeConfig->client_id,
|
||||
'client_secret' => $this->cafeConfig->client_secret,
|
||||
'refresh_token' => $this->cafeConfig->refresh_token,
|
||||
]);
|
||||
$response = json_decode($response, true);
|
||||
if (isset($response['access_token'])) {
|
||||
$this->cafeConfig->access_token = $response['access_token'];
|
||||
$this->cafeConfig->token_refreshed_at = now();
|
||||
$this->cafeConfig->save();
|
||||
}
|
||||
|
||||
return $response['access_token'] ?? null;
|
||||
}
|
||||
|
||||
private function sendRequest($url, $method, $params = [])
|
||||
{
|
||||
$response = Http::connectTimeout(2)->timeout(5)->retry(3, 500, throw: false)->send($method, $url, [
|
||||
'headers' => [
|
||||
'Authorization' => $this->getToken()
|
||||
],
|
||||
'form_params' => $params
|
||||
]);
|
||||
|
||||
return json_decode($response, true);
|
||||
}
|
||||
|
||||
public function checkPurchase($product, $token)
|
||||
{
|
||||
$response = $this->sendRequest("https://pardakht.cafebazaar.ir/devapi/v2/api/validate/{$product->packageName->name}/inapp/{$product->uuid}/purchases/$token/", 'GET');
|
||||
// if ($response['consumptionState']) {
|
||||
// return [
|
||||
// 'status' => false,
|
||||
// 'message' => 'already consumed',
|
||||
// ];
|
||||
// }
|
||||
|
||||
if ($response['purchaseState']) {
|
||||
return [
|
||||
'status' => false,
|
||||
'message' => 'refunded',
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'status' => true
|
||||
];
|
||||
}
|
||||
|
||||
public function consumePurchase($product, $token)
|
||||
{
|
||||
if ($product->type == Product::TYPES['permanent']) {
|
||||
$response = $this->sendRequest("https://pardakht.cafebazaar.ir/devapi/v2/api/consume/{$product->packageName->name}/purchases/", 'POST', ['token' => $token]);
|
||||
}
|
||||
}
|
||||
|
||||
public function checkSubscription($product, $token)
|
||||
{
|
||||
$response = $this->sendRequest("https://pardakht.cafebazaar.ir/devapi/v2/api/applications/{$product->packageName->name}/active-subscriptions/$token/", 'GET');
|
||||
return $response['subscriptions'] ?? false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Product;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class MyketService
|
||||
{
|
||||
private function sendRequest($url, $method, $params = [], $packageName)
|
||||
{
|
||||
$response = Http::send($method, $url, [
|
||||
'headers' => [
|
||||
'X-Access-Token' => $packageName->myket_access_token
|
||||
],
|
||||
'form_params' => $params
|
||||
]);
|
||||
|
||||
return json_decode($response, true);
|
||||
}
|
||||
|
||||
public function checkPurchase($product, $token)
|
||||
{
|
||||
$response = $this->sendRequest("https://developer.myket.ir/api/applications/{$product->packageName->name}/purchases/products/{$product->uuid}/tokens/$token", 'GET', [], $product->packageName);
|
||||
|
||||
if ($response['purchaseState']) {
|
||||
return [
|
||||
'status' => false,
|
||||
'message' => 'refunded',
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'status' => true
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user