Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/PaymentsStripeTest.php
Show First 20 Lines • Show All 66 Lines • ▼ Show 20 Lines | public function testMandates(): void | ||||
$response = $this->post("api/v4/payments/mandate", []); | $response = $this->post("api/v4/payments/mandate", []); | ||||
$response->assertStatus(401); | $response->assertStatus(401); | ||||
$response = $this->put("api/v4/payments/mandate", []); | $response = $this->put("api/v4/payments/mandate", []); | ||||
$response->assertStatus(401); | $response->assertStatus(401); | ||||
$response = $this->delete("api/v4/payments/mandate"); | $response = $this->delete("api/v4/payments/mandate"); | ||||
$response->assertStatus(401); | $response->assertStatus(401); | ||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('john@kolab.org'); | ||||
$wallet = $user->wallets()->first(); | |||||
// Test creating a mandate (invalid input) | // Test creating a mandate (invalid input) | ||||
$post = []; | $post = []; | ||||
$response = $this->actingAs($user)->post("api/v4/payments/mandate", $post); | $response = $this->actingAs($user)->post("api/v4/payments/mandate", $post); | ||||
$response->assertStatus(422); | $response->assertStatus(422); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
Show All 20 Lines | public function testMandates(): void | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame('error', $json['status']); | $this->assertSame('error', $json['status']); | ||||
$this->assertCount(1, $json['errors']); | $this->assertCount(1, $json['errors']); | ||||
$min = intval(PaymentProvider::MIN_AMOUNT / 100) . ' CHF'; | $min = intval(PaymentProvider::MIN_AMOUNT / 100) . ' CHF'; | ||||
$this->assertSame("Minimum amount for a single payment is {$min}.", $json['errors']['amount']); | $this->assertSame("Minimum amount for a single payment is {$min}.", $json['errors']['amount']); | ||||
// Test creating a mandate (negative balance, amount too small) | |||||
Wallet::where('id', $wallet->id)->update(['balance' => -2000]); | |||||
$post = ['amount' => PaymentProvider::MIN_AMOUNT / 100, 'balance' => 0]; | |||||
$response = $this->actingAs($user)->post("api/v4/payments/mandate", $post); | |||||
$response->assertStatus(422); | |||||
$json = $response->json(); | |||||
$this->assertSame('error', $json['status']); | |||||
$this->assertCount(1, $json['errors']); | |||||
$this->assertSame("The specified amount does not cover the balance on the account.", $json['errors']['amount']); | |||||
// Test creating a mandate (valid input) | // Test creating a mandate (valid input) | ||||
$post = ['amount' => 20.10, 'balance' => 0]; | $post = ['amount' => 20.10, 'balance' => 0]; | ||||
$response = $this->actingAs($user)->post("api/v4/payments/mandate", $post); | $response = $this->actingAs($user)->post("api/v4/payments/mandate", $post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame('success', $json['status']); | $this->assertSame('success', $json['status']); | ||||
$this->assertRegExp('|^cs_test_|', $json['id']); | $this->assertRegExp('|^cs_test_|', $json['id']); | ||||
// Assert the proper payment amount has been used | |||||
// Stripe in 'setup' mode does not allow to set the amount | |||||
$payment = Payment::where('wallet_id', $wallet->id)->first(); | |||||
$this->assertSame(0, $payment->amount); | |||||
$this->assertSame("Kolab Now Auto-Payment Setup", $payment->description); | |||||
$this->assertSame(PaymentProvider::TYPE_MANDATE, $payment->type); | |||||
// Test fetching the mandate information | // Test fetching the mandate information | ||||
$response = $this->actingAs($user)->get("api/v4/payments/mandate"); | $response = $this->actingAs($user)->get("api/v4/payments/mandate"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertEquals(20.10, $json['amount']); | $this->assertEquals(20.10, $json['amount']); | ||||
$this->assertEquals(0, $json['balance']); | $this->assertEquals(0, $json['balance']); | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | public function testMandates(): void | ||||
$this->assertEquals(0, $json['balance']); | $this->assertEquals(0, $json['balance']); | ||||
$this->assertEquals('Visa (**** **** **** 4242)', $json['method']); | $this->assertEquals('Visa (**** **** **** 4242)', $json['method']); | ||||
$this->assertSame(false, $json['isPending']); | $this->assertSame(false, $json['isPending']); | ||||
$this->assertSame(true, $json['isValid']); | $this->assertSame(true, $json['isValid']); | ||||
$this->assertSame(true, $json['isDisabled']); | $this->assertSame(true, $json['isDisabled']); | ||||
// Test updating mandate details (invalid input) | // Test updating mandate details (invalid input) | ||||
$wallet->setSetting('mandate_disabled', null); | $wallet->setSetting('mandate_disabled', null); | ||||
$wallet->balance = 1000; | |||||
$wallet->save(); | |||||
$user->refresh(); | $user->refresh(); | ||||
$post = []; | $post = []; | ||||
$response = $this->actingAs($user)->put("api/v4/payments/mandate", $post); | $response = $this->actingAs($user)->put("api/v4/payments/mandate", $post); | ||||
$response->assertStatus(422); | $response->assertStatus(422); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame('error', $json['status']); | $this->assertSame('error', $json['status']); | ||||
Show All 10 Lines | public function testMandates(): void | ||||
$this->assertSame('error', $json['status']); | $this->assertSame('error', $json['status']); | ||||
$this->assertCount(1, $json['errors']); | $this->assertCount(1, $json['errors']); | ||||
$this->assertSame("Minimum amount for a single payment is {$min}.", $json['errors']['amount']); | $this->assertSame("Minimum amount for a single payment is {$min}.", $json['errors']['amount']); | ||||
// Test updating a mandate (valid input) | // Test updating a mandate (valid input) | ||||
$client->addResponse($setupIntent); | $client->addResponse($setupIntent); | ||||
$client->addResponse($paymentMethod); | $client->addResponse($paymentMethod); | ||||
$post = ['amount' => 30.10, 'balance' => 1]; | $post = ['amount' => 30.10, 'balance' => 10]; | ||||
$response = $this->actingAs($user)->put("api/v4/payments/mandate", $post); | $response = $this->actingAs($user)->put("api/v4/payments/mandate", $post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame('success', $json['status']); | $this->assertSame('success', $json['status']); | ||||
$this->assertSame('The auto-payment has been updated.', $json['message']); | $this->assertSame('The auto-payment has been updated.', $json['message']); | ||||
$this->assertEquals(30.10, $wallet->getSetting('mandate_amount')); | $this->assertEquals(30.10, $wallet->getSetting('mandate_amount')); | ||||
$this->assertEquals(1, $wallet->getSetting('mandate_balance')); | $this->assertEquals(10, $wallet->getSetting('mandate_balance')); | ||||
$this->assertSame('AAA', $json['id']); | $this->assertSame('AAA', $json['id']); | ||||
$this->assertFalse($json['isDisabled']); | $this->assertFalse($json['isDisabled']); | ||||
// Test updating a disabled mandate (invalid input) | // Test updating a disabled mandate (invalid input) | ||||
$wallet->setSetting('mandate_disabled', 1); | $wallet->setSetting('mandate_disabled', 1); | ||||
$wallet->balance = -2000; | $wallet->balance = -2000; | ||||
$wallet->save(); | $wallet->save(); | ||||
$user->refresh(); // required so the controller sees the wallet update from above | $user->refresh(); // required so the controller sees the wallet update from above | ||||
▲ Show 20 Lines • Show All 178 Lines • ▼ Show 20 Lines | class PaymentsStripeTest extends TestCase | ||||
* Test receiving webhook request for setup intent | * Test receiving webhook request for setup intent | ||||
* | * | ||||
* @group stripe | * @group stripe | ||||
*/ | */ | ||||
public function testCreateMandateAndWebhook(): void | public function testCreateMandateAndWebhook(): void | ||||
{ | { | ||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('john@kolab.org'); | ||||
$wallet = $user->wallets()->first(); | $wallet = $user->wallets()->first(); | ||||
Wallet::where('id', $wallet->id)->update(['balance' => -1000]); | |||||
// Test creating a mandate (valid input) | // Test creating a mandate (valid input) | ||||
$post = ['amount' => 20.10, 'balance' => 0]; | $post = ['amount' => 20.10, 'balance' => 0]; | ||||
$response = $this->actingAs($user)->post("api/v4/payments/mandate", $post); | $response = $this->actingAs($user)->post("api/v4/payments/mandate", $post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$payment = $wallet->payments()->first(); | $payment = $wallet->payments()->first(); | ||||
Show All 16 Lines | public function testCreateMandateAndWebhook(): void | ||||
'last_setup_error' => null, | 'last_setup_error' => null, | ||||
'metadata' => [], | 'metadata' => [], | ||||
'status' => "succeeded" | 'status' => "succeeded" | ||||
] | ] | ||||
], | ], | ||||
'type' => "setup_intent.succeeded" | 'type' => "setup_intent.succeeded" | ||||
]; | ]; | ||||
Bus::fake(); | |||||
// Test payment succeeded event | // Test payment succeeded event | ||||
$response = $this->webhookRequest($post); | $response = $this->webhookRequest($post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$payment->refresh(); | $payment->refresh(); | ||||
$this->assertSame(PaymentProvider::STATUS_PAID, $payment->status); | $this->assertSame(PaymentProvider::STATUS_PAID, $payment->status); | ||||
$this->assertSame($payment->id, $wallet->fresh()->getSetting('stripe_mandate_id')); | $this->assertSame($payment->id, $wallet->fresh()->getSetting('stripe_mandate_id')); | ||||
// Expect a WalletCharge job if the balance is negative | |||||
Bus::assertDispatchedTimes(\App\Jobs\WalletCharge::class, 1); | |||||
Bus::assertDispatched(\App\Jobs\WalletCharge::class, function ($job) use ($wallet) { | |||||
$job_wallet = TestCase::getObjectProperty($job, 'wallet'); | |||||
return $job_wallet->id === $wallet->id; | |||||
}); | |||||
// TODO: test other setup_intent.* events | // TODO: test other setup_intent.* events | ||||
} | } | ||||
/** | /** | ||||
* Test automatic payment charges | * Test automatic payment charges | ||||
* | * | ||||
* @group stripe | * @group stripe | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 220 Lines • Show Last 20 Lines |