Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/PaymentsMollieTest.php
Show First 20 Lines • Show All 74 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(); | ||||
$this->assertSame('error', $json['status']); | $this->assertSame('error', $json['status']); | ||||
$this->assertCount(2, $json['errors']); | $this->assertCount(2, $json['errors']); | ||||
$this->assertSame('The amount field is required.', $json['errors']['amount'][0]); | $this->assertSame('The amount field is required.', $json['errors']['amount'][0]); | ||||
$this->assertSame('The balance field is required.', $json['errors']['balance'][0]); | $this->assertSame('The balance field is required.', $json['errors']['balance'][0]); | ||||
// Test creating a mandate (invalid input) | // Test creating a mandate (invalid input) | ||||
$post = ['amount' => 100, 'balance' => 'a']; | $post = ['amount' => 100, 'balance' => 'a']; | ||||
$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(); | ||||
$this->assertSame('error', $json['status']); | $this->assertSame('error', $json['status']); | ||||
$this->assertCount(1, $json['errors']); | $this->assertCount(1, $json['errors']); | ||||
$this->assertSame('The balance must be a number.', $json['errors']['balance'][0]); | $this->assertSame('The balance must be a number.', $json['errors']['balance'][0]); | ||||
// Test creating a mandate (invalid input) | // Test creating a mandate (amount smaller than the minimum value) | ||||
$post = ['amount' => -100, 'balance' => 0]; | $post = ['amount' => -100, 'balance' => 0]; | ||||
$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(); | ||||
$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('|^https://www.mollie.com|', $json['redirectUrl']); | $this->assertRegExp('|^https://www.mollie.com|', $json['redirectUrl']); | ||||
// Assert the proper payment amount has been used | |||||
$payment = Payment::where('id', $json['id'])->first(); | |||||
$this->assertSame(2010, $payment->amount); | |||||
$this->assertSame($wallet->id, $payment->wallet_id); | |||||
$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 All 34 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']); | ||||
Bus::fake(); | Bus::fake(); | ||||
$wallet->setSetting('mandate_disabled', null); | $wallet->setSetting('mandate_disabled', null); | ||||
$wallet->balance = 1000; | |||||
$wallet->save(); | |||||
// Test updating mandate details (invalid input) | // Test updating mandate details (invalid input) | ||||
$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(); | ||||
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) | ||||
$responseStack->append(new Response(200, [], json_encode($mollie_response))); | $responseStack->append(new Response(200, [], json_encode($mollie_response))); | ||||
$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->assertSame($mandate_id, $json['id']); | $this->assertSame($mandate_id, $json['id']); | ||||
$this->assertFalse($json['isDisabled']); | $this->assertFalse($json['isDisabled']); | ||||
$wallet->refresh(); | $wallet->refresh(); | ||||
$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')); | ||||
Bus::assertDispatchedTimes(\App\Jobs\WalletCharge::class, 0); | |||||
// 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 | ||||
$post = ['amount' => 15.10, 'balance' => 1]; | $post = ['amount' => 15.10, 'balance' => 1]; | ||||
▲ Show 20 Lines • Show All 216 Lines • ▼ Show 20 Lines | class PaymentsMollieTest extends TestCase | ||||
*/ | */ | ||||
public function testTopUp(): void | public function testTopUp(): void | ||||
{ | { | ||||
Bus::fake(); | Bus::fake(); | ||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('john@kolab.org'); | ||||
$wallet = $user->wallets()->first(); | $wallet = $user->wallets()->first(); | ||||
// Create a valid mandate first | // Create a valid mandate first (balance=0, so there's no extra payment yet) | ||||
$this->createMandate($wallet, ['amount' => 20.10, 'balance' => 10]); | $this->createMandate($wallet, ['amount' => 20.10, 'balance' => 0]); | ||||
$wallet->setSetting('mandate_balance', 10); | |||||
// Expect a recurring payment as we have a valid mandate at this point | // Expect a recurring payment as we have a valid mandate at this point | ||||
// and the balance is below the threshold | |||||
$result = PaymentsController::topUpWallet($wallet); | $result = PaymentsController::topUpWallet($wallet); | ||||
$this->assertTrue($result); | $this->assertTrue($result); | ||||
// Check that the payments table contains a new record with proper amount | // Check that the payments table contains a new record with proper amount. | ||||
// There should be two records, one for the first payment and another for | // There should be two records, one for the mandate payment and another for | ||||
// the recurring payment | // the top-up payment | ||||
$this->assertCount(1, $wallet->payments()->get()); | $payments = $wallet->payments()->orderBy('amount')->get(); | ||||
$payment = $wallet->payments()->first(); | $this->assertCount(2, $payments); | ||||
$this->assertSame(2010, $payment->amount); | $this->assertSame(0, $payments[0]->amount); | ||||
$this->assertSame(2010, $payments[1]->amount); | |||||
$payment = $payments[1]; | |||||
// In mollie we don't have to wait for a webhook, the response to | // In mollie we don't have to wait for a webhook, the response to | ||||
// PaymentIntent already sets the status to 'paid', so we can test | // PaymentIntent already sets the status to 'paid', so we can test | ||||
// immediately the balance update | // immediately the balance update | ||||
// Assert that email notification job has been dispatched | // Assert that email notification job has been dispatched | ||||
$this->assertSame(PaymentProvider::STATUS_PAID, $payment->status); | $this->assertSame(PaymentProvider::STATUS_PAID, $payment->status); | ||||
$this->assertEquals(2010, $wallet->fresh()->balance); | $this->assertEquals(2010, $wallet->fresh()->balance); | ||||
$transaction = $wallet->transactions() | $transaction = $wallet->transactions() | ||||
Show All 10 Lines | public function testTopUp(): void | ||||
$job_payment = $this->getObjectProperty($job, 'payment'); | $job_payment = $this->getObjectProperty($job, 'payment'); | ||||
return $job_payment->id === $payment->id; | return $job_payment->id === $payment->id; | ||||
}); | }); | ||||
// Expect no payment if the mandate is disabled | // Expect no payment if the mandate is disabled | ||||
$wallet->setSetting('mandate_disabled', 1); | $wallet->setSetting('mandate_disabled', 1); | ||||
$result = PaymentsController::topUpWallet($wallet); | $result = PaymentsController::topUpWallet($wallet); | ||||
$this->assertFalse($result); | $this->assertFalse($result); | ||||
$this->assertCount(1, $wallet->payments()->get()); | $this->assertCount(2, $wallet->payments()->get()); | ||||
// Expect no payment if balance is ok | // Expect no payment if balance is ok | ||||
$wallet->setSetting('mandate_disabled', null); | $wallet->setSetting('mandate_disabled', null); | ||||
$wallet->balance = 1000; | $wallet->balance = 1000; | ||||
$wallet->save(); | $wallet->save(); | ||||
$result = PaymentsController::topUpWallet($wallet); | $result = PaymentsController::topUpWallet($wallet); | ||||
$this->assertFalse($result); | $this->assertFalse($result); | ||||
$this->assertCount(1, $wallet->payments()->get()); | $this->assertCount(2, $wallet->payments()->get()); | ||||
// Expect no payment if the top-up amount is not enough | // Expect no payment if the top-up amount is not enough | ||||
$wallet->setSetting('mandate_disabled', null); | $wallet->setSetting('mandate_disabled', null); | ||||
$wallet->balance = -2050; | $wallet->balance = -2050; | ||||
$wallet->save(); | $wallet->save(); | ||||
$result = PaymentsController::topUpWallet($wallet); | $result = PaymentsController::topUpWallet($wallet); | ||||
$this->assertFalse($result); | $this->assertFalse($result); | ||||
$this->assertCount(1, $wallet->payments()->get()); | $this->assertCount(2, $wallet->payments()->get()); | ||||
Bus::assertDispatchedTimes(\App\Jobs\PaymentMandateDisabledEmail::class, 1); | Bus::assertDispatchedTimes(\App\Jobs\PaymentMandateDisabledEmail::class, 1); | ||||
Bus::assertDispatched(\App\Jobs\PaymentMandateDisabledEmail::class, function ($job) use ($wallet) { | Bus::assertDispatched(\App\Jobs\PaymentMandateDisabledEmail::class, function ($job) use ($wallet) { | ||||
$job_wallet = $this->getObjectProperty($job, 'wallet'); | $job_wallet = $this->getObjectProperty($job, 'wallet'); | ||||
return $job_wallet->id === $wallet->id; | return $job_wallet->id === $wallet->id; | ||||
}); | }); | ||||
// Expect no payment if there's no mandate | // Expect no payment if there's no mandate | ||||
$wallet->setSetting('mollie_mandate_id', null); | $wallet->setSetting('mollie_mandate_id', null); | ||||
$wallet->balance = 0; | $wallet->balance = 0; | ||||
$wallet->save(); | $wallet->save(); | ||||
$result = PaymentsController::topUpWallet($wallet); | $result = PaymentsController::topUpWallet($wallet); | ||||
$this->assertFalse($result); | $this->assertFalse($result); | ||||
$this->assertCount(1, $wallet->payments()->get()); | $this->assertCount(2, $wallet->payments()->get()); | ||||
Bus::assertDispatchedTimes(\App\Jobs\PaymentMandateDisabledEmail::class, 1); | Bus::assertDispatchedTimes(\App\Jobs\PaymentMandateDisabledEmail::class, 1); | ||||
// Test webhook for recurring payments | // Test webhook for recurring payments | ||||
$wallet->transactions()->delete(); | $wallet->transactions()->delete(); | ||||
$responseStack = $this->mockMollie(); | $responseStack = $this->mockMollie(); | ||||
▲ Show 20 Lines • Show All 257 Lines • Show Last 20 Lines |