switched to paddle inline checkout, removed paypal and most of stripe. added product sync between app and paddle.
This commit is contained in:
@@ -54,6 +54,7 @@ class EventControllerTest extends TestCase
|
||||
'event_id' => $event->id,
|
||||
'package_id' => $package->id,
|
||||
'type' => 'endcustomer_event',
|
||||
'provider' => 'manual',
|
||||
'provider_id' => 'manual',
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -2,19 +2,17 @@
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Mail\Welcome;
|
||||
use App\Models\Package;
|
||||
use App\Models\User;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\PackagePurchase;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\TenantPackage;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use App\Mail\Welcome;
|
||||
use App\Mail\PurchaseConfirmation;
|
||||
use Stripe\StripeClient;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class FullUserFlowTest extends TestCase
|
||||
{
|
||||
@@ -85,9 +83,9 @@ class FullUserFlowTest extends TestCase
|
||||
$paidPackage = Package::factory()->reseller()->create(['price' => 10]);
|
||||
|
||||
// Mock Stripe für Erfolg
|
||||
$this->mock(StripeClient::class, function ($mock) use ($user, $tenant, $paidPackage) {
|
||||
$this->mock(StripeClient::class, function ($mock) {
|
||||
$mock->shouldReceive('checkout->sessions->create')
|
||||
->andReturn((object)['url' => 'https://mock-stripe.com']);
|
||||
->andReturn((object) ['url' => 'https://mock-stripe.com']);
|
||||
});
|
||||
|
||||
// Simuliere Kauf (GET zu buy.packages, aber da es Redirect ist, prüfe Session oder folge)
|
||||
@@ -108,6 +106,7 @@ class FullUserFlowTest extends TestCase
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $paidPackage->id,
|
||||
'type' => 'reseller_subscription',
|
||||
'provider' => 'stripe',
|
||||
'provider_id' => 'stripe',
|
||||
'price' => 10,
|
||||
'purchased_at' => now(),
|
||||
@@ -120,6 +119,7 @@ class FullUserFlowTest extends TestCase
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $paidPackage->id,
|
||||
'type' => 'reseller_subscription',
|
||||
'provider' => 'stripe',
|
||||
]);
|
||||
|
||||
// Überprüfe, dass 2 Purchases existieren (Free + Paid)
|
||||
|
||||
55
tests/Feature/PaddleSyncPackagesCommandTest.php
Normal file
55
tests/Feature/PaddleSyncPackagesCommandTest.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Jobs\SyncPackageToPaddle;
|
||||
use App\Models\Package;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Bus as BusFacade;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PaddleSyncPackagesCommandTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_command_dispatches_jobs_for_packages(): void
|
||||
{
|
||||
Package::factory()->count(2)->create();
|
||||
|
||||
BusFacade::fake();
|
||||
|
||||
$this->artisan('paddle:sync-packages', [
|
||||
'--dry-run' => true,
|
||||
'--queue' => true,
|
||||
])->assertExitCode(0);
|
||||
|
||||
BusFacade::assertDispatched(SyncPackageToPaddle::class, 2);
|
||||
}
|
||||
|
||||
public function test_command_filters_packages_by_id(): void
|
||||
{
|
||||
$package = Package::factory()->create();
|
||||
Package::factory()->create();
|
||||
|
||||
BusFacade::fake();
|
||||
|
||||
$this->artisan('paddle:sync-packages', [
|
||||
'--dry-run' => true,
|
||||
'--queue' => true,
|
||||
'--package' => [$package->id],
|
||||
])->assertExitCode(0);
|
||||
|
||||
BusFacade::assertDispatched(SyncPackageToPaddle::class, function (SyncPackageToPaddle $job) use ($package) {
|
||||
return $this->getJobPackageId($job) === $package->id;
|
||||
});
|
||||
}
|
||||
|
||||
protected function getJobPackageId(SyncPackageToPaddle $job): int
|
||||
{
|
||||
$reflection = new \ReflectionClass($job);
|
||||
$property = $reflection->getProperty('packageId');
|
||||
$property->setAccessible(true);
|
||||
|
||||
return (int) $property->getValue($job);
|
||||
}
|
||||
}
|
||||
228
tests/Feature/PaddleWebhookControllerTest.php
Normal file
228
tests/Feature/PaddleWebhookControllerTest.php
Normal file
@@ -0,0 +1,228 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\CheckoutSession;
|
||||
use App\Models\Package;
|
||||
use App\Models\PackagePurchase;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\TenantPackage;
|
||||
use App\Models\User;
|
||||
use App\Services\Checkout\CheckoutSessionService;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Arr;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PaddleWebhookControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_transaction_completed_finalises_checkout(): void
|
||||
{
|
||||
config(['paddle.webhook_secret' => 'test_secret']);
|
||||
|
||||
[$tenant, $package, $session] = $this->prepareSession();
|
||||
|
||||
$payload = [
|
||||
'event_type' => 'transaction.completed',
|
||||
'data' => [
|
||||
'id' => 'txn_123',
|
||||
'status' => 'completed',
|
||||
'checkout_id' => 'chk_456',
|
||||
'metadata' => [
|
||||
'checkout_session_id' => $session->id,
|
||||
'tenant_id' => (string) $tenant->id,
|
||||
'package_id' => (string) $package->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$signature = hash_hmac('sha256', json_encode($payload), 'test_secret');
|
||||
|
||||
$response = $this->withHeader('Paddle-Webhook-Signature', $signature)
|
||||
->postJson('/paddle/webhook', $payload);
|
||||
|
||||
$response->assertOk()->assertJson(['status' => 'processed']);
|
||||
|
||||
$session->refresh();
|
||||
|
||||
$this->assertEquals(CheckoutSession::STATUS_COMPLETED, $session->status);
|
||||
$this->assertSame('paddle', $session->provider);
|
||||
$this->assertSame('txn_123', Arr::get($session->provider_metadata, 'paddle_transaction_id'));
|
||||
|
||||
$this->assertTrue(
|
||||
TenantPackage::query()
|
||||
->where('tenant_id', $tenant->id)
|
||||
->where('package_id', $package->id)
|
||||
->where('active', true)
|
||||
->exists()
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
PackagePurchase::query()
|
||||
->where('tenant_id', $tenant->id)
|
||||
->where('package_id', $package->id)
|
||||
->where('provider', 'paddle')
|
||||
->exists()
|
||||
);
|
||||
}
|
||||
|
||||
public function test_rejects_invalid_signature(): void
|
||||
{
|
||||
config(['paddle.webhook_secret' => 'secret']);
|
||||
|
||||
$response = $this->withHeader('Paddle-Webhook-Signature', 'invalid')
|
||||
->postJson('/paddle/webhook', ['event_type' => 'transaction.completed']);
|
||||
|
||||
$response->assertStatus(400)->assertJson(['status' => 'invalid']);
|
||||
}
|
||||
|
||||
public function test_unhandled_event_returns_accepted(): void
|
||||
{
|
||||
config(['paddle.webhook_secret' => null]);
|
||||
|
||||
$response = $this->postJson('/paddle/webhook', [
|
||||
'event_type' => 'transaction.unknown',
|
||||
'data' => [],
|
||||
]);
|
||||
|
||||
$response->assertStatus(202)->assertJson(['status' => 'ignored']);
|
||||
}
|
||||
|
||||
public function test_subscription_activation_creates_tenant_package(): void
|
||||
{
|
||||
config(['paddle.webhook_secret' => 'test_secret']);
|
||||
|
||||
$tenant = Tenant::factory()->create([
|
||||
'paddle_customer_id' => 'cus_123',
|
||||
'subscription_status' => 'free',
|
||||
]);
|
||||
|
||||
$package = Package::factory()->create([
|
||||
'type' => 'reseller',
|
||||
'price' => 129,
|
||||
'paddle_price_id' => 'price_sub_1',
|
||||
]);
|
||||
|
||||
$payload = [
|
||||
'event_type' => 'subscription.created',
|
||||
'data' => [
|
||||
'id' => 'sub_123',
|
||||
'status' => 'active',
|
||||
'customer_id' => 'cus_123',
|
||||
'created_at' => Carbon::now()->subDay()->toIso8601String(),
|
||||
'next_billing_date' => Carbon::now()->addMonth()->toIso8601String(),
|
||||
'metadata' => [
|
||||
'tenant_id' => (string) $tenant->id,
|
||||
'package_id' => (string) $package->id,
|
||||
],
|
||||
'items' => [
|
||||
[
|
||||
'price_id' => 'price_sub_1',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$signature = hash_hmac('sha256', json_encode($payload), 'test_secret');
|
||||
|
||||
$response = $this->withHeader('Paddle-Webhook-Signature', $signature)
|
||||
->postJson('/paddle/webhook', $payload);
|
||||
|
||||
$response->assertOk()->assertJson(['status' => 'processed']);
|
||||
|
||||
$tenant->refresh();
|
||||
|
||||
$tenantPackage = TenantPackage::where('tenant_id', $tenant->id)
|
||||
->where('package_id', $package->id)
|
||||
->first();
|
||||
|
||||
$this->assertNotNull($tenantPackage);
|
||||
$this->assertSame('sub_123', $tenantPackage->paddle_subscription_id);
|
||||
$this->assertTrue($tenantPackage->active);
|
||||
$this->assertEquals('active', $tenant->subscription_status);
|
||||
$this->assertNotNull($tenant->subscription_expires_at);
|
||||
}
|
||||
|
||||
public function test_subscription_cancellation_marks_package_inactive(): void
|
||||
{
|
||||
config(['paddle.webhook_secret' => 'test_secret']);
|
||||
|
||||
$tenant = Tenant::factory()->create([
|
||||
'paddle_customer_id' => 'cus_cancel',
|
||||
'subscription_status' => 'active',
|
||||
]);
|
||||
|
||||
$package = Package::factory()->create([
|
||||
'type' => 'reseller',
|
||||
'price' => 199,
|
||||
'paddle_price_id' => 'price_cancel',
|
||||
]);
|
||||
|
||||
TenantPackage::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'paddle_subscription_id' => 'sub_cancel',
|
||||
'active' => true,
|
||||
]);
|
||||
|
||||
$payload = [
|
||||
'event_type' => 'subscription.cancelled',
|
||||
'data' => [
|
||||
'id' => 'sub_cancel',
|
||||
'status' => 'cancelled',
|
||||
'customer_id' => 'cus_cancel',
|
||||
'metadata' => [
|
||||
'tenant_id' => (string) $tenant->id,
|
||||
'package_id' => (string) $package->id,
|
||||
],
|
||||
'items' => [
|
||||
[
|
||||
'price_id' => 'price_cancel',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$signature = hash_hmac('sha256', json_encode($payload), 'test_secret');
|
||||
|
||||
$response = $this->withHeader('Paddle-Webhook-Signature', $signature)
|
||||
->postJson('/paddle/webhook', $payload);
|
||||
|
||||
$response->assertOk()->assertJson(['status' => 'processed']);
|
||||
|
||||
$tenant->refresh();
|
||||
|
||||
$tenantPackage = TenantPackage::where('tenant_id', $tenant->id)
|
||||
->where('package_id', $package->id)
|
||||
->first();
|
||||
|
||||
$this->assertNotNull($tenantPackage);
|
||||
$this->assertFalse($tenantPackage->active);
|
||||
$this->assertEquals('expired', $tenant->subscription_status);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{\App\Models\Tenant, \App\Models\Package, \App\Models\CheckoutSession}
|
||||
*/
|
||||
protected function prepareSession(): array
|
||||
{
|
||||
$user = User::factory()->create(['email_verified_at' => now()]);
|
||||
$tenant = Tenant::factory()->create(['user_id' => $user->id]);
|
||||
$user->forceFill(['tenant_id' => $tenant->id])->save();
|
||||
|
||||
$package = Package::factory()->create([
|
||||
'type' => 'reseller',
|
||||
'price' => 99,
|
||||
'paddle_price_id' => 'price_123',
|
||||
]);
|
||||
|
||||
/** @var CheckoutSessionService $sessions */
|
||||
$sessions = app(CheckoutSessionService::class);
|
||||
$session = $sessions->createOrResume($user, $package, ['tenant' => $tenant]);
|
||||
$sessions->selectProvider($session, CheckoutSession::PROVIDER_PADDLE);
|
||||
|
||||
return [$tenant, $package, $session];
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Package;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\TenantPackage;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PayPalWebhookControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_subscription_activation_marks_tenant_active(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create(['subscription_status' => 'free']);
|
||||
$package = Package::factory()->reseller()->create();
|
||||
|
||||
$payload = [
|
||||
'webhook_id' => 'WH-activation',
|
||||
'webhook_event' => [
|
||||
'event_type' => 'BILLING.SUBSCRIPTION.ACTIVATED',
|
||||
'resource' => [
|
||||
'id' => 'I-123456',
|
||||
'custom_id' => json_encode([
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
]),
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$response = $this->postJson('/paypal/webhook', $payload);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJson(['status' => 'SUCCESS']);
|
||||
|
||||
$this->assertEquals('active', $tenant->fresh()->subscription_status);
|
||||
}
|
||||
|
||||
public function test_subscription_cancellation_deactivates_tenant_package(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create(['subscription_status' => 'active']);
|
||||
$package = Package::factory()->reseller()->create();
|
||||
|
||||
TenantPackage::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'active' => true,
|
||||
]);
|
||||
|
||||
$payload = [
|
||||
'webhook_id' => 'WH-cancel',
|
||||
'webhook_event' => [
|
||||
'event_type' => 'BILLING.SUBSCRIPTION.CANCELLED',
|
||||
'resource' => [
|
||||
'id' => 'I-123456',
|
||||
'custom_id' => json_encode([
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
]),
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$response = $this->postJson('/paypal/webhook', $payload);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJson(['status' => 'SUCCESS']);
|
||||
|
||||
$this->assertEquals('expired', $tenant->fresh()->subscription_status);
|
||||
$this->assertFalse($tenant->tenantPackages()->first()->fresh()->active);
|
||||
}
|
||||
}
|
||||
@@ -3,18 +3,12 @@
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Package;
|
||||
use App\Models\PackagePurchase;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\TenantPackage;
|
||||
use App\Models\User;
|
||||
use App\Services\PayPal\PaypalClientFactory;
|
||||
use App\Services\Paddle\PaddleCheckoutService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
use PaypalServerSdkLib\PaypalServerSdkClient;
|
||||
use PaypalServerSdkLib\Controllers\OrdersController;
|
||||
use PaypalServerSdkLib\Http\ApiResponse;
|
||||
|
||||
class PurchaseTest extends TestCase
|
||||
{
|
||||
@@ -26,298 +20,96 @@ class PurchaseTest extends TestCase
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_paypal_checkout_creates_order(): void
|
||||
public function test_create_paddle_checkout_requires_paddle_price(): void
|
||||
{
|
||||
[$tenant, $package] = $this->seedTenantWithPackage(price: 10);
|
||||
Auth::login($tenant->user);
|
||||
[$tenant, $package] = $this->seedTenantWithPackage(includePaddlePrice: false);
|
||||
$this->actingAs($tenant->user);
|
||||
|
||||
$apiResponse = $this->apiResponse((object) [
|
||||
'id' => 'ORDER-123',
|
||||
'links' => [
|
||||
(object) ['rel' => 'approve', 'href' => 'https://paypal.test/approve/ORDER-123'],
|
||||
],
|
||||
$response = $this->postJson('/paddle/create-checkout', [
|
||||
'package_id' => $package->id,
|
||||
]);
|
||||
|
||||
$ordersController = Mockery::mock(OrdersController::class);
|
||||
$ordersController->shouldReceive('createOrder')
|
||||
$response->assertStatus(422)
|
||||
->assertJsonValidationErrors('package_id');
|
||||
}
|
||||
|
||||
public function test_create_paddle_checkout_returns_checkout_url(): void
|
||||
{
|
||||
[$tenant, $package] = $this->seedTenantWithPackage(includePaddlePrice: true);
|
||||
$this->actingAs($tenant->user);
|
||||
|
||||
$service = Mockery::mock(PaddleCheckoutService::class);
|
||||
$service->shouldReceive('createCheckout')
|
||||
->once()
|
||||
->andReturn($apiResponse);
|
||||
->with(
|
||||
Mockery::on(fn ($arg) => $arg instanceof Tenant && $arg->is($tenant)),
|
||||
Mockery::on(fn ($arg) => $arg instanceof Package && $arg->is($package)),
|
||||
Mockery::on(function ($options) {
|
||||
return ($options['success_url'] ?? null) === null
|
||||
&& ($options['return_url'] ?? null) === null
|
||||
&& isset($options['metadata']['checkout_session_id']);
|
||||
})
|
||||
)
|
||||
->andReturn([
|
||||
'checkout_url' => 'https://paddle.test/checkout/abc',
|
||||
]);
|
||||
|
||||
$clientMock = Mockery::mock(PaypalServerSdkClient::class);
|
||||
$clientMock->shouldReceive('getOrdersController')->andReturn($ordersController);
|
||||
$this->app->instance(PaddleCheckoutService::class, $service);
|
||||
|
||||
$factory = Mockery::mock(PaypalClientFactory::class);
|
||||
$factory->shouldReceive('make')->andReturn($clientMock);
|
||||
$this->app->instance(PaypalClientFactory::class, $factory);
|
||||
|
||||
$response = $this->postJson('/paypal/create-order', [
|
||||
$response = $this->postJson('/paddle/create-checkout', [
|
||||
'package_id' => $package->id,
|
||||
]);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJson([
|
||||
'id' => 'ORDER-123',
|
||||
'approve_url' => 'https://paypal.test/approve/ORDER-123',
|
||||
'checkout_url' => 'https://paddle.test/checkout/abc',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_paypal_capture_creates_purchase_and_package(): void
|
||||
public function test_create_paddle_checkout_inline_returns_items(): void
|
||||
{
|
||||
[$tenant, $package] = $this->seedTenantWithPackage(price: 15);
|
||||
Auth::login($tenant->user);
|
||||
[$tenant, $package] = $this->seedTenantWithPackage(includePaddlePrice: true);
|
||||
$this->actingAs($tenant->user);
|
||||
|
||||
$metadata = json_encode([
|
||||
'tenant_id' => $tenant->id,
|
||||
$service = Mockery::mock(PaddleCheckoutService::class);
|
||||
$service->shouldNotReceive('createCheckout');
|
||||
$this->app->instance(PaddleCheckoutService::class, $service);
|
||||
|
||||
$response = $this->postJson('/paddle/create-checkout', [
|
||||
'package_id' => $package->id,
|
||||
'type' => 'endcustomer_event',
|
||||
'inline' => true,
|
||||
]);
|
||||
|
||||
$apiResponse = $this->apiResponse((object) [
|
||||
'id' => 'ORDER-456',
|
||||
'purchaseUnits' => [
|
||||
(object) [
|
||||
'customId' => $metadata,
|
||||
'amount' => (object) ['value' => '15.00'],
|
||||
$response->assertOk()
|
||||
->assertJson([
|
||||
'mode' => 'inline',
|
||||
])
|
||||
->assertJsonStructure([
|
||||
'mode',
|
||||
'items' => [
|
||||
['priceId', 'quantity'],
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$ordersController = Mockery::mock(OrdersController::class);
|
||||
$ordersController->shouldReceive('captureOrder')
|
||||
->once()
|
||||
->andReturn($apiResponse);
|
||||
|
||||
$clientMock = Mockery::mock(PaypalServerSdkClient::class);
|
||||
$clientMock->shouldReceive('getOrdersController')->andReturn($ordersController);
|
||||
|
||||
$factory = Mockery::mock(PaypalClientFactory::class);
|
||||
$factory->shouldReceive('make')->andReturn($clientMock);
|
||||
$this->app->instance(PaypalClientFactory::class, $factory);
|
||||
|
||||
$response = $this->postJson('/paypal/capture-order', [
|
||||
'order_id' => 'ORDER-456',
|
||||
]);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJson(['status' => 'captured']);
|
||||
|
||||
$this->assertDatabaseHas('package_purchases', [
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'provider_id' => 'ORDER-456',
|
||||
'price' => 15,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('tenant_packages', [
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'price' => 15,
|
||||
'active' => true,
|
||||
]);
|
||||
|
||||
$this->assertEquals('active', $tenant->fresh()->subscription_status);
|
||||
}
|
||||
|
||||
public function test_paypal_capture_failure_returns_error(): void
|
||||
{
|
||||
[$tenant, $package] = $this->seedTenantWithPackage();
|
||||
Auth::login($tenant->user);
|
||||
|
||||
$ordersController = Mockery::mock(OrdersController::class);
|
||||
$ordersController->shouldReceive('captureOrder')
|
||||
->once()
|
||||
->andThrow(new \RuntimeException('Capture failed'));
|
||||
|
||||
$clientMock = Mockery::mock(PaypalServerSdkClient::class);
|
||||
$clientMock->shouldReceive('getOrdersController')->andReturn($ordersController);
|
||||
|
||||
$factory = Mockery::mock(PaypalClientFactory::class);
|
||||
$factory->shouldReceive('make')->andReturn($clientMock);
|
||||
$this->app->instance(PaypalClientFactory::class, $factory);
|
||||
|
||||
$response = $this->postJson('/paypal/capture-order', [
|
||||
'order_id' => 'ORDER-999',
|
||||
]);
|
||||
|
||||
$response->assertStatus(500)
|
||||
->assertJson(['error' => 'Capture failed']);
|
||||
|
||||
$this->assertDatabaseCount('package_purchases', 0);
|
||||
$this->assertDatabaseCount('tenant_packages', 0);
|
||||
}
|
||||
|
||||
public function test_paypal_subscription_creation_creates_initial_records(): void
|
||||
{
|
||||
[$tenant, $package] = $this->seedTenantWithPackage(price: 99, type: 'reseller');
|
||||
Auth::login($tenant->user);
|
||||
|
||||
$apiResponse = $this->apiResponse((object) [
|
||||
'id' => 'ORDER-SUB-1',
|
||||
'links' => [
|
||||
(object) ['rel' => 'approve', 'href' => 'https://paypal.test/approve/ORDER-SUB-1'],
|
||||
],
|
||||
]);
|
||||
|
||||
$ordersController = Mockery::mock(OrdersController::class);
|
||||
$ordersController->shouldReceive('createOrder')
|
||||
->once()
|
||||
->andReturn($apiResponse);
|
||||
|
||||
$clientMock = Mockery::mock(PaypalServerSdkClient::class);
|
||||
$clientMock->shouldReceive('getOrdersController')->andReturn($ordersController);
|
||||
|
||||
$factory = Mockery::mock(PaypalClientFactory::class);
|
||||
$factory->shouldReceive('make')->andReturn($clientMock);
|
||||
$this->app->instance(PaypalClientFactory::class, $factory);
|
||||
|
||||
$response = $this->postJson('/paypal/create-subscription', [
|
||||
'package_id' => $package->id,
|
||||
'plan_id' => 'PLAN-123',
|
||||
]);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJson([
|
||||
'order_id' => 'ORDER-SUB-1',
|
||||
'approve_url' => 'https://paypal.test/approve/ORDER-SUB-1',
|
||||
'custom_data' => ['tenant_id', 'package_id', 'checkout_session_id'],
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('tenant_packages', [
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'price' => 99,
|
||||
'active' => true,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('package_purchases', [
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'provider_id' => 'ORDER-SUB-1_sub_PLAN-123',
|
||||
]);
|
||||
$payload = $response->json();
|
||||
$this->assertSame($package->paddle_price_id, $payload['items'][0]['priceId']);
|
||||
$this->assertSame(1, $payload['items'][0]['quantity']);
|
||||
}
|
||||
|
||||
public function test_paypal_webhook_capture_completes_purchase(): void
|
||||
{
|
||||
[$tenant, $package] = $this->seedTenantWithPackage(price: 20);
|
||||
|
||||
$metadata = json_encode([
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
]);
|
||||
|
||||
$apiResponse = $this->apiResponse((object) [
|
||||
'purchaseUnits' => [
|
||||
(object) ['customId' => $metadata],
|
||||
],
|
||||
]);
|
||||
|
||||
$ordersController = Mockery::mock(OrdersController::class);
|
||||
$ordersController->shouldReceive('showOrder')
|
||||
->andReturn($apiResponse);
|
||||
|
||||
$clientMock = Mockery::mock(PaypalServerSdkClient::class);
|
||||
$clientMock->shouldReceive('getOrdersController')->andReturn($ordersController);
|
||||
|
||||
$factory = Mockery::mock(PaypalClientFactory::class);
|
||||
$factory->shouldReceive('make')->andReturn($clientMock);
|
||||
$this->app->instance(PaypalClientFactory::class, $factory);
|
||||
|
||||
$event = [
|
||||
'event_type' => 'PAYMENT.CAPTURE.COMPLETED',
|
||||
'resource' => [
|
||||
'id' => 'CAPTURE-1',
|
||||
'order_id' => 'ORDER-WEBHOOK-1',
|
||||
],
|
||||
];
|
||||
|
||||
$response = $this->postJson('/paypal/webhook', [
|
||||
'webhook_id' => 'WH-1',
|
||||
'webhook_event' => $event,
|
||||
]);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJson(['status' => 'SUCCESS']);
|
||||
|
||||
$this->assertDatabaseHas('package_purchases', [
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'provider_id' => 'ORDER-WEBHOOK-1',
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('tenant_packages', [
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'active' => true,
|
||||
]);
|
||||
|
||||
$this->assertEquals('active', $tenant->fresh()->subscription_status);
|
||||
}
|
||||
|
||||
public function test_paypal_webhook_capture_is_idempotent(): void
|
||||
{
|
||||
[$tenant, $package] = $this->seedTenantWithPackage(price: 25);
|
||||
|
||||
$metadata = json_encode([
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
]);
|
||||
|
||||
$apiResponse = $this->apiResponse((object) [
|
||||
'purchaseUnits' => [
|
||||
(object) ['customId' => $metadata],
|
||||
],
|
||||
]);
|
||||
|
||||
$ordersController = Mockery::mock(OrdersController::class);
|
||||
$ordersController->shouldReceive('showOrder')
|
||||
->andReturn($apiResponse);
|
||||
|
||||
$clientMock = Mockery::mock(PaypalServerSdkClient::class);
|
||||
$clientMock->shouldReceive('getOrdersController')->andReturn($ordersController);
|
||||
|
||||
$factory = Mockery::mock(PaypalClientFactory::class);
|
||||
$factory->shouldReceive('make')->andReturn($clientMock);
|
||||
$this->app->instance(PaypalClientFactory::class, $factory);
|
||||
|
||||
$event = [
|
||||
'event_type' => 'PAYMENT.CAPTURE.COMPLETED',
|
||||
'resource' => [
|
||||
'id' => 'CAPTURE-2',
|
||||
'order_id' => 'ORDER-WEBHOOK-2',
|
||||
],
|
||||
];
|
||||
|
||||
$payload = [
|
||||
'webhook_id' => 'WH-3',
|
||||
'webhook_event' => $event,
|
||||
];
|
||||
|
||||
$this->postJson('/paypal/webhook', $payload)->assertOk();
|
||||
$this->postJson('/paypal/webhook', $payload)->assertOk();
|
||||
|
||||
$this->assertDatabaseCount('package_purchases', 1);
|
||||
$this->assertDatabaseHas('package_purchases', [
|
||||
'provider_id' => 'ORDER-WEBHOOK-2',
|
||||
]);
|
||||
}
|
||||
|
||||
private function apiResponse(object $result, int $status = 201): ApiResponse
|
||||
{
|
||||
$response = Mockery::mock(ApiResponse::class);
|
||||
$response->shouldReceive('getStatusCode')->andReturn($status);
|
||||
$response->shouldReceive('getResult')->andReturn($result);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
private function seedTenantWithPackage(int $price = 10, string $type = 'endcustomer'): array
|
||||
private function seedTenantWithPackage(int $price = 10, string $type = 'endcustomer', bool $includePaddlePrice = true): array
|
||||
{
|
||||
$user = User::factory()->create(['email_verified_at' => now()]);
|
||||
$tenant = Tenant::factory()->create(['user_id' => $user->id]);
|
||||
|
||||
$user->forceFill(['tenant_id' => $tenant->id])->save();
|
||||
|
||||
$package = Package::factory()->create([
|
||||
'price' => $price,
|
||||
'type' => $type,
|
||||
'paddle_price_id' => $includePaddlePrice ? 'price_123' : null,
|
||||
]);
|
||||
|
||||
return [$tenant->fresh(), $package];
|
||||
return [$tenant, $package];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,120 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Http\Controllers\Api\StripeWebhookController;
|
||||
use App\Models\Package;
|
||||
use App\Models\PackagePurchase;
|
||||
use App\Models\EventPackage;
|
||||
use App\Models\TenantPackage;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Stripe\Webhook;
|
||||
use Tests\TestCase;
|
||||
|
||||
class StripeWebhookTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
// Mock Stripe secret
|
||||
config(['services.stripe.webhook_secret' => 'whsec_test_secret']);
|
||||
}
|
||||
|
||||
public function test_handle_payment_intent_succeeded_creates_event_package(): void
|
||||
{
|
||||
$tenant = \App\Models\Tenant::factory()->create();
|
||||
$event = \App\Models\Event::factory()->create(['tenant_id' => $tenant->id]);
|
||||
$package = Package::factory()->create(['type' => 'endcustomer']);
|
||||
|
||||
$payload = [
|
||||
'id' => 'evt_test',
|
||||
'type' => 'payment_intent.succeeded',
|
||||
'data' => [
|
||||
'object' => [
|
||||
'id' => 'pi_test',
|
||||
'metadata' => [
|
||||
'type' => 'endcustomer_event',
|
||||
'tenant_id' => (string) $tenant->id,
|
||||
'event_id' => (string) $event->id,
|
||||
'package_id' => (string) $package->id,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$sigHeader = 't=12345,v1=' . base64_encode(hash_hmac('sha256', json_encode($payload), 'whsec_test_secret', true));
|
||||
|
||||
$response = $this->postJson('/api/v1/stripe/webhook', $payload, [
|
||||
'Stripe-Signature' => $sigHeader,
|
||||
]);
|
||||
|
||||
$response->assertStatus(200);
|
||||
|
||||
$this->assertDatabaseHas('package_purchases', [
|
||||
'package_id' => $package->id,
|
||||
'tenant_id' => $tenant->id,
|
||||
'event_id' => $event->id,
|
||||
'type' => 'endcustomer_event',
|
||||
'provider_id' => 'pi_test',
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('event_packages', [
|
||||
'event_id' => $event->id,
|
||||
'package_id' => $package->id,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_handle_invoice_paid_renews_tenant_package(): void
|
||||
{
|
||||
$tenant = \App\Models\Tenant::factory()->create();
|
||||
$package = Package::factory()->create(['type' => 'reseller']);
|
||||
|
||||
$payload = [
|
||||
'id' => 'evt_test',
|
||||
'type' => 'invoice.paid',
|
||||
'data' => [
|
||||
'object' => [
|
||||
'subscription' => 'sub_test',
|
||||
'metadata' => [
|
||||
'type' => 'reseller_subscription',
|
||||
'tenant_id' => (string) $tenant->id,
|
||||
'package_id' => (string) $package->id,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$sigHeader = 't=12345,v1=' . base64_encode(hash_hmac('sha256', json_encode($payload), 'whsec_test_secret', true));
|
||||
|
||||
$response = $this->postJson('/api/v1/stripe/webhook', $payload, [
|
||||
'Stripe-Signature' => $sigHeader,
|
||||
]);
|
||||
|
||||
$response->assertStatus(200);
|
||||
|
||||
$this->assertDatabaseHas('package_purchases', [
|
||||
'package_id' => $package->id,
|
||||
'tenant_id' => $tenant->id,
|
||||
'type' => 'reseller_subscription',
|
||||
]);
|
||||
|
||||
$tenantPackage = TenantPackage::where('tenant_id', $tenant->id)->first();
|
||||
$this->assertNotNull($tenantPackage);
|
||||
$this->assertTrue($tenantPackage->expires_at->isFuture());
|
||||
}
|
||||
|
||||
public function test_webhook_rejects_invalid_signature(): void
|
||||
{
|
||||
$payload = ['type' => 'invalid'];
|
||||
$sigHeader = 'invalid';
|
||||
|
||||
$response = $this->postJson('/api/v1/stripe/webhook', $payload, [
|
||||
'Stripe-Signature' => $sigHeader,
|
||||
]);
|
||||
|
||||
$response->assertStatus(400);
|
||||
}
|
||||
}
|
||||
83
tests/Feature/SyncPackageToPaddleJobTest.php
Normal file
83
tests/Feature/SyncPackageToPaddleJobTest.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Jobs\SyncPackageToPaddle;
|
||||
use App\Models\Package;
|
||||
use App\Services\Paddle\PaddleCatalogService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SyncPackageToPaddleJobTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
parent::tearDown();
|
||||
|
||||
Mockery::close();
|
||||
}
|
||||
|
||||
public function test_job_creates_product_and_price_and_updates_package(): void
|
||||
{
|
||||
$package = Package::factory()->create([
|
||||
'paddle_product_id' => null,
|
||||
'paddle_price_id' => null,
|
||||
'price' => 15.50,
|
||||
'slug' => 'silver-plan',
|
||||
]);
|
||||
|
||||
$service = Mockery::mock(PaddleCatalogService::class);
|
||||
$service->shouldReceive('createProduct')
|
||||
->once()
|
||||
->withArgs(function ($pkg, $overrides) use ($package) {
|
||||
return $pkg->is($package) && $overrides === [];
|
||||
})
|
||||
->andReturn(['id' => 'pro_123']);
|
||||
$service->shouldReceive('createPrice')
|
||||
->once()
|
||||
->withArgs(function ($pkg, $productId, $overrides) use ($package) {
|
||||
return $pkg->is($package) && $productId === 'pro_123' && $overrides === [];
|
||||
})
|
||||
->andReturn(['id' => 'pri_123']);
|
||||
$service->shouldReceive('buildProductPayload')
|
||||
->andReturn(['payload' => 'product']);
|
||||
$service->shouldReceive('buildPricePayload')
|
||||
->andReturn(['payload' => 'price']);
|
||||
|
||||
$job = new SyncPackageToPaddle($package->id);
|
||||
$job->handle($service);
|
||||
|
||||
$package->refresh();
|
||||
|
||||
$this->assertSame('pro_123', $package->paddle_product_id);
|
||||
$this->assertSame('pri_123', $package->paddle_price_id);
|
||||
$this->assertSame('synced', $package->paddle_sync_status);
|
||||
$this->assertNotNull($package->paddle_synced_at);
|
||||
$this->assertSame(['payload' => 'product'], $package->paddle_snapshot['payload']['product']);
|
||||
$this->assertSame(['payload' => 'price'], $package->paddle_snapshot['payload']['price']);
|
||||
}
|
||||
|
||||
public function test_dry_run_stores_snapshot_without_calling_paddle(): void
|
||||
{
|
||||
$package = Package::factory()->create([
|
||||
'slug' => 'gold-plan',
|
||||
]);
|
||||
|
||||
$service = Mockery::mock(PaddleCatalogService::class);
|
||||
$service->shouldReceive('buildProductPayload')->andReturn(['payload' => 'product']);
|
||||
$service->shouldReceive('buildPricePayload')->andReturn(['payload' => 'price']);
|
||||
|
||||
$job = new SyncPackageToPaddle($package->id, ['dry_run' => true]);
|
||||
$job->handle($service);
|
||||
|
||||
$package->refresh();
|
||||
|
||||
$this->assertSame('dry-run', $package->paddle_sync_status);
|
||||
$this->assertTrue($package->paddle_snapshot['dry_run']);
|
||||
$this->assertSame(['payload' => 'product'], $package->paddle_snapshot['payload']['product']);
|
||||
$this->assertSame(['payload' => 'price'], $package->paddle_snapshot['payload']['price']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user