|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature; |
| 4 | + |
| 5 | +use App\Jobs\HandleInvoicePaidJob; |
| 6 | +use App\Models\Cart; |
| 7 | +use App\Models\CartItem; |
| 8 | +use App\Models\DeveloperAccount; |
| 9 | +use App\Models\Plugin; |
| 10 | +use App\Models\PluginPayout; |
| 11 | +use App\Models\PluginPrice; |
| 12 | +use App\Models\User; |
| 13 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 14 | +use Laravel\Cashier\Subscription; |
| 15 | +use PHPUnit\Framework\Attributes\Test; |
| 16 | +use Stripe\Invoice; |
| 17 | +use Tests\TestCase; |
| 18 | + |
| 19 | +class DeveloperAccountPayoutTest extends TestCase |
| 20 | +{ |
| 21 | + use RefreshDatabase; |
| 22 | + |
| 23 | + private const MAX_PRICE_ID = 'price_1RoZk0AyFo6rlwXqjkLj4hZ0'; |
| 24 | + |
| 25 | + protected function setUp(): void |
| 26 | + { |
| 27 | + parent::setUp(); |
| 28 | + |
| 29 | + config(['subscriptions.plans.max.stripe_price_id' => self::MAX_PRICE_ID]); |
| 30 | + } |
| 31 | + |
| 32 | + #[Test] |
| 33 | + public function payout_percentage_defaults_to_seventy(): void |
| 34 | + { |
| 35 | + $account = DeveloperAccount::factory()->create(); |
| 36 | + |
| 37 | + $this->assertEquals(70, $account->payout_percentage); |
| 38 | + } |
| 39 | + |
| 40 | + #[Test] |
| 41 | + public function platform_fee_percent_is_inverse_of_payout_percentage(): void |
| 42 | + { |
| 43 | + $account = DeveloperAccount::factory()->create(['payout_percentage' => 80]); |
| 44 | + |
| 45 | + $this->assertEquals(20, $account->platformFeePercent()); |
| 46 | + } |
| 47 | + |
| 48 | + #[Test] |
| 49 | + public function platform_fee_percent_with_default_payout_percentage(): void |
| 50 | + { |
| 51 | + $account = DeveloperAccount::factory()->create(); |
| 52 | + |
| 53 | + $this->assertEquals(30, $account->platformFeePercent()); |
| 54 | + } |
| 55 | + |
| 56 | + #[Test] |
| 57 | + public function custom_payout_percentage_is_used_for_plugin_purchase(): void |
| 58 | + { |
| 59 | + $buyer = User::factory()->create(['stripe_id' => 'cus_test_buyer_'.uniqid()]); |
| 60 | + |
| 61 | + $developerAccount = DeveloperAccount::factory()->create(['payout_percentage' => 80]); |
| 62 | + $plugin = Plugin::factory()->approved()->paid()->create([ |
| 63 | + 'is_active' => true, |
| 64 | + 'is_official' => false, |
| 65 | + 'user_id' => $developerAccount->user_id, |
| 66 | + 'developer_account_id' => $developerAccount->id, |
| 67 | + ]); |
| 68 | + PluginPrice::factory()->regular()->amount(10000)->create(['plugin_id' => $plugin->id]); |
| 69 | + |
| 70 | + $cart = Cart::factory()->for($buyer)->create(); |
| 71 | + CartItem::create([ |
| 72 | + 'cart_id' => $cart->id, |
| 73 | + 'plugin_id' => $plugin->id, |
| 74 | + 'plugin_price_id' => $plugin->prices->first()->id, |
| 75 | + 'price_at_addition' => 10000, |
| 76 | + ]); |
| 77 | + |
| 78 | + $invoice = $this->createStripeInvoice($cart->id, $buyer->stripe_id); |
| 79 | + $job = new HandleInvoicePaidJob($invoice); |
| 80 | + $job->handle(); |
| 81 | + |
| 82 | + $payout = PluginPayout::first(); |
| 83 | + $this->assertNotNull($payout); |
| 84 | + $this->assertEquals(10000, $payout->gross_amount); |
| 85 | + $this->assertEquals(2000, $payout->platform_fee); |
| 86 | + $this->assertEquals(8000, $payout->developer_amount); |
| 87 | + } |
| 88 | + |
| 89 | + #[Test] |
| 90 | + public function ultra_subscriber_overrides_custom_payout_percentage_to_full(): void |
| 91 | + { |
| 92 | + $buyer = User::factory()->create(['stripe_id' => 'cus_test_buyer_'.uniqid()]); |
| 93 | + Subscription::factory()->for($buyer)->active()->create(['stripe_price' => self::MAX_PRICE_ID]); |
| 94 | + |
| 95 | + $developerAccount = DeveloperAccount::factory()->create(['payout_percentage' => 80]); |
| 96 | + $plugin = Plugin::factory()->approved()->paid()->create([ |
| 97 | + 'is_active' => true, |
| 98 | + 'is_official' => false, |
| 99 | + 'user_id' => $developerAccount->user_id, |
| 100 | + 'developer_account_id' => $developerAccount->id, |
| 101 | + ]); |
| 102 | + PluginPrice::factory()->regular()->amount(10000)->create(['plugin_id' => $plugin->id]); |
| 103 | + |
| 104 | + $cart = Cart::factory()->for($buyer)->create(); |
| 105 | + CartItem::create([ |
| 106 | + 'cart_id' => $cart->id, |
| 107 | + 'plugin_id' => $plugin->id, |
| 108 | + 'plugin_price_id' => $plugin->prices->first()->id, |
| 109 | + 'price_at_addition' => 10000, |
| 110 | + ]); |
| 111 | + |
| 112 | + $invoice = $this->createStripeInvoice($cart->id, $buyer->stripe_id); |
| 113 | + $job = new HandleInvoicePaidJob($invoice); |
| 114 | + $job->handle(); |
| 115 | + |
| 116 | + $payout = PluginPayout::first(); |
| 117 | + $this->assertNotNull($payout); |
| 118 | + $this->assertEquals(10000, $payout->gross_amount); |
| 119 | + $this->assertEquals(0, $payout->platform_fee); |
| 120 | + $this->assertEquals(10000, $payout->developer_amount); |
| 121 | + } |
| 122 | + |
| 123 | + #[Test] |
| 124 | + public function developer_with_hundred_percent_payout_gets_full_amount(): void |
| 125 | + { |
| 126 | + $buyer = User::factory()->create(['stripe_id' => 'cus_test_buyer_'.uniqid()]); |
| 127 | + |
| 128 | + $developerAccount = DeveloperAccount::factory()->create(['payout_percentage' => 100]); |
| 129 | + $plugin = Plugin::factory()->approved()->paid()->create([ |
| 130 | + 'is_active' => true, |
| 131 | + 'is_official' => false, |
| 132 | + 'user_id' => $developerAccount->user_id, |
| 133 | + 'developer_account_id' => $developerAccount->id, |
| 134 | + ]); |
| 135 | + PluginPrice::factory()->regular()->amount(5000)->create(['plugin_id' => $plugin->id]); |
| 136 | + |
| 137 | + $cart = Cart::factory()->for($buyer)->create(); |
| 138 | + CartItem::create([ |
| 139 | + 'cart_id' => $cart->id, |
| 140 | + 'plugin_id' => $plugin->id, |
| 141 | + 'plugin_price_id' => $plugin->prices->first()->id, |
| 142 | + 'price_at_addition' => 5000, |
| 143 | + ]); |
| 144 | + |
| 145 | + $invoice = $this->createStripeInvoice($cart->id, $buyer->stripe_id); |
| 146 | + $job = new HandleInvoicePaidJob($invoice); |
| 147 | + $job->handle(); |
| 148 | + |
| 149 | + $payout = PluginPayout::first(); |
| 150 | + $this->assertNotNull($payout); |
| 151 | + $this->assertEquals(5000, $payout->gross_amount); |
| 152 | + $this->assertEquals(0, $payout->platform_fee); |
| 153 | + $this->assertEquals(5000, $payout->developer_amount); |
| 154 | + } |
| 155 | + |
| 156 | + private function createStripeInvoice(string $cartId, string $customerId): Invoice |
| 157 | + { |
| 158 | + return Invoice::constructFrom([ |
| 159 | + 'id' => 'in_test_'.uniqid(), |
| 160 | + 'billing_reason' => Invoice::BILLING_REASON_MANUAL, |
| 161 | + 'customer' => $customerId, |
| 162 | + 'payment_intent' => 'pi_test_'.uniqid(), |
| 163 | + 'currency' => 'usd', |
| 164 | + 'metadata' => ['cart_id' => $cartId], |
| 165 | + 'lines' => [], |
| 166 | + ]); |
| 167 | + } |
| 168 | +} |
0 commit comments