116 lines
4.9 KiB
PHP
116 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\BazaarActiveSubscriptionsRequest;
|
|
use App\Http\Requests\BazaarSubscriptionRequest;
|
|
use App\Http\Requests\ConsumeBazaarPurchaseRequest;
|
|
use App\Http\Requests\ValidateBazaarPurchaseRequest;
|
|
use App\Http\Requests\VerifyBazaarSignatureRequest;
|
|
use App\Models\BazaarPurchase;
|
|
use App\Models\BazaarSubscription;
|
|
use App\Models\MobileApp;
|
|
use App\Services\CafeBazaar\BazaarPurchaseRecorder;
|
|
use App\Services\CafeBazaar\BazaarSignatureVerifier;
|
|
use App\Services\CafeBazaar\BazaarSubscriptionRecorder;
|
|
use App\Services\CafeBazaar\CafeBazaarClient;
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
class BazaarBillingController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly CafeBazaarClient $bazaar,
|
|
private readonly BazaarPurchaseRecorder $purchases,
|
|
private readonly BazaarSubscriptionRecorder $subscriptions,
|
|
private readonly BazaarSignatureVerifier $signatures,
|
|
) {}
|
|
|
|
public function validatePurchase(ValidateBazaarPurchaseRequest $request): JsonResponse
|
|
{
|
|
$app = $this->app($request);
|
|
$response = $this->bazaar->validatePurchase($app, $request->string('product_id')->toString(), $request->string('purchase_token')->toString());
|
|
$this->purchases->record($app, $request->string('product_id')->toString(), $request->string('purchase_token')->toString(), $response);
|
|
|
|
return $response->toJsonResponse();
|
|
}
|
|
|
|
public function verifySignature(VerifyBazaarSignatureRequest $request): JsonResponse
|
|
{
|
|
$app = $request->attributes->get('mobileApp');
|
|
|
|
if (blank($app->bazaar_billing_public_key)) {
|
|
return response()->json(['error' => [
|
|
'code' => 'bazaar_public_key_missing',
|
|
'message' => 'Configure the Bazaar billing public key in the app panel.',
|
|
]], 422);
|
|
}
|
|
|
|
return response()->json(['data' => [
|
|
'valid' => $this->signatures->verify(
|
|
$app->bazaar_billing_public_key,
|
|
$request->string('original_json')->toString(),
|
|
$request->string('signature')->toString(),
|
|
),
|
|
]]);
|
|
}
|
|
|
|
public function consumePurchase(ConsumeBazaarPurchaseRequest $request): JsonResponse
|
|
{
|
|
$app = $this->app($request);
|
|
$response = $this->bazaar->consumePurchase($app, $request->string('purchase_token')->toString());
|
|
|
|
if ($response->successful()) {
|
|
BazaarPurchase::query()->whereBelongsTo($app)->where('purchase_token_hash', hash('sha256', $request->string('purchase_token')->toString()))->update(['is_consumed' => true]);
|
|
}
|
|
|
|
return $response->toJsonResponse();
|
|
}
|
|
|
|
public function refundPurchase(ConsumeBazaarPurchaseRequest $request): JsonResponse
|
|
{
|
|
$app = $this->app($request);
|
|
$response = $this->bazaar->refundPurchase($app, $request->string('purchase_token')->toString());
|
|
|
|
if ($response->successful()) {
|
|
BazaarPurchase::query()->whereBelongsTo($app)->where('purchase_token_hash', hash('sha256', $request->string('purchase_token')->toString()))->update(['status' => 'refunded']);
|
|
BazaarSubscription::query()->whereBelongsTo($app)->where('purchase_token_hash', hash('sha256', $request->string('purchase_token')->toString()))->update(['status' => 'refunded']);
|
|
}
|
|
|
|
return $response->toJsonResponse();
|
|
}
|
|
|
|
public function subscription(BazaarSubscriptionRequest $request): JsonResponse
|
|
{
|
|
$app = $this->app($request);
|
|
$response = $this->bazaar->subscription($app, $request->string('subscription_id')->toString(), $request->string('purchase_token')->toString());
|
|
$this->subscriptions->record($app, $request->string('subscription_id')->toString(), $request->string('purchase_token')->toString(), $response);
|
|
|
|
return $response->toJsonResponse();
|
|
}
|
|
|
|
public function activeSubscriptions(BazaarActiveSubscriptionsRequest $request): JsonResponse
|
|
{
|
|
return $this->bazaar
|
|
->activeSubscriptions($this->app($request), $request->string('purchase_token')->toString())
|
|
->toJsonResponse();
|
|
}
|
|
|
|
public function cancelSubscription(BazaarSubscriptionRequest $request): JsonResponse
|
|
{
|
|
$app = $this->app($request);
|
|
$response = $this->bazaar->cancelSubscription($app, $request->string('subscription_id')->toString(), $request->string('purchase_token')->toString());
|
|
|
|
if ($response->successful()) {
|
|
BazaarSubscription::query()->whereBelongsTo($app)->where('purchase_token_hash', hash('sha256', $request->string('purchase_token')->toString()))->update(['status' => 'cancelled']);
|
|
}
|
|
|
|
return $response->toJsonResponse();
|
|
}
|
|
|
|
private function app(ValidateBazaarPurchaseRequest|ConsumeBazaarPurchaseRequest|BazaarSubscriptionRequest|BazaarActiveSubscriptionsRequest $request): MobileApp
|
|
{
|
|
return $request->attributes->get('mobileApp');
|
|
}
|
|
}
|