switched to paddle inline checkout, removed paypal and most of stripe. added product sync between app and paddle.
This commit is contained in:
@@ -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];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user