|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Services; |
| 4 | + |
| 5 | +use App\Enums\PayoutStatus; |
| 6 | +use App\Models\DeveloperAccount; |
| 7 | +use App\Models\Plugin; |
| 8 | +use App\Models\PluginLicense; |
| 9 | +use App\Models\PluginPayout; |
| 10 | +use App\Services\StripeConnectService; |
| 11 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 12 | +use PHPUnit\Framework\Attributes\Test; |
| 13 | +use Stripe\PaymentIntent; |
| 14 | +use Stripe\StripeClient; |
| 15 | +use Stripe\Transfer; |
| 16 | +use Tests\TestCase; |
| 17 | + |
| 18 | +class StripeConnectServiceTest extends TestCase |
| 19 | +{ |
| 20 | + use RefreshDatabase; |
| 21 | + |
| 22 | + #[Test] |
| 23 | + public function process_transfer_uses_the_source_charge_currency_not_the_developer_payout_currency(): void |
| 24 | + { |
| 25 | + $developerAccount = DeveloperAccount::factory()->create([ |
| 26 | + 'payout_currency' => 'EUR', |
| 27 | + 'stripe_connect_account_id' => 'acct_test_eur', |
| 28 | + ]); |
| 29 | + $plugin = Plugin::factory()->paid()->create(['user_id' => $developerAccount->user_id]); |
| 30 | + $license = PluginLicense::factory()->create([ |
| 31 | + 'plugin_id' => $plugin->id, |
| 32 | + 'stripe_payment_intent_id' => 'pi_test_usd', |
| 33 | + ]); |
| 34 | + |
| 35 | + $payout = PluginPayout::create([ |
| 36 | + 'plugin_license_id' => $license->id, |
| 37 | + 'developer_account_id' => $developerAccount->id, |
| 38 | + 'gross_amount' => 1000, |
| 39 | + 'platform_fee' => 300, |
| 40 | + 'developer_amount' => 700, |
| 41 | + 'status' => PayoutStatus::Pending, |
| 42 | + 'eligible_for_payout_at' => now()->subDay(), |
| 43 | + ]); |
| 44 | + |
| 45 | + $capturedTransferParams = null; |
| 46 | + |
| 47 | + $mockPaymentIntents = new class |
| 48 | + { |
| 49 | + public function retrieve(): PaymentIntent |
| 50 | + { |
| 51 | + return PaymentIntent::constructFrom([ |
| 52 | + 'id' => 'pi_test_usd', |
| 53 | + 'currency' => 'usd', |
| 54 | + 'latest_charge' => 'ch_test_usd', |
| 55 | + ]); |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + $mockTransfers = new class($capturedTransferParams) |
| 60 | + { |
| 61 | + public function __construct(private &$capturedTransferParams) {} |
| 62 | + |
| 63 | + public function create(array $params): Transfer |
| 64 | + { |
| 65 | + $this->capturedTransferParams = $params; |
| 66 | + |
| 67 | + return Transfer::constructFrom(['id' => 'tr_test_123']); |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + $mockStripeClient = $this->createMock(StripeClient::class); |
| 72 | + $mockStripeClient->paymentIntents = $mockPaymentIntents; |
| 73 | + $mockStripeClient->transfers = $mockTransfers; |
| 74 | + |
| 75 | + $this->app->bind(StripeClient::class, fn () => $mockStripeClient); |
| 76 | + |
| 77 | + $result = app(StripeConnectService::class)->processTransfer($payout); |
| 78 | + |
| 79 | + $this->assertTrue($result); |
| 80 | + $this->assertNotNull($capturedTransferParams); |
| 81 | + $this->assertSame('usd', $capturedTransferParams['currency']); |
| 82 | + $this->assertSame('ch_test_usd', $capturedTransferParams['source_transaction']); |
| 83 | + $this->assertSame(700, $capturedTransferParams['amount']); |
| 84 | + $this->assertSame('acct_test_eur', $capturedTransferParams['destination']); |
| 85 | + |
| 86 | + $this->assertTrue($payout->fresh()->isTransferred()); |
| 87 | + $this->assertSame('tr_test_123', $payout->fresh()->stripe_transfer_id); |
| 88 | + } |
| 89 | + |
| 90 | + #[Test] |
| 91 | + public function process_transfer_falls_back_to_payout_currency_when_charge_lookup_fails(): void |
| 92 | + { |
| 93 | + $developerAccount = DeveloperAccount::factory()->create([ |
| 94 | + 'payout_currency' => 'EUR', |
| 95 | + 'stripe_connect_account_id' => 'acct_test_eur', |
| 96 | + ]); |
| 97 | + $plugin = Plugin::factory()->paid()->create(['user_id' => $developerAccount->user_id]); |
| 98 | + $license = PluginLicense::factory()->create([ |
| 99 | + 'plugin_id' => $plugin->id, |
| 100 | + 'stripe_payment_intent_id' => null, |
| 101 | + ]); |
| 102 | + |
| 103 | + $payout = PluginPayout::create([ |
| 104 | + 'plugin_license_id' => $license->id, |
| 105 | + 'developer_account_id' => $developerAccount->id, |
| 106 | + 'gross_amount' => 1000, |
| 107 | + 'platform_fee' => 300, |
| 108 | + 'developer_amount' => 700, |
| 109 | + 'status' => PayoutStatus::Pending, |
| 110 | + 'eligible_for_payout_at' => now()->subDay(), |
| 111 | + ]); |
| 112 | + |
| 113 | + $capturedTransferParams = null; |
| 114 | + |
| 115 | + $mockTransfers = new class($capturedTransferParams) |
| 116 | + { |
| 117 | + public function __construct(private &$capturedTransferParams) {} |
| 118 | + |
| 119 | + public function create(array $params): Transfer |
| 120 | + { |
| 121 | + $this->capturedTransferParams = $params; |
| 122 | + |
| 123 | + return Transfer::constructFrom(['id' => 'tr_test_456']); |
| 124 | + } |
| 125 | + }; |
| 126 | + |
| 127 | + $mockStripeClient = $this->createMock(StripeClient::class); |
| 128 | + $mockStripeClient->transfers = $mockTransfers; |
| 129 | + |
| 130 | + $this->app->bind(StripeClient::class, fn () => $mockStripeClient); |
| 131 | + |
| 132 | + $result = app(StripeConnectService::class)->processTransfer($payout); |
| 133 | + |
| 134 | + $this->assertTrue($result); |
| 135 | + $this->assertNotNull($capturedTransferParams); |
| 136 | + $this->assertSame('eur', $capturedTransferParams['currency']); |
| 137 | + $this->assertArrayNotHasKey('source_transaction', $capturedTransferParams); |
| 138 | + } |
| 139 | +} |
0 commit comments