feat(myket): add myket

This commit is contained in:
2026-05-16 23:44:08 +03:30
parent c05587474f
commit 2796ce7de7
7 changed files with 280 additions and 48 deletions

View File

@@ -6,6 +6,7 @@ use App\Http\Controllers\Controller;
use App\Models\PaymentTransaction;
use App\Models\SubscribePlan;
use App\Models\UserSubscriber;
use App\Services\AppMarketPurchaseVerifier;
use App\Traits\BaseApiResponse;
use Carbon\Carbon;
use Illuminate\Http\Request;
@@ -17,6 +18,10 @@ class SubscribePlanController extends Controller
{
use BaseApiResponse;
public function __construct(private AppMarketPurchaseVerifier $marketVerifier)
{
}
public function index()
{
$subscribePlans = SubscribePlan::query()->where('is_active', true)->get()->map(function ($q) {
@@ -104,8 +109,6 @@ class SubscribePlanController extends Controller
public function subscribe_new(Request $request)
{
$package_name_bazzar = 'com.razzaghi.lawbook.android';
$request->validate([
'subscribe_plan_id' => 'required|exists:subscribe_plans,id',
'subscription_id' => 'nullable',
@@ -149,23 +152,13 @@ class SubscribePlanController extends Controller
return $this->failed(null, ['title' => 'Subscribe Plan', 'message' => 'Invalid subscription details.']);
}
$url = "https://pardakht.cafebazaar.ir/devapi/v2/api/applications/$package_name_bazzar/subscriptions/$subscription_id/purchases/$purchase_token";
$client = new \GuzzleHttp\Client();
try {
$response = $client->get($url, [
'headers' => [
'CAFEBAZAAR-PISHKHAN-API-SECRET' => 'eyJhbGciOiJIUzI1NiIsImtpZCI6ImFuY2llbnQiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJuYXNoZXItcGlzaGtoYW4tYXBpIiwiaWF0IjoxNzQwMjQ3NTMzLCJleHAiOjQ4OTM4NDc1MzMsImFwaV9hZ2VudF9pZCI6MzQ2NX0.UCrr3IHxCqn77ckxfnaubrsyCfrhPm18gJgyg1qNqwA',
]
]);
$bazaarExpiredAt = $this->marketVerifier->verifyBazaarSubscription($subscription_id, $purchase_token);
$data = json_decode($response->getBody(), true);
if (!isset($data['validUntilTimestampMsec']) || $data['validUntilTimestampMsec'] < now()->timestamp * 1000) {
if (!$bazaarExpiredAt) {
return $this->failed(null, ['title' => 'Subscribe Plan', 'message' => 'Invalid or expired subscription.']);
}
$bazaarExpiredAt = Carbon::createFromTimestampMs($data['validUntilTimestampMsec']);
$expiredAt = $activeSubscription ? $activeSubscription->expired_at->addDays($subscribePlan->expired_day) : $bazaarExpiredAt;
$user->userSubscribers()->whereHas('subscribe', function ($query) {
$query->where('is_free', true);
@@ -177,6 +170,8 @@ class SubscribePlanController extends Controller
'expired_at' => $expiredAt,
'subscription_id' => $subscription_id,
'purchase_token' => $purchase_token,
'market_provider' => 'bazaar',
'last_verified_at' => now(),
'is_free' => false
]);
@@ -187,6 +182,69 @@ class SubscribePlanController extends Controller
}
}
public function subscribeMyket(Request $request)
{
$request->validate([
'subscribe_plan_id' => 'required|exists:subscribe_plans,id',
'sku_id' => 'required|string',
'token' => 'required_without_all:tokenId,token_id,purchase_token|string',
'tokenId' => 'required_without_all:token,token_id,purchase_token|string',
'token_id' => 'required_without_all:token,tokenId,purchase_token|string',
'purchase_token' => 'required_without_all:token,tokenId,token_id|string',
]);
$subscribePlan = SubscribePlan::findOrFail($request->subscribe_plan_id);
$user = auth()->user();
$token = $request->input('token')
?? $request->input('tokenId')
?? $request->input('token_id')
?? $request->input('purchase_token');
if ($subscribePlan->is_free) {
return $this->failed(null, ['title' => 'Subscribe Plan', 'message' => 'Invalid subscription details.']);
}
try {
$purchase = $this->marketVerifier->verifyMyketProduct($request->sku_id, $token);
if (!$purchase) {
return $this->failed(null, ['title' => 'Subscribe Plan', 'message' => 'Invalid or failed Myket purchase.']);
}
$expiredAt = $purchase['purchased_at']->copy()->addDays($subscribePlan->expired_day);
if ($expiredAt->lessThan(now())) {
return $this->failed(null, ['title' => 'Subscribe Plan', 'message' => 'Invalid or expired subscription.']);
}
$user->userSubscribers()->whereHas('subscribe', function ($query) {
$query->where('is_free', true);
})->update(['expired_at' => now()]);
$user->userSubscribers()->updateOrCreate(
[
'market_provider' => 'myket',
'purchase_token' => $token,
],
[
'subscribe_plan_id' => $subscribePlan->id,
'expired_at' => $expiredAt,
'subscription_id' => $request->sku_id,
'product_id' => $request->sku_id,
'purchased_at' => $purchase['purchased_at'],
'last_verified_at' => now(),
'is_free' => false,
]
);
return $this->success(null, 'Subscribe Plan', 'Myket subscription successfully activated.');
} catch (\Exception $e) {
Log::error('Error in Myket subscription', ['Error' => $e->getMessage()]);
return $this->failed(null, ['title' => 'Subscribe Plan', 'message' => 'Failed to verify Myket subscription.']);
}
}
public function paymentCallback(Request $request)
{
try {