Files
app-agent/tests/Feature/BazaarBillingApiTest.php
2026-08-24 19:44:24 +03:30

97 lines
3.8 KiB
PHP

<?php
use App\Models\BazaarPurchase;
use App\Models\BazaarSubscription;
use App\Models\MobileApp;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
beforeEach(function () {
Http::preventStrayRequests();
});
it('validates and records a Bazaar purchase', function () {
$app = MobileApp::factory()->create(['package_name' => 'ir.example.billing']);
Http::fake([
'pardakht.cafebazaar.ir/devapi/v2/api/validate/*' => Http::response([
'consumptionState' => 1,
'purchaseState' => 0,
'kind' => 'androidpublisher#inappPurchase',
'developerPayload' => 'user-42',
'purchaseTime' => 1787551200000,
]),
]);
$this->withHeaders(appApiHeaders($app))
->postJson('/api/v1/apps/ir.example.billing/bazaar/billing/purchases/validate', [
'product_id' => 'coins_100',
'purchase_token' => 'purchase-token',
])
->assertSuccessful()
->assertJsonPath('data.purchaseState', 0);
$purchase = BazaarPurchase::firstOrFail();
expect($purchase->product_id)->toBe('coins_100')
->and($purchase->status)->toBe('purchased')
->and($purchase->is_consumed)->toBeFalse();
Http::assertSent(fn (Request $request): bool => $request->hasHeader('CAFEBAZAAR-PISHKHAN-API-SECRET', $app->bazaar_api_secret));
});
it('records subscription activity and protects cancellation', function () {
$app = MobileApp::factory()->create(['package_name' => 'ir.example.subscription']);
Http::fake([
'pardakht.cafebazaar.ir/devapi/v2/api/applications/*/subscriptions/*/purchases/*' => Http::response([
'kind' => 'androidpublisher#subscriptionPurchase',
'initiationTimestampMsec' => now()->subDay()->getTimestampMs(),
'validUntilTimestampMsec' => now()->addMonth()->getTimestampMs(),
'autoRenewing' => true,
'linkedSubscriptionToken' => 'linked-token',
]),
]);
$payload = ['subscription_id' => 'monthly', 'purchase_token' => 'subscription-token'];
$this->withHeaders(appApiHeaders($app))
->postJson('/api/v1/apps/ir.example.subscription/bazaar/billing/subscriptions/validate', $payload)
->assertSuccessful();
$subscription = BazaarSubscription::firstOrFail();
expect($subscription->status)->toBe('active')
->and($subscription->auto_renewing)->toBeTrue();
$this->withHeaders(appApiHeaders($app))
->postJson('/api/v1/apps/ir.example.subscription/bazaar/billing/subscriptions/cancel', $payload)
->assertForbidden();
});
it('requires the management key for refunds', function () {
$app = MobileApp::factory()->create(['package_name' => 'ir.example.refund']);
$this->withHeaders(appApiHeaders($app))
->postJson('/api/v1/apps/ir.example.refund/bazaar/billing/purchases/refund', ['purchase_token' => 'token'])
->assertForbidden()
->assertJsonPath('error.code', 'invalid_management_key');
});
it('verifies Bazaar purchase signatures on the server', function () {
$privateKey = openssl_pkey_new(['private_key_bits' => 2048]);
openssl_pkey_export($privateKey, $privateKeyPem);
$publicKey = openssl_pkey_get_details($privateKey)['key'];
$originalJson = '{"orderId":"12999763169054705758.1371079406387615"}';
openssl_sign($originalJson, $signature, $privateKeyPem, OPENSSL_ALGO_SHA1);
$app = MobileApp::factory()->create([
'package_name' => 'ir.example.signature',
'bazaar_billing_public_key' => $publicKey,
]);
$this->withHeaders(appApiHeaders($app))
->postJson('/api/v1/apps/ir.example.signature/bazaar/billing/signatures/verify', [
'original_json' => $originalJson,
'signature' => base64_encode($signature),
])
->assertSuccessful()
->assertJsonPath('data.valid', true);
});