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'); } }